# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1496144289 -19800
#      Tue May 30 17:08:09 2017 +0530
# Node ID 80531ce0a8bb2b494802703208e46491783263c7
# Parent  6e7556bd0d9982ed6f9832720e89b7b325c54554
Related to issue 35830 : Payment method amount should be prevalidated if it is configured with provider

diff -r 6e7556bd0d99 -r 80531ce0a8bb src-db/database/sourcedata/AD_MESSAGE.xml
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon May 29 11:50:13 2017 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Tue May 30 17:08:09 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>
@@ -5525,6 +5525,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 6e7556bd0d99 -r 80531ce0a8bb web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js	Mon May 29 11:50:13 2017 +0200
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/keyboard-toolbars.js	Tue May 30 17:08:09 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,43 @@
         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
-          }
+        auxReceipt = new OB.Model.Order();
+        OB.UTIL.clone(receiptToPay, auxReceipt);
+        auxReceipt.set('id', null);
+        auxReceipt.addPayment(paymentLine, function () {
+          receiptToPay.trigger('checkValidPaymentsByReceipt', auxReceipt, amount, function (result) {
+            OB.Dal.remove(auxReceipt);
+            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 +178,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 6e7556bd0d99 -r 80531ce0a8bb web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js	Mon May 29 11:50:13 2017 +0200
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js	Tue May 30 17:08:09 2017 +0530
@@ -292,6 +292,24 @@
     this.receipt.on('extrainfo', function (info) {
       this.updateExtraInfo(info);
     }, this);
+    this.receipt.on('checkValidPaymentsByReceipt', function (auxReceipt, amount, callback) {
+      var me = this,
+          selectedPayment = OB.MobileApp.model.paymentnames[auxReceipt.get('selectedPayment') || OB.MobileApp.model.get('paymentcash')];
+      this.$.errorLabelArea.hide();
+      this.checkValidPayments(auxReceipt.getPaymentStatus(), selectedPayment, function () {
+        var firstShowingObject = me.getFirstShowingObject(me.errorLabels),
+            hasPaymentError = _.isEmpty(firstShowingObject);
+        me.hideErrorMessages(auxReceipt.getPaymentStatus());
+        me.$.errorLabelArea.show();
+        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);
   },
 
   updateExtraInfo: function (info) {
@@ -653,20 +671,18 @@
     return check;
   },
 
-  checkValidPayments: function (paymentstatus, selectedPayment) {
-    var resultOK, me = this;
+  checkValidPayments: function (paymentstatus, selectedPayment, callback) {
+    var resultOK, callbackFunc, me = this;
 
     if (!selectedPayment) {
       return;
     }
-    // Hide all error labels. Error labels are shown by check... functions
-    if (_.isNull(paymentstatus.overpayment)) {
-      this.$.changeexceedlimit.hide();
-      this.$.overpaymentnotavailable.hide();
-      this.$.overpaymentexceedlimit.hide();
-    }
-    this.$.noenoughchangelbl.hide();
-    this.$.onlycashpaymentmethod.hide();
+    this.hideErrorMessages(paymentstatus);
+    callbackFunc = function () {
+      if (callback) {
+        callback();
+      }
+    };
 
     // Do the checkins
     this.receipt.stopAddingPayments = !_.isEmpty(this.getShowingErrorMessages());
@@ -676,6 +692,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 +711,7 @@
             this.checkEnoughCashAvailable(paymentstatus, selectedPayment, this, 'Credit', function (success) {
               this.setStatusButtons(success, 'Credit');
             });
+            callbackFunc();
           });
         }
       } else if (!this.receipt.stopAddingPayments) {
@@ -701,13 +719,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 +792,21 @@
     errorLabelArray.push(this.$.extrainfo);
     return errorLabelArray;
   },
+  hideErrorMessages: function (paymentstatus) {
+    // Hide all error labels. Error labels are shown by check... functions
+    if (_.isNull(paymentstatus.overpayment)) {
+      this.$.changeexceedlimit.hide();
+      this.$.overpaymentnotavailable.hide();
+      this.$.overpaymentexceedlimit.hide();
+    }
+    this.$.noenoughchangelbl.hide();
+    this.$.onlycashpaymentmethod.hide();
+
+    this.$.donebutton.setLocalDisabled(false);
+    this.$.exactbutton.setLocalDisabled(false);
+    this.$.layawayaction.setLocalDisabled(false);
+    this.$.creditsalesaction.setLocalDisabled(false);
+  },
   getFirstShowingObject: function (errorLabelArray) {
     var showingObj = '',
         i;
@@ -826,7 +860,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;
         }
       }
     }
