# HG changeset patch
# User Adrián Romero <adrianromero@openbravo.com>
# Date 1528800121 -7200
#      Tue Jun 12 12:42:01 2018 +0200
# Node ID df58b5110b8efed22afe47b9de728a90bddee334
# Parent  6c2de6fd6df897a359ea8ef22da2738255c59216
[MultiChange] Fixes pending calculation with the precision of the payment currency

diff --git a/web/org.openbravo.retail.posterminal/js/model/order.js b/web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js
@@ -4358,15 +4358,8 @@
       }
     },
     getPrecision: function (payment) {
-      var i, p, max;
-      for (i = 0, max = OB.MobileApp.model.get('payments').length; i < max; i++) {
-        p = OB.MobileApp.model.get('payments')[i];
-        if (p.payment.searchKey === payment.paymenttype) {
-          if (p.obposPrecision) {
-            return p.obposPrecision;
-          }
-        }
-      }
+      var p = OB.MobileApp.model.paymentnames[payment.get('kind')];
+      return p ? p.obposPosprecision : OB.DEC.getScale();
     },
     getSumOfOrigAmounts: function (paymentToIgnore) {
       //returns a result with the sum up of every payments based on origAmount field
@@ -4396,9 +4389,10 @@
       //Result is returned in the currency used by current payment
       var differenceInDefaultCurrency;
       var differenceInForeingCurrency;
+      var p = this.getPrecision(currentPayment);
       differenceInDefaultCurrency = this.getDifferenceBetweenPaymentsAndTotal(currentPayment);
       if (currentPayment && currentPayment.get('rate')) {
-        differenceInForeingCurrency = OB.DEC.div(differenceInDefaultCurrency, currentPayment.get('rate'));
+        differenceInForeingCurrency = OB.DEC.div(differenceInDefaultCurrency, currentPayment.get('rate'), p);
         return differenceInForeingCurrency;
       } else {
         return differenceInDefaultCurrency;
@@ -4448,7 +4442,7 @@
           //and finally we transform this difference to the foreign amount
           //if the payment in the foreign amount makes pending to pay zero, then we will ensure that the payment
           //in the default currency is satisfied
-          if (OB.DEC.compare(OB.DEC.sub(this.getDifferenceRemovingSpecificPayment(p), OB.DEC.abs(p.get('amount')))) === OB.DEC.Zero) {
+          if (OB.DEC.compare(OB.DEC.sub(this.getDifferenceRemovingSpecificPayment(p), OB.DEC.abs(p.get('amount'), precision), precision)) === OB.DEC.Zero) {
             multiCurrencyDifference = this.getDifferenceBetweenPaymentsAndTotal(p);
             if (OB.DEC.abs(p.get('origAmount')) !== OB.DEC.abs(multiCurrencyDifference)) {
               p.set('origAmount', multiCurrencyDifference);
@@ -6806,15 +6800,8 @@
       };
     },
     getPrecision: function (payment) {
-      var i, p, max;
-      for (i = 0, max = OB.MobileApp.model.get('payments').length; i < max; i++) {
-        p = OB.MobileApp.model.get('payments')[i];
-        if (p.payment.searchKey === payment.paymenttype) {
-          if (p.obposPrecision) {
-            return p.obposPrecision;
-          }
-        }
-      }
+      var p = OB.MobileApp.model.paymentnames[payment.get('kind')];
+      return p ? p.obposPosprecision : OB.DEC.getScale();
     },
     getSumOfOrigAmounts: function (paymentToIgnore) {
       //returns a result with the sum up of every payments based on origAmount field
@@ -6844,9 +6831,10 @@
       //Result is returned in the currency used by current payment
       var differenceInDefaultCurrency;
       var differenceInForeingCurrency;
+      var p = this.getPrecision(currentPayment);
       differenceInDefaultCurrency = this.getDifferenceBetweenPaymentsAndTotal(currentPayment);
       if (currentPayment && currentPayment.get('rate')) {
-        differenceInForeingCurrency = OB.DEC.div(differenceInDefaultCurrency, currentPayment.get('rate'));
+        differenceInForeingCurrency = OB.DEC.div(differenceInDefaultCurrency, currentPayment.get('rate'), p);
         return differenceInForeingCurrency;
       } else {
         return differenceInDefaultCurrency;
@@ -6878,7 +6866,7 @@
           //and finally we transform this difference to the foreign amount
           //if the payment in the foreign amount makes pending to pay zero, then we will ensure that the payment
           //in the default currency is satisfied
-          if (OB.DEC.compare(OB.DEC.sub(this.getDifferenceRemovingSpecificPayment(p), OB.DEC.abs(p.get('amount')))) === OB.DEC.Zero) {
+          if (OB.DEC.compare(OB.DEC.sub(this.getDifferenceRemovingSpecificPayment(p), OB.DEC.abs(p.get('amount'), precision), precision)) === OB.DEC.Zero) {
             multiCurrencyDifference = this.getDifferenceBetweenPaymentsAndTotal(p);
             if (p.get('origAmount') !== multiCurrencyDifference) {
               p.set('origAmount', multiCurrencyDifference);
# HG changeset patch
# User Adrián Romero <adrianromero@openbravo.com>
# Date 1528821271 -7200
#      Tue Jun 12 18:34:31 2018 +0200
# Node ID 80caaeefb9d4646d451a61e1bfe824f94d775f38
# Parent  df58b5110b8efed22afe47b9de728a90bddee334
[MultiChange] Fixes pending calculation with the precision of the payment currency.

diff --git a/web/org.openbravo.retail.posterminal/js/model/order.js b/web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js
@@ -4518,7 +4518,7 @@
     },
 
     addPayment: function (payment, callback) {
-      var payments, total;
+      var payments, total, precision;
       var i, max, p, order;
 
       if (this.get('isPaid') && !payment.get('isReversePayment') && !this.get('doCancelAndReplace') && this.getPrePaymentQty() === OB.DEC.sub(this.getTotal(), this.getCredit()) && !this.isNewReversed()) {
@@ -4547,6 +4547,7 @@
 
       payments = this.get('payments');
       total = OB.DEC.abs(this.getTotal());
+      precision = this.getPrecision(payment);
       OB.UTIL.HookManager.executeHooks('OBPOS_preAddPayment', {
         paymentToAdd: payment,
         payments: payments,
@@ -4588,7 +4589,7 @@
             for (i = 0, max = payments.length; i < max; i++) {
               p = payments.at(i);
               if (p.get('kind') === payment.get('kind') && !p.get('isPrePayment') && !p.get('reversedPaymentId')) {
-                p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount')));
+              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount'), precision));
                 if (p.get('rate') && p.get('rate') !== '1') {
                   p.set('origAmount', OB.DEC.add(payment.get('origAmount'), OB.DEC.mul(p.get('origAmount'), p.get('rate'))));
                 }
@@ -4601,7 +4602,7 @@
             for (i = 0, max = payments.length; i < max; i++) {
               p = payments.at(i);
               if (p.get('kind') === payment.get('kind') && p.get('paymentData') && payment.get('paymentData') && p.get('paymentData').groupingCriteria && payment.get('paymentData').groupingCriteria && p.get('paymentData').groupingCriteria === payment.get('paymentData').groupingCriteria && !p.get('reversedPaymentId') && !p.get('isPrePayment')) {
-                p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount')));
+              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount'), precision));
                 if (p.get('rate') && p.get('rate') !== '1') {
                   p.set('origAmount', OB.DEC.add(payment.get('origAmount'), OB.DEC.mul(p.get('origAmount'), p.get('rate'))));
                 }
@@ -6928,7 +6929,7 @@
       }
     },
     addPayment: function (payment, callback) {
-      var payments, total;
+      var payments, total, precision;
       var i, max, p, order;
 
       if (!OB.DEC.isNumber(payment.get('amount'))) {
@@ -6947,6 +6948,7 @@
 
       payments = this.get('payments');
       total = OB.DEC.abs(this.getTotal());
+      precision = this.getPrecision(payment);
       order = this;
       OB.UTIL.HookManager.executeHooks('OBPOS_preAddPayment', {
         paymentToAdd: payment,
@@ -6978,7 +6980,7 @@
           for (i = 0, max = payments.length; i < max; i++) {
             p = payments.at(i);
             if (p.get('kind') === payment.get('kind') && !p.get('isPrePayment')) {
-              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount')));
+              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount'), precision));
               if (p.get('rate') && p.get('rate') !== '1') {
                 p.set('origAmount', OB.DEC.add(payment.get('origAmount'), OB.DEC.mul(p.get('origAmount'), p.get('rate'))));
               }
@@ -6993,7 +6995,7 @@
           for (i = 0, max = payments.length; i < max; i++) {
             p = payments.at(i);
             if (p.get('kind') === payment.get('kind') && p.get('paymentData') && payment.get('paymentData') && p.get('paymentData').groupingCriteria && payment.get('paymentData').groupingCriteria && p.get('paymentData').groupingCriteria === payment.get('paymentData').groupingCriteria) {
-              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount')));
+              p.set('amount', OB.DEC.add(payment.get('amount'), p.get('amount'), precision));
               if (p.get('rate') && p.get('rate') !== '1') {
                 p.set('origAmount', OB.DEC.add(payment.get('origAmount'), OB.DEC.mul(p.get('origAmount'), p.get('rate'))));
               }
diff --git a/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js b/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js
@@ -524,7 +524,7 @@
                 amount = altexactamount[exactpayment.payment.searchKey];
               }
               if (exactpayment.rate && exactpayment.rate !== '1') {
-                amount = OB.DEC.div(amount, exactpayment.rate);
+            amount = OB.DEC.div(amount, exactpayment.rate, exactpayment.obposPosprecision);
               }
 
               if (amount > 0 && exactpayment && OB.MobileApp.model.hasPermission(exactpayment.payment.searchKey)) {
diff --git a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
@@ -33,8 +33,8 @@
     }
     return null;
   },
-  setTotalPending: function (pending, mulrate, symbol, currencySymbolAtTheRight, inSender, inEvent) {
-    this.$.totalpending.setContent(OB.I18N.formatCurrencyWithSymbol(OB.DEC.mul(pending, mulrate), symbol, currencySymbolAtTheRight));
+  setTotalPending: function (pending, symbol, currencySymbolAtTheRight) {
+    this.$.totalpending.setContent(OB.I18N.formatCurrencyWithSymbol(pending, symbol, currencySymbolAtTheRight));
   },
   clearPaymentMethodSelect: function (inSender, inEvent) {
     this.$.paymentMethodSelect.setContent('');
@@ -83,7 +83,7 @@
         this.calculateChange(payment, change);
       } else if (!_.isNull(pending) && pending) {
         this.calculateChangeReset();
-        this.setTotalPending(pending, payment.mulrate, payment.symbol, payment.currencySymbolAtTheRight, inSender, inEvent);
+        this.setTotalPending(OB.DEC.mul(pending, payment.mulrate, payment.obposPosprecision), payment.symbol, payment.currencySymbolAtTheRight);
       }
       if (paymentstatus && inEvent.value.status !== "" && !this.receipt.isCalculateReceiptLocked && !this.receipt.isCalculateGrossLocked) {
         this.checkValidPayments(paymentstatus, payment);
@@ -488,6 +488,7 @@
     var paymentstatus = this.receipt.getPaymentStatus();
     var symbol = '',
         rate = OB.DEC.One,
+        precision = null,
         symbolAtRight = true,
         isCashType = true;
 
@@ -498,6 +499,7 @@
     if (!_.isUndefined(this.receipt) && !_.isUndefined(OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')])) {
       symbol = OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')].symbol;
       rate = OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')].mulrate;
+      precision = OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')].obposPosprecision;
       symbolAtRight = OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')].currencySymbolAtTheRight;
       isCashType = OB.MobileApp.model.paymentnames[this.receipt.get('selectedPayment')].paymentMethod.iscash;
     }
@@ -525,7 +527,7 @@
       this.updateCreditSalesAction();
       this.$.layawayaction.hide();
     } else {
-      this.setTotalPending(this.receipt.getPending(), rate, symbol, symbolAtRight);
+      this.setTotalPending(OB.DEC.mul(this.receipt.getPending(), rate, precision), symbol, symbolAtRight);
       this.$.totalpending.show();
       if (paymentstatus.isNegative) {
         this.$.totalpendinglbl.setContent(OB.I18N.getLabel('OBPOS_ReturnRemaining'));
@@ -577,6 +579,7 @@
     var symbol = '',
         symbolAtRight = true,
         rate = OB.DEC.One,
+        precision = null,
         isCashType = true,
         selectedPayment;
     this.updateExtraInfo('');
@@ -593,6 +596,7 @@
     if (!_.isUndefined(selectedPayment)) {
       symbol = selectedPayment.symbol;
       rate = selectedPayment.mulrate;
+      precision = selectedPayment.obposPosprecision;
       symbolAtRight = selectedPayment.currencySymbolAtTheRight;
       isCashType = selectedPayment.paymentMethod.iscash;
     }
@@ -620,7 +624,7 @@
       }
       this.updateCreditSalesAction();
     } else {
-      this.setTotalPending(OB.DEC.sub(paymentstatus.get('total'), paymentstatus.get('payment')), rate, symbol, symbolAtRight);
+      this.setTotalPending(OB.DEC.mul(OB.DEC.sub(paymentstatus.get('total'), paymentstatus.get('payment')), rate, precision), symbol, symbolAtRight);
       this.$.totalpending.show();
       this.$.totalpendinglbl.show();
       this.$.donebutton.hide();
