From 9222a199e438b4d4b2eb8a2318c41f44cf92c01f Mon Sep 17 00:00:00 2001
From: Prakash M <prakash@qualiantech.com>
Date: Fri, 13 Nov 2020 19:35:54 +0530
Subject: [PATCH] Fixed BUG-45390: Fixes js error on moving to 2nd step in
 Count Safe Box * This is because paymentExpectedList is empty *
 paymentExpectedList is created in CloseCash - initializationProcess
 (cashup-model.js) * But however this CloseCash - initializationProcess method
 is overrided in countsafebox-model.js * And created of paymentExpectedList,
 paymentEmptyList is not implemented in new method * Moved paymentList
 creation to separate function * Therefore it can be shared by both cashup and
 safe boxes models

---
 .../js/closecash/model/cashup/cashup-model.js | 196 +-----------------
 .../js/closecash/model/closecash-model.js     | 191 +++++++++++++++++
 .../model/countsafebox/countsafebox-model.js  |   2 +
 3 files changed, 203 insertions(+), 186 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 422668d92..5516d6824 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
@@ -7,7 +7,7 @@
  ************************************************************************************
  */
 
-/*global OB, Backbone, _ */
+/*global OB, Backbone */
 
 OB.OBPOSCashUp = OB.OBPOSCashUp || {};
 OB.OBPOSCashUp.Model = OB.OBPOSCashUp.Model || {};
@@ -98,192 +98,16 @@ OB.OBPOSCashUp.Model.CashUp = OB.OBPOSCloseCash.Model.CloseCash.extend({
 
     this.set('orderlist', new OB.Collection.OrderList());
 
-    OB.Dal.find(
-      OB.Model.CashUp,
-      {
-        isprocessed: 'N'
-      },
-      cashUp => {
-        OB.Dal.find(
-          OB.Model.PaymentMethodCashUp,
-          {
-            cashup_id: cashUp.at(0).get('id'),
-            _orderByClause: 'lineNo asc'
-          },
-          payMthds => {
-            //OB.Dal.find success
-            // Get list of active payments
-            let activePaymentsList = [],
-              tempList = new Backbone.Collection();
-            OB.MobileApp.model.get('payments').forEach(payment => {
-              if (
-                payment.payment.active === true &&
-                payment.paymentMethod.countpaymentincashup &&
-                !payment.paymentMethod.issafebox
-              ) {
-                activePaymentsList.push(payment);
-              }
-            });
-            activePaymentsList.forEach((payment, index) => {
-              let expected = 0;
-              const auxPay = payMthds.filter(function(payMthd) {
-                return payMthd.get('paymentmethod_id') === payment.payment.id;
-              })[0];
-              if (!auxPay) {
-                //We cannot find this payment in local database, it must be a new payment method, we skip it.
-                return;
-              }
+    this.setPaymentList(true);
 
-              // Not add shared payment to slave terminal
-              if (
-                !terminalSlave ||
-                !OB.MobileApp.model.paymentnames[payment.payment.searchKey]
-                  .paymentMethod.isshared
-              ) {
-                auxPay.set('_id', payment.payment.searchKey);
-                auxPay.set('isocode', payment.isocode);
-                auxPay.set('paymentMethod', payment.paymentMethod);
-                auxPay.set('id', payment.payment.id);
-                if (auxPay.get('totalDeposits') === null) {
-                  auxPay.set('totalDeposits', 0);
-                }
-                if (auxPay.get('totalDrops') === null) {
-                  auxPay.set('totalDrops', 0);
-                }
-                const cStartingCash = auxPay.get('startingCash'),
-                  cTotalReturns = auxPay.get('totalReturns'),
-                  cTotalSales = auxPay.get('totalSales'),
-                  cTotalDeposits = OB.DEC.sub(
-                    auxPay.get('totalDeposits'),
-                    OB.DEC.abs(auxPay.get('totalDrops'))
-                  );
-                expected = OB.DEC.add(
-                  OB.DEC.add(
-                    cStartingCash,
-                    OB.DEC.sub(
-                      cTotalSales,
-                      cTotalReturns,
-                      payment.obposPosprecision
-                    ),
-                    payment.obposPosprecision
-                  ),
-                  cTotalDeposits,
-                  payment.obposPosprecision
-                );
-                const fromCurrencyId = auxPay.get('paymentMethod').currency;
-                auxPay.set(
-                  'expected',
-                  OB.UTIL.currency.toDefaultCurrency(fromCurrencyId, expected)
-                );
-                auxPay.set('foreignExpected', expected);
-                const paymentShared =
-                  (OB.POS.modelterminal.get('terminal').ismaster ||
-                    OB.POS.modelterminal.get('terminal').isslave) &&
-                  OB.MobileApp.model.paymentnames[payment.payment.searchKey]
-                    .paymentMethod.isshared;
-                if (paymentShared) {
-                  auxPay.set(
-                    'name',
-                    auxPay.get('name') +
-                      (paymentShared
-                        ? OB.I18N.getLabel('OBPOS_LblPaymentMethodShared')
-                        : '')
-                  );
-                }
-                tempList.add(auxPay);
-              }
-
-              if (index === activePaymentsList.length - 1) {
-                if (terminalSlave && tempList.length === 0) {
-                  // Desactivate all steps
-                  this.stepsDefinition[
-                    this.stepIndex('OB.CloseCash.PaymentMethods')
-                  ].active = false;
-                }
-                if (
-                  OB.MobileApp.model.hasPermission(
-                    'OBPOS_retail.cashupGroupExpectedPayment',
-                    true
-                  )
-                ) {
-                  // Split payment methods
-                  const expectedList = _.filter(tempList.models, function(pm) {
-                      return pm.get('expected') !== 0;
-                    }),
-                    emptyList = _.filter(tempList.models, function(pm) {
-                      return pm.get('expected') === 0;
-                    });
-
-                  this.set(
-                    'paymentExpectedList',
-                    new Backbone.Collection(expectedList)
-                  );
-                  this.set(
-                    'paymentEmptyList',
-                    new Backbone.Collection(emptyList)
-                  );
-
-                  if (emptyList.length > 0) {
-                    emptyList[0].set('firstEmptyPayment', true);
-                  }
-                  const models = expectedList.concat(emptyList);
-
-                  this.get('paymentList').reset(models);
-                } else {
-                  this.get('paymentList').reset(tempList.models);
-                }
-                // Active/Desactive CashPayments and CashToKeep tabs
-                let cashPayments = false,
-                  cashToKeep = false;
-                const paymentsIndex = this.stepIndex(
-                    'OB.CloseCash.CashPayments'
-                  ),
-                  toKeepIndex = this.stepIndex('OB.CloseCash.CashToKeep');
-                for (let i = 0; i < tempList.length; i++) {
-                  if (
-                    this.closeCashSteps[paymentsIndex].isSubstepAvailable(
-                      this,
-                      i
-                    )
-                  ) {
-                    cashPayments = true;
-                  }
-                  if (
-                    this.closeCashSteps[toKeepIndex].isSubstepAvailable(this, i)
-                  ) {
-                    cashToKeep = true;
-                  }
-                }
-                this.stepsDefinition[paymentsIndex].active = cashPayments;
-                this.stepsDefinition[toKeepIndex].active = cashToKeep;
-                this.set(
-                  'totalExpected',
-                  this.get('paymentList').models.reduce((total, model) => {
-                    return OB.DEC.add(total, model.get('expected'));
-                  }, 0)
-                );
-                this.set(
-                  'totalDifference',
-                  OB.DEC.sub(
-                    this.get('totalDifference'),
-                    this.get('totalExpected')
-                  )
-                );
-                this.setIgnoreStep3();
-              }
-            });
-            this.stepsDefinition[
-              this.stepIndex('OB.CloseCash.CashPayments')
-            ].loaded = true;
-            this.stepsDefinition[
-              this.stepIndex('OB.CloseCash.PaymentMethods')
-            ].loaded = true;
-            synch1 = true;
-            finish();
-          }
-        );
-      }
-    );
+    this.stepsDefinition[
+      this.stepIndex('OB.CloseCash.CashPayments')
+    ].loaded = true;
+    this.stepsDefinition[
+      this.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 e905649da..f5599d62c 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
@@ -435,5 +435,196 @@ OB.OBPOSCloseCash.Model.CloseCash = OB.Model.TerminalWindowModel.extend({
   },
   processAndFinishCloseCash: function() {
     this.processAndFinish();
+  },
+  setPaymentList: function(validateSteps) {
+    const terminalSlave =
+      !OB.POS.modelterminal.get('terminal').ismaster &&
+      OB.POS.modelterminal.get('terminal').isslave;
+
+    OB.Dal.find(
+      OB.Model.CashUp,
+      {
+        isprocessed: 'N'
+      },
+      cashUp => {
+        OB.Dal.find(
+          OB.Model.PaymentMethodCashUp,
+          {
+            cashup_id: cashUp.at(0).get('id'),
+            _orderByClause: 'lineNo asc'
+          },
+          payMthds => {
+            //OB.Dal.find success
+            // Get list of active payments
+            let activePaymentsList = [],
+              tempList = new Backbone.Collection();
+            OB.MobileApp.model.get('payments').forEach(payment => {
+              if (
+                payment.payment.active === true &&
+                payment.paymentMethod.countpaymentincashup &&
+                !payment.paymentMethod.issafebox
+              ) {
+                activePaymentsList.push(payment);
+              }
+            });
+            activePaymentsList.forEach((payment, index) => {
+              let expected = 0;
+              const auxPay = payMthds.filter(function(payMthd) {
+                return payMthd.get('paymentmethod_id') === payment.payment.id;
+              })[0];
+              if (!auxPay) {
+                //We cannot find this payment in local database, it must be a new payment method, we skip it.
+                return;
+              }
+
+              // Not add shared payment to slave terminal
+              if (
+                !terminalSlave ||
+                !OB.MobileApp.model.paymentnames[payment.payment.searchKey]
+                  .paymentMethod.isshared
+              ) {
+                auxPay.set('_id', payment.payment.searchKey);
+                auxPay.set('isocode', payment.isocode);
+                auxPay.set('paymentMethod', payment.paymentMethod);
+                auxPay.set('id', payment.payment.id);
+                if (auxPay.get('totalDeposits') === null) {
+                  auxPay.set('totalDeposits', 0);
+                }
+                if (auxPay.get('totalDrops') === null) {
+                  auxPay.set('totalDrops', 0);
+                }
+                const cStartingCash = auxPay.get('startingCash'),
+                  cTotalReturns = auxPay.get('totalReturns'),
+                  cTotalSales = auxPay.get('totalSales'),
+                  cTotalDeposits = OB.DEC.sub(
+                    auxPay.get('totalDeposits'),
+                    OB.DEC.abs(auxPay.get('totalDrops'))
+                  );
+                expected = OB.DEC.add(
+                  OB.DEC.add(
+                    cStartingCash,
+                    OB.DEC.sub(
+                      cTotalSales,
+                      cTotalReturns,
+                      payment.obposPosprecision
+                    ),
+                    payment.obposPosprecision
+                  ),
+                  cTotalDeposits,
+                  payment.obposPosprecision
+                );
+                const fromCurrencyId = auxPay.get('paymentMethod').currency;
+                auxPay.set(
+                  'expected',
+                  OB.UTIL.currency.toDefaultCurrency(fromCurrencyId, expected)
+                );
+                auxPay.set('foreignExpected', expected);
+                const paymentShared =
+                  (OB.POS.modelterminal.get('terminal').ismaster ||
+                    OB.POS.modelterminal.get('terminal').isslave) &&
+                  OB.MobileApp.model.paymentnames[payment.payment.searchKey]
+                    .paymentMethod.isshared;
+                if (paymentShared) {
+                  auxPay.set(
+                    'name',
+                    auxPay.get('name') +
+                      (paymentShared
+                        ? OB.I18N.getLabel('OBPOS_LblPaymentMethodShared')
+                        : '')
+                  );
+                }
+                tempList.add(auxPay);
+              }
+
+              if (index === activePaymentsList.length - 1) {
+                if (
+                  OB.MobileApp.model.hasPermission(
+                    'OBPOS_retail.cashupGroupExpectedPayment',
+                    true
+                  )
+                ) {
+                  // Split payment methods
+                  const expectedList = _.filter(tempList.models, function(pm) {
+                      return pm.get('expected') !== 0;
+                    }),
+                    emptyList = _.filter(tempList.models, function(pm) {
+                      return pm.get('expected') === 0;
+                    });
+
+                  this.set(
+                    'paymentExpectedList',
+                    new Backbone.Collection(expectedList)
+                  );
+                  this.set(
+                    'paymentEmptyList',
+                    new Backbone.Collection(emptyList)
+                  );
+
+                  if (emptyList.length > 0) {
+                    emptyList[0].set('firstEmptyPayment', true);
+                  }
+                  const models = expectedList.concat(emptyList);
+
+                  this.get('paymentList').reset(models);
+                } else {
+                  this.get('paymentList').reset(tempList.models);
+                }
+
+                if (validateSteps) {
+                  if (terminalSlave && tempList.length === 0) {
+                    // Desactivate all steps
+                    this.stepsDefinition[
+                      this.stepIndex('OB.CloseCash.PaymentMethods')
+                    ].active = false;
+                  }
+
+                  // Active/Desactive CashPayments and CashToKeep tabs
+                  let cashPayments = false,
+                    cashToKeep = false;
+                  const paymentsIndex = this.stepIndex(
+                      'OB.CloseCash.CashPayments'
+                    ),
+                    toKeepIndex = this.stepIndex('OB.CloseCash.CashToKeep');
+                  for (let i = 0; i < tempList.length; i++) {
+                    if (
+                      this.closeCashSteps[paymentsIndex].isSubstepAvailable(
+                        this,
+                        i
+                      )
+                    ) {
+                      cashPayments = true;
+                    }
+                    if (
+                      this.closeCashSteps[toKeepIndex].isSubstepAvailable(
+                        this,
+                        i
+                      )
+                    ) {
+                      cashToKeep = true;
+                    }
+                  }
+                  this.stepsDefinition[paymentsIndex].active = cashPayments;
+                  this.stepsDefinition[toKeepIndex].active = cashToKeep;
+                  this.set(
+                    'totalExpected',
+                    this.get('paymentList').models.reduce((total, model) => {
+                      return OB.DEC.add(total, model.get('expected'));
+                    }, 0)
+                  );
+                  this.set(
+                    'totalDifference',
+                    OB.DEC.sub(
+                      this.get('totalDifference'),
+                      this.get('totalExpected')
+                    )
+                  );
+                  this.setIgnoreStep3();
+                }
+              }
+            });
+          }
+        );
+      }
+    );
   }
 });
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 a8c7071d3..d30dda181 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
@@ -71,6 +71,8 @@ OB.OBPOSCountSafeBox.Model.CountSafeBox = OB.OBPOSCloseCash.Model.CloseCash.exte
 
       this.set('safeBoxesList', new Backbone.Collection());
 
+      this.setPaymentList(false);
+
       initModelsCallback();
 
       const indexStepSafeBoxList = this.stepIndex(
-- 
2.20.1

