foodbot-cart-calculations 1.0.50 → 1.0.52

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.
@@ -79,11 +79,11 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
79
79
  count++;
80
80
  });
81
81
 
82
- // Apply points to service charge if enabled
82
+ // Apply points to service charge if enabled
83
83
  if (points.enable_service_charge_usage === 1 && serviceAmount > 0 && storeAmount > 0) {
84
84
  // Check if service discount is already applied through promotions
85
85
  let hasServicePromo = cartObject.is_service_discount === true;
86
-
86
+
87
87
  if (!hasServicePromo) {
88
88
  cartObject.is_service_discount = true;
89
89
  if (parseFloat(storeAmount) >= parseFloat(serviceAmount)) {
@@ -138,8 +138,7 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
138
138
 
139
139
  if (count == carts.length) {
140
140
  let singlePriceDiscount = pointsAmount / itemsTotal;
141
- singlePriceDiscount = (itemsTotal && itemsTotal > 0)?parseFloat(singlePriceDiscount).toFixed(6):0;
142
-
141
+ singlePriceDiscount = (itemsTotal && itemsTotal > 0) ? parseFloat(singlePriceDiscount).toFixed(6) : 0;
143
142
  let indexes = [];
144
143
  let itemTotalDicount = 0;
145
144
 
@@ -147,101 +146,100 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
147
146
  item.points_discount = item.points_discount ? item.points_discount : 0;
148
147
  item.points_discount_modifier = item.points_discount_modifier ? item.points_discount_modifier : 0;
149
148
 
150
- // Replace the package items section (around line 120-170) with this corrected version:
151
-
152
- if (item.isPackage == 1) {
153
- if (item.point_discount_status) {
154
- let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
155
-
156
- // For package items, calculate discount based on actual item price
157
- let mainItemPrice = parseFloat(item.item_price) * item.quantity;
158
- let mainItemDiscount = ((mainItemPrice - promoDiscount) * parseFloat(singlePriceDiscount));
159
- mainItemDiscount = Math.max(0, mainItemDiscount);
160
- mainItemDiscount = parseFloat(mainItemDiscount).toFixed(2);
161
-
162
- // Cap main item discount
163
- if (parseFloat(mainItemDiscount) > (mainItemPrice - promoDiscount)) {
164
- mainItemDiscount = (mainItemPrice - promoDiscount).toFixed(2);
149
+ // Replace the package items section (around line 120-170) with this corrected version:
150
+ if (item.isPackage == 1) {
151
+ if (item.point_discount_status) {
152
+ let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
153
+
154
+ // For package items, calculate discount based on actual item price
155
+ let mainItemPrice = parseFloat(item.item_price) * item.quantity;
156
+ let mainItemDiscount = ((mainItemPrice - promoDiscount) * parseFloat(singlePriceDiscount));
157
+ mainItemDiscount = Math.max(0, mainItemDiscount);
158
+ mainItemDiscount = parseFloat(mainItemDiscount).toFixed(2);
159
+
160
+ // Cap main item discount
161
+ if (parseFloat(mainItemDiscount) > (mainItemPrice - promoDiscount)) {
162
+ mainItemDiscount = (mainItemPrice - promoDiscount).toFixed(2);
163
+ }
164
+
165
+ if (parseFloat(mainItemDiscount) > 0)
166
+ indexes.push(index);
167
+
168
+ let totalPackageItemsDiscount = 0;
169
+ let totalPackageModifiersDiscount = 0;
170
+
171
+ item.package_items.forEach((packageItem, packageItemIndex) => {
172
+ let packageItemPromoDiscount = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0;
173
+ packageItem.points_discount_modifier = 0;
174
+
175
+ // Calculate base package item price
176
+ let packageItemBasePrice = (packageItem.price * parseInt(item.quantity)) - parseFloat(packageItem.default_combo_discount || 0) - parseFloat(packageItemPromoDiscount);
177
+
178
+ // Ensure base price is not negative
179
+ if (packageItemBasePrice < 0) {
180
+ packageItemBasePrice = 0;
181
+ }
182
+
183
+ // Calculate discount for package item
184
+ let packageItemDiscount = (packageItemBasePrice * parseFloat(singlePriceDiscount));
185
+ packageItemDiscount = parseFloat(packageItemDiscount).toFixed(2);
186
+
187
+ // Cap package item discount to not exceed its actual price
188
+ if (parseFloat(packageItemDiscount) > packageItemBasePrice) {
189
+ packageItemDiscount = parseFloat(packageItemBasePrice).toFixed(2);
190
+ }
191
+
192
+ // Ensure discount is not negative
193
+ if (parseFloat(packageItemDiscount) < 0) {
194
+ packageItemDiscount = "0.00";
195
+ }
196
+
197
+ packageItem.points_discount = packageItemDiscount;
198
+ totalPackageItemsDiscount = parseFloat(totalPackageItemsDiscount) + parseFloat(packageItemDiscount);
199
+
200
+ // Handle modifiers
201
+ if (packageItem.modifiers && packageItem.modifiers.length > 0) {
202
+ packageItem.modifiers.forEach((modi, modiIndex) => {
203
+ let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
204
+
205
+ // Calculate modifier base price
206
+ let modiBasePrice = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity - modiPromoDiscount;
207
+
208
+ // Ensure modifier base price is not negative
209
+ if (modiBasePrice < 0) {
210
+ modiBasePrice = 0;
165
211
  }
166
-
167
- if (parseFloat(mainItemDiscount) > 0)
168
- indexes.push(index);
169
-
170
- let totalPackageItemsDiscount = 0;
171
- let totalPackageModifiersDiscount = 0;
172
-
173
- item.package_items.forEach((packageItem, packageItemIndex) => {
174
- let packageItemPromoDiscount = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0;
175
- packageItem.points_discount_modifier = 0;
176
-
177
- // Calculate base package item price
178
- let packageItemBasePrice = (packageItem.price * parseInt(item.quantity)) - parseFloat(packageItem.default_combo_discount || 0) - parseFloat(packageItemPromoDiscount);
179
-
180
- // Ensure base price is not negative
181
- if (packageItemBasePrice < 0) {
182
- packageItemBasePrice = 0;
183
- }
184
-
185
- // Calculate discount for package item
186
- let packageItemDiscount = (packageItemBasePrice * parseFloat(singlePriceDiscount));
187
- packageItemDiscount = parseFloat(packageItemDiscount).toFixed(2);
188
-
189
- // Cap package item discount to not exceed its actual price
190
- if (parseFloat(packageItemDiscount) > packageItemBasePrice) {
191
- packageItemDiscount = parseFloat(packageItemBasePrice).toFixed(2);
192
- }
193
-
194
- // Ensure discount is not negative
195
- if (parseFloat(packageItemDiscount) < 0) {
196
- packageItemDiscount = "0.00";
197
- }
198
-
199
- packageItem.points_discount = packageItemDiscount;
200
- totalPackageItemsDiscount = parseFloat(totalPackageItemsDiscount) + parseFloat(packageItemDiscount);
201
-
202
- // Handle modifiers
203
- if (packageItem.modifiers && packageItem.modifiers.length > 0) {
204
- packageItem.modifiers.forEach((modi, modiIndex) => {
205
- let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
206
-
207
- // Calculate modifier base price
208
- let modiBasePrice = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity - modiPromoDiscount;
209
-
210
- // Ensure modifier base price is not negative
211
- if (modiBasePrice < 0) {
212
- modiBasePrice = 0;
213
- }
214
-
215
- let modiItemAmount = (modiBasePrice * parseFloat(singlePriceDiscount));
216
- modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
217
-
218
- // Cap modifier discount
219
- if (parseFloat(modiItemAmount) > modiBasePrice) {
220
- modiItemAmount = parseFloat(modiBasePrice).toFixed(2);
221
- }
222
-
223
- // Ensure discount is not negative
224
- if (parseFloat(modiItemAmount) < 0) {
225
- modiItemAmount = "0.00";
226
- }
227
-
228
- modi.points_discount = modiItemAmount;
229
- packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount_modifier) + parseFloat(modi.points_discount)).toFixed(2);
230
- totalPackageModifiersDiscount = parseFloat(totalPackageModifiersDiscount) + parseFloat(modiItemAmount);
231
- });
232
- }
233
- });
234
-
235
- // CRITICAL FIX: Set main package discount to match the actual main item price
236
- // This ensures tax calculation gets the correct discount amount
237
- item.points_discount = mainItemDiscount;
238
- item.points_discount_modifier = "0.00";
239
-
240
- // Add main item discount to cart total (not package items discount)
241
- itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(mainItemDiscount);
242
- itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
243
- }
244
- } else {
212
+
213
+ let modiItemAmount = (modiBasePrice * parseFloat(singlePriceDiscount));
214
+ modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
215
+
216
+ // Cap modifier discount
217
+ if (parseFloat(modiItemAmount) > modiBasePrice) {
218
+ modiItemAmount = parseFloat(modiBasePrice).toFixed(2);
219
+ }
220
+
221
+ // Ensure discount is not negative
222
+ if (parseFloat(modiItemAmount) < 0) {
223
+ modiItemAmount = "0.00";
224
+ }
225
+
226
+ modi.points_discount = modiItemAmount;
227
+ packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount_modifier) + parseFloat(modi.points_discount)).toFixed(2);
228
+ totalPackageModifiersDiscount = parseFloat(totalPackageModifiersDiscount) + parseFloat(modiItemAmount);
229
+ });
230
+ }
231
+ });
232
+
233
+ // CRITICAL FIX: Set main package discount to match the actual main item price
234
+ // This ensures tax calculation gets the correct discount amount
235
+ item.points_discount = mainItemDiscount;
236
+ item.points_discount_modifier = "0.00";
237
+
238
+ // Add main item discount to cart total (not package items discount)
239
+ itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(mainItemDiscount);
240
+ itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
241
+ }
242
+ } else {
245
243
  if (item.point_discount_status) {
246
244
  let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
247
245
 
@@ -249,7 +247,8 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
249
247
  let basePrice = parseFloat(item.item_price) * item.quantity;
250
248
  let baseDiscount = ((basePrice - promoDiscount) * parseFloat(singlePriceDiscount));
251
249
  baseDiscount = parseFloat(baseDiscount).toFixed(2);
252
-
250
+ console.log("singlePriceDiscount -->", singlePriceDiscount);
251
+ console.log("baseDiscount -->", baseDiscount);
253
252
  // cap base discount
254
253
  if (parseFloat(baseDiscount) > basePrice) {
255
254
  baseDiscount = basePrice.toFixed(2);
@@ -264,9 +263,11 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
264
263
  let modsTotalDiscount = 0;
265
264
  if (item.item_modifiers && item.item_modifiers.length > 0) {
266
265
  item.item_modifiers.forEach((modi) => {
266
+ let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
267
267
  let modiBase = parseFloat(modi.modifier_item_price) * modi.quantity * item.quantity;
268
- let modiDiscount = (modiBase * parseFloat(singlePriceDiscount));
268
+ let modiDiscount = ((modiBase - modiPromoDiscount) * parseFloat(singlePriceDiscount));
269
269
  modiDiscount = parseFloat(modiDiscount).toFixed(2);
270
+ console.log("modiDiscount -->", modiDiscount);
270
271
 
271
272
  // cap modifier
272
273
  if (parseFloat(modiDiscount) > modiBase) {
@@ -299,6 +300,17 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
299
300
  }
300
301
  });
301
302
 
303
+ if ((Number(itemTotalDicount) < Number(pointsAmount) || Number(itemTotalDicount) > Number(pointsAmount)) && pointsAmount > 0 && itemTotalDicount > 0) {
304
+ let difference = Number(pointsAmount) - Number(itemTotalDicount);
305
+ difference = parseFloat(difference).toFixed(2);
306
+ if (indexes.length > 0 && difference) {
307
+ carts[indexes[0]].points_discount = (difference > 0) ? parseFloat(carts[indexes[0]].points_discount) + parseFloat(difference) : parseFloat(carts[indexes[0]].points_discount) - Math.abs(difference);
308
+ carts[indexes[0]].points_discount = parseFloat(carts[indexes[0]].points_discount).toFixed(2);
309
+ itemTotalDicount = (difference < 0) ? itemTotalDicount + difference : itemTotalDicount - Math.abs(difference);
310
+ itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
311
+ }
312
+ }
313
+
302
314
  cartObject.item_details = carts
303
315
  cartObject.store_value = pointsAmount
304
316
  cartObject.point_discount_amount = pointsAmount
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.50",
3
+ "version": "1.0.52",
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": {