From be9628e25db581dedcd38986220852392b07e630 Mon Sep 17 00:00:00 2001
From: Beatriz Lago <beatrizlago@openbravo.com>
Date: Mon, 15 Nov 2021 12:04:02 +0100
Subject: [PATCH] Fixed ISSUE-42143: It is not possible to return cash for a
 cancelled layaway in more than one currency.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The problem was that POS was only taking into account the cash avaiable in the selected payment method.
That is, having 400€ and 100$ would make a total of 476.08€ in cash. If you had to return this amount and
selected the cash payment method, a message would appear saying there is not enough cash, because it was
only counting the 400€ of that specific payment method, instead of the total sum of all the payment methods.
I changed this so it would check if you have enough cash in total to pay the return, and then checks the
selected payment method cash so that you cannot return 450€ if you only have 400€ in this currency.
---
 .../cashup/PaymentMethodUtils.js              | 19 +++++++++++
 .../js/pointofsale/view/payment.js            | 33 +++++++++++--------
 2 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/web/org.openbravo.retail.posterminal/app/model/business-object/cashup/PaymentMethodUtils.js b/web/org.openbravo.retail.posterminal/app/model/business-object/cashup/PaymentMethodUtils.js
index e8bc0b437..333f08b08 100644
--- a/web/org.openbravo.retail.posterminal/app/model/business-object/cashup/PaymentMethodUtils.js
+++ b/web/org.openbravo.retail.posterminal/app/model/business-object/cashup/PaymentMethodUtils.js
@@ -249,6 +249,25 @@
       );
       return { currentCash, foreignCurrentCash };
     },
+    getAllCashAvaiable(paymentMethods) {
+      let currentCash = OB.DEC.Zero;
+      paymentMethods.forEach(method => {
+        const paymentMethod = OB.MobileApp.model.paymentnames[method.searchKey];
+        if (paymentMethod && paymentMethod.paymentMethod.iscash) {
+          currentCash = OB.DEC.add(
+            currentCash,
+            OB.App.State.Cashup.Utils.getPaymentMethodCurrentCash(
+              paymentMethods,
+              paymentMethod.payment.id,
+              OB.MobileApp.model.paymentnames,
+              OB.UTIL.currency.webPOSDefaultCurrencyId(),
+              OB.UTIL.currency.conversions
+            ).currentCash || OB.DEC.Zero
+          );
+        }
+      });
+      return currentCash;
+    },
 
     /**
      *   Only send to backend the payments that we have in the terminal.
diff --git a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
index 3aa17abc4..5cbaa387f 100644
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
@@ -1587,7 +1587,7 @@ enyo.kind({
       this,
       function(currentCash) {
         var changeAmt, selectedChange;
-
+        var totalCash = currentCash;
         // If there are reverse payments search for those of cash payment method. It will be needed to check if there is enough cash to reverse those payments.
         if (paymentstatus.isReversal) {
           paymentstatus.payments.each(function(payment) {
@@ -1626,6 +1626,14 @@ enyo.kind({
         }
 
         if (hasEnoughCash) {
+          var currentPaymentMethodCash = OB.App.State.Cashup.Utils.getPaymentMethodCurrentCash(
+            OB.App.State.getState().Cashup.cashPaymentMethodInfo,
+            selectedPayment.payment.id,
+            OB.MobileApp.model.paymentnames,
+            OB.UTIL.currency.webPOSDefaultCurrencyId(),
+            OB.UTIL.currency.conversions
+          ).currentCash;
+
           if (
             OB.UTIL.isNullOrUndefined(selectedPayment) ||
             !selectedPayment.paymentMethod.iscash
@@ -1651,6 +1659,10 @@ enyo.kind({
                   requiredCash,
                   payment.get('origAmount')
                 );
+                currentPaymentMethodCash = OB.DEC.sub(
+                  currentPaymentMethodCash,
+                  payment.get('origAmount')
+                );
               } else {
                 OB.POS.terminal.terminal.paymentnames[payment.get('kind')];
                 if (
@@ -1693,7 +1705,10 @@ enyo.kind({
             hasEnoughCash = true;
           } else if (!_.isUndefined(requiredCash)) {
             hasEnoughCash =
-              OB.DEC.compare(OB.DEC.sub(currentCash, requiredCash)) >= 0;
+              OB.DEC.compare(OB.DEC.sub(totalCash, requiredCash)) >= 0;
+            if (currentPaymentMethodCash < 0) {
+              hasEnoughCash = false;
+            }
           }
 
           failedPaymentMethods = this.checkEnoughMultiChange();
@@ -2228,17 +2243,9 @@ enyo.kind({
       }
     }
 
-    var currentCash = OB.DEC.Zero;
-    if (selectedPayment && selectedPayment.paymentMethod.iscash) {
-      currentCash =
-        OB.App.State.Cashup.Utils.getPaymentMethodCurrentCash(
-          OB.App.State.getState().Cashup.cashPaymentMethodInfo,
-          selectedPayment.payment.id,
-          OB.MobileApp.model.paymentnames,
-          OB.UTIL.currency.webPOSDefaultCurrencyId(),
-          OB.UTIL.currency.conversions
-        ).currentCash || OB.DEC.Zero;
-    }
+    var currentCash = OB.App.State.Cashup.Utils.getAllCashAvaiable(
+      OB.App.State.getState().Cashup.cashPaymentMethodInfo
+    );
     if (
       (OB.POS.modelterminal.get('terminal').ismaster ||
         OB.POS.modelterminal.get('terminal').isslave) &&
-- 
GitLab

