foodbot-cart-calculations 1.0.2-dev.1 → 1.0.3-dev.1

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.
@@ -968,39 +968,37 @@ function getFixedPriceProductOffer(cartObject, offer) {
968
968
  x.toString().length > 10 ? x : parseInt(x, 10)
969
969
  );
970
970
 
971
- var cartItem = carts;
971
+
972
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
+
973
977
  cartItem.forEach((element, index) => {
974
- // console.log(discountedItemIds);
975
- // console.log(appliedQty);
976
978
  var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
977
979
  let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
978
- // console.log(!discountedItemIds.has(itemId) );
980
+
979
981
  if (
980
982
  (offer.offer_products.offer_discount_products.includes(itemId) ||
981
983
  offer.offer_products.package_items.includes(itemId))
982
- && !discountedItemIds.has(itemId) // allow only first occurrence
984
+ && !discountedItemIds.has(itemId)
983
985
  ) {
984
986
  var offerAmount = 0;
985
987
  var singleItemCost = element.item_price;
986
- // :white_tick: Apply max_quantity limit
988
+
987
989
  const maxQty = offer.max_quantity || 1;
988
990
  let remainingQty = maxQty - appliedQty;
989
991
  const qtyToDiscount = Math.min(element.quantity, remainingQty);
992
+
990
993
  if (qtyToDiscount > 0 && singleItemCost >= fixedPrice) {
991
- if (singleItemCost >= fixedPrice) {
992
- offerAmount = (singleItemCost * qtyToDiscount) - (fixedPrice * qtyToDiscount);
993
- }
994
- console.log(offerAmount)
995
- appliedQty += qtyToDiscount; // increase total discounted qty
996
- // discountedItemIds.add(itemId); // mark this item as already discounted
997
- // if(singleItemCost >= fixedPrice){
998
- // offerAmount = (singleItemCost * element.quantity) - (fixedPrice * element.quantity);
999
- // }
1000
- total = total + parseFloat(offerAmount);
994
+ offerAmount = (singleItemCost * qtyToDiscount) - (fixedPrice * qtyToDiscount);
995
+ appliedQty += qtyToDiscount;
996
+
997
+ total += parseFloat(offerAmount);
1001
998
  carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
1002
999
  carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
1003
1000
  carts[ItemIndex].remove = true;
1001
+
1004
1002
  if (offerAmount > 0) {
1005
1003
  carts[ItemIndex].redeem_promotion_id = offer.offer_id;
1006
1004
  } else {
@@ -1009,16 +1007,17 @@ function getFixedPriceProductOffer(cartObject, offer) {
1009
1007
  res.redeem_promotion_id = 0;
1010
1008
  });
1011
1009
  }
1012
- // :white_tick: only add to discountedItemIds if FULL item.quantity was used
1010
+
1013
1011
  if (appliedQty == maxQty) {
1014
1012
  discountedItemIds.add(itemId);
1015
1013
  }
1016
1014
  }
1017
1015
  }
1016
+
1018
1017
  count++;
1019
1018
  if (count == carts.length && carts) {
1020
- cartObject.item_details = carts
1021
- cartObject.discount = total
1019
+ cartObject.item_details = carts;
1020
+ cartObject.discount = total;
1022
1021
  resolve(cartObject);
1023
1022
  }
1024
1023
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.2-dev.1",
3
+ "version": "1.0.3-dev.1",
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": {