foodbot-cart-calculations 1.0.7 → 1.0.8-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.
- package/calculations.js +65 -12
- package/functions/pointsCalculation.js +220 -89
- package/functions/promotionCalculation.js +366 -266
- package/functions/taxCalculation.js +58 -38
- package/index.js +793 -69
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function offerCalculation(carts, offer) {
|
|
1
|
+
function offerCalculation(carts, offer, deliveryCost = 0) {
|
|
2
2
|
return new Promise(function (resolve, reject) {
|
|
3
3
|
try {
|
|
4
4
|
carts.total = 0
|
|
@@ -9,6 +9,7 @@ function offerCalculation(carts, offer) {
|
|
|
9
9
|
if(element.item_modifiers && element.item_modifiers.length>0){
|
|
10
10
|
element.item_modifiers.forEach(mod => {
|
|
11
11
|
delete mod.promotion_discount;
|
|
12
|
+
delete mod.points_discount;
|
|
12
13
|
let modTotal = (mod.modifier_item_price * mod.quantity) * element.quantity
|
|
13
14
|
carts.total += modTotal
|
|
14
15
|
});
|
|
@@ -19,24 +20,28 @@ function offerCalculation(carts, offer) {
|
|
|
19
20
|
if (p.item_modifiers)
|
|
20
21
|
p.item_modifiers.forEach((pMod) => {
|
|
21
22
|
delete pMod.points_discount;
|
|
23
|
+
delete pMod.promotion_discount;
|
|
22
24
|
});
|
|
23
25
|
|
|
24
26
|
if (p.modifiers)
|
|
25
27
|
p.modifiers.forEach((pMod) => {
|
|
26
28
|
delete pMod.points_discount;
|
|
29
|
+
delete pMod.promotion_discount;
|
|
27
30
|
});
|
|
28
31
|
});
|
|
29
32
|
}
|
|
30
33
|
});
|
|
34
|
+
|
|
31
35
|
if (offer && offer.offer_discount_type && offer.offer_discount_type == 'buy_x_get_y') {
|
|
32
36
|
getOfferDetails(carts, offer).then(res => {
|
|
33
37
|
carts = res
|
|
38
|
+
// console.log("res",res)
|
|
34
39
|
if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
|
|
35
40
|
|
|
36
41
|
// offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
|
|
37
42
|
// offer.oo_offer_values = JSON.parse(JSON.stringify(offer.oo_offer_value))
|
|
38
43
|
offer.oo_offer_type = 'absolute'
|
|
39
|
-
offer.oo_offer_value = Math.round(carts.discount)
|
|
44
|
+
offer.oo_offer_value = (carts && carts.discount)?Math.round(carts.discount):0
|
|
40
45
|
getOfferDetails(carts, offer).then(res => {
|
|
41
46
|
resolve(res)
|
|
42
47
|
}).catch(error=>{
|
|
@@ -50,6 +55,7 @@ function offerCalculation(carts, offer) {
|
|
|
50
55
|
reject(error);
|
|
51
56
|
})
|
|
52
57
|
} else if (offer && offer.offer_discount_type && (offer.offer_discount_type == 'discount' || offer.offer_discount_type == 'manual')) {
|
|
58
|
+
|
|
53
59
|
getCategorySpeificOffer(carts, offer).then(res => {
|
|
54
60
|
carts = res
|
|
55
61
|
|
|
@@ -76,6 +82,18 @@ function offerCalculation(carts, offer) {
|
|
|
76
82
|
}).catch(error => {
|
|
77
83
|
reject(error);
|
|
78
84
|
})
|
|
85
|
+
} else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'fixed_price') {
|
|
86
|
+
getFixedPriceProductOffer(carts, offer).then(free => {
|
|
87
|
+
resolve(free)
|
|
88
|
+
}).catch(error => {
|
|
89
|
+
reject(error);
|
|
90
|
+
})
|
|
91
|
+
} else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'free_delivery') {
|
|
92
|
+
getFreeDeliveryOffer(carts, offer, deliveryCost).then(free => {
|
|
93
|
+
resolve(free)
|
|
94
|
+
}).catch(error => {
|
|
95
|
+
reject(error);
|
|
96
|
+
})
|
|
79
97
|
} else {
|
|
80
98
|
carts.discount = 0
|
|
81
99
|
resolve(carts)
|
|
@@ -109,13 +127,15 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
109
127
|
if (parseFloat(offer.oo_min_amount) <= parseFloat(afterTotal)) {
|
|
110
128
|
|
|
111
129
|
if (offer.offer_discount_type != 'manual')
|
|
112
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
130
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
131
|
+
val.toString().length > 10 ? val : Number(val)
|
|
132
|
+
);
|
|
113
133
|
|
|
114
134
|
if (offer.offer_discount_type != 'manual')
|
|
115
135
|
if (offer.offer_products.package_items) {
|
|
116
|
-
offer.offer_products.package_items = offer.offer_products.package_items.map(
|
|
117
|
-
|
|
118
|
-
|
|
136
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
137
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
138
|
+
);
|
|
119
139
|
}
|
|
120
140
|
else {
|
|
121
141
|
offer.offer_products.package_items = []
|
|
@@ -123,18 +143,20 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
123
143
|
|
|
124
144
|
let foundItemsSum = 0
|
|
125
145
|
carts.item_details.filter((res) => {
|
|
126
|
-
if
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
146
|
+
if(!res.purchase_points){
|
|
147
|
+
let itemId = res.item_id?.toString().length > 10 ? res.item_id : parseInt(res.item_id);
|
|
148
|
+
if (!res.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(itemId) && res.isPackage == 0) || (offer.offer_products.package_items.includes(itemId) && res.isPackage == 1))) {
|
|
149
|
+
foundItemsSum = foundItemsSum + res.item_price_with_modifier * res.quantity
|
|
150
|
+
return foundItemsSum
|
|
151
|
+
}
|
|
130
152
|
}
|
|
131
153
|
})
|
|
132
154
|
|
|
133
155
|
let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
|
|
134
156
|
let remaningOfferAmount = offerAmount
|
|
135
157
|
carts.item_details.forEach((element, index) => {
|
|
136
|
-
|
|
137
|
-
if (!element.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(
|
|
158
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
159
|
+
if (!element.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(itemId) && element.isPackage == 0) || (offer.offer_products.package_items.includes(itemId) && element.isPackage == 1))) {
|
|
138
160
|
var itemAmount = 0;
|
|
139
161
|
|
|
140
162
|
// Apply discount on Packages
|
|
@@ -142,7 +164,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
142
164
|
if (offer.oo_offer_type == 'percentage') {
|
|
143
165
|
let itemTotalDiscount = 0
|
|
144
166
|
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
145
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
167
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
146
168
|
let itemDiscount = parseFloat(itemAmount).toFixed(2);
|
|
147
169
|
|
|
148
170
|
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
@@ -159,7 +181,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
159
181
|
let modiItemAmount = 0
|
|
160
182
|
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
161
183
|
|
|
162
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
184
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
163
185
|
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
164
186
|
total = parseFloat(total) + parseFloat(discount)
|
|
165
187
|
total = parseFloat(total).toFixed(2);
|
|
@@ -210,7 +232,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
210
232
|
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
211
233
|
|
|
212
234
|
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
213
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
235
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
214
236
|
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
215
237
|
total = parseFloat(total).toFixed(2);
|
|
216
238
|
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
@@ -259,7 +281,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
259
281
|
if (offer.oo_offer_type == 'percentage') {
|
|
260
282
|
let itemTotalDiscount = 0
|
|
261
283
|
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
262
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
284
|
+
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount).toFixed(2)
|
|
263
285
|
let itemDiscount = parseFloat(itemAmount).toFixed(2)
|
|
264
286
|
|
|
265
287
|
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
@@ -273,7 +295,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
273
295
|
let modiItemAmount = 0
|
|
274
296
|
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
275
297
|
|
|
276
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
298
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
277
299
|
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
278
300
|
total = parseFloat(total) + parseFloat(discount)
|
|
279
301
|
total = parseFloat(total).toFixed(2);
|
|
@@ -307,7 +329,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
307
329
|
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
308
330
|
|
|
309
331
|
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
310
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
332
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
311
333
|
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
312
334
|
total = parseFloat(total).toFixed(2);
|
|
313
335
|
carts.item_details[index].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
@@ -337,7 +359,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
337
359
|
|
|
338
360
|
total = parseFloat(total).toFixed(2)
|
|
339
361
|
|
|
340
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
362
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
341
363
|
remaningOfferAmount = remaningOfferAmount - itemAmount
|
|
342
364
|
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
|
|
343
365
|
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
|
|
@@ -354,13 +376,8 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
354
376
|
let difference = Number(offerAmount) - Number(total)
|
|
355
377
|
difference = parseFloat(difference).toFixed(2)
|
|
356
378
|
difference = Math.abs(difference)
|
|
357
|
-
|
|
358
379
|
if (indexes.length > 0) {
|
|
359
380
|
|
|
360
|
-
// console.log("difference", difference)
|
|
361
|
-
// console.log("total", total)
|
|
362
|
-
// console.log("offerAmount", offerAmount)
|
|
363
|
-
|
|
364
381
|
carts.item_details[indexes[0]].promotion_discount = (Number(total) < Number(offerAmount)) ? parseFloat(carts.item_details[indexes[0]].promotion_discount) + parseFloat(difference) : parseFloat(carts.item_details[indexes[0]].promotion_discount) - difference
|
|
365
382
|
carts.item_details[indexes[0]].promotion_discount = parseFloat(carts.item_details[indexes[0]].promotion_discount).toFixed(2)
|
|
366
383
|
|
|
@@ -372,7 +389,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
372
389
|
total = parseFloat(total).toFixed(2)
|
|
373
390
|
}
|
|
374
391
|
}
|
|
375
|
-
|
|
392
|
+
console.log("total", total)
|
|
376
393
|
carts.discount = total
|
|
377
394
|
resolve(carts);
|
|
378
395
|
}
|
|
@@ -438,7 +455,14 @@ function getOfferDetails(carts, offer = []) {
|
|
|
438
455
|
const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
|
|
439
456
|
const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
|
|
440
457
|
addItemQuantity(carts.item_details, offer).then((data) => {
|
|
441
|
-
const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ?
|
|
458
|
+
// const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? 1 : -1);
|
|
459
|
+
let descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? 1 : -1);
|
|
460
|
+
descending = data.sort((a, b) => {
|
|
461
|
+
if (a.get && !b.get) return 1; // GET item first
|
|
462
|
+
if (!a.get && b.get) return -1; // Non-GET after
|
|
463
|
+
return 0; // Keep original amount order for ties
|
|
464
|
+
});
|
|
465
|
+
|
|
442
466
|
let total = 0
|
|
443
467
|
let buyItemAmount=0;
|
|
444
468
|
let buyItemss =[]
|
|
@@ -452,7 +476,7 @@ function getOfferDetails(carts, offer = []) {
|
|
|
452
476
|
reject(error);
|
|
453
477
|
})
|
|
454
478
|
} else {
|
|
455
|
-
resolve(
|
|
479
|
+
resolve(carts);
|
|
456
480
|
}
|
|
457
481
|
} catch (error) {
|
|
458
482
|
reject(error)
|
|
@@ -460,234 +484,163 @@ function getOfferDetails(carts, offer = []) {
|
|
|
460
484
|
})
|
|
461
485
|
};
|
|
462
486
|
|
|
463
|
-
function getBuyxGetYOnCondition(descending, cartData, offer,total,buyItemAmount,buyItemss) {
|
|
464
|
-
return new Promise(function (resolve, reject) {
|
|
465
|
-
|
|
466
|
-
try {
|
|
467
|
-
let carts = cartData.item_details
|
|
468
|
-
var desc = JSON.parse(JSON.stringify(descending));
|
|
469
|
-
var desc1 = JSON.parse(JSON.stringify(descending));
|
|
470
|
-
var amount = 0;
|
|
471
|
-
var buyArray = [];
|
|
472
|
-
var getArray = [];
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
if (!cartData.totalDiscount)
|
|
476
|
-
cartData.totalDiscount = 0
|
|
477
|
-
|
|
478
|
-
var buyItems = desc.filter((e) => {
|
|
479
|
-
if (offer.offer_products.offer_buy_products.includes(parseInt(e.item_id))) {
|
|
480
|
-
return e.item_id;
|
|
481
|
-
}
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
let totalCount = parseInt(offer.offer_products.get_item_count) + parseInt(offer.offer_products.buy_item_count);
|
|
487
|
-
|
|
488
|
-
if (buyItems && parseInt(buyItems.length) >= parseInt(offer.offer_products.buy_item_count) && desc.length > 1 && descending.length >= totalCount) {
|
|
489
|
-
var buyCount = 0;
|
|
490
|
-
var getCount = 0;
|
|
491
|
-
|
|
492
|
-
for (var i = 0, ii = desc.length; i < ii; i++) {
|
|
493
|
-
let itemAmount = 0;
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
if (desc[i] && desc[i].get && getCount < offer.offer_products.get_item_count && getCount != offer.offer_products.get_item_count && desc[i].buy && buyCount < offer.offer_products.buy_item_count && buyCount != offer.offer_products.buy_item_count
|
|
497
|
-
) {
|
|
498
|
-
var amt = 0;
|
|
499
|
-
let amtWithModi = 0;
|
|
500
|
-
getArray.push(desc[i].item_id);
|
|
501
|
-
|
|
502
|
-
buyCount++;
|
|
503
|
-
desc1[i].remove = true;
|
|
504
|
-
buyArray.push(desc[i].item_id);
|
|
505
|
-
var itemIndex = -1;
|
|
506
|
-
|
|
507
|
-
if (desc[i]) itemIndex = carts.findIndex((obj) => obj.item_id == desc[i].item.item_id);
|
|
508
|
-
|
|
509
|
-
if (offer.oo_offer_type == "percentage") {
|
|
510
|
-
|
|
511
|
-
console.log('desc[i].item.item_pricedesc[i].item.item_price', desc[i].item.item_price)
|
|
512
|
-
|
|
513
|
-
let itemTotalDiscount = 0;
|
|
514
|
-
itemAmount =
|
|
515
|
-
(parseFloat(desc[i].item.item_price)) *
|
|
516
|
-
parseFloat(offer.oo_offer_value) /
|
|
517
|
-
100;
|
|
518
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
|
|
519
|
-
amt = parseFloat(amt) + parseFloat(itemAmount);
|
|
520
|
-
amt = parseFloat(amt);
|
|
521
|
-
amtWithModi = amt;
|
|
522
|
-
|
|
523
|
-
let modiItemAmount = 0;
|
|
524
|
-
cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(amt)
|
|
525
|
-
carts[itemIndex].promotion_discount_modifier = parseFloat(
|
|
526
|
-
parseFloat(carts[itemIndex].promotion_discount_modifier) +
|
|
527
|
-
parseFloat(amt)
|
|
528
|
-
).toFixed(2);
|
|
529
|
-
|
|
530
|
-
if (desc[i].item.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
|
|
531
|
-
let modiCount = 0;
|
|
532
|
-
|
|
533
|
-
desc[i].item.item_modifiers.forEach((modi, mIndex) => {
|
|
534
|
-
modiItemAmount =
|
|
535
|
-
(((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) *
|
|
536
|
-
parseFloat(offer.oo_offer_value)) /
|
|
537
|
-
100;
|
|
538
|
-
|
|
539
|
-
if (carts[itemIndex].item_modifiers[mIndex]) {
|
|
540
|
-
carts[itemIndex].item_modifiers[mIndex].promotion_discount = parseFloat(
|
|
541
|
-
modiItemAmount
|
|
542
|
-
).toFixed(2);
|
|
543
|
-
|
|
544
|
-
carts[itemIndex].item_modifiers[mIndex].redeem_promotion_id = offer.offer_id;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(modiItemAmount)
|
|
548
|
-
carts[itemIndex].promotion_discount_modifier = parseFloat(
|
|
549
|
-
parseFloat(carts[itemIndex].promotion_discount_modifier) +
|
|
550
|
-
parseFloat(modiItemAmount)
|
|
551
|
-
).toFixed(2);
|
|
552
|
-
|
|
553
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
|
|
554
|
-
amtWithModi = parseFloat(amtWithModi) + parseFloat(modiItemAmount);
|
|
555
|
-
amtWithModi = parseFloat(amtWithModi);
|
|
556
|
-
amt = parseFloat(amt) + parseFloat(amtWithModi);
|
|
557
|
-
|
|
558
|
-
var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
|
|
559
|
-
return obj.modifier_item_id == modi.modifier_item_id;
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
// if (carts[itemIndex].item_modifiers[modiIndex]) {
|
|
563
|
-
// carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(
|
|
564
|
-
// modiItemAmount
|
|
565
|
-
// ).toFixed(2);
|
|
566
|
-
// carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
567
|
-
// }
|
|
568
|
-
|
|
569
|
-
// modiCount++;
|
|
570
|
-
});
|
|
571
|
-
} else {
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
carts[itemIndex].promotion_discount = parseFloat(
|
|
575
|
-
parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
|
|
576
|
-
).toFixed(2);
|
|
577
|
-
|
|
578
|
-
// carts[itemIndex].promotion_discount_modifier = parseFloat(
|
|
579
|
-
// parseFloat(carts[itemIndex].promotion_discount_modifier) +
|
|
580
|
-
// parseFloat(itemTotalDiscount)
|
|
581
|
-
// ).toFixed(2);
|
|
582
|
-
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
583
|
-
} else {
|
|
584
|
-
buyItemAmount = buyItemAmount + desc[i].amount;
|
|
585
|
-
desc[i].item.indexss = i;
|
|
586
|
-
buyItemss.push(desc[i].item);
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
if (parseInt(amt) != 0) {
|
|
590
|
-
let amtModiTotal = amt;
|
|
591
|
-
// carts[itemIndex].promotion_discount_modifier = amtModiTotal;
|
|
592
|
-
// carts[itemIndex].promotion_discount = (
|
|
593
|
-
// parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
|
|
594
|
-
// ).toFixed(2);
|
|
595
|
-
total = total + amtWithModi;
|
|
596
|
-
|
|
597
|
-
// console.log('amount modifffffffffffffff', amtModiTotal)
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
getCount++;
|
|
605
|
-
desc1[i].remove = true;
|
|
606
|
-
desc1[i + 1].remove = true;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
if (
|
|
610
|
-
getCount == offer.offer_products.get_item_count &&
|
|
611
|
-
buyCount == offer.offer_products.buy_item_count
|
|
612
|
-
) {
|
|
613
|
-
total = parseFloat(total) + parseFloat(amount);
|
|
614
|
-
// cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(amount)
|
|
615
|
-
|
|
616
|
-
if (offer.offer_products.order_multi_use == 1) {
|
|
617
|
-
var filterItem = desc1.filter((e) => {
|
|
618
|
-
if (!e.remove) {
|
|
619
|
-
return e.item_id;
|
|
620
|
-
}
|
|
621
|
-
});
|
|
622
|
-
|
|
623
487
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
488
|
+
function getBuyxGetYOnCondition(descending, cartData, offer,total,buyItemAmount,buyItemss) {
|
|
489
|
+
return new Promise((resolve, reject) => {
|
|
490
|
+
let carts = cartData.item_details;
|
|
491
|
+
const desc = JSON.parse(JSON.stringify(descending));
|
|
492
|
+
const desc1 = JSON.parse(JSON.stringify(descending));
|
|
493
|
+
let amount = 0;
|
|
494
|
+
const buyArray = [];
|
|
495
|
+
const getArray = [];
|
|
496
|
+
const buyItems = desc.filter(e => {
|
|
497
|
+
if (offer.offer_products.offer_buy_products.some(item => item == e.item_id)) {
|
|
498
|
+
return e.item_id;
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// console.log(descending)
|
|
503
|
+
// console.log(buyItemAmount)
|
|
504
|
+
// console.log(buyItemss)
|
|
505
|
+
|
|
506
|
+
const totalCount = parseInt(offer.offer_products.get_item_count) + parseInt(offer.offer_products.buy_item_count);
|
|
507
|
+
if (buyItems && parseInt(buyItems.length) >= parseInt(offer.offer_products.buy_item_count) && desc.length > 1 && descending.length >= totalCount) {
|
|
508
|
+
let buyCount = 0;
|
|
509
|
+
let getCount = 0;
|
|
510
|
+
for (let i = 0; i < desc.length; i++) {
|
|
511
|
+
let itemAmount = 0;
|
|
512
|
+
|
|
513
|
+
if (desc[i].buy && buyCount < offer.offer_products.buy_item_count && buyCount != offer.offer_products.buy_item_count) {
|
|
514
|
+
buyCount++;
|
|
515
|
+
desc1[i].remove = true;
|
|
516
|
+
// buyItemss.push(desc[i].item_id);
|
|
517
|
+
} else if (desc[i] && desc[i].get && getCount < offer.offer_products.get_item_count && getCount != offer.offer_products.get_item_count) {
|
|
518
|
+
let amt = 0;
|
|
519
|
+
let amtWithModi = 0;
|
|
520
|
+
// getArray.push(desc[i].item_id);
|
|
521
|
+
|
|
522
|
+
let itemIndex = -1;
|
|
523
|
+
if (desc[i]) itemIndex = carts.findIndex(obj => obj.item_id == desc[i].item.item_id && !obj.discount_status);
|
|
524
|
+
if (itemIndex == -1) itemIndex = carts.findIndex(obj => obj.item_id == desc[i].item.item_id);
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
if (offer.oo_offer_type === "percentage") {
|
|
528
|
+
let itemTotalDiscount = 0;
|
|
529
|
+
|
|
530
|
+
itemAmount = (parseFloat(desc[i].item.item_price) * parseFloat(offer.oo_offer_value)) / 100;
|
|
531
|
+
buyItemAmount = itemAmount
|
|
532
|
+
itemTotalDiscount += itemAmount;
|
|
533
|
+
amt += itemAmount;
|
|
534
|
+
amtWithModi = amt;
|
|
535
|
+
let modiItemAmount = 0;
|
|
536
|
+
|
|
537
|
+
if (desc[i].item.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
|
|
538
|
+
let modiCount = 0;
|
|
539
|
+
desc[i].item.item_modifiers.forEach(modi => {
|
|
540
|
+
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
541
|
+
itemTotalDiscount += modiItemAmount;
|
|
542
|
+
amtWithModi += modiItemAmount;
|
|
543
|
+
amt += amtWithModi;
|
|
544
|
+
|
|
545
|
+
const modiIndex = carts[itemIndex].item_modifiers.findIndex(obj => obj.modifier_item_id == modi.modifier_item_id);
|
|
546
|
+
if (carts[itemIndex].item_modifiers[modiIndex]) {
|
|
547
|
+
carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2);
|
|
548
|
+
carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
549
|
+
}
|
|
550
|
+
modiCount++;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
653
553
|
} else {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
})
|
|
661
|
-
}
|
|
662
|
-
else {
|
|
663
|
-
cartData.discount = cartData.totalDiscount
|
|
664
|
-
resolve(cartData);
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
|
|
554
|
+
// Handle absolute discount type
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
buyItemAmount = parseFloat(buyItemAmount) + desc[i].amount;
|
|
558
|
+
desc[i].item.indexss = i;
|
|
559
|
+
buyItemss.push(desc[i].item);
|
|
668
560
|
}
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
561
|
+
|
|
562
|
+
if (parseInt(amt) != 0) {
|
|
563
|
+
const amtModiTotal = (offer.offer_products.order_modifiercost_use == 1)
|
|
564
|
+
? (parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(amtWithModi)).toFixed(2)
|
|
565
|
+
: (parseFloat(carts[itemIndex].promotion_discount) + parseFloat(amt)).toFixed(2);
|
|
566
|
+
|
|
567
|
+
carts[itemIndex].promotion_discount_modifier = amtModiTotal;
|
|
568
|
+
carts[itemIndex].promotion_discount = (parseFloat(carts[itemIndex].promotion_discount) + parseFloat(buyItemAmount)).toFixed(2);
|
|
569
|
+
carts[itemIndex].discount_status = true;
|
|
570
|
+
total = total + amtWithModi;
|
|
571
|
+
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
572
|
+
|
|
573
|
+
}
|
|
574
|
+
getCount++;
|
|
575
|
+
desc1[i].remove = true;
|
|
576
|
+
cartData.totalDiscount = total;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (getCount == offer.offer_products.get_item_count && buyCount == offer.offer_products.buy_item_count) {
|
|
580
|
+
|
|
581
|
+
total = parseFloat(total) + parseFloat(amount);
|
|
582
|
+
if (offer.offer_products.order_multi_use == 1) {
|
|
583
|
+
const filterItem = desc1.filter(e => !e.remove);
|
|
584
|
+
cartData.item_details = carts
|
|
585
|
+
getBuyxGetYOnCondition(filterItem, cartData, offer,total,buyItemAmount,buyItemss).then((data) => {
|
|
586
|
+
resolve(data)
|
|
587
|
+
}).catch(error => {
|
|
588
|
+
reject(error);
|
|
589
|
+
})
|
|
590
|
+
break;
|
|
591
|
+
} else {
|
|
592
|
+
if (offer.oo_offer_type === 'absolute') {
|
|
593
|
+
cartData.item_details = carts
|
|
594
|
+
getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount,total).then((data) => {
|
|
595
|
+
resolve(data);
|
|
596
|
+
}).catch(error => {
|
|
597
|
+
reject(error);
|
|
598
|
+
})
|
|
599
|
+
}else{
|
|
600
|
+
cartData.discount = cartData.totalDiscount
|
|
601
|
+
resolve(cartData);
|
|
602
|
+
}
|
|
603
|
+
break;
|
|
604
|
+
}
|
|
605
|
+
}else{
|
|
606
|
+
if(i == desc.length -1){
|
|
607
|
+
cartData.discount = cartData.totalDiscount?cartData.totalDiscount:0;
|
|
608
|
+
resolve(cartData);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
678
611
|
}
|
|
612
|
+
} else {
|
|
613
|
+
if (offer.oo_offer_type === 'absolute') {
|
|
614
|
+
|
|
615
|
+
cartData.item_details = carts
|
|
616
|
+
getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount,total).then((data) => {
|
|
617
|
+
resolve(data);
|
|
618
|
+
}).catch(error => {
|
|
619
|
+
reject(error);
|
|
620
|
+
})
|
|
621
|
+
}else{
|
|
622
|
+
cartData.discount = cartData.totalDiscount
|
|
623
|
+
resolve(cartData);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// setTimeout(() => {
|
|
628
|
+
// resolve(0);
|
|
629
|
+
// }, 20);
|
|
679
630
|
});
|
|
680
|
-
}
|
|
681
|
-
|
|
631
|
+
}
|
|
632
|
+
|
|
682
633
|
function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total) {
|
|
683
634
|
return new Promise(function (resolve, reject) {
|
|
684
635
|
try {
|
|
636
|
+
console.log("ooooooooooooooo",JSON.parse(JSON.stringify(offer)))
|
|
685
637
|
let offerAmount = offer.oo_offer_value;
|
|
686
638
|
let singlePriceDiscount = offerAmount / buyItemAmount;
|
|
687
639
|
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
688
640
|
let remaningOfferAmount = offer.oo_offer_value;
|
|
689
641
|
let count = 0;
|
|
690
|
-
let carts = cartObject.item_details
|
|
642
|
+
let carts = cartObject.item_details;
|
|
643
|
+
console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",buyItemss,total)
|
|
691
644
|
if(buyItemss && buyItemss.length>0){
|
|
692
645
|
buyItemss.forEach((element, buyIndex) => {
|
|
693
646
|
let itemAmount = 0;
|
|
@@ -699,8 +652,10 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total)
|
|
|
699
652
|
if (itemIndex == -1) itemIndex = carts.findIndex((obj) => {
|
|
700
653
|
return obj.item_id == element.item_id;
|
|
701
654
|
});
|
|
655
|
+
|
|
656
|
+
console.log("carts index",carts,itemIndex)
|
|
702
657
|
|
|
703
|
-
if (!carts[itemIndex].promotion_discount_modifier) carts[itemIndex].promotion_discount_modifier = 0;
|
|
658
|
+
if ( !carts[itemIndex].promotion_discount_modifier) carts[itemIndex].promotion_discount_modifier = 0;
|
|
704
659
|
|
|
705
660
|
if (element.item_modifiers && element.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
|
|
706
661
|
let modiCount = 0;
|
|
@@ -745,6 +700,7 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total)
|
|
|
745
700
|
carts[itemIndex].discount_applied = true;
|
|
746
701
|
|
|
747
702
|
if (buyItemss[buyItemss.length - 1].item_id == element.item_id) {
|
|
703
|
+
console.log("innnn ifffffffffffff",element.item_price,remaningOfferAmount)
|
|
748
704
|
|
|
749
705
|
itemAmount = (element.item_price < remaningOfferAmount) ? element.item_price : parseFloat(remaningOfferAmount).toFixed(2);
|
|
750
706
|
} else {
|
|
@@ -752,6 +708,7 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total)
|
|
|
752
708
|
}
|
|
753
709
|
|
|
754
710
|
itemAmount = parseFloat(itemAmount);
|
|
711
|
+
console.log("iiiiiiiiii",itemAmount)
|
|
755
712
|
total = parseFloat(total) + parseFloat(itemAmount);
|
|
756
713
|
total = parseFloat(total).toFixed(2);
|
|
757
714
|
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
|
|
@@ -762,10 +719,10 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total)
|
|
|
762
719
|
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
763
720
|
|
|
764
721
|
if (count == buyItemss.length) {
|
|
722
|
+
console.log("ttttttttttttttt",total)
|
|
765
723
|
cartObject.item_details = carts
|
|
766
724
|
cartObject.discount = total
|
|
767
|
-
|
|
768
|
-
|
|
725
|
+
|
|
769
726
|
resolve(cartObject);
|
|
770
727
|
}
|
|
771
728
|
});
|
|
@@ -783,8 +740,15 @@ function addItemQuantity(items, offer) {
|
|
|
783
740
|
try {
|
|
784
741
|
var finalItems = [];
|
|
785
742
|
var count = 0;
|
|
743
|
+
offer.offer_products.offer_buy_products=offer.offer_products.offer_buy_products.map(item =>
|
|
744
|
+
item.toString().length > 10 ? item : parseInt(item, 10)
|
|
745
|
+
);
|
|
746
|
+
offer.offer_products.offer_get_products=offer.offer_products.offer_get_products.map(item =>
|
|
747
|
+
item.toString().length > 10 ? item : parseInt(item, 10)
|
|
748
|
+
)
|
|
786
749
|
items.forEach(function (element) {
|
|
787
|
-
|
|
750
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
751
|
+
if (offer.offer_products.offer_buy_products.includes(itemId) || offer.offer_products.offer_get_products.includes(itemId)) {
|
|
788
752
|
for (var i = 1; i <= element.quantity; i++) {
|
|
789
753
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
790
754
|
var amount = element.item_price;
|
|
@@ -792,9 +756,9 @@ function addItemQuantity(items, offer) {
|
|
|
792
756
|
if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
|
|
793
757
|
amount = element.item_price_with_modifier;
|
|
794
758
|
} else {
|
|
795
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
796
|
-
|
|
797
|
-
|
|
759
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
760
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
761
|
+
);
|
|
798
762
|
var modifiers = [];
|
|
799
763
|
element.item_modifiers.forEach(function (modifier) {
|
|
800
764
|
|
|
@@ -810,8 +774,8 @@ function addItemQuantity(items, offer) {
|
|
|
810
774
|
'item_id': element.item_id,
|
|
811
775
|
'amount': amount,
|
|
812
776
|
'item': item,
|
|
813
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
814
|
-
'get': offer.offer_products.offer_get_products.includes(
|
|
777
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
778
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
815
779
|
});
|
|
816
780
|
} else {
|
|
817
781
|
finalItems.push({
|
|
@@ -819,13 +783,13 @@ function addItemQuantity(items, offer) {
|
|
|
819
783
|
'item_id': element.item_id,
|
|
820
784
|
// / element.quantity
|
|
821
785
|
'amount': element.item_price,
|
|
822
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
823
|
-
'get': offer.offer_products.offer_get_products.includes(
|
|
786
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
787
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
824
788
|
});
|
|
789
|
+
|
|
790
|
+
// console.log(finalItems)
|
|
825
791
|
}
|
|
826
792
|
}
|
|
827
|
-
} else {
|
|
828
|
-
count++;
|
|
829
793
|
}
|
|
830
794
|
count++;
|
|
831
795
|
if (count == items.length) {
|
|
@@ -852,8 +816,6 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
852
816
|
});
|
|
853
817
|
});
|
|
854
818
|
|
|
855
|
-
var cartItem = JSON.parse(JSON.stringify(carts));
|
|
856
|
-
|
|
857
819
|
if (carts) {
|
|
858
820
|
carts.map((a) => {
|
|
859
821
|
a.promotion_discount = 0;
|
|
@@ -865,24 +827,30 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
865
827
|
var total = 0;
|
|
866
828
|
var count = 0;
|
|
867
829
|
var offerAmount = offer.oo_offer_value;
|
|
868
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
869
|
-
|
|
830
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
831
|
+
val.toString().length > 10 ? val : Number(val)
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
var cartItem = carts;
|
|
870
835
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
871
836
|
cartItem.sort(function (a, b) {
|
|
872
837
|
return a.item_price_with_modifier / a.quantity > b.item_price_with_modifier / b.quantity ? 1 : -1;
|
|
873
838
|
});
|
|
874
839
|
} else {
|
|
875
840
|
cartItem.sort(function (a, b) {
|
|
841
|
+
// If price is zero, move it to the end
|
|
842
|
+
if (a.item_price == 0) return 1;
|
|
843
|
+
if (b.item_price == 0) return -1;
|
|
876
844
|
return a.item_price / a.quantity > b.item_price / b.quantity ? 1 : -1;
|
|
877
845
|
});
|
|
878
846
|
}
|
|
879
847
|
|
|
880
848
|
var maxProduct = offer.offer_products.max_product;
|
|
881
|
-
|
|
882
849
|
cartItem.forEach((element, index) => {
|
|
883
850
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
884
851
|
|
|
885
|
-
|
|
852
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
853
|
+
if (offer.offer_products.offer_discount_products.includes(itemId)) {
|
|
886
854
|
var offerAmount = 0;
|
|
887
855
|
let costWithOutModifier = 0;
|
|
888
856
|
var singleItemCost = element.item_price;
|
|
@@ -901,9 +869,9 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
901
869
|
singleItemCost = singleItemCost + singModPrice;
|
|
902
870
|
});
|
|
903
871
|
} else {
|
|
904
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
905
|
-
|
|
906
|
-
|
|
872
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
873
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
874
|
+
);
|
|
907
875
|
|
|
908
876
|
element.item_modifiers.forEach((modifier, modiIndex) => {
|
|
909
877
|
if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
|
|
@@ -967,6 +935,135 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
967
935
|
}
|
|
968
936
|
};
|
|
969
937
|
|
|
938
|
+
function getFixedPriceProductOffer(cartObject, offer) {
|
|
939
|
+
try {
|
|
940
|
+
let carts = cartObject.item_details;
|
|
941
|
+
carts.forEach(function (v) {
|
|
942
|
+
v.promotion_discount_modifier = 0;
|
|
943
|
+
v.promotion_discount = 0;
|
|
944
|
+
v.redeem_promotion_id = 0;
|
|
945
|
+
if (v.item_modifiers) v.item_modifiers.forEach((mod) => {
|
|
946
|
+
mod.promotion_discount = 0;
|
|
947
|
+
mod.redeem_promotion_id = 0;
|
|
948
|
+
});
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
if (carts && offer.oo_offer_type == "absolute") {
|
|
952
|
+
carts.map((a) => {
|
|
953
|
+
a.promotion_discount = 0;
|
|
954
|
+
a.redeem_promotion_id = 0;
|
|
955
|
+
a.remove = false;
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
return new Promise(function (resolve, reject) {
|
|
959
|
+
var total = 0;
|
|
960
|
+
var count = 0;
|
|
961
|
+
var fixedPrice = offer.oo_offer_value;
|
|
962
|
+
let appliedQty = 0; // track how many quantities discounted
|
|
963
|
+
let discountedItemIds = new Set(); // track which items already discounted
|
|
964
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
965
|
+
val.toString().length > 10 ? val : Number(val)
|
|
966
|
+
);
|
|
967
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
968
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
969
|
+
);
|
|
970
|
+
|
|
971
|
+
|
|
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
|
+
|
|
977
|
+
cartItem.forEach((element, index) => {
|
|
978
|
+
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
979
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
980
|
+
|
|
981
|
+
if (
|
|
982
|
+
(offer.offer_products.offer_discount_products.includes(itemId) ||
|
|
983
|
+
offer.offer_products.package_items.includes(itemId))
|
|
984
|
+
&& !discountedItemIds.has(itemId)
|
|
985
|
+
) {
|
|
986
|
+
var offerAmount = 0;
|
|
987
|
+
var singleItemCost = element.item_price;
|
|
988
|
+
|
|
989
|
+
const maxQty = offer.max_quantity || 1;
|
|
990
|
+
let remainingQty = maxQty - appliedQty;
|
|
991
|
+
const qtyToDiscount = Math.min(element.quantity, remainingQty);
|
|
992
|
+
|
|
993
|
+
if (qtyToDiscount > 0 && singleItemCost >= fixedPrice) {
|
|
994
|
+
offerAmount = (singleItemCost * qtyToDiscount) - (fixedPrice * qtyToDiscount);
|
|
995
|
+
appliedQty += qtyToDiscount;
|
|
996
|
+
|
|
997
|
+
total += parseFloat(offerAmount);
|
|
998
|
+
carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
|
|
999
|
+
carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
|
|
1000
|
+
carts[ItemIndex].remove = true;
|
|
1001
|
+
|
|
1002
|
+
if (offerAmount > 0) {
|
|
1003
|
+
carts[ItemIndex].redeem_promotion_id = offer.offer_id;
|
|
1004
|
+
} else {
|
|
1005
|
+
carts[ItemIndex].item_modifiers.map((res) => {
|
|
1006
|
+
res.promotion_discount = 0;
|
|
1007
|
+
res.redeem_promotion_id = 0;
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
if (appliedQty == maxQty) {
|
|
1012
|
+
discountedItemIds.add(itemId);
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
count++;
|
|
1018
|
+
if (count == carts.length && carts) {
|
|
1019
|
+
cartObject.item_details = carts;
|
|
1020
|
+
cartObject.discount = total;
|
|
1021
|
+
resolve(cartObject);
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
});
|
|
1025
|
+
} else {
|
|
1026
|
+
return new Promise(function (resolve, reject) {
|
|
1027
|
+
cartObject.discount = 0
|
|
1028
|
+
resolve(cartObject);
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
} catch (error) {
|
|
1032
|
+
reject(error)
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
|
|
1037
|
+
return new Promise((resolve, reject) => {
|
|
1038
|
+
try {
|
|
1039
|
+
let discountRoundOff = offer.discount_roundoff; // 1 = Round off, 0 = No rounding
|
|
1040
|
+
let deliveryDiscount = 0; // Default
|
|
1041
|
+
|
|
1042
|
+
if (cartObject.total >= offer.oo_min_amount && offer.oo_offer_type == 'percentage' && offer.oo_offer_value > 0) {
|
|
1043
|
+
// Calculate discount as a percentage of delivery cost
|
|
1044
|
+
deliveryDiscount = (deliveryCost * offer.oo_offer_value) / 100;
|
|
1045
|
+
|
|
1046
|
+
// Apply rounding if discountRoundOff is 1
|
|
1047
|
+
if (discountRoundOff == 1) {
|
|
1048
|
+
deliveryDiscount = Math.round(deliveryDiscount);
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
cartObject.discount = 0;
|
|
1052
|
+
cartObject.delivery_discount = deliveryDiscount;
|
|
1053
|
+
cartObject.is_delivery_discount = true;
|
|
1054
|
+
} else {
|
|
1055
|
+
cartObject.discount = 0;
|
|
1056
|
+
cartObject.delivery_discount = 0;
|
|
1057
|
+
cartObject.is_delivery_discount = false;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
resolve(cartObject); // Return updated cartObject
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
reject(error);
|
|
1063
|
+
}
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
1066
|
+
|
|
970
1067
|
function setPackageDefaultDiscount(cart) {
|
|
971
1068
|
return new Promise(async (resolve, reject) => {
|
|
972
1069
|
try {
|
|
@@ -982,7 +1079,9 @@ function setPackageDefaultDiscount(cart) {
|
|
|
982
1079
|
let itemTotal = 0;
|
|
983
1080
|
if (item.package_items && item.package_items.length > 0) {
|
|
984
1081
|
item.package_items.forEach(function (v) {
|
|
985
|
-
|
|
1082
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1083
|
+
// itemTotal += ((item.quantity * v.quantity) * v.price);
|
|
1084
|
+
itemTotal += (item.quantity * v.price);
|
|
986
1085
|
})
|
|
987
1086
|
let packagePrice = item.quantity * item.item_price;
|
|
988
1087
|
let totalDiscount = (packagePrice < itemTotal) ? (parseFloat(itemTotal) - parseFloat(packagePrice)).toFixed(2) : 0;
|
|
@@ -1020,10 +1119,11 @@ function distributeDiscount(packageItems, totalDiscount, itemTotal, quantity) {
|
|
|
1020
1119
|
let totalApplied = 0;
|
|
1021
1120
|
let singleUnit = (quantity * parseFloat(totalDiscount) / parseFloat(itemTotal)).toFixed(2);
|
|
1022
1121
|
for (const [index, item] of packageItems.entries()) {
|
|
1122
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1123
|
+
// let discountPerItem = (parseFloat(singleUnit) * (parseInt(item.quantity) * parseFloat(item.price))).toFixed(2);
|
|
1023
1124
|
let discountPerItem = (parseFloat(singleUnit) * parseFloat(item.price)).toFixed(2);
|
|
1024
1125
|
totalApplied = (parseFloat(totalApplied) + parseFloat(discountPerItem)).toFixed(2);
|
|
1025
1126
|
item.default_combo_discount = parseFloat(discountPerItem).toFixed(2);
|
|
1026
|
-
|
|
1027
1127
|
if (index == packageItems.length - 1) {
|
|
1028
1128
|
|
|
1029
1129
|
let remaningOfferAmount = (parseFloat(totalDiscount) - parseFloat(totalApplied)).toFixed(2);
|