# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1589820972 -19800
#      Mon May 18 22:26:12 2020 +0530
# Node ID df9ff9761dfaaaf75bea24f06dceed2a3b05d5a1
# Parent  132fdcec4c2f031bbf4ef530685235dcbfa88785
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 132fdcec4c2f -r df9ff9761dfa 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	Thu Apr 02 15:43:26 2020 +0200
+++ b/web/org.openbravo.retail.discounts.bytotal/js/promotion-by-total-utils.js	Mon May 18 22:26:12 2020 +0530
@@ -308,7 +308,6 @@
       if (!discountRule.get('applyNext')) {
         OB.Model.Discounts.DISCT.removeAutoPromotionsFromReceipt(receipt);
       }
-
       var accumdiscount = 0;
       OB.Model.Discounts.DISCT.preprocess(
         discountRule,
@@ -321,32 +320,43 @@
             ruleFromReceipt.get('rule').userAmt ||
             discountRule.get('userAmt') ||
             discountRule.get('disctTotalamountdisc');
-          preprocess.linesToCompute = _.sortBy(
-            preprocess.linesToCompute,
-            function(rule) {
-              return rule.line.get('discountedNet');
-            }
-          );
           //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;
-              });
+              promotionList = l.line.get('promotions') || [];
+            var promoApplied = _.filter(promotionList, function(p) {
+              return (
+                p.ruleId === ruleFromReceipt.get('rule').id &&
+                p.discountinstance ===
+                  ruleFromReceipt.get('rule').discountinstance
+              );
+            });
+            if (promoApplied.length > 0) {
+              return memo;
+            }
+            if (l.line.get('orderManualPromotionsAlreadyApplied')) {
+              return memo;
+            }
+            promotionList.forEach(function(p) {
+              if (OB.UTIL.isNullOrUndefined(p.amt) || p.manual) {
+                return;
+              }
+
+              if (
+                p.ruleId !== discountRule.get('id') &&
+                p.discountinstance ===
+                  ruleFromReceipt.get('rule').discountinstance
+              ) {
+                discount = OB.DEC.add(discount, p.amt);
+              }
+            });
             if (
-              !l.line.get('splitline') &&
-              OB.UTIL.isNullOrUndefined(ruleApplied)
+              l.line.get('manualPromotions') &&
+              l.line.get('manualPromotions').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('manualPromotions').forEach(function(p) {
+                discount = OB.DEC.add(discount, p.amt);
+              });
             }
             return OB.DEC.add(memo, discount);
           }, 0);
@@ -359,26 +369,28 @@
               linePrice = l.total,
               promotionList = [],
               promoApplied = null;
-            // distributing  discount in all lines based on total of each one
-            // if PIT, in gross
-            // if no PIT, in net
-            if (!l.line.get('splitline')) {
-              if (!OB.UTIL.isNullOrUndefined(l.line.get('promotions'))) {
-                promotionList = l.line.get('promotions');
-              }
-              if (!OB.UTIL.isNullOrUndefined(l.line.get('manualPromotions'))) {
-                promotionList = promotionList.concat(
-                  l.line.get('manualPromotions')
-                );
-              }
-              promotionList = _.uniq(promotionList, false, function(p) {
-                return p.ruleId;
+
+            if (
+              !OB.UTIL.isNullOrUndefined(l.line.get('promotions')) &&
+              l.line.get('promotions').length > 0
+            ) {
+              l.line.get('promotions').forEach(function(p) {
+                if (OB.UTIL.isNullOrUndefined(p.amt) || p.manual) {
+                  return;
+                }
+                promotionList.push(p);
               });
             }
+            if (!OB.UTIL.isNullOrUndefined(l.line.get('manualPromotions'))) {
+              promotionList = promotionList.concat(
+                l.line.get('manualPromotions')
+              );
+            }
             promoApplied = _.find(promotionList, function(p) {
               return (
                 p.ruleId === discountRule.get('id') &&
-                p.rule.get('userAmt') === discountRule.get('userAmt')
+                p.discountinstance ===
+                  ruleFromReceipt.get('rule').discountinstance
               );
             });
             if (!OB.UTIL.isNullOrUndefined(promoApplied)) {
