foodbot-cart-calculations 1.0.4 → 1.0.5-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 +59 -12
- package/functions/pointsCalculation.js +201 -89
- package/functions/promotionCalculation.js +382 -282
- 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,23 +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
|
-
|
|
36
|
-
offer.
|
|
40
|
+
|
|
41
|
+
// offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
|
|
42
|
+
// offer.oo_offer_values = JSON.parse(JSON.stringify(offer.oo_offer_value))
|
|
37
43
|
offer.oo_offer_type = 'absolute'
|
|
38
|
-
offer.oo_offer_value = Math.round(carts.discount)
|
|
44
|
+
offer.oo_offer_value = (carts && carts.discount)?Math.round(carts.discount):0
|
|
39
45
|
getOfferDetails(carts, offer).then(res => {
|
|
40
46
|
resolve(res)
|
|
41
47
|
}).catch(error=>{
|
|
@@ -49,12 +55,13 @@ function offerCalculation(carts, offer) {
|
|
|
49
55
|
reject(error);
|
|
50
56
|
})
|
|
51
57
|
} else if (offer && offer.offer_discount_type && (offer.offer_discount_type == 'discount' || offer.offer_discount_type == 'manual')) {
|
|
58
|
+
|
|
52
59
|
getCategorySpeificOffer(carts, offer).then(res => {
|
|
53
60
|
carts = res
|
|
54
61
|
|
|
55
62
|
if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
|
|
56
|
-
offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
|
|
57
|
-
offer.oo_offer_values = JSON.parse(JSON.stringify(offer.oo_offer_value))
|
|
63
|
+
// offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
|
|
64
|
+
// offer.oo_offer_values = JSON.parse(JSON.stringify(offer.oo_offer_value))
|
|
58
65
|
offer.oo_offer_type = 'absolute'
|
|
59
66
|
offer.oo_offer_value = Math.round(carts.discount)
|
|
60
67
|
getCategorySpeificOffer(carts, offer).then(res => {
|
|
@@ -75,6 +82,18 @@ function offerCalculation(carts, offer) {
|
|
|
75
82
|
}).catch(error => {
|
|
76
83
|
reject(error);
|
|
77
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
|
+
})
|
|
78
97
|
} else {
|
|
79
98
|
carts.discount = 0
|
|
80
99
|
resolve(carts)
|
|
@@ -108,13 +127,15 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
108
127
|
if (parseFloat(offer.oo_min_amount) <= parseFloat(afterTotal)) {
|
|
109
128
|
|
|
110
129
|
if (offer.offer_discount_type != 'manual')
|
|
111
|
-
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
|
+
);
|
|
112
133
|
|
|
113
134
|
if (offer.offer_discount_type != 'manual')
|
|
114
135
|
if (offer.offer_products.package_items) {
|
|
115
|
-
offer.offer_products.package_items = offer.offer_products.package_items.map(
|
|
116
|
-
|
|
117
|
-
|
|
136
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
137
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
138
|
+
);
|
|
118
139
|
}
|
|
119
140
|
else {
|
|
120
141
|
offer.offer_products.package_items = []
|
|
@@ -122,18 +143,20 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
122
143
|
|
|
123
144
|
let foundItemsSum = 0
|
|
124
145
|
carts.item_details.filter((res) => {
|
|
125
|
-
if
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
+
}
|
|
129
152
|
}
|
|
130
153
|
})
|
|
131
154
|
|
|
132
155
|
let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
|
|
133
156
|
let remaningOfferAmount = offerAmount
|
|
134
157
|
carts.item_details.forEach((element, index) => {
|
|
135
|
-
|
|
136
|
-
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))) {
|
|
137
160
|
var itemAmount = 0;
|
|
138
161
|
|
|
139
162
|
// Apply discount on Packages
|
|
@@ -141,7 +164,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
141
164
|
if (offer.oo_offer_type == 'percentage') {
|
|
142
165
|
let itemTotalDiscount = 0
|
|
143
166
|
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
144
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
167
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
145
168
|
let itemDiscount = parseFloat(itemAmount).toFixed(2);
|
|
146
169
|
|
|
147
170
|
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
@@ -158,7 +181,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
158
181
|
let modiItemAmount = 0
|
|
159
182
|
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
160
183
|
|
|
161
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
184
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
162
185
|
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
163
186
|
total = parseFloat(total) + parseFloat(discount)
|
|
164
187
|
total = parseFloat(total).toFixed(2);
|
|
@@ -209,7 +232,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
209
232
|
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
210
233
|
|
|
211
234
|
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
212
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
235
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
213
236
|
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
214
237
|
total = parseFloat(total).toFixed(2);
|
|
215
238
|
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
@@ -258,7 +281,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
258
281
|
if (offer.oo_offer_type == 'percentage') {
|
|
259
282
|
let itemTotalDiscount = 0
|
|
260
283
|
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
261
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
284
|
+
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount).toFixed(2)
|
|
262
285
|
let itemDiscount = parseFloat(itemAmount).toFixed(2)
|
|
263
286
|
|
|
264
287
|
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
@@ -272,7 +295,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
272
295
|
let modiItemAmount = 0
|
|
273
296
|
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
274
297
|
|
|
275
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
298
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
276
299
|
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
277
300
|
total = parseFloat(total) + parseFloat(discount)
|
|
278
301
|
total = parseFloat(total).toFixed(2);
|
|
@@ -306,7 +329,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
306
329
|
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
307
330
|
|
|
308
331
|
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
309
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
|
|
332
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
310
333
|
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
311
334
|
total = parseFloat(total).toFixed(2);
|
|
312
335
|
carts.item_details[index].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
@@ -336,7 +359,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
336
359
|
|
|
337
360
|
total = parseFloat(total).toFixed(2)
|
|
338
361
|
|
|
339
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
362
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
340
363
|
remaningOfferAmount = remaningOfferAmount - itemAmount
|
|
341
364
|
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
|
|
342
365
|
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
|
|
@@ -353,13 +376,8 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
353
376
|
let difference = Number(offerAmount) - Number(total)
|
|
354
377
|
difference = parseFloat(difference).toFixed(2)
|
|
355
378
|
difference = Math.abs(difference)
|
|
356
|
-
|
|
357
379
|
if (indexes.length > 0) {
|
|
358
380
|
|
|
359
|
-
// console.log("difference", difference)
|
|
360
|
-
// console.log("total", total)
|
|
361
|
-
// console.log("offerAmount", offerAmount)
|
|
362
|
-
|
|
363
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
|
|
364
382
|
carts.item_details[indexes[0]].promotion_discount = parseFloat(carts.item_details[indexes[0]].promotion_discount).toFixed(2)
|
|
365
383
|
|
|
@@ -371,7 +389,7 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
371
389
|
total = parseFloat(total).toFixed(2)
|
|
372
390
|
}
|
|
373
391
|
}
|
|
374
|
-
|
|
392
|
+
console.log("total", total)
|
|
375
393
|
carts.discount = total
|
|
376
394
|
resolve(carts);
|
|
377
395
|
}
|
|
@@ -397,9 +415,9 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
397
415
|
function getOfferDetails(carts, offer = []) {
|
|
398
416
|
return new Promise(function (resolve, reject) {
|
|
399
417
|
try {
|
|
400
|
-
buyItemAmount = 0
|
|
401
|
-
buyItemss = []
|
|
402
|
-
total = 0
|
|
418
|
+
// buyItemAmount = 0
|
|
419
|
+
// buyItemss = []
|
|
420
|
+
// total = 0
|
|
403
421
|
|
|
404
422
|
offer.offer_products.get_item_count = parseInt(offer.offer_products.get_item_count);
|
|
405
423
|
offer.offer_products.buy_item_count = parseInt(offer.offer_products.buy_item_count);
|
|
@@ -432,23 +450,33 @@ function getOfferDetails(carts, offer = []) {
|
|
|
432
450
|
a.promotion_discount_modifier = 0;
|
|
433
451
|
a.discount_applied = false;
|
|
434
452
|
});
|
|
435
|
-
|
|
453
|
+
|
|
436
454
|
const loopArray = [];
|
|
437
455
|
const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
|
|
438
456
|
const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
|
|
439
457
|
addItemQuantity(carts.item_details, offer).then((data) => {
|
|
440
|
-
const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ?
|
|
441
|
-
|
|
442
|
-
|
|
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
|
+
|
|
466
|
+
let total = 0
|
|
467
|
+
let buyItemAmount=0;
|
|
468
|
+
let buyItemss =[]
|
|
469
|
+
getBuyxGetYOnCondition(descending, carts, offer,total,buyItemAmount,buyItemss).then((total) => {
|
|
470
|
+
// carts.discount = carts.totalDiscount
|
|
443
471
|
resolve(carts)
|
|
444
472
|
}).catch(error => {
|
|
445
473
|
reject(error)
|
|
446
|
-
})
|
|
474
|
+
});
|
|
447
475
|
}).catch(error => {
|
|
448
476
|
reject(error);
|
|
449
477
|
})
|
|
450
478
|
} else {
|
|
451
|
-
resolve(
|
|
479
|
+
resolve(carts);
|
|
452
480
|
}
|
|
453
481
|
} catch (error) {
|
|
454
482
|
reject(error)
|
|
@@ -456,238 +484,163 @@ function getOfferDetails(carts, offer = []) {
|
|
|
456
484
|
})
|
|
457
485
|
};
|
|
458
486
|
|
|
459
|
-
function getBuyxGetYOnCondition(descending, cartData, offer) {
|
|
460
|
-
return new Promise(function (resolve, reject) {
|
|
461
487
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
).toFixed(2);
|
|
528
|
-
|
|
529
|
-
if (desc[i].item.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
|
|
530
|
-
let modiCount = 0;
|
|
531
|
-
|
|
532
|
-
desc[i].item.item_modifiers.forEach((modi, mIndex) => {
|
|
533
|
-
modiItemAmount =
|
|
534
|
-
(((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) *
|
|
535
|
-
parseFloat(offer.oo_offer_value)) /
|
|
536
|
-
100;
|
|
537
|
-
|
|
538
|
-
if (carts[itemIndex].item_modifiers[mIndex]) {
|
|
539
|
-
carts[itemIndex].item_modifiers[mIndex].promotion_discount = parseFloat(
|
|
540
|
-
modiItemAmount
|
|
541
|
-
).toFixed(2);
|
|
542
|
-
|
|
543
|
-
carts[itemIndex].item_modifiers[mIndex].redeem_promotion_id = offer.offer_id;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(modiItemAmount)
|
|
547
|
-
carts[itemIndex].promotion_discount_modifier = parseFloat(
|
|
548
|
-
parseFloat(carts[itemIndex].promotion_discount_modifier) +
|
|
549
|
-
parseFloat(modiItemAmount)
|
|
550
|
-
).toFixed(2);
|
|
551
|
-
|
|
552
|
-
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
|
|
553
|
-
amtWithModi = parseFloat(amtWithModi) + parseFloat(modiItemAmount);
|
|
554
|
-
amtWithModi = parseFloat(amtWithModi);
|
|
555
|
-
amt = parseFloat(amt) + parseFloat(amtWithModi);
|
|
556
|
-
|
|
557
|
-
var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
|
|
558
|
-
return obj.modifier_item_id == modi.modifier_item_id;
|
|
559
|
-
});
|
|
560
|
-
|
|
561
|
-
// if (carts[itemIndex].item_modifiers[modiIndex]) {
|
|
562
|
-
// carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(
|
|
563
|
-
// modiItemAmount
|
|
564
|
-
// ).toFixed(2);
|
|
565
|
-
// carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
566
|
-
// }
|
|
567
|
-
|
|
568
|
-
// modiCount++;
|
|
569
|
-
});
|
|
570
|
-
} else {
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
carts[itemIndex].promotion_discount = parseFloat(
|
|
574
|
-
parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
|
|
575
|
-
).toFixed(2);
|
|
576
|
-
|
|
577
|
-
// carts[itemIndex].promotion_discount_modifier = parseFloat(
|
|
578
|
-
// parseFloat(carts[itemIndex].promotion_discount_modifier) +
|
|
579
|
-
// parseFloat(itemTotalDiscount)
|
|
580
|
-
// ).toFixed(2);
|
|
581
|
-
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
582
|
-
} else {
|
|
583
|
-
buyItemAmount = buyItemAmount + desc[i].amount;
|
|
584
|
-
desc[i].item.indexss = i;
|
|
585
|
-
buyItemss.push(desc[i].item);
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
if (parseInt(amt) != 0) {
|
|
589
|
-
let amtModiTotal = amt;
|
|
590
|
-
// carts[itemIndex].promotion_discount_modifier = amtModiTotal;
|
|
591
|
-
// carts[itemIndex].promotion_discount = (
|
|
592
|
-
// parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
|
|
593
|
-
// ).toFixed(2);
|
|
594
|
-
total = total + amtWithModi;
|
|
595
|
-
|
|
596
|
-
// console.log('amount modifffffffffffffff', amtModiTotal)
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
getCount++;
|
|
604
|
-
desc1[i].remove = true;
|
|
605
|
-
desc1[i + 1].remove = true;
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
if (
|
|
609
|
-
getCount == offer.offer_products.get_item_count &&
|
|
610
|
-
buyCount == offer.offer_products.buy_item_count
|
|
611
|
-
) {
|
|
612
|
-
total = parseFloat(total) + parseFloat(amount);
|
|
613
|
-
// cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(amount)
|
|
614
|
-
|
|
615
|
-
if (offer.offer_products.order_multi_use == 1) {
|
|
616
|
-
var filterItem = desc1.filter((e) => {
|
|
617
|
-
if (!e.remove) {
|
|
618
|
-
return e.item_id;
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
|
|
622
|
-
console.log('dddddddddfilterItem', filterItem)
|
|
623
|
-
|
|
624
|
-
cartData.item_details = carts
|
|
625
|
-
getBuyxGetYOnCondition(filterItem, cartData, offer).then((data) => {
|
|
626
|
-
resolve(data)
|
|
627
|
-
}).catch(error => {
|
|
628
|
-
reject(error);
|
|
629
|
-
})
|
|
630
|
-
break;
|
|
631
|
-
} else {
|
|
632
|
-
if (offer.oo_offer_type == 'absolute') {
|
|
633
|
-
cartData.item_details = carts
|
|
634
|
-
getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount).then((data) => {
|
|
635
|
-
resolve(data);
|
|
636
|
-
}).catch(error => {
|
|
637
|
-
reject(error);
|
|
638
|
-
})
|
|
639
|
-
}
|
|
640
|
-
else {
|
|
641
|
-
cartData.discount = cartData.totalDiscount
|
|
642
|
-
resolve(cartData);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
break;
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
else {
|
|
649
|
-
cartData.discount = cartData.totalDiscount
|
|
650
|
-
resolve(cartData);
|
|
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
|
+
}
|
|
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);
|
|
678
624
|
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// setTimeout(() => {
|
|
628
|
+
// resolve(0);
|
|
629
|
+
// }, 20);
|
|
679
630
|
});
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
631
|
+
}
|
|
632
|
+
|
|
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) {
|
|
|
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) {
|
|
|
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) {
|
|
|
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) {
|
|
|
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);
|