# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1558417819 -19800
#      Tue May 21 11:20:19 2019 +0530
# Node ID 951b286e769d12530841ea6e65828e9d2da748d1
# Parent  600f23560d88366d6b61a0744161894f977f734c
Related to issue 40812 : If a manual discount is applied to an order, it is not possible to apply it again. It will remain the first discount
Description

diff -r 600f23560d88 -r 951b286e769d web/org.openbravo.retail.posterminal/js/model/order.js
--- a/web/org.openbravo.retail.posterminal/js/model/order.js	Mon May 20 18:59:22 2019 +0530
+++ b/web/org.openbravo.retail.posterminal/js/model/order.js	Tue May 21 11:20:19 2019 +0530
@@ -227,7 +227,7 @@
           return true;
         }
         for (i = 0; i < promotions.length; i++) {
-          if (!promotions[i].applyNext) {
+          if (!promotions[i].manual && !promotions[i].applyNext) {
             return true;
           }
         }
@@ -3394,6 +3394,9 @@
       var promotions = line.get('promotions') || [],
           disc = {},
           i, replaced = false,
+          unitsConsumed = 0,
+          unitsConsumedByNoCascadeRules = 0,
+          unitsConsumedByTheSameRule = 0,
           discountRule = OB.Model.Discounts.discountRules[rule.attributes.discountType];
       if (discountRule.getIdentifier) {
         disc.identifier = discountRule.getIdentifier(rule, discount);
@@ -3471,43 +3474,43 @@
       disc.obdiscAllowinnegativelines = (!OB.UTIL.isNullOrUndefined(rule.get('obdiscAllowinnegativelines'))) ? rule.get('obdiscAllowinnegativelines') : false;
       disc.executedAtTheEndPromo = discount.executedAtTheEndPromo || false;
 
-      var unitsConsumed = 0;
-      var unitsConsumedByNoCascadeRules = 0;
-      var unitsConsumedByTheSameRule = 0;
-      for (i = 0; i < promotions.length; i++) {
-        if (!promotions[i].applyNext) {
-          unitsConsumedByNoCascadeRules += promotions[i].qtyOffer;
-        } else if (promotions[i].ruleId === disc.ruleId) {
-          unitsConsumedByTheSameRule += promotions[i].qtyOffer;
-        }
-      }
-
-      if (disc.applyNext && unitsConsumedByTheSameRule === 0) {
-        unitsConsumed = unitsConsumedByNoCascadeRules;
-      } else {
-        unitsConsumed = unitsConsumedByNoCascadeRules + unitsConsumedByTheSameRule + disc.qtyOffer;
-      }
       if (!disc.manual) {
         for (i = 0; i < promotions.length; i++) {
-          if (unitsConsumed > line.get('qty')) {
-            if (discount.forceReplace) {
-              if (promotions[i].ruleId === rule.id && discount.discountinstance === promotions[i].discountinstance) {
-                if (promotions[i].hidden !== true) {
-                  promotions[i] = disc;
-                }
-              }
-            }
-            replaced = true;
-            break;
-          } else if (discount.forceReplace) {
+          if (!promotions[i].applyNext) {
+            unitsConsumedByNoCascadeRules += promotions[i].qtyOffer;
+          } else if (promotions[i].ruleId === disc.ruleId) {
+            unitsConsumedByTheSameRule += promotions[i].qtyOffer;
+          }
+        }
+
+        if (disc.applyNext && unitsConsumedByTheSameRule === 0) {
+          unitsConsumed = unitsConsumedByNoCascadeRules;
+        } else {
+          unitsConsumed = unitsConsumedByNoCascadeRules + unitsConsumedByTheSameRule + disc.qtyOffer;
+        }
+      }
+
+      for (i = 0; i < promotions.length; i++) {
+        if (!disc.manual && unitsConsumed > line.get('qty')) {
+          if (discount.forceReplace) {
             if (promotions[i].ruleId === rule.id && discount.discountinstance === promotions[i].discountinstance) {
               if (promotions[i].hidden !== true) {
                 promotions[i] = disc;
-                replaced = true;
-                break;
               }
             }
           }
+          replaced = true;
+          break;
+        } else if (disc.manual || discount.forceReplace) {
+          if (promotions[i].ruleId === rule.id && discount.discountinstance === promotions[i].discountinstance) {
+            if (promotions[i].hidden !== true) {
+              if (disc.applyNext === false) {
+                promotions[i] = disc;
+              }
+              replaced = true;
+              break;
+            }
+          }
         }
       }
 
