foodbot-cart-calculations 1.0.2 → 1.0.3-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.
@@ -1,26 +1,52 @@
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
5
5
  carts.item_details.forEach(element => {
6
+ delete element.promotion_discount;
6
7
  let itemTotal = element.item_price * element.quantity
7
- carts.total = carts.total + itemTotal
8
- element.item_modifiers.forEach(mod => {
9
- let modTotal = (mod.modifier_item_price * mod.quantity) * element.quantity
10
- carts.total += modTotal
11
- });
8
+ carts.total = carts.total + itemTotal;
9
+ if(element.item_modifiers && element.item_modifiers.length>0){
10
+ element.item_modifiers.forEach(mod => {
11
+ delete mod.promotion_discount;
12
+ delete mod.points_discount;
13
+ let modTotal = (mod.modifier_item_price * mod.quantity) * element.quantity
14
+ carts.total += modTotal
15
+ });
16
+ }
17
+ if (element.isPackage == 1) {
18
+ element.package_items.forEach(function (p) {
19
+ delete p.promotion_discount;
20
+ if (p.item_modifiers)
21
+ p.item_modifiers.forEach((pMod) => {
22
+ delete pMod.points_discount;
23
+ delete pMod.promotion_discount;
24
+ });
25
+
26
+ if (p.modifiers)
27
+ p.modifiers.forEach((pMod) => {
28
+ delete pMod.points_discount;
29
+ delete pMod.promotion_discount;
30
+ });
31
+ });
32
+ }
12
33
  });
34
+
13
35
  if (offer && offer.offer_discount_type && offer.offer_discount_type == 'buy_x_get_y') {
14
36
  getOfferDetails(carts, offer).then(res => {
15
37
  carts = res
38
+ // console.log("res",res)
16
39
  if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
17
- offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
18
- offer.oo_offer_values = JSON.parse(JSON.stringify(offer.oo_offer_value))
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))
19
43
  offer.oo_offer_type = 'absolute'
20
- offer.oo_offer_value = Math.round(carts.discount)
44
+ offer.oo_offer_value = (carts && carts.discount)?Math.round(carts.discount):0
21
45
  getOfferDetails(carts, offer).then(res => {
22
46
  resolve(res)
23
- }).catch(error);
47
+ }).catch(error=>{
48
+ reject(error)
49
+ });
24
50
  }
25
51
  else {
26
52
  resolve(carts)
@@ -29,12 +55,13 @@ function offerCalculation(carts, offer) {
29
55
  reject(error);
30
56
  })
31
57
  } else if (offer && offer.offer_discount_type && (offer.offer_discount_type == 'discount' || offer.offer_discount_type == 'manual')) {
58
+
32
59
  getCategorySpeificOffer(carts, offer).then(res => {
33
60
  carts = res
34
61
 
35
62
  if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
36
- offer.oo_offer_types = JSON.parse(JSON.stringify(offer.oo_offer_type))
37
- 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))
38
65
  offer.oo_offer_type = 'absolute'
39
66
  offer.oo_offer_value = Math.round(carts.discount)
40
67
  getCategorySpeificOffer(carts, offer).then(res => {
@@ -55,6 +82,18 @@ function offerCalculation(carts, offer) {
55
82
  }).catch(error => {
56
83
  reject(error);
57
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
+ })
58
97
  } else {
59
98
  carts.discount = 0
60
99
  resolve(carts)
@@ -88,13 +127,15 @@ function getCategorySpeificOffer(carts, offer) {
88
127
  if (parseFloat(offer.oo_min_amount) <= parseFloat(afterTotal)) {
89
128
 
90
129
  if (offer.offer_discount_type != 'manual')
91
- offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(Number);
130
+ offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
131
+ val.toString().length > 10 ? val : Number(val)
132
+ );
92
133
 
93
134
  if (offer.offer_discount_type != 'manual')
94
135
  if (offer.offer_products.package_items) {
95
- offer.offer_products.package_items = offer.offer_products.package_items.map(function (x) {
96
- return parseInt(x, 10);
97
- });
136
+ offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
137
+ x.toString().length > 10 ? x : parseInt(x, 10)
138
+ );
98
139
  }
99
140
  else {
100
141
  offer.offer_products.package_items = []
@@ -102,18 +143,20 @@ function getCategorySpeificOffer(carts, offer) {
102
143
 
103
144
  let foundItemsSum = 0
104
145
  carts.item_details.filter((res) => {
105
- if (!res.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(parseInt(res.item_id)) && res.isPackage == 0) || (offer.offer_products.package_items.includes(parseInt(res.item_id)) && res.isPackage == 1))) {
106
-
107
- foundItemsSum = foundItemsSum + res.item_price_with_modifier * res.quantity
108
- return foundItemsSum
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
+ }
109
152
  }
110
153
  })
111
154
 
112
155
  let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
113
156
  let remaningOfferAmount = offerAmount
114
157
  carts.item_details.forEach((element, index) => {
115
-
116
- if (!element.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(parseInt(element.item_id)) && element.isPackage == 0) || (offer.offer_products.package_items.includes(parseInt(element.item_id)) && element.isPackage == 1))) {
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))) {
117
160
  var itemAmount = 0;
118
161
 
119
162
  // Apply discount on Packages
@@ -121,7 +164,7 @@ function getCategorySpeificOffer(carts, offer) {
121
164
  if (offer.oo_offer_type == 'percentage') {
122
165
  let itemTotalDiscount = 0
123
166
  itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
124
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
167
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
125
168
  let itemDiscount = parseFloat(itemAmount).toFixed(2);
126
169
 
127
170
  total = parseFloat(total) + parseFloat(itemDiscount)
@@ -138,7 +181,7 @@ function getCategorySpeificOffer(carts, offer) {
138
181
  let modiItemAmount = 0
139
182
  modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
140
183
 
141
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
184
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
142
185
  let discount = parseFloat(modiItemAmount).toFixed(2)
143
186
  total = parseFloat(total) + parseFloat(discount)
144
187
  total = parseFloat(total).toFixed(2);
@@ -189,7 +232,7 @@ function getCategorySpeificOffer(carts, offer) {
189
232
  modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
190
233
 
191
234
  modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
192
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
235
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
193
236
  total = parseFloat(total) + parseFloat(modiItemAmount)
194
237
  total = parseFloat(total).toFixed(2);
195
238
  carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
@@ -238,7 +281,7 @@ function getCategorySpeificOffer(carts, offer) {
238
281
  if (offer.oo_offer_type == 'percentage') {
239
282
  let itemTotalDiscount = 0
240
283
  itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
241
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
284
+ itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount).toFixed(2)
242
285
  let itemDiscount = parseFloat(itemAmount).toFixed(2)
243
286
 
244
287
  total = parseFloat(total) + parseFloat(itemDiscount)
@@ -252,7 +295,7 @@ function getCategorySpeificOffer(carts, offer) {
252
295
  let modiItemAmount = 0
253
296
  modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
254
297
 
255
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
298
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
256
299
  let discount = parseFloat(modiItemAmount).toFixed(2)
257
300
  total = parseFloat(total) + parseFloat(discount)
258
301
  total = parseFloat(total).toFixed(2);
@@ -286,7 +329,7 @@ function getCategorySpeificOffer(carts, offer) {
286
329
  modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
287
330
 
288
331
  modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
289
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)
332
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
290
333
  total = parseFloat(total) + parseFloat(modiItemAmount)
291
334
  total = parseFloat(total).toFixed(2);
292
335
  carts.item_details[index].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
@@ -316,7 +359,7 @@ function getCategorySpeificOffer(carts, offer) {
316
359
 
317
360
  total = parseFloat(total).toFixed(2)
318
361
 
319
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
362
+ itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
320
363
  remaningOfferAmount = remaningOfferAmount - itemAmount
321
364
  carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
322
365
  carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
@@ -333,13 +376,8 @@ function getCategorySpeificOffer(carts, offer) {
333
376
  let difference = Number(offerAmount) - Number(total)
334
377
  difference = parseFloat(difference).toFixed(2)
335
378
  difference = Math.abs(difference)
336
-
337
379
  if (indexes.length > 0) {
338
380
 
339
- // console.log("difference", difference)
340
- // console.log("total", total)
341
- // console.log("offerAmount", offerAmount)
342
-
343
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
344
382
  carts.item_details[indexes[0]].promotion_discount = parseFloat(carts.item_details[indexes[0]].promotion_discount).toFixed(2)
345
383
 
@@ -351,7 +389,7 @@ function getCategorySpeificOffer(carts, offer) {
351
389
  total = parseFloat(total).toFixed(2)
352
390
  }
353
391
  }
354
- // console.log("total", total)
392
+ console.log("total", total)
355
393
  carts.discount = total
356
394
  resolve(carts);
357
395
  }
@@ -375,384 +413,322 @@ function getCategorySpeificOffer(carts, offer) {
375
413
  }
376
414
 
377
415
  function getOfferDetails(carts, offer = []) {
378
- try {
379
- buyItemAmount = 0
380
- buyItemss = []
381
- total = 0
416
+ return new Promise(function (resolve, reject) {
417
+ try {
418
+ // buyItemAmount = 0
419
+ // buyItemss = []
420
+ // total = 0
382
421
 
383
- offer.offer_products.get_item_count = parseInt(offer.offer_products.get_item_count);
384
- offer.offer_products.buy_item_count = parseInt(offer.offer_products.buy_item_count);
422
+ offer.offer_products.get_item_count = parseInt(offer.offer_products.get_item_count);
423
+ offer.offer_products.buy_item_count = parseInt(offer.offer_products.buy_item_count);
385
424
 
386
- // const buyItems = [];
387
- // let buyItemAmount = 0;
425
+ // const buyItems = [];
426
+ // let buyItemAmount = 0;
388
427
 
389
- carts.item_details.forEach(function (v) {
390
- delete v.promotion_discount_modifier;
391
- delete v.promotion_discount;
392
- delete v.redeem_promotion_id;
393
- });
428
+ // carts.item_details.forEach(function (v) {
429
+ // delete v.promotion_discount_modifier;
430
+ // delete v.promotion_discount;
431
+ // delete v.redeem_promotion_id;
432
+ // });
394
433
 
395
- carts.item_details.forEach(function (v) {
396
- delete v.promotion_discount_modifier;
397
- delete v.promotion_discount;
398
- delete v.redeem_promotion_id;
399
- if (v.item_modifiers) {
400
- v.item_modifiers.forEach((mod) => {
401
- delete mod.promotion_discount;
402
- delete mod.redeem_promotion_id;
403
- });
404
- }
405
- });
406
-
407
- if (carts && carts.item_details) {
408
- carts.item_details.map((a) => {
409
- a.promotion_discount = 0;
410
- a.redeem_promotion_id = 0;
411
- a.promotion_discount_modifier = 0;
412
- a.discount_applied = false;
434
+ carts.item_details.forEach(function (v) {
435
+ delete v.promotion_discount_modifier;
436
+ delete v.promotion_discount;
437
+ delete v.redeem_promotion_id;
438
+ if (v.item_modifiers) {
439
+ v.item_modifiers.forEach((mod) => {
440
+ delete mod.promotion_discount;
441
+ delete mod.redeem_promotion_id;
442
+ });
443
+ }
413
444
  });
414
- let total = 0;
415
- const loopArray = [];
416
- const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
417
- const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
418
445
 
419
- return new Promise(function (resolve, reject) {
446
+ if (carts && carts.item_details) {
447
+ carts.item_details.map((a) => {
448
+ a.promotion_discount = 0;
449
+ a.redeem_promotion_id = 0;
450
+ a.promotion_discount_modifier = 0;
451
+ a.discount_applied = false;
452
+ });
453
+
454
+ const loopArray = [];
455
+ const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
456
+ const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
420
457
  addItemQuantity(carts.item_details, offer).then((data) => {
421
- const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? -1 : 1);
422
- getBuyxGetYOnCondition(descending, carts, offer).then((total) => {
423
- carts.discount = carts.totalDiscount
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
424
471
  resolve(carts)
425
472
  }).catch(error => {
426
473
  reject(error)
427
- });;
474
+ });
428
475
  }).catch(error => {
429
476
  reject(error);
430
477
  })
431
- });
432
- } else {
433
- return new Promise(function (resolve, reject) {
434
- resolve({ 'offer_amount': 0, 'amount': '' });
435
- });
478
+ } else {
479
+ resolve(carts);
480
+ }
481
+ } catch (error) {
482
+ reject(error)
436
483
  }
437
- } catch (error) {
438
- reject(error)
439
- }
440
-
484
+ })
441
485
  };
442
486
 
443
- function getBuyxGetYOnCondition(descending, cartData, offer) {
444
- return new Promise(function (resolve, reject) {
445
-
446
- try {
447
- let carts = cartData.item_details
448
- var desc = JSON.parse(JSON.stringify(descending));
449
- var desc1 = JSON.parse(JSON.stringify(descending));
450
- var amount = 0;
451
- var buyArray = [];
452
- var getArray = [];
453
- let total = 0
454
-
455
- if (!cartData.totalDiscount)
456
- cartData.totalDiscount = 0
457
-
458
- var buyItems = desc.filter((e) => {
459
- if (offer.offer_products.offer_buy_products.includes(parseInt(e.item_id))) {
460
- return e.item_id;
461
- }
462
- });
463
487
 
464
- let totalCount = parseInt(offer.offer_products.get_item_count) + parseInt(offer.offer_products.buy_item_count);
465
-
466
- if (
467
- buyItems &&
468
- parseInt(buyItems.length) >= parseInt(offer.offer_products.buy_item_count) &&
469
- desc.length > 1 &&
470
- descending.length >= totalCount
471
- ) {
472
- var buyCount = 0;
473
- var getCount = 0;
474
-
475
- for (var i = 0, ii = desc.length; i < ii; i++) {
476
- let itemAmount = 0;
477
-
478
-
479
- 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
480
- ) {
481
- var amt = 0;
482
- let amtWithModi = 0;
483
- getArray.push(desc[i].item_id);
484
-
485
- buyCount++;
486
- desc1[i].remove = true;
487
- buyArray.push(desc[i].item_id);
488
- var itemIndex = -1;
489
-
490
- if (desc[i]) itemIndex = carts.findIndex((obj) => obj.item_id == desc[i].item.item_id);
491
-
492
- if (offer.oo_offer_type == "percentage") {
493
-
494
- console.log('desc[i].item.item_pricedesc[i].item.item_price', desc[i].item.item_price)
495
-
496
- let itemTotalDiscount = 0;
497
- itemAmount =
498
- (parseFloat(desc[i].item.item_price)) *
499
- parseFloat(offer.oo_offer_value) /
500
- 100;
501
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
502
- amt = parseFloat(amt) + parseFloat(itemAmount);
503
- amt = parseFloat(amt);
504
- amtWithModi = amt;
505
-
506
- let modiItemAmount = 0;
507
- cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(amt)
508
- carts[itemIndex].promotion_discount_modifier = parseFloat(
509
- parseFloat(carts[itemIndex].promotion_discount_modifier) +
510
- parseFloat(amt)
511
- ).toFixed(2);
512
-
513
- if (desc[i].item.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
514
- let modiCount = 0;
515
-
516
- desc[i].item.item_modifiers.forEach((modi, mIndex) => {
517
- modiItemAmount =
518
- (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) *
519
- parseFloat(offer.oo_offer_value)) /
520
- 100;
521
-
522
- if (carts[itemIndex].item_modifiers[mIndex]) {
523
- carts[itemIndex].item_modifiers[mIndex].promotion_discount = parseFloat(
524
- modiItemAmount
525
- ).toFixed(2);
526
-
527
- carts[itemIndex].item_modifiers[mIndex].redeem_promotion_id = offer.offer_id;
528
- }
529
-
530
- cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(modiItemAmount)
531
- carts[itemIndex].promotion_discount_modifier = parseFloat(
532
- parseFloat(carts[itemIndex].promotion_discount_modifier) +
533
- parseFloat(modiItemAmount)
534
- ).toFixed(2);
535
-
536
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
537
- amtWithModi = parseFloat(amtWithModi) + parseFloat(modiItemAmount);
538
- amtWithModi = parseFloat(amtWithModi);
539
- amt = parseFloat(amt) + parseFloat(amtWithModi);
540
-
541
- var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
542
- return obj.modifier_item_id == modi.modifier_item_id;
543
- });
544
-
545
- // if (carts[itemIndex].item_modifiers[modiIndex]) {
546
- // carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(
547
- // modiItemAmount
548
- // ).toFixed(2);
549
- // carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
550
- // }
551
-
552
- // modiCount++;
553
- });
554
- } else {
555
- }
556
-
557
- carts[itemIndex].promotion_discount = parseFloat(
558
- parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
559
- ).toFixed(2);
560
-
561
- // carts[itemIndex].promotion_discount_modifier = parseFloat(
562
- // parseFloat(carts[itemIndex].promotion_discount_modifier) +
563
- // parseFloat(itemTotalDiscount)
564
- // ).toFixed(2);
565
- carts[itemIndex].redeem_promotion_id = offer.offer_id;
566
- } else {
567
- buyItemAmount = buyItemAmount + desc[i].amount;
568
- desc[i].item.indexss = i;
569
- buyItemss.push(desc[i].item);
570
- }
571
-
572
- if (parseInt(amt) != 0) {
573
- let amtModiTotal = amt;
574
- // carts[itemIndex].promotion_discount_modifier = amtModiTotal;
575
- // carts[itemIndex].promotion_discount = (
576
- // parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)
577
- // ).toFixed(2);
578
- total = total + amtWithModi;
579
-
580
- // console.log('amount modifffffffffffffff', amtModiTotal)
581
-
582
-
583
-
584
- carts[itemIndex].redeem_promotion_id = offer.offer_id;
585
- }
586
-
587
- getCount++;
588
- desc1[i].remove = true;
589
- desc1[i + 1].remove = true;
590
- }
591
-
592
- if (
593
- getCount == offer.offer_products.get_item_count &&
594
- buyCount == offer.offer_products.buy_item_count
595
- ) {
596
- total = parseFloat(total) + parseFloat(amount);
597
- // cartData.totalDiscount = parseFloat(cartData.totalDiscount) + parseFloat(amount)
598
-
599
- if (offer.offer_products.order_multi_use == 1) {
600
- var filterItem = desc1.filter((e) => {
601
- if (!e.remove) {
602
- return e.item_id;
603
- }
604
- });
605
-
606
- console.log('dddddddddfilterItem', filterItem)
607
-
608
- cartData.item_details = carts
609
- getBuyxGetYOnCondition(filterItem, cartData, offer).then((data) => {
610
- resolve(data)
611
- }).catch(error => {
612
- reject(error);
613
- })
614
- break;
615
- } else {
616
- if (offer.oo_offer_type == 'absolute') {
617
- cartData.item_details = carts
618
- getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount).then((data) => {
619
- resolve(data);
620
- }).catch(error => {
621
- reject(error);
622
- })
623
- }
624
- else {
625
- cartData.discount = cartData.totalDiscount
626
- resolve(cartData);
627
- }
628
-
629
- break;
630
- }
631
- }
632
- else {
633
- cartData.discount = cartData.totalDiscount
634
- resolve(cartData);
635
- }
636
- }
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
+ }
637
553
  } else {
638
- if (offer.oo_offer_type == 'absolute') {
639
- cartData.item_details = carts
640
- getAbsoluteDiscount(cartData, offer, buyItemss, buyItemAmount).then((data) => {
641
- resolve(data);
642
- }).catch(error => {
643
- reject(error);
644
- })
645
- }
646
- else {
647
- cartData.discount = cartData.totalDiscount
648
- resolve(cartData);
649
- }
650
-
651
-
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);
652
560
  }
653
-
654
- // setTimeout(() => {
655
- // if (total && total != 0) {
656
- // cartData.discount = cartData.totalDiscount
657
- // resolve(cartData);
658
- // }
659
- // }, 20);
660
- } catch (e) {
661
- reject(e);
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);
662
624
  }
625
+ }
626
+
627
+ // setTimeout(() => {
628
+ // resolve(0);
629
+ // }, 20);
663
630
  });
664
- };
665
-
666
- function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
631
+ }
632
+
633
+ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total) {
667
634
  return new Promise(function (resolve, reject) {
668
635
  try {
636
+ console.log("ooooooooooooooo",JSON.parse(JSON.stringify(offer)))
669
637
  let offerAmount = offer.oo_offer_value;
670
638
  let singlePriceDiscount = offerAmount / buyItemAmount;
671
639
  singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
672
640
  let remaningOfferAmount = offer.oo_offer_value;
673
641
  let count = 0;
674
- let carts = cartObject.item_details
675
-
676
- buyItemss.forEach((element, buyIndex) => {
677
- let itemAmount = 0;
678
- let itemTotalDiscount = 0;
679
- let itemIndex = carts.findIndex((obj) => {
680
- return obj.item_id == element.item_id && !obj.discount_applied;
681
- });
682
-
683
- if (itemIndex == -1) itemIndex = carts.findIndex((obj) => {
684
- return obj.item_id == element.item_id;
685
- });
686
-
687
- if (!carts[itemIndex].promotion_discount_modifier) carts[itemIndex].promotion_discount_modifier = 0;
688
-
689
- if (element.item_modifiers && element.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
690
- let modiCount = 0;
691
- element.item_modifiers.forEach((modi) => {
692
- let modiItemAmount = 0;
693
- let modOfferAmount = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity * parseFloat(singlePriceDiscount);
694
- modiItemAmount = (modi.modifier_item_price < modOfferAmount) ? modi.modifier_item_price : modOfferAmount;
695
- modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
696
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
697
- total = parseFloat(total) + parseFloat(modiItemAmount);
698
- total = parseFloat(total).toFixed(2);
699
-
700
- var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
701
- return obj.modifier_item_id == modi.modifier_item_id;
702
- });
703
-
704
- carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(modiItemAmount);
705
- carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier).toFixed(2);
642
+ let carts = cartObject.item_details;
643
+ console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",buyItemss,total)
644
+ if(buyItemss && buyItemss.length>0){
645
+ buyItemss.forEach((element, buyIndex) => {
646
+ let itemAmount = 0;
647
+ let itemTotalDiscount = 0;
648
+ let itemIndex = carts.findIndex((obj) => {
649
+ return obj.item_id == element.item_id && !obj.discount_applied;
650
+ });
651
+
652
+ if (itemIndex == -1) itemIndex = carts.findIndex((obj) => {
653
+ return obj.item_id == element.item_id;
654
+ });
706
655
 
707
- if (modiIndex >= 0) {
708
- if (!carts[itemIndex].item_modifiers[modiIndex].promotion_discount) carts[itemIndex].item_modifiers[modiIndex].promotion_discount = 0;
709
- if (!carts[itemIndex].item_modifiers[modiIndex]) {
710
- carts[itemIndex].item_modifiers[modiIndex] = {};
656
+ console.log("carts index",carts,itemIndex)
657
+
658
+ if ( !carts[itemIndex].promotion_discount_modifier) carts[itemIndex].promotion_discount_modifier = 0;
659
+
660
+ if (element.item_modifiers && element.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
661
+ let modiCount = 0;
662
+ element.item_modifiers.forEach((modi) => {
663
+ let modiItemAmount = 0;
664
+ let modOfferAmount = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity * parseFloat(singlePriceDiscount);
665
+ modiItemAmount = (modi.modifier_item_price < modOfferAmount) ? modi.modifier_item_price : modOfferAmount;
666
+ modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
667
+ itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
668
+ total = parseFloat(total) + parseFloat(modiItemAmount);
669
+ total = parseFloat(total).toFixed(2);
670
+
671
+ var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
672
+ return obj.modifier_item_id == modi.modifier_item_id;
673
+ });
674
+
675
+ carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(modiItemAmount);
676
+ carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier).toFixed(2);
677
+
678
+ if (modiIndex >= 0) {
679
+ if (!carts[itemIndex].item_modifiers[modiIndex].promotion_discount) carts[itemIndex].item_modifiers[modiIndex].promotion_discount = 0;
680
+ if (!carts[itemIndex].item_modifiers[modiIndex]) {
681
+ carts[itemIndex].item_modifiers[modiIndex] = {};
682
+ carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
683
+ }
684
+
685
+ carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount) + parseFloat(modiItemAmount);
686
+ carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount).toFixed(2);
711
687
  carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
688
+ modiCount++;
689
+ remaningOfferAmount = remaningOfferAmount - modiItemAmount;
690
+ } else {
691
+ modiCount++;
712
692
  }
693
+
694
+ if (modiCount == element.item_modifiers.length) count++;
695
+ });
696
+ } else {
697
+ count++;
698
+ }
699
+
700
+ carts[itemIndex].discount_applied = true;
701
+
702
+ if (buyItemss[buyItemss.length - 1].item_id == element.item_id) {
703
+ console.log("innnn ifffffffffffff",element.item_price,remaningOfferAmount)
704
+
705
+ itemAmount = (element.item_price < remaningOfferAmount) ? element.item_price : parseFloat(remaningOfferAmount).toFixed(2);
706
+ } else {
707
+ itemAmount = element.item_price * singlePriceDiscount;
708
+ }
709
+
710
+ itemAmount = parseFloat(itemAmount);
711
+ console.log("iiiiiiiiii",itemAmount)
712
+ total = parseFloat(total) + parseFloat(itemAmount);
713
+ total = parseFloat(total).toFixed(2);
714
+ itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
715
+ remaningOfferAmount = remaningOfferAmount - itemAmount;
716
+
717
+ carts[itemIndex].promotion_discount = (parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
718
+ carts[itemIndex].promotion_discount_modifier = (parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
719
+ carts[itemIndex].redeem_promotion_id = offer.offer_id;
720
+
721
+ if (count == buyItemss.length) {
722
+ console.log("ttttttttttttttt",total)
723
+ cartObject.item_details = carts
724
+ cartObject.discount = total
713
725
 
714
- carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount) + parseFloat(modiItemAmount);
715
- carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount).toFixed(2);
716
- carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
717
- modiCount++;
718
- remaningOfferAmount = remaningOfferAmount - modiItemAmount;
719
- } else {
720
- modiCount++;
721
- }
722
-
723
- if (modiCount == element.item_modifiers.length) count++;
724
- });
725
- } else {
726
- count++;
727
- }
728
-
729
- carts[itemIndex].discount_applied = true;
730
-
731
- if (buyItemss[buyItemss.length - 1].item_id == element.item_id) {
732
-
733
- itemAmount = (element.item_price < remaningOfferAmount) ? element.item_price : parseFloat(remaningOfferAmount).toFixed(2);
734
- } else {
735
- itemAmount = element.item_price * singlePriceDiscount;
736
- }
737
-
738
- itemAmount = parseFloat(itemAmount);
739
- total = parseFloat(total) + parseFloat(itemAmount);
740
- total = parseFloat(total).toFixed(2);
741
- itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
742
- remaningOfferAmount = remaningOfferAmount - itemAmount;
743
-
744
- carts[itemIndex].promotion_discount = (parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
745
- carts[itemIndex].promotion_discount_modifier = (parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
746
- carts[itemIndex].redeem_promotion_id = offer.offer_id;
747
-
748
- if (count == buyItemss.length) {
749
- cartObject.item_details = carts
750
- cartObject.discount = total
751
-
752
-
753
- resolve(cartObject);
754
- }
755
- });
726
+ resolve(cartObject);
727
+ }
728
+ });
729
+ }else{
730
+ resolve(cartObject);
731
+ }
756
732
  } catch (error) {
757
733
  reject(error);
758
734
  }
@@ -764,8 +740,15 @@ function addItemQuantity(items, offer) {
764
740
  try {
765
741
  var finalItems = [];
766
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
+ )
767
749
  items.forEach(function (element) {
768
- if (offer.offer_products.offer_buy_products.includes(parseInt(element.item_id)) || offer.offer_products.offer_get_products.includes(parseInt(element.item_id))) {
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)) {
769
752
  for (var i = 1; i <= element.quantity; i++) {
770
753
  if (offer.offer_products.order_modifiercost_use == 1) {
771
754
  var amount = element.item_price;
@@ -773,9 +756,9 @@ function addItemQuantity(items, offer) {
773
756
  if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
774
757
  amount = element.item_price_with_modifier;
775
758
  } else {
776
- offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(function (x) {
777
- return parseInt(x, 10);
778
- });
759
+ offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
760
+ x.toString().length > 10 ? x : parseInt(x, 10)
761
+ );
779
762
  var modifiers = [];
780
763
  element.item_modifiers.forEach(function (modifier) {
781
764
 
@@ -791,8 +774,8 @@ function addItemQuantity(items, offer) {
791
774
  'item_id': element.item_id,
792
775
  'amount': amount,
793
776
  'item': item,
794
- 'buy': offer.offer_products.offer_buy_products.includes(parseInt(element.item_id)),
795
- 'get': offer.offer_products.offer_get_products.includes(parseInt(element.item_id))
777
+ 'buy': offer.offer_products.offer_buy_products.includes(itemId),
778
+ 'get': offer.offer_products.offer_get_products.includes(itemId)
796
779
  });
797
780
  } else {
798
781
  finalItems.push({
@@ -800,13 +783,13 @@ function addItemQuantity(items, offer) {
800
783
  'item_id': element.item_id,
801
784
  // / element.quantity
802
785
  'amount': element.item_price,
803
- 'buy': offer.offer_products.offer_buy_products.includes(parseInt(element.item_id)),
804
- 'get': offer.offer_products.offer_get_products.includes(parseInt(element.item_id))
786
+ 'buy': offer.offer_products.offer_buy_products.includes(itemId),
787
+ 'get': offer.offer_products.offer_get_products.includes(itemId)
805
788
  });
789
+
790
+ // console.log(finalItems)
806
791
  }
807
792
  }
808
- } else {
809
- count++;
810
793
  }
811
794
  count++;
812
795
  if (count == items.length) {
@@ -833,8 +816,6 @@ function getFreeProductOffer(cartObject, offer) {
833
816
  });
834
817
  });
835
818
 
836
- var cartItem = JSON.parse(JSON.stringify(carts));
837
-
838
819
  if (carts) {
839
820
  carts.map((a) => {
840
821
  a.promotion_discount = 0;
@@ -846,24 +827,30 @@ function getFreeProductOffer(cartObject, offer) {
846
827
  var total = 0;
847
828
  var count = 0;
848
829
  var offerAmount = offer.oo_offer_value;
849
- offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(Number);
850
-
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;
851
835
  if (offer.offer_products.order_modifiercost_use == 1) {
852
836
  cartItem.sort(function (a, b) {
853
837
  return a.item_price_with_modifier / a.quantity > b.item_price_with_modifier / b.quantity ? 1 : -1;
854
838
  });
855
839
  } else {
856
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;
857
844
  return a.item_price / a.quantity > b.item_price / b.quantity ? 1 : -1;
858
845
  });
859
846
  }
860
847
 
861
848
  var maxProduct = offer.offer_products.max_product;
862
-
863
849
  cartItem.forEach((element, index) => {
864
850
  var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
865
851
 
866
- if (offer.offer_products.offer_discount_products.includes(parseInt(element.item_id))) {
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)) {
867
854
  var offerAmount = 0;
868
855
  let costWithOutModifier = 0;
869
856
  var singleItemCost = element.item_price;
@@ -882,9 +869,9 @@ function getFreeProductOffer(cartObject, offer) {
882
869
  singleItemCost = singleItemCost + singModPrice;
883
870
  });
884
871
  } else {
885
- offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(function (x) {
886
- return parseInt(x, 10);
887
- });
872
+ offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
873
+ x.toString().length > 10 ? x : parseInt(x, 10)
874
+ );
888
875
 
889
876
  element.item_modifiers.forEach((modifier, modiIndex) => {
890
877
  if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
@@ -948,6 +935,135 @@ function getFreeProductOffer(cartObject, offer) {
948
935
  }
949
936
  };
950
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
+
951
1067
  function setPackageDefaultDiscount(cart) {
952
1068
  return new Promise(async (resolve, reject) => {
953
1069
  try {
@@ -963,7 +1079,9 @@ function setPackageDefaultDiscount(cart) {
963
1079
  let itemTotal = 0;
964
1080
  if (item.package_items && item.package_items.length > 0) {
965
1081
  item.package_items.forEach(function (v) {
966
- itemTotal += (item.quantity * v.price);
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);
967
1085
  })
968
1086
  let packagePrice = item.quantity * item.item_price;
969
1087
  let totalDiscount = (packagePrice < itemTotal) ? (parseFloat(itemTotal) - parseFloat(packagePrice)).toFixed(2) : 0;
@@ -1001,10 +1119,11 @@ function distributeDiscount(packageItems, totalDiscount, itemTotal, quantity) {
1001
1119
  let totalApplied = 0;
1002
1120
  let singleUnit = (quantity * parseFloat(totalDiscount) / parseFloat(itemTotal)).toFixed(2);
1003
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);
1004
1124
  let discountPerItem = (parseFloat(singleUnit) * parseFloat(item.price)).toFixed(2);
1005
1125
  totalApplied = (parseFloat(totalApplied) + parseFloat(discountPerItem)).toFixed(2);
1006
1126
  item.default_combo_discount = parseFloat(discountPerItem).toFixed(2);
1007
-
1008
1127
  if (index == packageItems.length - 1) {
1009
1128
 
1010
1129
  let remaningOfferAmount = (parseFloat(totalDiscount) - parseFloat(totalApplied)).toFixed(2);