# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1589358087 -19800
#      Wed May 13 13:51:27 2020 +0530
# Node ID 81eaca9f48fa85ee0ab598c5d68ce9fcedb85d94
# Parent  95a79c86bcff41cddf04bd0924f53a40415b7485
Related to issue 43880 : Discount should be calculated based on Total Receipt Amount

* Total Discount Amount will be calculated for the total receipt amount which excludes
  manual promotions amount or any other promotions amount.

diff -r 95a79c86bcff -r 81eaca9f48fa web/org.openbravo.retail.discounts.bytotal/js/promotion-by-total-utils.js
--- a/web/org.openbravo.retail.discounts.bytotal/js/promotion-by-total-utils.js	Tue May 05 11:00:47 2020 +0200
+++ b/web/org.openbravo.retail.discounts.bytotal/js/promotion-by-total-utils.js	Wed May 13 13:51:27 2020 +0530
@@ -117,24 +117,32 @@
           );
           //Calculate the discounts applied to receipt, to update the total receipt
           discountReceipt = preprocess.linesToCompute.reduce(function(memo, l) {
-            var discount = 0,
-              ruleApplied = _.find(l.line.get('promotions'), function(p) {
-                return p.ruleId === ruleFromReceipt.get('rule').id;
-              });
+            var discount = 0;
             if (
-              !l.line.get('splitline') &&
-              OB.UTIL.isNullOrUndefined(ruleApplied)
+              l.line.get('promotions') &&
+              l.line.get('promotions').length > 0
             ) {
-              discount =
-                l.line.getDiscount() > 0
-                  ? l.line.getDiscount()
-                  : !OB.UTIL.isNullOrUndefined(
-                      l.line.get('manualPromotions')
-                    ) && l.line.get('manualPromotions').length > 0
-                  ? l.line.get('manualPromotions').reduce(function(memo, mp) {
-                      return OB.DEC.add(memo, mp.amt);
-                    }, 0)
-                  : 0;
+              l.line.get('promotions').forEach(function(prom) {
+                if (OB.UTIL.isNullOrUndefined(prom.amt)) {
+                  return;
+                }
+                if (
+                  prom.ruleId !== discountRule.get('id') &&
+                  prom.rule.get('userAmt') !== discountRule.get('userAmt')
+                ) {
+                  discount = OB.DEC.add(discount, prom.amt);
+                }
+              });
+            } else if (
+              l.line.get('manualPromotions') &&
+              l.line.get('manualPromotions').length > 0
+            ) {
+              l.line.get('manualPromotions').forEach(function(prom) {
+                if (OB.UTIL.isNullOrUndefined(prom.amt)) {
+                  return;
+                }
+                discount = OB.DEC.add(discount, prom.amt);
+              });
             }
             return OB.DEC.add(memo, discount);
           }, 0);
