foodbot-cart-calculations 1.0.69 → 1.0.70

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.
@@ -89,6 +89,12 @@ function offerCalculation(carts, offer, deliveryCost = 0) {
89
89
  }).catch(error => {
90
90
  reject(error);
91
91
  })
92
+ } else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'discount_modifier') {
93
+ applyDiscountModifierPromotion(carts, offer).then(free => {
94
+ resolve(free)
95
+ }).catch(error => {
96
+ reject(error);
97
+ })
92
98
  } else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'free_delivery') {
93
99
  getFreeDeliveryOffer(carts, offer, deliveryCost).then(free => {
94
100
  resolve(free)
@@ -1035,6 +1041,94 @@ function getFixedPriceProductOffer(cartObject, offer) {
1035
1041
  }
1036
1042
  };
1037
1043
 
1044
+ function applyDiscountModifierPromotion(cartObject, offer) {
1045
+ return new Promise(function (resolve, reject) {
1046
+ try {
1047
+ let carts = cartObject.item_details;
1048
+ let totalDiscount = 0;
1049
+
1050
+ // 1. Reset all existing promotion fields
1051
+ carts.forEach(function (v) {
1052
+ v.promotion_discount_modifier = 0;
1053
+ v.promotion_discount = 0;
1054
+ v.redeem_promotion_id = 0;
1055
+ if (v.item_modifiers) {
1056
+ v.item_modifiers.forEach((mod) => {
1057
+ mod.promotion_discount = 0;
1058
+ mod.redeem_promotion_id = 0;
1059
+ });
1060
+ }
1061
+ });
1062
+
1063
+ // 2. Identify and Flatten Eligible Modifiers
1064
+ // We create a "unit" for every single quantity to handle the max_quantity limit correctly
1065
+ let eligibleUnits = [];
1066
+ const allowedCats = offer.modifier_category || [];
1067
+
1068
+ carts.forEach((item, itemIdx) => {
1069
+ if (item.item_modifiers) {
1070
+ item.item_modifiers.forEach((mod, modIdx) => {
1071
+ // Check if "all" is selected or if specific category matches
1072
+ const isEligible = allowedCats.some(cat =>
1073
+ cat.toString().toLowerCase() == 'all' ||
1074
+ cat.toString() == mod.modifier_category_id.toString()
1075
+ );
1076
+
1077
+ if (isEligible && mod.modifier_item_price > 0) {
1078
+ for (let i = 0; i < mod.quantity; i++) {
1079
+ eligibleUnits.push({
1080
+ price: parseFloat(mod.modifier_item_price),
1081
+ itemIdx: itemIdx,
1082
+ modIdx: modIdx
1083
+ });
1084
+ }
1085
+ }
1086
+ });
1087
+ }
1088
+ });
1089
+
1090
+ // 3. Sort by price (Lowest Price first as per Scenario B)
1091
+ eligibleUnits.sort((a, b) => a.price - b.price);
1092
+
1093
+ // 4. Apply Discount up to Max Limit
1094
+ const maxLimit = offer.max_quantity || 1;
1095
+ const discountValue = offer.oo_offer_value; // e.g., 100 for 100%
1096
+ const unitsToDiscount = eligibleUnits.slice(0, maxLimit);
1097
+
1098
+ unitsToDiscount.forEach(unit => {
1099
+ let unitDiscount = 0;
1100
+ let targetMod = carts[unit.itemIdx].item_modifiers[unit.modIdx];
1101
+
1102
+ if (offer.oo_offer_type == 'percentage') {
1103
+ unitDiscount = (unit.price * discountValue) / 100;
1104
+ } else {
1105
+ // Flat discount (limited by the price of the modifier)
1106
+ unitDiscount = Math.min(unit.price, discountValue);
1107
+ }
1108
+
1109
+ // Update Modifier level
1110
+ targetMod.promotion_discount = (parseFloat(targetMod.promotion_discount) + unitDiscount).toFixed(2);
1111
+ targetMod.redeem_promotion_id = offer.offer_id;
1112
+
1113
+ // Update Item level (promotion_discount_modifier tracks total mod discounts for that item)
1114
+ carts[unit.itemIdx].promotion_discount_modifier = (parseFloat(carts[unit.itemIdx].promotion_discount_modifier) + unitDiscount).toFixed(2);
1115
+ carts[unit.itemIdx].redeem_promotion_id = offer.offer_id;
1116
+
1117
+ totalDiscount += unitDiscount;
1118
+ });
1119
+
1120
+ // 5. Finalize Cart Object
1121
+ cartObject.item_details = carts;
1122
+ cartObject.discount = totalDiscount.toFixed(2);
1123
+
1124
+ resolve(cartObject);
1125
+
1126
+ } catch (error) {
1127
+ reject(error);
1128
+ }
1129
+ });
1130
+ };
1131
+
1038
1132
  function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
1039
1133
  return new Promise((resolve, reject) => {
1040
1134
  try {
@@ -115,7 +115,6 @@ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,
115
115
  let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
116
116
  let itemTotalTax = 0;
117
117
  let tax_array = []
118
- console.log("isSplit", toFix)
119
118
  if (itemTaxs && itemTaxs.length > 0) {
120
119
  itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
121
120
  // let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
@@ -803,7 +802,7 @@ async function calculateItemWiseSplit(cart, itemWiseItems) {
803
802
  'status': 'error',
804
803
  'status_code': 500,
805
804
  'message': 'Internal server error',
806
- 'error': error.stack,
805
+ 'error': err.stack,
807
806
  });
808
807
  }
809
808
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.69",
3
+ "version": "1.0.70",
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": {