# HG changeset patch
# User Asier Martirena <asier.martirena@openbravo.com>
# Date 1559128048 -7200
#      Wed May 29 13:07:28 2019 +0200
# Node ID 10a8ca917b2ab93a376b8f4d75157960e9e5a3f9
# Parent  c7de4f837a3fa182c2666e7e4ef68b81dcfca7ef
Fixed issue 40805: The deferred service can generate the invoice now

When a deferred service is delivered due to an order loader hook, the invoice is not created in the case that should. Now, it will be possible to load again that order in the Web POS and synchronize it again creating the invoice for the deferred service lines.

diff --git a/web/org.openbravo.retail.posterminal/js/model/order.js b/web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js
@@ -6527,47 +6527,36 @@
       var receiptShouldBeInvoiced = false,
           me = this,
           invoice, isDeleted = this.get('obposIsDeleted'),
-          receiptShouldBeShipped = false,
           deliveredNotInvoicedLine;
 
       function finalCallback(invoice) {
-        if (callback instanceof Function) {
+        if (callback && callback instanceof Function) {
           callback(invoice);
         }
       }
 
-      if (!isDeleted) {
-        if ((this.get('bp').get('invoiceTerms') === 'I' && this.get('generateInvoice')) || this.get('payOnCredit')) {
+      if (isDeleted || (!this.get('payOnCredit') && !this.get('completeTicket'))) {
+        finalCallback();
+        return;
+      }
+
+      if ((this.get('bp').get('invoiceTerms') === 'I' && this.get('generateInvoice')) || this.get('payOnCredit')) {
+        receiptShouldBeInvoiced = true;
+      } else if (this.get('bp').get('invoiceTerms') === 'O') {
+        if (this.get('deliver')) {
           receiptShouldBeInvoiced = true;
-        } else if (this.get('bp').get('invoiceTerms') === 'O') {
-          if (this.get('deliver')) {
-            receiptShouldBeInvoiced = true;
-          } else if (this.get('iscancelled') || this.get('replacedorder')) {
-            var notDeliveredLine = _.find(this.get('lines').models, function (line) {
-              return line.get('obposQtytodeliver') !== line.get('qty');
-            });
-            // If the ticket is delivered but some line is pending to be invoiced, generate the invoice for them
-            if (_.isUndefined(notDeliveredLine)) {
-              deliveredNotInvoicedLine = _.find(this.get('lines').models, function (line) {
-                return line.get('obposQtytodeliver') !== line.get('invoicedQuantity');
-              });
-              receiptShouldBeInvoiced = !_.isUndefined(deliveredNotInvoicedLine);
-            }
-          }
-        } else if (this.get('bp').get('invoiceTerms') === 'D') {
-          receiptShouldBeShipped = this.get('payOnCredit') || this.get('completeTicket');
-          if (receiptShouldBeShipped) {
-            if (this.get('generateShipment')) {
-              receiptShouldBeInvoiced = true;
-            } else if (this.get('iscancelled') || this.get('replacedorder')) {
-              deliveredNotInvoicedLine = _.find(this.get('lines').models, function (line) {
-                return line.getDeliveredQuantity() === line.get('qty') && line.getDeliveredQuantity() !== line.get('invoicedQuantity');
-              });
-              receiptShouldBeInvoiced = !_.isUndefined(deliveredNotInvoicedLine);
-            }
-          }
-        }
-      }
+        }
+      } else if (this.get('bp').get('invoiceTerms') === 'D') {
+        if (this.get('generateShipment')) {
+          receiptShouldBeInvoiced = true;
+        } else {
+          deliveredNotInvoicedLine = _.find(this.get('lines').models, function (line) {
+            return line.getDeliveredQuantity() !== line.get('invoicedQuantity');
+          });
+          receiptShouldBeInvoiced = !_.isUndefined(deliveredNotInvoicedLine);
+        }
+      }
+
       if (receiptShouldBeInvoiced) {
         invoice = new OB.Model.Order();
         OB.UTIL.clone(this, invoice);
@@ -6584,21 +6573,10 @@
               qtyAlreadyInvoiced = ol.get('invoicedQuantity') || OB.DEC.Zero,
               qtyPendingToBeInvoiced = OB.DEC.sub(ol.get('qty'), qtyAlreadyInvoiced),
               qtyToDeliver = !OB.UTIL.isNullOrUndefined(ol.get('obposQtytodeliver')) ? ol.get('obposQtytodeliver') : ol.get('qty'),
-              qtyPendingToDeliver = OB.DEC.sub(qtyToDeliver, ol.getDeliveredQuantity()),
               qtyToInvoice = OB.DEC.Zero,
               lineToInvoice;
           if (me.get('bp').get('invoiceTerms') === 'D') {
-            if (qtyPendingToDeliver !== 0) {
-              if (OB.DEC.compare(OB.DEC.sub(OB.DEC.abs(qtyPendingToDeliver), OB.DEC.abs(qtyPendingToBeInvoiced))) === 1) {
-                qtyToInvoice = qtyPendingToBeInvoiced;
-              } else {
-                qtyToInvoice = qtyPendingToDeliver;
-              }
-            } else if (qtyToDeliver !== qtyAlreadyInvoiced) {
-              qtyToInvoice = OB.DEC.sub(qtyToDeliver, qtyAlreadyInvoiced);
-            } else {
-              qtyToInvoice = 0;
-            }
+            qtyToInvoice = OB.DEC.sub(qtyToDeliver, qtyAlreadyInvoiced);
           } else if (me.get('bp').get('invoiceTerms') === 'I' || me.get('bp').get('invoiceTerms') === 'O') {
             qtyToInvoice = qtyPendingToBeInvoiced;
           }
