# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1567575865 -19800
#      Wed Sep 04 11:14:25 2019 +0530
# Node ID ba404bdadcf59543f58e0ca34158ea1da8f01a45
# Parent  b5ee2a511b8ff2d645c7670c1ca54e858ac97ccf
Related to issue 41604 : Payments should be generated with Triggers Enabled

diff -r b5ee2a511b8f -r ba404bdadcf5 src/org/openbravo/retail/posterminal/OrderLoader.java
--- a/src/org/openbravo/retail/posterminal/OrderLoader.java	Fri Aug 30 17:53:47 2019 -0400
+++ b/src/org/openbravo/retail/posterminal/OrderLoader.java	Wed Sep 04 11:14:25 2019 +0530
@@ -455,38 +455,6 @@
               + "; Shipment: " + (t115 - t113) + "; Approvals" + (t11 - t115) + "; stock"
               + (t4 - t3) + "; Invoice: " + (t116 - t4) + ";");
         }
-
-        if (useOrderDocumentNoForRelatedDocs) {
-          paymentCount = countPayments(order);
-        }
-        if (log.isDebugEnabled()) {
-          t5 = System.currentTimeMillis();
-        }
-
-        if (!isQuotation && !isDeleted) {
-          // Payment
-          paymentResponse = handlePayments(jsonorder, order, invoice);
-          if (paymentResponse
-              .getInt(JsonConstants.RESPONSE_STATUS) == JsonConstants.RPCREQUEST_STATUS_FAILURE) {
-            return paymentResponse;
-          }
-        }
-
-        if (createShipment || createInvoice || (!isQuotation && !isDeleted)) {
-          // do the docnumbers at the end
-          OBContext.setAdminMode(false);
-          try {
-            for (DocumentNoHandler documentNoHandler : documentNoHandlers.get()) {
-              documentNoHandler.setDocumentNoAndSave();
-            }
-            OBDal.getInstance().flush();
-          } finally {
-            // set to null, should not be used anymore after this.
-            documentNoHandlers.set(null);
-            OBContext.restorePreviousMode();
-          }
-        }
-
       } catch (Exception ex) {
         throw new OBException("Error in OrderLoader: " + ex.getMessage(), ex);
       } finally {
@@ -498,6 +466,45 @@
         } catch (Throwable ignored) {
         }
       }
+
+      if (useOrderDocumentNoForRelatedDocs) {
+        paymentCount = countPayments(order);
+      }
+      if (log.isDebugEnabled()) {
+        t5 = System.currentTimeMillis();
+      }
+
+      if (!isQuotation && !isDeleted) {
+        // Payment
+        paymentResponse = handlePayments(jsonorder, order, invoice);
+        if (paymentResponse
+            .getInt(JsonConstants.RESPONSE_STATUS) == JsonConstants.RPCREQUEST_STATUS_FAILURE) {
+          return paymentResponse;
+        }
+      }
+
+      if (createShipment || createInvoice || (!isQuotation && !isDeleted)) {
+        // do the docnumbers at the end
+        OBContext.setAdminMode(false);
+        TriggerHandler.getInstance().disable();
+        try {
+          for (DocumentNoHandler documentNoHandler : documentNoHandlers.get()) {
+            documentNoHandler.setDocumentNoAndSave();
+          }
+          OBDal.getInstance().flush();
+        } finally {
+          // set to null, should not be used anymore after this.
+          documentNoHandlers.set(null);
+          try {
+            // enable triggers contains a flush in getConnection method
+            TriggerHandler.getInstance().enable();
+          } catch (Throwable ignored) {
+
+          }
+          OBContext.restorePreviousMode();
+        }
+      }
+
       if (!isQuotation && !isDeleted) {
         if (doCancelAndReplace && order.getReplacedorder() != null) {
           TriggerHandler.getInstance().disable();
