# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1577448919 -19800
#      Fri Dec 27 17:45:19 2019 +0530
# Node ID 7654fb550de92bea91a3f5835a081d9ce9916ee8
# Parent  dcfdf7d1a13348a3f557f4c8d05a009807e0c65e
Fixed issue 42569 : Receipt Lines Gross should not be recalculated when opening paid receipt

diff -r dcfdf7d1a133 -r 7654fb550de9 web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js	Mon Dec 23 18:54:02 2019 +0530
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js	Fri Dec 27 17:45:19 2019 +0530
@@ -926,14 +926,16 @@
         }
       };
 
-      this.get('lines').forEach(function(line) {
-        line.calculateGross();
-      });
-
-      if (this.get('priceIncludesTax')) {
-        this.calculateTaxes(function() {
-          var gross = me.get('lines').reduce(function(memo, e) {
-            var grossLine = e.getGross();
+      if (this.get('isEditable') || this.get('isCancelling')) {
+        this.get('lines').forEach(function(line) {
+          line.calculateGross();
+        });
+      }
+      this.calculateTaxes(function() {
+        var gross, grossLine;
+        if (me.get('priceIncludesTax')) {
+          gross = me.get('lines').reduce(function(memo, e) {
+            grossLine = e.getGross();
             if (e.get('qty') !== 0 && e.get('promotions')) {
               grossLine = e.get('promotions').reduce(function(memo, e) {
                 return OB.DEC.sub(
@@ -949,40 +951,22 @@
             }
             return OB.DEC.add(memo, grossLine);
           }, OB.DEC.Zero);
-          saveAndTriggerEvents(gross, save);
-        });
-      } else {
-        this.calculateTaxes(function() {
-          //If the price doesn't include tax, the discounted gross has already been calculated
-          var gross = me.get('lines').reduce(function(memo, e) {
+        } else {
+          // If the price doesn't include tax, the discounted gross has already been calculated
+          gross = me.get('lines').reduce(function(memo, e) {
             if (_.isUndefined(e.get('discountedGross'))) {
               return memo;
             }
-            var grossLine = e.get('discountedGross');
+            grossLine = e.get('discountedGross');
             if (grossLine) {
               return OB.DEC.add(memo, grossLine);
             } else {
               return memo;
             }
           }, OB.DEC.Zero);
-          if (
-            !me.get('isEditable') &&
-            !me.get('forceCalculateTaxes') &&
-            !OB.UTIL.isNullOrUndefined(me.get('totalamount'))
-          ) {
-            if (gross !== me.get('totalamount')) {
-              OB.warn(
-                'Calculated gross ' +
-                  gross +
-                  ' is not equal to totalamount of the receipt ' +
-                  me.get('totalamount')
-              );
-            }
-            gross = me.get('totalamount');
-          }
-          saveAndTriggerEvents(gross, save);
-        });
-      }
+        }
+        saveAndTriggerEvents(gross, save);
+      });
     },
 
     addToListOfCallbacks: function(callback) {
@@ -10002,7 +9986,13 @@
             i,
             sortedPayments = false;
           _.each(model.receiptLines, function(iter) {
-            var price = OB.DEC.number(iter.unitPrice);
+            var price = OB.DEC.number(iter.unitPrice),
+              lineNet = order.get('priceIncludesTax')
+                ? null
+                : OB.DEC.number(iter.lineGrossAmount || iter.linenetamount),
+              lineGross = order.get('priceIncludesTax')
+                ? OB.DEC.number(iter.lineGrossAmount)
+                : null;
             iter.linepos = linepos;
             var addLineForProduct = function(prod) {
               if (
@@ -10139,6 +10129,8 @@
                         ? price
                         : prod.get('listPrice'),
                     grossListPrice: iter.grossListPrice,
+                    net: lineNet,
+                    gross: lineGross,
                     promotions: iter.promotions,
                     description: iter.description,
                     priceIncludesTax: order.get('priceIncludesTax'),
