foodbot-cart-calculations 1.0.6 → 1.0.7-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 +201 -89
- package/functions/promotionCalculation.js +381 -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
|
}
|
|
@@ -398,8 +416,8 @@ function getOfferDetails(carts, offer = []) {
|
|
|
398
416
|
return new Promise(function (resolve, reject) {
|
|
399
417
|
try {
|
|
400
418
|
// buyItemAmount = 0
|
|
401
|
-
buyItemss = []
|
|
402
|
-
total = 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,239 +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
|
-
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
|
-
console.log('dddddddddfilterItem', filterItem)
|
|
624
|
-
|
|
625
|
-
cartData.item_details = carts
|
|
626
|
-
getBuyxGetYOnCondition(filterItem, cartData, offer).then((data) => {
|
|
627
|
-
resolve(data)
|
|
628
|
-
}).catch(error => {
|
|
629
|
-
reject(error);
|
|
630
|
-
})
|
|
631
|
-
break;
|
|
632
|
-
} else {
|
|
633
|
-
if (offer.oo_offer_type == 'absolute') {
|
|
634
|
-
cartData.item_details = carts
|
|
635
|
-
getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount).then((data) => {
|
|
636
|
-
resolve(data);
|
|
637
|
-
}).catch(error => {
|
|
638
|
-
reject(error);
|
|
639
|
-
})
|
|
640
|
-
}
|
|
641
|
-
else {
|
|
642
|
-
cartData.discount = cartData.totalDiscount
|
|
643
|
-
resolve(cartData);
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
break;
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
else {
|
|
650
|
-
cartData.discount = cartData.totalDiscount
|
|
651
|
-
resolve(cartData);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
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
|
+
}
|
|
654
553
|
} else {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
})
|
|
662
|
-
}
|
|
663
|
-
else {
|
|
664
|
-
cartData.discount = cartData.totalDiscount
|
|
665
|
-
resolve(cartData);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
|
|
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);
|
|
669
560
|
}
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
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);
|
|
679
624
|
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// setTimeout(() => {
|
|
628
|
+
// resolve(0);
|
|
629
|
+
// }, 20);
|
|
680
630
|
});
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total) {
|
|
684
634
|
return new Promise(function (resolve, reject) {
|
|
685
635
|
try {
|
|
636
|
+
console.log("ooooooooooooooo",JSON.parse(JSON.stringify(offer)))
|
|
686
637
|
let offerAmount = offer.oo_offer_value;
|
|
687
638
|
let singlePriceDiscount = offerAmount / buyItemAmount;
|
|
688
639
|
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
689
640
|
let remaningOfferAmount = offer.oo_offer_value;
|
|
690
641
|
let count = 0;
|
|
691
|
-
let carts = cartObject.item_details
|
|
642
|
+
let carts = cartObject.item_details;
|
|
643
|
+
console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",buyItemss,total)
|
|
692
644
|
if(buyItemss && buyItemss.length>0){
|
|
693
645
|
buyItemss.forEach((element, buyIndex) => {
|
|
694
646
|
let itemAmount = 0;
|
|
@@ -700,8 +652,10 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
|
700
652
|
if (itemIndex == -1) itemIndex = carts.findIndex((obj) => {
|
|
701
653
|
return obj.item_id == element.item_id;
|
|
702
654
|
});
|
|
655
|
+
|
|
656
|
+
console.log("carts index",carts,itemIndex)
|
|
703
657
|
|
|
704
|
-
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;
|
|
705
659
|
|
|
706
660
|
if (element.item_modifiers && element.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
|
|
707
661
|
let modiCount = 0;
|
|
@@ -746,6 +700,7 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
|
746
700
|
carts[itemIndex].discount_applied = true;
|
|
747
701
|
|
|
748
702
|
if (buyItemss[buyItemss.length - 1].item_id == element.item_id) {
|
|
703
|
+
console.log("innnn ifffffffffffff",element.item_price,remaningOfferAmount)
|
|
749
704
|
|
|
750
705
|
itemAmount = (element.item_price < remaningOfferAmount) ? element.item_price : parseFloat(remaningOfferAmount).toFixed(2);
|
|
751
706
|
} else {
|
|
@@ -753,6 +708,7 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
|
753
708
|
}
|
|
754
709
|
|
|
755
710
|
itemAmount = parseFloat(itemAmount);
|
|
711
|
+
console.log("iiiiiiiiii",itemAmount)
|
|
756
712
|
total = parseFloat(total) + parseFloat(itemAmount);
|
|
757
713
|
total = parseFloat(total).toFixed(2);
|
|
758
714
|
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
|
|
@@ -763,10 +719,10 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
|
|
|
763
719
|
carts[itemIndex].redeem_promotion_id = offer.offer_id;
|
|
764
720
|
|
|
765
721
|
if (count == buyItemss.length) {
|
|
722
|
+
console.log("ttttttttttttttt",total)
|
|
766
723
|
cartObject.item_details = carts
|
|
767
724
|
cartObject.discount = total
|
|
768
|
-
|
|
769
|
-
|
|
725
|
+
|
|
770
726
|
resolve(cartObject);
|
|
771
727
|
}
|
|
772
728
|
});
|
|
@@ -784,8 +740,15 @@ function addItemQuantity(items, offer) {
|
|
|
784
740
|
try {
|
|
785
741
|
var finalItems = [];
|
|
786
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
|
+
)
|
|
787
749
|
items.forEach(function (element) {
|
|
788
|
-
|
|
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)) {
|
|
789
752
|
for (var i = 1; i <= element.quantity; i++) {
|
|
790
753
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
791
754
|
var amount = element.item_price;
|
|
@@ -793,9 +756,9 @@ function addItemQuantity(items, offer) {
|
|
|
793
756
|
if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
|
|
794
757
|
amount = element.item_price_with_modifier;
|
|
795
758
|
} else {
|
|
796
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
797
|
-
|
|
798
|
-
|
|
759
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
760
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
761
|
+
);
|
|
799
762
|
var modifiers = [];
|
|
800
763
|
element.item_modifiers.forEach(function (modifier) {
|
|
801
764
|
|
|
@@ -811,8 +774,8 @@ function addItemQuantity(items, offer) {
|
|
|
811
774
|
'item_id': element.item_id,
|
|
812
775
|
'amount': amount,
|
|
813
776
|
'item': item,
|
|
814
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
815
|
-
'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)
|
|
816
779
|
});
|
|
817
780
|
} else {
|
|
818
781
|
finalItems.push({
|
|
@@ -820,13 +783,13 @@ function addItemQuantity(items, offer) {
|
|
|
820
783
|
'item_id': element.item_id,
|
|
821
784
|
// / element.quantity
|
|
822
785
|
'amount': element.item_price,
|
|
823
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
824
|
-
'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)
|
|
825
788
|
});
|
|
789
|
+
|
|
790
|
+
// console.log(finalItems)
|
|
826
791
|
}
|
|
827
792
|
}
|
|
828
|
-
} else {
|
|
829
|
-
count++;
|
|
830
793
|
}
|
|
831
794
|
count++;
|
|
832
795
|
if (count == items.length) {
|
|
@@ -853,8 +816,6 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
853
816
|
});
|
|
854
817
|
});
|
|
855
818
|
|
|
856
|
-
var cartItem = JSON.parse(JSON.stringify(carts));
|
|
857
|
-
|
|
858
819
|
if (carts) {
|
|
859
820
|
carts.map((a) => {
|
|
860
821
|
a.promotion_discount = 0;
|
|
@@ -866,24 +827,30 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
866
827
|
var total = 0;
|
|
867
828
|
var count = 0;
|
|
868
829
|
var offerAmount = offer.oo_offer_value;
|
|
869
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
870
|
-
|
|
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;
|
|
871
835
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
872
836
|
cartItem.sort(function (a, b) {
|
|
873
837
|
return a.item_price_with_modifier / a.quantity > b.item_price_with_modifier / b.quantity ? 1 : -1;
|
|
874
838
|
});
|
|
875
839
|
} else {
|
|
876
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;
|
|
877
844
|
return a.item_price / a.quantity > b.item_price / b.quantity ? 1 : -1;
|
|
878
845
|
});
|
|
879
846
|
}
|
|
880
847
|
|
|
881
848
|
var maxProduct = offer.offer_products.max_product;
|
|
882
|
-
|
|
883
849
|
cartItem.forEach((element, index) => {
|
|
884
850
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
885
851
|
|
|
886
|
-
|
|
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)) {
|
|
887
854
|
var offerAmount = 0;
|
|
888
855
|
let costWithOutModifier = 0;
|
|
889
856
|
var singleItemCost = element.item_price;
|
|
@@ -902,9 +869,9 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
902
869
|
singleItemCost = singleItemCost + singModPrice;
|
|
903
870
|
});
|
|
904
871
|
} else {
|
|
905
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
906
|
-
|
|
907
|
-
|
|
872
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
873
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
874
|
+
);
|
|
908
875
|
|
|
909
876
|
element.item_modifiers.forEach((modifier, modiIndex) => {
|
|
910
877
|
if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
|
|
@@ -968,6 +935,135 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
968
935
|
}
|
|
969
936
|
};
|
|
970
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
|
+
|
|
971
1067
|
function setPackageDefaultDiscount(cart) {
|
|
972
1068
|
return new Promise(async (resolve, reject) => {
|
|
973
1069
|
try {
|
|
@@ -983,7 +1079,9 @@ function setPackageDefaultDiscount(cart) {
|
|
|
983
1079
|
let itemTotal = 0;
|
|
984
1080
|
if (item.package_items && item.package_items.length > 0) {
|
|
985
1081
|
item.package_items.forEach(function (v) {
|
|
986
|
-
|
|
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);
|
|
987
1085
|
})
|
|
988
1086
|
let packagePrice = item.quantity * item.item_price;
|
|
989
1087
|
let totalDiscount = (packagePrice < itemTotal) ? (parseFloat(itemTotal) - parseFloat(packagePrice)).toFixed(2) : 0;
|
|
@@ -1021,10 +1119,11 @@ function distributeDiscount(packageItems, totalDiscount, itemTotal, quantity) {
|
|
|
1021
1119
|
let totalApplied = 0;
|
|
1022
1120
|
let singleUnit = (quantity * parseFloat(totalDiscount) / parseFloat(itemTotal)).toFixed(2);
|
|
1023
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);
|
|
1024
1124
|
let discountPerItem = (parseFloat(singleUnit) * parseFloat(item.price)).toFixed(2);
|
|
1025
1125
|
totalApplied = (parseFloat(totalApplied) + parseFloat(discountPerItem)).toFixed(2);
|
|
1026
1126
|
item.default_combo_discount = parseFloat(discountPerItem).toFixed(2);
|
|
1027
|
-
|
|
1028
1127
|
if (index == packageItems.length - 1) {
|
|
1029
1128
|
|
|
1030
1129
|
let remaningOfferAmount = (parseFloat(totalDiscount) - parseFloat(totalApplied)).toFixed(2);
|