From 9f702f74782887c5b94fc71cda3446bdde9280a2 Mon Sep 17 00:00:00 2001
From: Rafael Queralta <rafaelcuba81@gmail.com>
Date: Wed, 23 Mar 2022 00:19:42 -0400
Subject: [PATCH] Fixed BUG-48732: This problems is fixed in Issue 48069. These
 is the backport for 20Q3

---
 .../rules/by-total-free-item-discount.js      | 20 +++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/web/org.openbravo.retail.discounts.bytotal/app/model/business-logic/discounts-engine/engine/rules/by-total-free-item-discount.js b/web/org.openbravo.retail.discounts.bytotal/app/model/business-logic/discounts-engine/engine/rules/by-total-free-item-discount.js
index 7970eb5..1dad567 100644
--- a/web/org.openbravo.retail.discounts.bytotal/app/model/business-logic/discounts-engine/engine/rules/by-total-free-item-discount.js
+++ b/web/org.openbravo.retail.discounts.bytotal/app/model/business-logic/discounts-engine/engine/rules/by-total-free-item-discount.js
@@ -130,6 +130,17 @@
       return '4755A35B4DA34F6CB08F15462BA123CF';
     }
 
+    /* @Override */
+    isApplicableToLine(line, rule) {
+      const applicable = super.isApplicableToLine(line, rule);
+      return (
+        applicable ||
+        !!rule.freeItems.find(
+          freeItem => freeItem.product.id === line.product.id
+        )
+      );
+    }
+
     /* @Override */
     getDiscountableLines(lines) {
       const participantFreeProduts = this.getParticipantFreeProducts(lines);
@@ -177,6 +188,10 @@
           // line of free products
           let lineQtyThatCanBeUsed = lineQty;
           if (!amountFulfilled) {
+            // Free Product should not be included to reach total amount of discount
+            if (!super.isApplicableToLine(line, this.discountImpl)) {
+              return;
+            }
             // use free products to reach the total, but will not be part of the discount
             const lineQtyUsedForReachTotal = this.getLineQtyNeededToReachTotal(
               line,
@@ -247,6 +262,11 @@
         }
       });
     }
+
+    /* @Override */
+    static isTicketDiscount() {
+      return true;
+    }
   }
 
   // Register rule class we just created
-- 
2.25.1

