foodbot-cart-calculations 1.0.50 → 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,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
 
@@ -264,8 +262,9 @@ function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, se
264
262
  let modsTotalDiscount = 0;
265
263
  if (item.item_modifiers && item.item_modifiers.length > 0) {
266
264
  item.item_modifiers.forEach((modi) => {
265
+ let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
267
266
  let modiBase = parseFloat(modi.modifier_item_price) * modi.quantity * item.quantity;
268
- let modiDiscount = (modiBase * parseFloat(singlePriceDiscount));
267
+ let modiDiscount = ((modiBase - modiPromoDiscount) * parseFloat(singlePriceDiscount));
269
268
  modiDiscount = parseFloat(modiDiscount).toFixed(2);
270
269
 
271
270
  // cap modifier
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.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": {