foodbot-cart-calculations 1.0.49 → 1.0.51

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,103 +146,106 @@ 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
- let itemDiscount = ((item.item_price * item.quantity) - promoDiscount) * singlePriceDiscount;
156
- itemDiscount = parseFloat(itemDiscount).toFixed(2);
157
-
158
- // Cap item discount to not exceed actual item price
159
- let maxItemDiscount = (item.item_price * item.quantity) - promoDiscount;
160
- if (parseFloat(itemDiscount) > maxItemDiscount) {
161
- itemDiscount = parseFloat(maxItemDiscount).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;
162
211
  }
163
-
164
- if (itemDiscount > 0)
165
- indexes.push(index);
166
-
167
- let totalPackageItemsDiscount = 0;
168
- let totalPackageModifiersDiscount = 0;
169
-
170
- item.package_items.forEach((packageItem, packageItemIndex) => {
171
- let packageItemPromoDiscount = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0;
172
- packageItem.points_discount_modifier = 0;
173
-
174
- // Calculate base package item price
175
- let packageItemBasePrice = (packageItem.price * parseInt(item.quantity)) - parseFloat(packageItem.default_combo_discount || 0) - parseFloat(packageItemPromoDiscount);
176
-
177
- // Ensure base price is not negative
178
- if (packageItemBasePrice < 0) {
179
- packageItemBasePrice = 0;
180
- }
181
-
182
- // Calculate discount for package item
183
- let packageItemDiscount = (packageItemBasePrice * parseFloat(singlePriceDiscount));
184
- packageItemDiscount = parseFloat(packageItemDiscount).toFixed(2);
185
-
186
- // Cap package item discount to not exceed its actual price
187
- if (parseFloat(packageItemDiscount) > packageItemBasePrice) {
188
- packageItemDiscount = parseFloat(packageItemBasePrice).toFixed(2);
189
- }
190
-
191
- // Ensure discount is not negative
192
- if (parseFloat(packageItemDiscount) < 0) {
193
- packageItemDiscount = "0.00";
194
- }
195
-
196
- packageItem.points_discount = packageItemDiscount;
197
- totalPackageItemsDiscount = parseFloat(totalPackageItemsDiscount) + parseFloat(packageItemDiscount);
198
-
199
- // Handle modifiers
200
- if (packageItem.modifiers && packageItem.modifiers.length > 0) {
201
- packageItem.modifiers.forEach((modi, modiIndex) => {
202
- let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
203
-
204
- // Calculate modifier base price
205
- let modiBasePrice = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity - modiPromoDiscount;
206
-
207
- // Ensure modifier base price is not negative
208
- if (modiBasePrice < 0) {
209
- modiBasePrice = 0;
210
- }
211
-
212
- let modiItemAmount = (modiBasePrice * parseFloat(singlePriceDiscount));
213
- modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
214
-
215
- // Cap modifier discount
216
- if (parseFloat(modiItemAmount) > modiBasePrice) {
217
- modiItemAmount = parseFloat(modiBasePrice).toFixed(2);
218
- }
219
-
220
- // Ensure discount is not negative
221
- if (parseFloat(modiItemAmount) < 0) {
222
- modiItemAmount = "0.00";
223
- }
224
-
225
- modi.points_discount = modiItemAmount;
226
- packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount_modifier) + parseFloat(modi.points_discount)).toFixed(2);
227
- totalPackageModifiersDiscount = parseFloat(totalPackageModifiersDiscount) + parseFloat(modiItemAmount);
228
- });
229
- }
230
- });
231
-
232
- // Set main package item discounts to reflect total of package items
233
- item.points_discount = parseFloat(totalPackageItemsDiscount).toFixed(2);
234
- item.points_discount_modifier = parseFloat(totalPackageModifiersDiscount).toFixed(2);
235
-
236
- // Add to cart-level total
237
- itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(totalPackageItemsDiscount) + parseFloat(totalPackageModifiersDiscount);
238
- itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
239
- }
240
- } 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 {
241
243
  if (item.point_discount_status) {
242
244
  let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
243
245
 
244
246
  // --- Base item discount ---
245
247
  let basePrice = parseFloat(item.item_price) * item.quantity;
246
- let baseDiscount = ((basePrice - promoDiscount) * singlePriceDiscount);
248
+ let baseDiscount = ((basePrice - promoDiscount) * parseFloat(singlePriceDiscount));
247
249
  baseDiscount = parseFloat(baseDiscount).toFixed(2);
248
250
 
249
251
  // cap base discount
@@ -260,8 +262,9 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
260
262
  let modsTotalDiscount = 0;
261
263
  if (item.item_modifiers && item.item_modifiers.length > 0) {
262
264
  item.item_modifiers.forEach((modi) => {
265
+ let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
263
266
  let modiBase = parseFloat(modi.modifier_item_price) * modi.quantity * item.quantity;
264
- let modiDiscount = (modiBase * parseFloat(singlePriceDiscount));
267
+ let modiDiscount = ((modiBase - modiPromoDiscount) * parseFloat(singlePriceDiscount));
265
268
  modiDiscount = parseFloat(modiDiscount).toFixed(2);
266
269
 
267
270
  // cap modifier
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.49",
3
+ "version": "1.0.51",
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": {