foodbot-cart-calculations 1.0.47 → 1.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -959,49 +959,65 @@ function getFixedPriceProductOffer(cartObject, offer) {
959
959
  var total = 0;
960
960
  var count = 0;
961
961
  var fixedPrice = offer.oo_offer_value;
962
-
963
- offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
962
+ let appliedQty = 0; // track how many quantities discounted
963
+ let discountedItemIds = new Set(); // track which items already discounted
964
+ offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
964
965
  val.toString().length > 10 ? val : Number(val)
965
966
  );
966
- offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
967
+ offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
967
968
  x.toString().length > 10 ? x : parseInt(x, 10)
968
969
  );
969
970
 
970
- var cartItem = carts;
971
+
971
972
 
973
+ // ✅ Sort by price (descending → highest first)
974
+ carts.sort((a, b) => (parseInt(b.item_price) > parseInt(a.item_price)) ? -1 : 1);
975
+ var cartItem = JSON.parse(JSON.stringify(carts))
976
+
972
977
  cartItem.forEach((element, index) => {
973
978
  var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
974
979
  let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
975
- if (offer.offer_products.offer_discount_products.includes(itemId) || offer.offer_products.package_items.includes(itemId)) {
980
+
981
+ if (
982
+ (offer.offer_products.offer_discount_products.includes(itemId) ||
983
+ offer.offer_products.package_items.includes(itemId))
984
+ && !discountedItemIds.has(itemId)
985
+ ) {
976
986
  var offerAmount = 0;
977
987
  var singleItemCost = element.item_price;
978
-
979
- if(singleItemCost >= fixedPrice){
980
- offerAmount = (singleItemCost * element.quantity) - (fixedPrice * element.quantity);
981
- }
982
-
983
- total = total + parseFloat(offerAmount);
984
-
985
- carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
986
- carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
987
- carts[ItemIndex].remove = true;
988
-
989
- if (offerAmount > 0) {
990
- carts[ItemIndex].redeem_promotion_id = offer.offer_id;
991
- } else {
992
- carts[ItemIndex].item_modifiers.map((res) => {
993
- res.promotion_discount = 0;
994
- res.redeem_promotion_id = 0;
995
- });
988
+
989
+ const maxQty = offer.max_quantity || 1;
990
+ let remainingQty = maxQty - appliedQty;
991
+ const qtyToDiscount = Math.min(element.quantity, remainingQty);
992
+
993
+ if (qtyToDiscount > 0 && singleItemCost >= fixedPrice) {
994
+ offerAmount = (singleItemCost * qtyToDiscount) - (fixedPrice * qtyToDiscount);
995
+ appliedQty += qtyToDiscount;
996
+
997
+ total += parseFloat(offerAmount);
998
+ carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
999
+ carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
1000
+ carts[ItemIndex].remove = true;
1001
+
1002
+ if (offerAmount > 0) {
1003
+ carts[ItemIndex].redeem_promotion_id = offer.offer_id;
1004
+ } else {
1005
+ carts[ItemIndex].item_modifiers.map((res) => {
1006
+ res.promotion_discount = 0;
1007
+ res.redeem_promotion_id = 0;
1008
+ });
1009
+ }
1010
+
1011
+ if (appliedQty == maxQty) {
1012
+ discountedItemIds.add(itemId);
1013
+ }
996
1014
  }
997
- count++;
998
- } else {
999
- count++;
1000
1015
  }
1001
-
1016
+
1017
+ count++;
1002
1018
  if (count == carts.length && carts) {
1003
- cartObject.item_details = carts
1004
- cartObject.discount = total
1019
+ cartObject.item_details = carts;
1020
+ cartObject.discount = total;
1005
1021
  resolve(cartObject);
1006
1022
  }
1007
1023
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "description": "Package for cart calculations in which it performs discount distribution and tax distribution on order",
5
5
  "main": "index.js",
6
6
  "scripts": {