# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1496822493 -19800
#      Wed Jun 07 13:31:33 2017 +0530
# Node ID 87726c2ebd6168dacefabf4136fd57c5a79fe11a
# Parent  7c326d82a3b44bc239b3c046024ab0437e71f037
Related to issue 35830 : Payment method amount should be prevalidated if it is configured with provider

diff -r 7c326d82a3b4 -r 87726c2ebd61 src-db/database/sourcedata/AD_MESSAGE.xml
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Jun 05 12:16:17 2017 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Wed Jun 07 13:31:33 2017 +0530
@@ -607,7 +607,7 @@
 <!--0E92603689384A99BA501760F099AB46-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--0E92603689384A99BA501760F099AB46-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--0E92603689384A99BA501760F099AB46-->  <VALUE><![CDATA[OBPOS_NoEnoughCash]]></VALUE>
-<!--0E92603689384A99BA501760F099AB46-->  <MSGTEXT><![CDATA[There is not enough cash available.]]></MSGTEXT>
+<!--0E92603689384A99BA501760F099AB46-->  <MSGTEXT><![CDATA[There is not enough cash available]]></MSGTEXT>
 <!--0E92603689384A99BA501760F099AB46-->  <MSGTYPE><![CDATA[I]]></MSGTYPE>
 <!--0E92603689384A99BA501760F099AB46-->  <AD_MODULE_ID><![CDATA[FF808181326CC34901326D53DBCF0018]]></AD_MODULE_ID>
 <!--0E92603689384A99BA501760F099AB46-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
@@ -5537,6 +5537,18 @@
 <!--9AAC477E73BD4EF78C1DBA133F2E04D9-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
 <!--9AAC477E73BD4EF78C1DBA133F2E04D9--></AD_MESSAGE>
 
+<!--9ABD838BA24749CFB31BDB44AA4BCC54--><AD_MESSAGE>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <AD_MESSAGE_ID><![CDATA[9ABD838BA24749CFB31BDB44AA4BCC54]]></AD_MESSAGE_ID>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <VALUE><![CDATA[OBPOS_ErrorInProviderAddPayment]]></VALUE>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <MSGTEXT><![CDATA[%0, if amount %1 is added under %2 payment method]]></MSGTEXT>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <AD_MODULE_ID><![CDATA[FF808181326CC34901326D53DBCF0018]]></AD_MODULE_ID>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54-->  <ISINCLUDEINI18N><![CDATA[N]]></ISINCLUDEINI18N>
+<!--9ABD838BA24749CFB31BDB44AA4BCC54--></AD_MESSAGE>
+
 <!--9ADC48AFE274433C82DE67C1CBCA18EF--><AD_MESSAGE>
 <!--9ADC48AFE274433C82DE67C1CBCA18EF-->  <AD_MESSAGE_ID><![CDATA[9ADC48AFE274433C82DE67C1CBCA18EF]]></AD_MESSAGE_ID>
 <!--9ADC48AFE274433C82DE67C1CBCA18EF-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r 7c326d82a3b4 -r 87726c2ebd61 web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js	Mon Jun 05 12:16:17 2017 +0200
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js	Wed Jun 07 13:31:33 2017 +0530
@@ -93,7 +93,8 @@
     }
 
     if (OB.DEC.compare(amount) > 0) {
-      var provider, receiptToPay, me = this;
+      var provider, receiptToPay, me = this,
+          paymentLine, auxReceipt;
       if (this.model.get('leftColumnViewManager').isOrder()) {
         receiptToPay = this.receipt;
       }
@@ -108,20 +109,51 @@
         provider = paymentMethod.refundProvider;
       }
 
+      paymentLine = new OB.Model.PaymentLine({
+        'kind': key,
+        'name': name,
+        'amount': amount,
+        'rate': rate,
+        'mulrate': mulrate,
+        'isocode': isocode,
+        'allowOpenDrawer': paymentMethod.allowopendrawer,
+        'isCash': paymentMethod.iscash,
+        'openDrawer': paymentMethod.openDrawer,
+        'printtwice': paymentMethod.printtwice
+      });
+
       if (provider) {
-        this.doShowPopup({
-          popup: 'modalpayment',
-          args: {
-            'receipt': receiptToPay,
-            'provider': provider,
-            'key': key,
-            'name': name,
-            'paymentMethod': paymentMethod,
-            'amount': amount,
-            'rate': rate,
-            'mulrate': mulrate,
-            'isocode': isocode
-          }
+        if (this.model.get('leftColumnViewManager').isOrder()) {
+          auxReceipt = new OB.Model.Order();
+        } else if (this.model.get('leftColumnViewManager').isMultiOrder()) {
+          auxReceipt = new OB.Model.MultiOrders();
+        }
+        OB.UTIL.clone(receiptToPay, auxReceipt);
+        auxReceipt.set('id', null);
+        auxReceipt.addPayment(paymentLine, function () {
+          receiptToPay.trigger('checkValidPaymentsByReceipt', auxReceipt, amount, function (result) {
+            if (me.model.get('leftColumnViewManager').isOrder()) {
+              OB.Dal.remove(auxReceipt);
+            } else if (me.model.get('leftColumnViewManager').isMultiOrder()) {
+              auxReceipt.resetValues();
+            }
+            if (result) {
+              me.doShowPopup({
+                popup: 'modalpayment',
+                args: {
+                  'receipt': receiptToPay,
+                  'provider': provider,
+                  'key': key,
+                  'name': name,
+                  'paymentMethod': paymentMethod,
+                  'amount': amount,
+                  'rate': rate,
+                  'mulrate': mulrate,
+                  'isocode': isocode
+                }
+              });
+            }
+          });
         });
       } else {
         // Calculate total amount to pay with selected PaymentMethod  
@@ -154,18 +186,7 @@
             currency: '',
             symbolAtRight: true
           });
-          this.model.addPayment(new OB.Model.PaymentLine({
-            'kind': key,
-            'name': name,
-            'amount': amount,
-            'rate': rate,
-            'mulrate': mulrate,
-            'isocode': isocode,
-            'allowOpenDrawer': paymentMethod.allowopendrawer,
-            'isCash': paymentMethod.iscash,
-            'openDrawer': paymentMethod.openDrawer,
-            'printtwice': paymentMethod.printtwice
-          }), callback);
+          this.model.addPayment(paymentLine, callback);
         }
       }
     }
diff -r 7c326d82a3b4 -r 87726c2ebd61 web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js	Mon Jun 05 12:16:17 2017 +0200
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js	Wed Jun 07 13:31:33 2017 +0530
@@ -245,7 +245,8 @@
   }],
 
   receiptChanged: function () {
-    var me = this;
+    var me = this,
+        validatePaymentsByReceipt;
     this.$.payments.setCollection(this.receipt.get('payments'));
     this.$.multiPayments.setCollection(this.model.get('multiOrders').get('payments'));
     this.receipt.on('change:payment change:change calculategross change:bp change:gross', function () {
@@ -292,6 +293,30 @@
     this.receipt.on('extrainfo', function (info) {
       this.updateExtraInfo(info);
     }, this);
+
+    validatePaymentsByReceipt = function (auxReceipt, amount, callback) {
+      var selectedPayment = OB.MobileApp.model.paymentnames[auxReceipt.get('selectedPayment') || OB.MobileApp.model.get('paymentcash')];
+      me.$.errorLabelArea.hide();
+      me.checkValidPayments(auxReceipt.getPaymentStatus(), selectedPayment, function () {
+        var firstShowingObject = me.getFirstShowingObject(me.errorLabels),
+            hasPaymentError = _.isEmpty(firstShowingObject);
+        me.resetLabelAndButton();
+        if (!hasPaymentError) {
+          OB.UTIL.showConfirmation.display('', OB.I18N.getLabel('OBPOS_ErrorInProviderAddPayment', [firstShowingObject.getContent(), amount, selectedPayment.payment.commercialName]));
+          me.receipt.stopAddingPayments = false;
+        }
+        if (callback) {
+          callback(hasPaymentError);
+        }
+      });
+    };
+
+    this.receipt.on('checkValidPaymentsByReceipt', function (auxReceipt, amount, callback) {
+      validatePaymentsByReceipt(auxReceipt, amount, callback);
+    }, this);
+    this.model.get('multiOrders').on('checkValidPaymentsByReceipt', function (auxReceipt, amount, callback) {
+      validatePaymentsByReceipt(auxReceipt, amount, callback);
+    }, this);
   },
 
   updateExtraInfo: function (info) {
@@ -653,8 +678,8 @@
     return check;
   },
 
-  checkValidPayments: function (paymentstatus, selectedPayment) {
-    var resultOK, me = this;
+  checkValidPayments: function (paymentstatus, selectedPayment, callback) {
+    var resultOK, callbackFunc, me = this;
 
     if (!selectedPayment) {
       return;
@@ -667,6 +692,11 @@
     }
     this.$.noenoughchangelbl.hide();
     this.$.onlycashpaymentmethod.hide();
+    callbackFunc = function () {
+      if (callback) {
+        callback();
+      }
+    };
 
     // Do the checkins
     this.receipt.stopAddingPayments = !_.isEmpty(this.getShowingErrorMessages());
@@ -676,6 +706,7 @@
         // avoid checking for shared paymentMethod
         if (paymentstatus.change && selectedPayment.paymentMethod.isshared) {
           resultOK = true;
+          callbackFunc();
         } else {
           resultOK = this.checkEnoughCashAvailable(paymentstatus, selectedPayment, this, 'Done', function (success) {
             var lsuccess = success;
@@ -694,6 +725,7 @@
             this.checkEnoughCashAvailable(paymentstatus, selectedPayment, this, 'Credit', function (success) {
               this.setStatusButtons(success, 'Credit');
             });
+            callbackFunc();
           });
         }
       } else if (!this.receipt.stopAddingPayments) {
@@ -701,13 +733,14 @@
         this.$.exactbutton.setLocalDisabled(false);
         this.$.layawayaction.setLocalDisabled(false);
         this.$.creditsalesaction.setLocalDisabled(false);
+        callbackFunc();
       }
-
     } else {
       me.receipt.stopAddingPayments = !_.isEmpty(me.getShowingErrorMessages());
       // Finally set status of buttons
       this.setStatusButtons(resultOK, 'Done');
       this.setStatusButtons(resultOK, 'Layaway');
+      callbackFunc();
     }
     if (resultOK) {
       this.$.noenoughchangelbl.hide();
@@ -773,6 +806,19 @@
     errorLabelArray.push(this.$.extrainfo);
     return errorLabelArray;
   },
+  resetLabelAndButton: function () {
+    this.$.changeexceedlimit.hide();
+    this.$.overpaymentnotavailable.hide();
+    this.$.overpaymentexceedlimit.hide();
+    this.$.noenoughchangelbl.hide();
+    this.$.onlycashpaymentmethod.hide();
+    this.$.errorLabelArea.show();
+
+    this.$.donebutton.setLocalDisabled(false);
+    this.$.exactbutton.setLocalDisabled(false);
+    this.$.layawayaction.setLocalDisabled(false);
+    this.$.creditsalesaction.setLocalDisabled(false);
+  },
   getFirstShowingObject: function (errorLabelArray) {
     var showingObj = '',
         i;
@@ -826,7 +872,7 @@
         var arrayContent = this.errorLabels[i];
         if (arrayContent.showing && arrayContent.type === 'error') {
           count = count + 1;
-          msgToReturn = msgToReturn + '\n' + count + ')' + arrayContent.content;
+          msgToReturn = msgToReturn + '\n' + count + ') ' + arrayContent.content;
         }
       }
     }
