From 5b0abdf7befd3ecbd54f53a476adfed37084c7f0 Mon Sep 17 00:00:00 2001
From: Prakash M <prakash@qualiantech.com>
Date: Thu, 12 Nov 2020 16:29:33 +0530
Subject: [PATCH] Fixed BUG-45378: Fixes Simplified Invoice being created with
 Generate Simplified Invoice flag false * Validated in Sell On Credit that if
 Generate Simplified Invoice is false then Full Invoice is set

---
 .../js/pointofsale/view/payment.js            | 266 ++++++++++--------
 1 file changed, 151 insertions(+), 115 deletions(-)

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 6384c4946..7143ab5b0 100644
--- a/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
+++ b/web/org.openbravo.retail.posterminal/js/pointofsale/view/payment.js
@@ -3332,49 +3332,73 @@ enyo.kind({
     this.inherited(arguments);
     this.setDisabled(!OB.MobileApp.model.hasPermission(this.permission));
   },
+  checkInvoice: function(isMultiOrder) {
+    let me = this,
+      validInvoice = true;
+    if (!OB.MobileApp.model.get('terminal').terminalType.generateInvoice) {
+      if (isMultiOrder) {
+        for (
+          let i = 0;
+          i < me.model.get('multiOrders').get('multiOrdersList').models.length;
+          i++
+        ) {
+          const model = me.model.get('multiOrders').get('multiOrdersList')
+            .models[i];
+          if (
+            !model.get('fullInvoice') &&
+            !model.setFullInvoice(true, true, true)
+          ) {
+            validInvoice = false;
+            break;
+          }
+        }
+      } else {
+        if (
+          !me.model.get('order').get('fullInvoice') &&
+          !me.model.get('order').setFullInvoice(true, true, true)
+        ) {
+          validInvoice = false;
+        }
+      }
+    }
+    return validInvoice;
+  },
   tap: function() {
     if (this.disabled) {
       return true;
     }
-    var execution = OB.UTIL.ProcessController.start('payOnCredit');
 
-    OB.UTIL.ProcessController.finish('payOnCredit', execution);
-    if (
-      !_.isNull(this.model.get('order').get('bp')) &&
-      _.isNull(
-        this.model
-          .get('order')
-          .get('bp')
-          .get('locId')
-      )
-    ) {
+    const isMultiOrder = !this.model.get('leftColumnViewManager').isOrder();
+
+    if (this.checkInvoice(isMultiOrder)) {
+      var execution = OB.UTIL.ProcessController.start('payOnCredit');
+
       OB.UTIL.ProcessController.finish('payOnCredit', execution);
-      OB.UTIL.showConfirmation.display(
-        OB.I18N.getLabel('OBPOS_InformationTitle'),
-        OB.I18N.getLabel('OBPOS_EmptyAddrBillToText'),
-        [
-          {
-            label: OB.I18N.getLabel('OBPOS_LblOk')
-          }
-        ]
-      );
-      return true;
-    }
-    // Checking blind returned lines
-    if (!OB.MobileApp.model.get('terminal').returns_anonymouscustomer) {
-      if (this.model.get('leftColumnViewManager').isOrder()) {
-        if (this.owner.receipt.isAnonymousBlindReturn()) {
-          OB.UTIL.ProcessController.finish('payOnCredit', execution);
-          OB.UTIL.showConfirmation.display(
-            OB.I18N.getLabel('OBMOBC_Error'),
-            OB.I18N.getLabel('OBPOS_returnServicesWithAnonimousCust')
-          );
-          return;
-        }
-      } else {
-        var orderList = this.model.get('multiOrders').get('multiOrdersList');
-        orderList.forEach(function(receipt) {
-          if (receipt.isAnonymousBlindReturn()) {
+      if (
+        !_.isNull(this.model.get('order').get('bp')) &&
+        _.isNull(
+          this.model
+            .get('order')
+            .get('bp')
+            .get('locId')
+        )
+      ) {
+        OB.UTIL.ProcessController.finish('payOnCredit', execution);
+        OB.UTIL.showConfirmation.display(
+          OB.I18N.getLabel('OBPOS_InformationTitle'),
+          OB.I18N.getLabel('OBPOS_EmptyAddrBillToText'),
+          [
+            {
+              label: OB.I18N.getLabel('OBPOS_LblOk')
+            }
+          ]
+        );
+        return true;
+      }
+      // Checking blind returned lines
+      if (!OB.MobileApp.model.get('terminal').returns_anonymouscustomer) {
+        if (this.model.get('leftColumnViewManager').isOrder()) {
+          if (this.owner.receipt.isAnonymousBlindReturn()) {
             OB.UTIL.ProcessController.finish('payOnCredit', execution);
             OB.UTIL.showConfirmation.display(
               OB.I18N.getLabel('OBMOBC_Error'),
@@ -3382,96 +3406,108 @@ enyo.kind({
             );
             return;
           }
-        });
+        } else {
+          var orderList = this.model.get('multiOrders').get('multiOrdersList');
+          orderList.forEach(function(receipt) {
+            if (receipt.isAnonymousBlindReturn()) {
+              OB.UTIL.ProcessController.finish('payOnCredit', execution);
+              OB.UTIL.showConfirmation.display(
+                OB.I18N.getLabel('OBMOBC_Error'),
+                OB.I18N.getLabel('OBPOS_returnServicesWithAnonimousCust')
+              );
+              return;
+            }
+          });
+        }
       }
-    }
-    var me = this,
-      paymentstatus = this.model.get('order').getPaymentStatus(),
-      process = new OB.DS.Process(
-        'org.openbravo.retail.posterminal.CheckBusinessPartnerCredit'
-      );
-    if (!paymentstatus.isReturn) {
-      //this.setContent(OB.I18N.getLabel('OBPOS_LblLoading'));
-      process.exec(
-        {
-          businessPartnerId: this.model
-            .get('order')
-            .get('bp')
-            .get('id'),
-          totalPending: paymentstatus.pendingAmt
-        },
-        function(data) {
-          if (data) {
-            if (data.enoughCredit) {
+      var me = this,
+        paymentstatus = this.model.get('order').getPaymentStatus(),
+        process = new OB.DS.Process(
+          'org.openbravo.retail.posterminal.CheckBusinessPartnerCredit'
+        );
+      if (!paymentstatus.isReturn) {
+        //this.setContent(OB.I18N.getLabel('OBPOS_LblLoading'));
+        process.exec(
+          {
+            businessPartnerId: this.model
+              .get('order')
+              .get('bp')
+              .get('id'),
+            totalPending: paymentstatus.pendingAmt
+          },
+          function(data) {
+            if (data) {
+              if (data.enoughCredit) {
+                OB.UTIL.ProcessController.finish('payOnCredit', execution);
+                me.doShowPopup({
+                  popup: 'modalEnoughCredit',
+                  args: {
+                    order: me.model.get('order')
+                  }
+                });
+                //this.setContent(OB.I18N.getLabel('OBPOS_LblCreditSales'));
+              } else {
+                var bpName = data.bpName;
+                var actualCredit = data.actualCredit;
+                OB.UTIL.ProcessController.finish('payOnCredit', execution);
+                me.doShowPopup({
+                  popup: 'modalNotEnoughCredit',
+                  args: {
+                    bpName: bpName,
+                    actualCredit: actualCredit
+                  }
+                });
+                //this.setContent(OB.I18N.getLabel('OBPOS_LblCreditSales'));
+                //OB.UI.UTILS.domIdEnyoReference['modalNotEnoughCredit'].$.bodyContent.children[0].setContent();
+              }
+            } else {
+              OB.UTIL.showError(OB.I18N.getLabel('OBPOS_MsgErrorCreditSales'));
+              OB.UTIL.ProcessController.finish('payOnCredit', execution);
+            }
+            me.setDisabled(false);
+          },
+          function() {
+            if (
+              OB.MobileApp.model.hasPermission(
+                'OBPOS_AllowSellOnCreditWhileOffline',
+                true
+              )
+            ) {
               OB.UTIL.ProcessController.finish('payOnCredit', execution);
               me.doShowPopup({
                 popup: 'modalEnoughCredit',
                 args: {
-                  order: me.model.get('order')
+                  order: me.model.get('order'),
+                  message: 'OBPOS_Unabletocheckcredit'
                 }
               });
-              //this.setContent(OB.I18N.getLabel('OBPOS_LblCreditSales'));
             } else {
-              var bpName = data.bpName;
-              var actualCredit = data.actualCredit;
               OB.UTIL.ProcessController.finish('payOnCredit', execution);
-              me.doShowPopup({
-                popup: 'modalNotEnoughCredit',
-                args: {
-                  bpName: bpName,
-                  actualCredit: actualCredit
-                }
-              });
-              //this.setContent(OB.I18N.getLabel('OBPOS_LblCreditSales'));
-              //OB.UI.UTILS.domIdEnyoReference['modalNotEnoughCredit'].$.bodyContent.children[0].setContent();
+              OB.UTIL.showConfirmation.display(
+                OB.I18N.getLabel('OBPOS_SellingOnCreditHeader'),
+                OB.I18N.getLabel('OBPOS_UnabletoSellOncredit'),
+                [
+                  {
+                    isConfirmButton: true,
+                    label: OB.I18N.getLabel('OBMOBC_LblOk')
+                  }
+                ]
+              );
             }
-          } else {
-            OB.UTIL.showError(OB.I18N.getLabel('OBPOS_MsgErrorCreditSales'));
-            OB.UTIL.ProcessController.finish('payOnCredit', execution);
           }
-          me.setDisabled(false);
-        },
-        function() {
-          if (
-            OB.MobileApp.model.hasPermission(
-              'OBPOS_AllowSellOnCreditWhileOffline',
-              true
-            )
-          ) {
-            OB.UTIL.ProcessController.finish('payOnCredit', execution);
-            me.doShowPopup({
-              popup: 'modalEnoughCredit',
-              args: {
-                order: me.model.get('order'),
-                message: 'OBPOS_Unabletocheckcredit'
-              }
-            });
-          } else {
-            OB.UTIL.ProcessController.finish('payOnCredit', execution);
-            OB.UTIL.showConfirmation.display(
-              OB.I18N.getLabel('OBPOS_SellingOnCreditHeader'),
-              OB.I18N.getLabel('OBPOS_UnabletoSellOncredit'),
-              [
-                {
-                  isConfirmButton: true,
-                  label: OB.I18N.getLabel('OBMOBC_LblOk')
-                }
-              ]
-            );
+        );
+        //    } else if (this.model.get('order').get('orderType') === 1) {
+      } else if (paymentstatus.isReturn) {
+        OB.UTIL.ProcessController.finish('payOnCredit', execution);
+        this.doShowPopup({
+          popup: 'modalEnoughCredit',
+          args: {
+            order: this.model.get('order')
           }
-        }
-      );
-      //    } else if (this.model.get('order').get('orderType') === 1) {
-    } else if (paymentstatus.isReturn) {
-      OB.UTIL.ProcessController.finish('payOnCredit', execution);
-      this.doShowPopup({
-        popup: 'modalEnoughCredit',
-        args: {
-          order: this.model.get('order')
-        }
-      });
-    } else {
-      OB.UTIL.ProcessController.finish('payOnCredit', execution);
+        });
+      } else {
+        OB.UTIL.ProcessController.finish('payOnCredit', execution);
+      }
     }
   }
 });
-- 
2.20.1

