# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1590391784 -19800
#      Mon May 25 12:59:44 2020 +0530
# Node ID b1749737146291fd0cf3efd5d10c81117f60c9e1
# 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 b17497371462 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 25 12:59:44 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,59 @@
             ruleFromReceipt.get('rule').userAmt ||
             discountRule.get('userAmt') ||
             discountRule.get('disctTotalamountdisc');
-          preprocess.linesToCompute = _.sortBy(
-            preprocess.linesToCompute,
-            function(rule) {
-              return rule.line.get('discountedNet');
-            }
-          );
+
+          var orderManualPromotions =
+              receipt.get('orderManualPromotions') || [],
+            discountOrderNo = _.find(orderManualPromotions.models, function(p) {
+              return p.get('discountRule').get('id') === discountRule.get('id');
+            }).get('rule').noOrder,
+            validateDiscountOrder = function(currentDiscountRule) {
+              var appliedTotalDiscount = _.find(
+                orderManualPromotions.models,
+                function(p) {
+                  return (
+                    p.get('discountRule').get('id') ===
+                    currentDiscountRule.ruleId
+                  );
+                }
+              );
+              if (
+                appliedTotalDiscount &&
+                appliedTotalDiscount.get('rule').noOrder &&
+                appliedTotalDiscount.get('rule').noOrder >= discountOrderNo
+              ) {
+                return true;
+              }
+              return false;
+            };
+
           //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') || [];
+            promotionList.forEach(function(p) {
+              if (
+                OB.UTIL.isNullOrUndefined(p.amt) ||
+                p.manual ||
+                validateDiscountOrder(p)
+              ) {
+                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 +385,32 @@
               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 ||
+                  validateDiscountOrder(p)
+                ) {
+                  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)) {
@@ -431,19 +463,14 @@
             definition.extraProperties =
               ruleFromReceipt.get('rule').extraProperties || {};
             definition.extraProperties.lineQtyOffer = l.line.get('qty');
-            if (
-              !line.get('splitline') &&
-              l.line.get('promotions') &&
-              l.line.get('promotions').length > 0
-            ) {
-              var discountedGross = l.line
-                .get('promotions')
-                .reduce(function(memo, element) {
-                  return OB.DEC.add(
-                    memo,
-                    element.actualAmt || element.amt || 0
-                  );
-                }, 0);
+            if (promotionList.length > 0) {
+              var discountedGross = promotionList.reduce(function(
+                memo,
+                element
+              ) {
+                return OB.DEC.add(memo, element.actualAmt || element.amt || 0);
+              },
+              0);
               if (
                 OB.DEC.sub(
                   l.line.getGross(),
