From 8f983358dd01cc595ccbb235f0d0909927ec66c6 Mon Sep 17 00:00:00 2001
From: Prakash M <prakash@qualiantech.com>
Date: Sat, 14 Nov 2020 21:49:55 +0530
Subject: [PATCH] Fixed BUG-45390: Fixed try errors in countsafebox * Due to
 OB.Dal.Find in setPaymentList method it is executed asynchronously * Because
 of this remaining part of code in initializationProcess executed before this
 * Fixed by adding call back

---
 .../js/closecash/model/cashup/cashup-model.js | 23 ++---
 .../js/closecash/model/closecash-model.js     |  3 +-
 .../model/countsafebox/countsafebox-model.js  | 88 ++++++++++---------
 3 files changed, 60 insertions(+), 54 deletions(-)

diff --git a/web/org.openbravo.retail.posterminal/js/closecash/model/cashup/cashup-model.js b/web/org.openbravo.retail.posterminal/js/closecash/model/cashup/cashup-model.js
index 5516d6824..7fd04e91e 100644
--- a/web/org.openbravo.retail.posterminal/js/closecash/model/cashup/cashup-model.js
+++ b/web/org.openbravo.retail.posterminal/js/closecash/model/cashup/cashup-model.js
@@ -75,7 +75,8 @@ OB.OBPOSCashUp.Model.CashUp = OB.OBPOSCloseCash.Model.CloseCash.extend({
     let closeCashReport,
       synch1 = false,
       synch2 = false,
-      synch3 = false;
+      synch3 = false,
+      me = this;
 
     const finish = () => {
       if (synch1 && synch2 && synch3) {
@@ -98,16 +99,16 @@ OB.OBPOSCashUp.Model.CashUp = OB.OBPOSCloseCash.Model.CloseCash.extend({
 
     this.set('orderlist', new OB.Collection.OrderList());
 
-    this.setPaymentList(true);
-
-    this.stepsDefinition[
-      this.stepIndex('OB.CloseCash.CashPayments')
-    ].loaded = true;
-    this.stepsDefinition[
-      this.stepIndex('OB.CloseCash.PaymentMethods')
-    ].loaded = true;
-    synch1 = true;
-    finish();
+    this.setPaymentList(true, function() {
+      me.stepsDefinition[
+        me.stepIndex('OB.CloseCash.CashPayments')
+      ].loaded = true;
+      me.stepsDefinition[
+        me.stepIndex('OB.CloseCash.PaymentMethods')
+      ].loaded = true;
+      synch1 = true;
+      finish();
+    });
 
     OB.Dal.find(
       OB.Model.CashUp,
diff --git a/web/org.openbravo.retail.posterminal/js/closecash/model/closecash-model.js b/web/org.openbravo.retail.posterminal/js/closecash/model/closecash-model.js
index f5599d62c..980eff72e 100644
--- a/web/org.openbravo.retail.posterminal/js/closecash/model/closecash-model.js
+++ b/web/org.openbravo.retail.posterminal/js/closecash/model/closecash-model.js
@@ -436,7 +436,7 @@ OB.OBPOSCloseCash.Model.CloseCash = OB.Model.TerminalWindowModel.extend({
   processAndFinishCloseCash: function() {
     this.processAndFinish();
   },
-  setPaymentList: function(validateSteps) {
+  setPaymentList: function(validateSteps, callback) {
     const terminalSlave =
       !OB.POS.modelterminal.get('terminal').ismaster &&
       OB.POS.modelterminal.get('terminal').isslave;
@@ -622,6 +622,7 @@ OB.OBPOSCloseCash.Model.CloseCash = OB.Model.TerminalWindowModel.extend({
                 }
               }
             });
+            callback();
           }
         );
       }
diff --git a/web/org.openbravo.retail.posterminal/js/closecash/model/countsafebox/countsafebox-model.js b/web/org.openbravo.retail.posterminal/js/closecash/model/countsafebox/countsafebox-model.js
index d30dda181..0632170f3 100644
--- a/web/org.openbravo.retail.posterminal/js/closecash/model/countsafebox/countsafebox-model.js
+++ b/web/org.openbravo.retail.posterminal/js/closecash/model/countsafebox/countsafebox-model.js
@@ -59,6 +59,7 @@ OB.OBPOSCountSafeBox.Model.CountSafeBox = OB.OBPOSCloseCash.Model.CloseCash.exte
       });
     },
     initializationProcess: function(initModelsCallback) {
+      let me = this;
       this.stepsDefinition[
         this.stepIndex('OB.CountSafeBox.StepSafeBoxList')
       ].loaded = false;
@@ -71,52 +72,55 @@ OB.OBPOSCountSafeBox.Model.CountSafeBox = OB.OBPOSCloseCash.Model.CloseCash.exte
 
       this.set('safeBoxesList', new Backbone.Collection());
 
-      this.setPaymentList(false);
+      this.setPaymentList(false, function() {
+        initModelsCallback();
 
-      initModelsCallback();
-
-      const indexStepSafeBoxList = this.stepIndex(
-        'OB.CountSafeBox.StepSafeBoxList'
-      );
+        const indexStepSafeBoxList = me.stepIndex(
+          'OB.CountSafeBox.StepSafeBoxList'
+        );
 
-      if (
-        OB.MobileApp.model.hasPermission('OBPOS_approval.manager.safebox', true)
-      ) {
-        this.stepsDefinition[indexStepSafeBoxList].active = true;
-        this.stepsDefinition[indexStepSafeBoxList].loaded = true;
-        this.stepsDefinition[
-          this.stepIndex('OB.CloseCash.CashPayments')
-        ].loaded = true;
-        this.stepsDefinition[
-          this.stepIndex('OB.CloseCash.PaymentMethods')
-        ].loaded = true;
-      } else {
-        this.stepsDefinition[indexStepSafeBoxList].loaded = true;
-      }
+        if (
+          OB.MobileApp.model.hasPermission(
+            'OBPOS_approval.manager.safebox',
+            true
+          )
+        ) {
+          me.stepsDefinition[indexStepSafeBoxList].active = true;
+          me.stepsDefinition[indexStepSafeBoxList].loaded = true;
+          me.stepsDefinition[
+            me.stepIndex('OB.CloseCash.CashPayments')
+          ].loaded = true;
+          me.stepsDefinition[
+            me.stepIndex('OB.CloseCash.PaymentMethods')
+          ].loaded = true;
+        } else {
+          me.stepsDefinition[indexStepSafeBoxList].loaded = true;
+        }
 
-      this.loadSafeBoxesInformation(
-        safeBoxes => {
-          if (
-            OB.MobileApp.model.hasPermission(
-              'OBPOS_approval.manager.safebox',
-              true
-            ) &&
-            safeBoxes.length > 0
-          ) {
-            // Insert all safe boxes in safeBoxList
-            this.get('safeBoxesList').reset(safeBoxes);
-            this.finishLoad();
-          } else if (safeBoxes.length === 1) {
-            // At this point, only 1 safeBox should we received
-            const safeBox = safeBoxes[0];
-            this.prepareCashUpReport(safeBox, () => this.finishLoad());
+        me.loadSafeBoxesInformation(
+          safeBoxes => {
+            if (
+              OB.MobileApp.model.hasPermission(
+                'OBPOS_approval.manager.safebox',
+                true
+              ) &&
+              safeBoxes.length > 0
+            ) {
+              // Insert all safe boxes in safeBoxList
+              me.get('safeBoxesList').reset(safeBoxes);
+              me.finishLoad();
+            } else if (safeBoxes.length === 1) {
+              // At this point, only 1 safeBox should we received
+              const safeBox = safeBoxes[0];
+              me.prepareCashUpReport(safeBox, () => me.finishLoad());
+            }
+          },
+          () => {
+            initModelsCallback();
+            me.finishLoad();
           }
-        },
-        () => {
-          initModelsCallback();
-          this.finishLoad();
-        }
-      );
+        );
+      });
     },
     prepareCashUpReport: function(safeBox, callback) {
       let safeBoxPayments = new Backbone.Collection();
-- 
2.20.1

