From e7d850199410196fa33c82c7a6f1ee2c53474093 Mon Sep 17 00:00:00 2001
From: Shi Yao Wang <shi-yao.wang@inlibro.com>
Date: Tue, 10 May 2022 13:34:15 -0400
Subject: [PATCH] Bug 30139: Fix javascript moneyFormat to be compatible with
 FR format

New way of formatting money amount to make it work with FR format

Test plan:
1- Set CurrencyFormat pref to FR
2- Activate Point of sale and cash register features
3- Add cash register
4- Add debit type that 'can be sold' with a price of 10.00
5- Go to point of sale, click on the item to purchase
6- Verify the Amount to be paid shows the wrong amount and can't be fixed on the interface
7- Apply the patch
8- Refresh the page and do step 5 again
9- Notice it now shows the right amount

Signed-off-by: David Nind <david@davidnind.com>
---
 .../intranet-tmpl/prog/en/modules/pos/pay.tt  | 27 ++++++++++---------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
index 531869cb25..905ed4c0f0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/pos/pay.tt
@@ -203,28 +203,29 @@
         var newValue = textObj.value;
         var decAmount = "";
         var dolAmount = "";
-        var decFlag   = false;
+        var dolFlag   = false;
         var aChar     = "";
 
-        for(var i=0; i < newValue.length; i++) {
-            aChar = newValue.substring(i, i+1);
-            if (aChar >= "0" && aChar <= "9") {
-                if(decFlag) {
-                    decAmount = "" + decAmount + aChar;
+        for(var i = newValue.length; 0 < i; i--) {
+            aChar = newValue.substring(i-1, i);
+            if ("0" <= aChar && aChar <= "9") {
+                if(dolFlag) {
+                    dolAmount = "" + aChar + dolAmount;
                 }
                 else {
-                    dolAmount = "" + dolAmount + aChar;
+                    decAmount = "" + aChar + decAmount;
                 }
             }
-            if (aChar == ".") {
-                if (decFlag) {
-                    dolAmount = "";
-                    break;
-                }
-                decFlag = true;
+            if (aChar == "." || aChar == ",") {
+                dolFlag = true;
             }
         }
 
+        if (!dolFlag) {
+            dolAmount = decAmount;
+            decAmount = "";
+        }
+
         if (dolAmount == "") {
             dolAmount = "0";
         }
-- 
2.30.2