From 1d58681c6f052b7e129b0e72f9814d620d5dd35e Mon Sep 17 00:00:00 2001
From: Daniel Martins <daniel.martins@openbravo.com>
Date: Wed, 15 Apr 2020 18:17:57 +0100
Subject: [PATCH] [Reservations] Activate triggers to manipulate Reservations

---
 .../businessUtility/CancelAndReplaceUtils.java        |  9 +++++++++
 .../businessUtility/ReplaceOrderExecutor.java         | 11 +++++++++++
 2 files changed, 20 insertions(+)

diff --git a/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java b/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java
index c4352b9d40..1015e254f2 100644
--- a/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java
+++ b/src/org/openbravo/erpCommon/businessUtility/CancelAndReplaceUtils.java
@@ -307,6 +307,8 @@ public class CancelAndReplaceUtils {
   static void closeOldReservations(Order oldOrder) {
     if (getEnableStockReservationsPreferenceValue(oldOrder.getOrganization())) {
       ScrollableResults oldOrderLines = null;
+      boolean isTriggersDisabled = TriggerHandler.getInstance().isDisabled();
+      boolean isEnableDuringProcess = false;
       try {
         // Iterate old order lines
         oldOrderLines = getOrderLineList(oldOrder);
@@ -315,6 +317,10 @@ public class CancelAndReplaceUtils {
           OrderLine oldOrderLine = (OrderLine) oldOrderLines.get(0);
           Reservation reservation = getReservationForOrderLine(oldOrderLine);
           if (reservation != null) {
+            if (isTriggersDisabled && !isEnableDuringProcess) {
+              TriggerHandler.getInstance().enable();
+              isEnableDuringProcess = true;
+            }
             ReservationUtils.processReserve(reservation, "CL");
           }
           if ((++i % 100) == 0) {
@@ -326,6 +332,9 @@ public class CancelAndReplaceUtils {
         log4j.error("Error in CancelAndReplaceUtils.releaseOldReservations", e);
         throw new OBException(e.getMessage(), e);
       } finally {
+        if (isEnableDuringProcess) {
+          TriggerHandler.getInstance().disable();
+        }
         if (oldOrderLines != null) {
           oldOrderLines.close();
         }
diff --git a/src/org/openbravo/erpCommon/businessUtility/ReplaceOrderExecutor.java b/src/org/openbravo/erpCommon/businessUtility/ReplaceOrderExecutor.java
index f76e1db14c..ff2771cf82 100644
--- a/src/org/openbravo/erpCommon/businessUtility/ReplaceOrderExecutor.java
+++ b/src/org/openbravo/erpCommon/businessUtility/ReplaceOrderExecutor.java
@@ -45,6 +45,7 @@ import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.core.TriggerHandler;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
@@ -660,6 +661,8 @@ class ReplaceOrderExecutor extends CancelAndReplaceUtils {
 
   private void createNewReservations(Order newOrder) {
     if (getEnableStockReservationsPreferenceValue(newOrder.getOrganization())) {
+      boolean isTriggersDisabled = TriggerHandler.getInstance().isDisabled();
+      boolean isEnableDuringProcess = false;
       // Iterate old order lines
       try (final ScrollableResults newOrderLines = getOrderLineList(newOrder)) {
         int i = 0;
@@ -672,6 +675,10 @@ class ReplaceOrderExecutor extends CancelAndReplaceUtils {
           }
           Reservation reservation = getReservationForOrderLine(newOrderLine.getReplacedorderline());
           if (reservation != null) {
+            if (isTriggersDisabled && !isEnableDuringProcess) {
+              TriggerHandler.getInstance().enable();
+              isEnableDuringProcess = true;
+            }
             ReservationUtils.createReserveFromSalesOrderLine(newOrderLine, true);
           }
           if ((++i % 100) == 0) {
@@ -682,6 +689,10 @@ class ReplaceOrderExecutor extends CancelAndReplaceUtils {
       } catch (Exception e) {
         log4j.error("Error in CancelAndReplaceUtils.createNewReservations", e);
         throw new OBException(e.getMessage(), e);
+      } finally {
+        if (isEnableDuringProcess) {
+          TriggerHandler.getInstance().disable();
+        }
       }
     }
   }
-- 
2.23.0

