# HG changeset patch
# User Shivanand Madiwalar <shivanand.madiwalar@promantia.com>
# Date 1479122260 -19800
#      Mon Nov 14 16:47:40 2016 +0530
# Node ID d00ea58784c9af3ccc93a01c1f446df7eb7dbcf9
# Parent  893bc9cbf8290af3ba08b1f9def517932cdbbc97
Related To Issue 33390 : Promotion Buy X Pay Y implementation For Non-Grouped Products .

Improved the Some Part Of Logic .

diff --git a/web/org.openbravo.retail.discounts/js/promotion-XYsameProduct.js b/web/org.openbravo.retail.discounts/js/promotion-XYsameProduct.js
--- a/web/org.openbravo.retail.discounts/js/promotion-XYsameProduct.js
+++ b/web/org.openbravo.retail.discounts/js/promotion-XYsameProduct.js
@@ -11,12 +11,12 @@
 OB.Model.Discounts.discountRules.E08EE3C23EBA49358A881EF06C139D63 = {
   async: false,
   implementation: function (discountRule, receipt, line) {
-    var alerts, qty, x, y, mod, chunks, price, finalPrice, totalQty = 0,
-        ttopay = 0,
+    var alerts, qty, x, y, mod;
+    var totalToPay = 0,
+        chunks, price, finalPrice, totalQty = 0,
         totalToGift = 0,
-        totalChunks, product, appliedNoOfLines = 1,
-        discountRulePay, discountRuleGift, topay, togift, totalMod;
-
+        totalChunks, product, lineToApply, isManual = false,
+        discountRulePay, discountRuleGift, topay, togift, totalMod, appliedNoOfLines = 1;
     x = discountRule.get('oBDISCX');
     y = discountRule.get('oBDISCY');
 
@@ -27,38 +27,42 @@
 
     mod = qty % x;
 
-    //calculate the totalQty and totaltogift if product is non-grouped to Apply Promotions Each Unit Line Level .
-    receipt.get('lines').forEach(function (l) {
-      if (line.get('product').id === l.get('product').id && l.get('price') === line.get('price')) {
-        totalQty = totalQty + l.get('qty');
-        if (totalQty >= x) {
-          product = line.get('product');
-          totalChunks = Math.floor(totalQty / x);
-          totalToGift = (x - y) * totalChunks;
-          totalMod = totalQty % x;
+    //calculate the totalQty , totaltogift and totalToPay if product is non-grouped to Apply Promotions Each Unit Line Level .
+    //when ,If Product(Non-Grouped) Enforces To Apply Promotions Each Line Then Find Such Product and Calculate and Apply Promotions To 
+    //Each Line which has The same Product till AppliedPromotions Lines Count Reaches The TotalToGift of Respective Product .
+    if (line.get('splitline')) {
+      receipt.get('lines').forEach(function (l) {
+        if (line.get('product').id === l.get('product').id && l.get('price') === line.get('price') && l.get('splitline')) {
+          totalQty = totalQty + l.get('qty');
+          if (totalQty >= x) {
+            lineToApply = l;
+            totalChunks = Math.floor(totalQty / x);
+            totalToGift = (x - y) * totalChunks;
+            totalToPay = y * totalChunks;
+            totalMod = totalQty % x;
+          }
+          if (!OB.UTIL.isNullOrUndefined(l.get('gifted'))) {
+            appliedNoOfLines = appliedNoOfLines + 1;
+          }
+          if (l.get('promotions')) {
+            l.get('promotions').forEach(function (promotion) {
+              if (promotion.manual) {
+                isManual = promotion.manual;
+              }
+            });
+          }
+
         }
-        if (!OB.UTIL.isNullOrUndefined(l.get('chunks'))) {
-          appliedNoOfLines = appliedNoOfLines + 1;
-        }
-
-      }
-    });
-    //If Product(Non-Grouped) Enforces To Apply Promotions Each Line Then Find Such Product and Calculate and Apply Promotions To 
-    //Each Line which has The same Product till AppliedPromotions Lines Count Reaches The TotalToGift of Respective Product .
-    if (!OB.UTIL.isNullOrUndefined(product) && !product.get('groupProduct')) {
-      if (OB.UTIL.isNullOrUndefined(line.get('chunks'))) {
-        line.set('chunks', OB.DEC.toNumber(OB.DEC.toBigDecimal(0)));
-      }
-      if (OB.DEC.toNumber(OB.DEC.toBigDecimal(appliedNoOfLines)) <= totalToGift && OB.DEC.toNumber(OB.DEC.toBigDecimal(appliedNoOfLines)) !== OB.DEC.toNumber(OB.DEC.toBigDecimal(0))) {
+      });
+      if (qty >= x && !isManual) {
         discountRulePay = discountRule.clone();
         discountRuleGift = discountRule.clone();
         chunks = Math.floor(qty / x);
         topay = y * chunks;
         togift = (x - y) * chunks;
-        if (totalMod !== 0) {
+        if (mod !== 0) {
           alerts = OB.I18N.getLabel('OBPOS_DISCAlertXYSameProduct', [x - mod, line.get('product').get('_identifier'), discountRule.get('printName') || discountRule.get('name')]);
         }
-        var promotionQtyForEachLine = 1;
         // Units to pay
         discountRulePay.set('qtyOffer', topay);
         receipt.addPromotion(line, discountRulePay, {
@@ -71,40 +75,91 @@
         price = line.get('discountedLinePrice') || line.get('price');
         // note discountedLinePrice is not updated since this rule shouldn't change it
         finalPrice = OB.DEC.add(OB.DEC.mul(OB.DEC.mul(chunks, y), price), OB.DEC.mul(mod, price));
-        discountRuleGift.set('qtyOffer', togift);
+        discountRuleGift.set('qtyOffer', totalToGift);
         receipt.addPromotion(line, discountRuleGift, {
-          amt: finalPrice,
-          chunks: promotionQtyForEachLine
+          amt: OB.DEC.sub(OB.DEC.mul(qty, price), finalPrice),
+          chunks: chunks
         });
-        line.set('chunks', OB.DEC.toNumber(OB.DEC.toBigDecimal(appliedNoOfLines)));
       }
     }
-    if (qty >= x) {
-      discountRulePay = discountRule.clone();
-      discountRuleGift = discountRule.clone();
-      chunks = Math.floor(qty / x);
-      topay = y * chunks;
-      togift = (x - y) * chunks;
-      if (mod !== 0) {
-        alerts = OB.I18N.getLabel('OBPOS_DISCAlertXYSameProduct', [x - mod, line.get('product').get('_identifier'), discountRule.get('printName') || discountRule.get('name')]);
+    if (!line.get('splitline')) {
+      receipt.get('lines').forEach(function (l) {
+        if (line.get('product').id === l.get('product').id && l.get('price') === line.get('price') && !l.get('splitline')) {
+          totalQty = totalQty + l.get('qty');
+          if (totalQty >= x) {
+            lineToApply = l;
+            totalChunks = Math.floor(totalQty / x);
+            totalToGift = (x - y) * totalChunks;
+            totalToPay = y * totalChunks;
+            totalMod = totalQty % x;
+          }
+          if (!OB.UTIL.isNullOrUndefined(l.get('gifted'))) {
+            appliedNoOfLines = appliedNoOfLines + 1;
+          }
+          if (l.get('promotions')) {
+            l.get('promotions').forEach(function (promotion) {
+              if (promotion.manual) {
+                isManual = promotion.manual;
+              }
+            });
+          }
+
+        }
+      });
+      if (qty >= x) {
+        discountRulePay = discountRule.clone();
+        discountRuleGift = discountRule.clone();
+        chunks = Math.floor(qty / x);
+        topay = y * chunks;
+        togift = (x - y) * chunks;
+        if (mod !== 0) {
+          alerts = OB.I18N.getLabel('OBPOS_DISCAlertXYSameProduct', [x - mod, line.get('product').get('_identifier'), discountRule.get('printName') || discountRule.get('name')]);
+        }
+        // Units to pay
+        discountRulePay.set('qtyOffer', topay);
+        receipt.addPromotion(line, discountRulePay, {
+          amt: 0,
+          hidden: true,
+          preserve: true,
+          chunks: chunks
+        });
+        // Units to gift
+        price = line.get('discountedLinePrice') || line.get('price');
+        // note discountedLinePrice is not updated since this rule shouldn't change it
+        finalPrice = OB.DEC.add(OB.DEC.mul(OB.DEC.mul(chunks, y), price), OB.DEC.mul(mod, price));
+        discountRuleGift.set('qtyOffer', totalToGift);
+        receipt.addPromotion(line, discountRuleGift, {
+          amt: OB.DEC.sub(OB.DEC.mul(qty, price), finalPrice),
+          chunks: chunks
+        });
+      } else if (OB.DEC.toNumber(OB.DEC.toBigDecimal(appliedNoOfLines)) <= totalToGift) {
+
+        if (OB.UTIL.isNullOrUndefined(line.get('gifted'))) {
+          line.set('gifted', OB.DEC.toNumber(OB.DEC.toBigDecimal(0)));
+        }
+        discountRulePay = discountRule.clone();
+        discountRuleGift = discountRule.clone();
+        if (totalMod !== 0) {
+          alerts = OB.I18N.getLabel('OBPOS_DISCAlertXYSameProduct', [x - mod, line.get('product').get('_identifier'), discountRule.get('printName') || discountRule.get('name')]);
+        }
+        // Units to pay
+        discountRulePay.set('qtyOffer', totalToPay);
+        receipt.addPromotion(line, discountRulePay, {
+          amt: 0,
+          hidden: true,
+          preserve: true,
+          chunks: totalChunks
+        });
+        // Units to gift
+        price = line.get('discountedLinePrice') || line.get('price');
+        discountRuleGift.set('qtyOffer', totalToGift);
+        receipt.addPromotion(line, discountRuleGift, {
+          amt: OB.DEC.mul(qty, price),
+          chunks: totalChunks
+        });
+        line.set('gifted', OB.DEC.toNumber(OB.DEC.toBigDecimal(appliedNoOfLines)));
       }
-      // Units to pay
-      discountRulePay.set('qtyOffer', topay);
-      receipt.addPromotion(line, discountRulePay, {
-        amt: 0,
-        hidden: true,
-        preserve: true,
-        chunks: chunks
-      });
-      // Units to gift
-      price = line.get('discountedLinePrice') || line.get('price');
-      // note discountedLinePrice is not updated since this rule shouldn't change it
-      finalPrice = OB.DEC.add(OB.DEC.mul(OB.DEC.mul(chunks, y), price), OB.DEC.mul(mod, price));
-      discountRuleGift.set('qtyOffer', togift);
-      receipt.addPromotion(line, discountRuleGift, {
-        amt: OB.DEC.sub(OB.DEC.mul(qty, price), finalPrice),
-        chunks: chunks
-      });
+
     }
     return {
       alerts: alerts
