foodbot-cart-calculations 1.0.0-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/README.MD +41 -0
- package/calculations.js +148 -0
- package/functions/pointsCalculation.js +307 -0
- package/functions/promotionCalculation.js +1133 -0
- package/functions/taxCalculation.js +690 -0
- package/index.js +869 -0
- package/package.json +11 -0
|
@@ -0,0 +1,1133 @@
|
|
|
1
|
+
function offerCalculation(carts, offer, deliveryCost = 0) {
|
|
2
|
+
return new Promise(function (resolve, reject) {
|
|
3
|
+
try {
|
|
4
|
+
carts.total = 0
|
|
5
|
+
carts.item_details.forEach(element => {
|
|
6
|
+
delete element.promotion_discount;
|
|
7
|
+
let itemTotal = element.item_price * element.quantity
|
|
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
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (offer && offer.offer_discount_type && offer.offer_discount_type == 'buy_x_get_y') {
|
|
36
|
+
getOfferDetails(carts, offer).then(res => {
|
|
37
|
+
carts = res
|
|
38
|
+
// console.log("res",res)
|
|
39
|
+
if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
|
|
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))
|
|
43
|
+
offer.oo_offer_type = 'absolute'
|
|
44
|
+
offer.oo_offer_value = (carts && carts.discount)?Math.round(carts.discount):0
|
|
45
|
+
getOfferDetails(carts, offer).then(res => {
|
|
46
|
+
resolve(res)
|
|
47
|
+
}).catch(error=>{
|
|
48
|
+
reject(error)
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
resolve(carts)
|
|
53
|
+
}
|
|
54
|
+
}).catch(error => {
|
|
55
|
+
reject(error);
|
|
56
|
+
})
|
|
57
|
+
} else if (offer && offer.offer_discount_type && (offer.offer_discount_type == 'discount' || offer.offer_discount_type == 'manual')) {
|
|
58
|
+
|
|
59
|
+
getCategorySpeificOffer(carts, offer).then(res => {
|
|
60
|
+
carts = res
|
|
61
|
+
|
|
62
|
+
if (offer && offer.oo_offer_type == 'percentage' && offer.discount_roundoff && offer.discount_roundoff == 1) {
|
|
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))
|
|
65
|
+
offer.oo_offer_type = 'absolute'
|
|
66
|
+
offer.oo_offer_value = Math.round(carts.discount)
|
|
67
|
+
getCategorySpeificOffer(carts, offer).then(res => {
|
|
68
|
+
resolve(carts)
|
|
69
|
+
}).catch(error => {
|
|
70
|
+
reject(error);
|
|
71
|
+
})
|
|
72
|
+
} else {
|
|
73
|
+
resolve(carts)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
}).catch(error => {
|
|
77
|
+
reject(error);
|
|
78
|
+
})
|
|
79
|
+
} else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'free_product') {
|
|
80
|
+
getFreeProductOffer(carts, offer).then(free => {
|
|
81
|
+
resolve(free)
|
|
82
|
+
}).catch(error => {
|
|
83
|
+
reject(error);
|
|
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
|
+
})
|
|
97
|
+
} else {
|
|
98
|
+
carts.discount = 0
|
|
99
|
+
resolve(carts)
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
reject(error);
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function getCategorySpeificOffer(carts, offer) {
|
|
108
|
+
try {
|
|
109
|
+
if (carts && carts.item_details) {
|
|
110
|
+
carts.item_details.map((a) => { a.promotion_discount = 0; a.redeem_promotion_id = 0 });
|
|
111
|
+
|
|
112
|
+
carts.item_details.forEach(function (v) {
|
|
113
|
+
v.promotion_discount_modifier = 0, v.promotion_discount = 0; v.redeem_promotion_id = 0
|
|
114
|
+
if (v.item_modifiers)
|
|
115
|
+
v.item_modifiers.forEach((mod) => {
|
|
116
|
+
mod.promotion_discount = 0; mod.redeem_promotion_id = 0
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return new Promise(function (resolve, reject) {
|
|
121
|
+
var total = 0;
|
|
122
|
+
var count = 0
|
|
123
|
+
carts.discount = (carts.discount) ? carts.discount : 0
|
|
124
|
+
let afterTotal = parseFloat(carts.total) + parseFloat(carts.discount)
|
|
125
|
+
let indexes = []
|
|
126
|
+
|
|
127
|
+
if (parseFloat(offer.oo_min_amount) <= parseFloat(afterTotal)) {
|
|
128
|
+
|
|
129
|
+
if (offer.offer_discount_type != 'manual')
|
|
130
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
131
|
+
val.toString().length > 10 ? val : Number(val)
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
if (offer.offer_discount_type != 'manual')
|
|
135
|
+
if (offer.offer_products.package_items) {
|
|
136
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
137
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
offer.offer_products.package_items = []
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let foundItemsSum = 0
|
|
145
|
+
carts.item_details.filter((res) => {
|
|
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
|
+
}
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
|
|
156
|
+
let remaningOfferAmount = offerAmount
|
|
157
|
+
carts.item_details.forEach((element, index) => {
|
|
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))) {
|
|
160
|
+
var itemAmount = 0;
|
|
161
|
+
|
|
162
|
+
// Apply discount on Packages
|
|
163
|
+
if (element.isPackage == 1) {
|
|
164
|
+
if (offer.oo_offer_type == 'percentage') {
|
|
165
|
+
let itemTotalDiscount = 0
|
|
166
|
+
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
167
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
168
|
+
let itemDiscount = parseFloat(itemAmount).toFixed(2);
|
|
169
|
+
|
|
170
|
+
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
171
|
+
total = parseFloat(total)
|
|
172
|
+
|
|
173
|
+
carts.item_details[index].package_items.forEach((packageItem, packageItemIndex) => {
|
|
174
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount = 0
|
|
175
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = 0
|
|
176
|
+
|
|
177
|
+
//apply discount on element modifier
|
|
178
|
+
if (packageItem.modifiers.length > 0) {
|
|
179
|
+
let modiCount = 0
|
|
180
|
+
packageItem.modifiers.forEach((modi, modiIndex) => {
|
|
181
|
+
let modiItemAmount = 0
|
|
182
|
+
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
183
|
+
|
|
184
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
185
|
+
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
186
|
+
total = parseFloat(total) + parseFloat(discount)
|
|
187
|
+
total = parseFloat(total).toFixed(2);
|
|
188
|
+
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
189
|
+
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
190
|
+
modiCount++;
|
|
191
|
+
// if (modiCount == element.item_modifiers.length)
|
|
192
|
+
// count++
|
|
193
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier) + parseFloat(modiItemAmount)).toFixed(2);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
// count++
|
|
198
|
+
}
|
|
199
|
+
// console.log("package carts", carts.item_details[index]);
|
|
200
|
+
// discount/totalamount
|
|
201
|
+
|
|
202
|
+
let singlePriceDiscount = ((parseFloat(offer.oo_offer_value) * foundItemsSum) / 100) / foundItemsSum
|
|
203
|
+
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
204
|
+
// console.log("singlePriceDiscount", singlePriceDiscount);
|
|
205
|
+
// let packageDisc = ((packageItem.price - packageItem.default_combo_discount) * singlePriceDiscount).toFixed(2);
|
|
206
|
+
let packageDisc = (((packageItem.price * parseInt(packageItem.quantity)) - packageItem.default_combo_discount) * singlePriceDiscount).toFixed(2);
|
|
207
|
+
|
|
208
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount) + parseFloat(packageDisc)).toFixed(2);
|
|
209
|
+
carts.item_details[index].package_items[packageItemIndex].redeem_promotion_id = offer.offer_id;
|
|
210
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier) + parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount)).toFixed(2);
|
|
211
|
+
|
|
212
|
+
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount)).toFixed(2);
|
|
213
|
+
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier)).toFixed(2);
|
|
214
|
+
carts.item_details[index].redeem_promotion_id = offer.offer_id;
|
|
215
|
+
})
|
|
216
|
+
count++
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
let singlePriceDiscount = (offerAmount / foundItemsSum)
|
|
220
|
+
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6)
|
|
221
|
+
|
|
222
|
+
let itemTotalDiscount = 0
|
|
223
|
+
|
|
224
|
+
carts.item_details[index].package_items.forEach((packageItem, packageItemIndex) => {
|
|
225
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount = 0
|
|
226
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = 0
|
|
227
|
+
|
|
228
|
+
if (packageItem.modifiers.length > 0) {
|
|
229
|
+
let modiCount = 0
|
|
230
|
+
packageItem.modifiers.forEach((modi, modiIndex) => {
|
|
231
|
+
let modiItemAmount = 0
|
|
232
|
+
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
233
|
+
|
|
234
|
+
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
235
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
236
|
+
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
237
|
+
total = parseFloat(total).toFixed(2);
|
|
238
|
+
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
239
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier) + parseFloat(modiItemAmount)).toFixed(2);
|
|
240
|
+
carts.item_details[index].package_items[packageItemIndex].modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
241
|
+
modiCount++;
|
|
242
|
+
|
|
243
|
+
// remaningOfferAmount = remaningOfferAmount - modiItemAmount
|
|
244
|
+
|
|
245
|
+
// if (modiCount == packageItem.item_modifiers.length)
|
|
246
|
+
// count++
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
// count++
|
|
251
|
+
}
|
|
252
|
+
// if (totalItemFound[totalItemFound.length - 1].item_id == element.item_id) {
|
|
253
|
+
// itemAmount = parseFloat(remaningOfferAmount).toFixed(2)
|
|
254
|
+
// }
|
|
255
|
+
// else {
|
|
256
|
+
|
|
257
|
+
//divide single price discount with package items length
|
|
258
|
+
itemAmount = (((packageItem.price * parseInt(element.quantity)) - packageItem.default_combo_discount) * (singlePriceDiscount)).toFixed(2)
|
|
259
|
+
// }
|
|
260
|
+
itemAmount = parseFloat(itemAmount).toFixed(2)
|
|
261
|
+
total = parseFloat(total) + parseFloat(itemAmount)
|
|
262
|
+
|
|
263
|
+
if (itemAmount > 0)
|
|
264
|
+
indexes.push(index)
|
|
265
|
+
|
|
266
|
+
total = parseFloat(total).toFixed(2)
|
|
267
|
+
|
|
268
|
+
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount)
|
|
269
|
+
remaningOfferAmount = remaningOfferAmount - itemAmount
|
|
270
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
|
|
271
|
+
carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier = (parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
|
|
272
|
+
carts.item_details[index].package_items[packageItemIndex].redeem_promotion_id = offer.offer_id;
|
|
273
|
+
|
|
274
|
+
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount)).toFixed(2);
|
|
275
|
+
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(carts.item_details[index].package_items[packageItemIndex].promotion_discount_modifier)).toFixed(2);
|
|
276
|
+
carts.item_details[index].redeem_promotion_id = offer.offer_id;
|
|
277
|
+
})
|
|
278
|
+
count++;
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
if (offer.oo_offer_type == 'percentage') {
|
|
282
|
+
let itemTotalDiscount = 0
|
|
283
|
+
itemAmount = ((parseFloat(element.item_price) * parseFloat(element.quantity)) * parseFloat(offer.oo_offer_value)) / 100;
|
|
284
|
+
itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount).toFixed(2)
|
|
285
|
+
let itemDiscount = parseFloat(itemAmount).toFixed(2)
|
|
286
|
+
|
|
287
|
+
total = parseFloat(total) + parseFloat(itemDiscount)
|
|
288
|
+
total = parseFloat(total)
|
|
289
|
+
carts.item_details[index].promotion_discount_modifier = 0
|
|
290
|
+
|
|
291
|
+
//apply discount on element modifier
|
|
292
|
+
if (element.item_modifiers.length > 0) {
|
|
293
|
+
let modiCount = 0
|
|
294
|
+
element.item_modifiers.forEach((modi, modiIndex) => {
|
|
295
|
+
let modiItemAmount = 0
|
|
296
|
+
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseFloat(modi.quantity))) * element.quantity * parseFloat(offer.oo_offer_value)) / 100;
|
|
297
|
+
|
|
298
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
299
|
+
let discount = parseFloat(modiItemAmount).toFixed(2)
|
|
300
|
+
total = parseFloat(total) + parseFloat(discount)
|
|
301
|
+
total = parseFloat(total).toFixed(2);
|
|
302
|
+
carts.item_details[index].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
303
|
+
carts.item_details[index].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
304
|
+
modiCount++;
|
|
305
|
+
if (modiCount == element.item_modifiers.length)
|
|
306
|
+
count++
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
count++
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// console.log("carts", carts.item_details[index]);
|
|
314
|
+
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
|
|
315
|
+
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(itemTotalDiscount)).toFixed(2);
|
|
316
|
+
carts.item_details[index].redeem_promotion_id = offer.offer_id;
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
// console.log("carts", carts.item_details[index]);
|
|
320
|
+
|
|
321
|
+
let singlePriceDiscount = offerAmount / foundItemsSum
|
|
322
|
+
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6)
|
|
323
|
+
let itemTotalDiscount = 0
|
|
324
|
+
carts.item_details[index].promotion_discount_modifier = 0
|
|
325
|
+
if (element.item_modifiers.length > 0) {
|
|
326
|
+
let modiCount = 0
|
|
327
|
+
element.item_modifiers.forEach((modi, modiIndex) => {
|
|
328
|
+
let modiItemAmount = 0
|
|
329
|
+
modiItemAmount = ((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity) * parseFloat(singlePriceDiscount);
|
|
330
|
+
|
|
331
|
+
modiItemAmount = parseFloat(modiItemAmount).toFixed(2)
|
|
332
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount)).toFixed(2)
|
|
333
|
+
total = parseFloat(total) + parseFloat(modiItemAmount)
|
|
334
|
+
total = parseFloat(total).toFixed(2);
|
|
335
|
+
carts.item_details[index].item_modifiers[modiIndex].promotion_discount = parseFloat(modiItemAmount).toFixed(2)
|
|
336
|
+
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(modiItemAmount)).toFixed(2);
|
|
337
|
+
carts.item_details[index].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
338
|
+
modiCount++;
|
|
339
|
+
|
|
340
|
+
// remaningOfferAmount = remaningOfferAmount - modiItemAmount
|
|
341
|
+
if (modiCount == element.item_modifiers.length)
|
|
342
|
+
count++
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
count++
|
|
347
|
+
}
|
|
348
|
+
// if (totalItemFound[totalItemFound.length - 1].item_id == element.item_id) {
|
|
349
|
+
// itemAmount = parseFloat(remaningOfferAmount).toFixed(2)
|
|
350
|
+
// }
|
|
351
|
+
// else {
|
|
352
|
+
itemAmount = (element.item_price * element.quantity) * singlePriceDiscount
|
|
353
|
+
// }
|
|
354
|
+
itemAmount = parseFloat(itemAmount).toFixed(2)
|
|
355
|
+
total = parseFloat(total) + parseFloat(itemAmount)
|
|
356
|
+
|
|
357
|
+
if (itemAmount > 0)
|
|
358
|
+
indexes.push(index)
|
|
359
|
+
|
|
360
|
+
total = parseFloat(total).toFixed(2)
|
|
361
|
+
|
|
362
|
+
itemTotalDiscount = (parseFloat(itemTotalDiscount) + parseFloat(itemAmount)).toFixed(2)
|
|
363
|
+
remaningOfferAmount = remaningOfferAmount - itemAmount
|
|
364
|
+
carts.item_details[index].promotion_discount = (parseFloat(carts.item_details[index].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
|
|
365
|
+
carts.item_details[index].promotion_discount_modifier = (parseFloat(carts.item_details[index].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
|
|
366
|
+
carts.item_details[index].redeem_promotion_id = offer.offer_id;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
count++
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// console.log("count", count)
|
|
374
|
+
if (count == carts.item_details.length && carts.item_details) {
|
|
375
|
+
if ((Number(total) < Number(offerAmount) || Number(total) > Number(offerAmount)) && offer.oo_offer_type != 'percentage') {
|
|
376
|
+
let difference = Number(offerAmount) - Number(total)
|
|
377
|
+
difference = parseFloat(difference).toFixed(2)
|
|
378
|
+
difference = Math.abs(difference)
|
|
379
|
+
if (indexes.length > 0) {
|
|
380
|
+
|
|
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
|
|
382
|
+
carts.item_details[indexes[0]].promotion_discount = parseFloat(carts.item_details[indexes[0]].promotion_discount).toFixed(2)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
carts.item_details[indexes[0]].promotion_discount_modifier = (Number(total) < Number(offerAmount)) ? parseFloat(carts.item_details[indexes[0]].promotion_discount_modifier) + parseFloat(difference) : parseFloat(carts.item_details[indexes[0]].promotion_discount_modifier) - difference
|
|
386
|
+
carts.item_details[indexes[0]].promotion_discount_modifier = parseFloat(carts.item_details[indexes[0]].promotion_discount_modifier).toFixed(2)
|
|
387
|
+
|
|
388
|
+
total = (Number(total) < Number(offerAmount)) ? parseFloat(total) + parseFloat(difference) : parseFloat(total) - difference
|
|
389
|
+
total = parseFloat(total).toFixed(2)
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
console.log("total", total)
|
|
393
|
+
carts.discount = total
|
|
394
|
+
resolve(carts);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
carts.discount = 0
|
|
401
|
+
resolve(carts);
|
|
402
|
+
}
|
|
403
|
+
})
|
|
404
|
+
} else {
|
|
405
|
+
return new Promise(function (resolve, reject) {
|
|
406
|
+
carts.discount = 0
|
|
407
|
+
resolve(carts);
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
} catch (error) {
|
|
411
|
+
reject(error)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function getOfferDetails(carts, offer = []) {
|
|
416
|
+
return new Promise(function (resolve, reject) {
|
|
417
|
+
try {
|
|
418
|
+
// buyItemAmount = 0
|
|
419
|
+
// buyItemss = []
|
|
420
|
+
// total = 0
|
|
421
|
+
|
|
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);
|
|
424
|
+
|
|
425
|
+
// const buyItems = [];
|
|
426
|
+
// let buyItemAmount = 0;
|
|
427
|
+
|
|
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
|
+
// });
|
|
433
|
+
|
|
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
|
+
}
|
|
444
|
+
});
|
|
445
|
+
|
|
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);
|
|
457
|
+
addItemQuantity(carts.item_details, offer).then((data) => {
|
|
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
|
|
471
|
+
resolve(carts)
|
|
472
|
+
}).catch(error => {
|
|
473
|
+
reject(error)
|
|
474
|
+
});
|
|
475
|
+
}).catch(error => {
|
|
476
|
+
reject(error);
|
|
477
|
+
})
|
|
478
|
+
} else {
|
|
479
|
+
resolve(carts);
|
|
480
|
+
}
|
|
481
|
+
} catch (error) {
|
|
482
|
+
reject(error)
|
|
483
|
+
}
|
|
484
|
+
})
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
|
|
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
|
+
}
|
|
553
|
+
} else {
|
|
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);
|
|
560
|
+
}
|
|
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);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// setTimeout(() => {
|
|
628
|
+
// resolve(0);
|
|
629
|
+
// }, 20);
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount,total) {
|
|
634
|
+
return new Promise(function (resolve, reject) {
|
|
635
|
+
try {
|
|
636
|
+
console.log("ooooooooooooooo",JSON.parse(JSON.stringify(offer)))
|
|
637
|
+
let offerAmount = offer.oo_offer_value;
|
|
638
|
+
let singlePriceDiscount = offerAmount / buyItemAmount;
|
|
639
|
+
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
640
|
+
let remaningOfferAmount = offer.oo_offer_value;
|
|
641
|
+
let count = 0;
|
|
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
|
+
});
|
|
655
|
+
|
|
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);
|
|
687
|
+
carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
688
|
+
modiCount++;
|
|
689
|
+
remaningOfferAmount = remaningOfferAmount - modiItemAmount;
|
|
690
|
+
} else {
|
|
691
|
+
modiCount++;
|
|
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
|
|
725
|
+
|
|
726
|
+
resolve(cartObject);
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
}else{
|
|
730
|
+
resolve(cartObject);
|
|
731
|
+
}
|
|
732
|
+
} catch (error) {
|
|
733
|
+
reject(error);
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
function addItemQuantity(items, offer) {
|
|
739
|
+
return new Promise(function (resolve, reject) {
|
|
740
|
+
try {
|
|
741
|
+
var finalItems = [];
|
|
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
|
+
)
|
|
749
|
+
items.forEach(function (element) {
|
|
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)) {
|
|
752
|
+
for (var i = 1; i <= element.quantity; i++) {
|
|
753
|
+
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
754
|
+
var amount = element.item_price;
|
|
755
|
+
var item = JSON.parse(JSON.stringify(element));
|
|
756
|
+
if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
|
|
757
|
+
amount = element.item_price_with_modifier;
|
|
758
|
+
} else {
|
|
759
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
760
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
761
|
+
);
|
|
762
|
+
var modifiers = [];
|
|
763
|
+
element.item_modifiers.forEach(function (modifier) {
|
|
764
|
+
|
|
765
|
+
if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
|
|
766
|
+
amount = amount + modifier.modifier_item_price;
|
|
767
|
+
modifiers.push(modifier);
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
item.item_modifiers = modifiers;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
finalItems.push({
|
|
774
|
+
'item_id': element.item_id,
|
|
775
|
+
'amount': amount,
|
|
776
|
+
'item': item,
|
|
777
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
778
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
779
|
+
});
|
|
780
|
+
} else {
|
|
781
|
+
finalItems.push({
|
|
782
|
+
'item': element,
|
|
783
|
+
'item_id': element.item_id,
|
|
784
|
+
// / element.quantity
|
|
785
|
+
'amount': element.item_price,
|
|
786
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
787
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
// console.log(finalItems)
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
count++;
|
|
795
|
+
if (count == items.length) {
|
|
796
|
+
resolve(finalItems);
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
} catch (error) {
|
|
800
|
+
reject(error)
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
});
|
|
804
|
+
};
|
|
805
|
+
|
|
806
|
+
function getFreeProductOffer(cartObject, offer) {
|
|
807
|
+
try {
|
|
808
|
+
let carts = cartObject.item_details;
|
|
809
|
+
carts.forEach(function (v) {
|
|
810
|
+
v.promotion_discount_modifier = 0;
|
|
811
|
+
v.promotion_discount = 0;
|
|
812
|
+
v.redeem_promotion_id = 0;
|
|
813
|
+
if (v.item_modifiers) v.item_modifiers.forEach((mod) => {
|
|
814
|
+
mod.promotion_discount = 0;
|
|
815
|
+
mod.redeem_promotion_id = 0;
|
|
816
|
+
});
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
if (carts) {
|
|
820
|
+
carts.map((a) => {
|
|
821
|
+
a.promotion_discount = 0;
|
|
822
|
+
a.redeem_promotion_id = 0;
|
|
823
|
+
a.remove = false;
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
return new Promise(function (resolve, reject) {
|
|
827
|
+
var total = 0;
|
|
828
|
+
var count = 0;
|
|
829
|
+
var offerAmount = offer.oo_offer_value;
|
|
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;
|
|
835
|
+
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
836
|
+
cartItem.sort(function (a, b) {
|
|
837
|
+
return a.item_price_with_modifier / a.quantity > b.item_price_with_modifier / b.quantity ? 1 : -1;
|
|
838
|
+
});
|
|
839
|
+
} else {
|
|
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;
|
|
844
|
+
return a.item_price / a.quantity > b.item_price / b.quantity ? 1 : -1;
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
var maxProduct = offer.offer_products.max_product;
|
|
849
|
+
cartItem.forEach((element, index) => {
|
|
850
|
+
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
851
|
+
|
|
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)) {
|
|
854
|
+
var offerAmount = 0;
|
|
855
|
+
let costWithOutModifier = 0;
|
|
856
|
+
var singleItemCost = element.item_price;
|
|
857
|
+
|
|
858
|
+
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
859
|
+
if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
|
|
860
|
+
element.item_modifiers.forEach((elementMod, modiIndex) => {
|
|
861
|
+
if (!carts[ItemIndex].item_modifiers[modiIndex].promotion_discount)
|
|
862
|
+
carts[ItemIndex].item_modifiers[modiIndex].promotion_discount = 0;
|
|
863
|
+
|
|
864
|
+
let singModPrice = elementMod.modifier_item_price * elementMod.quantity;
|
|
865
|
+
let promotionDiscount = singModPrice * (element.quantity <= maxProduct ? element.quantity : maxProduct);
|
|
866
|
+
|
|
867
|
+
carts[ItemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(promotionDiscount).toFixed(2);
|
|
868
|
+
carts[ItemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
869
|
+
singleItemCost = singleItemCost + singModPrice;
|
|
870
|
+
});
|
|
871
|
+
} else {
|
|
872
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
873
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
874
|
+
);
|
|
875
|
+
|
|
876
|
+
element.item_modifiers.forEach((modifier, modiIndex) => {
|
|
877
|
+
if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
|
|
878
|
+
let singModPrice1 = modifier.modifier_item_price * modifier.quantity;
|
|
879
|
+
|
|
880
|
+
if (!carts[ItemIndex].item_modifiers[modiIndex].promotion_discount)
|
|
881
|
+
carts[ItemIndex].item_modifiers[modiIndex].promotion_discount = 0;
|
|
882
|
+
|
|
883
|
+
carts[ItemIndex].item_modifiers[modiIndex].promotion_discount = singModPrice1;
|
|
884
|
+
carts[ItemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[ItemIndex].item_modifiers[modiIndex].promotion_discount).toFixed(2);
|
|
885
|
+
carts[ItemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
|
|
886
|
+
singleItemCost = singleItemCost + singModPrice1;
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
if (element.quantity <= maxProduct) {
|
|
893
|
+
maxProduct = maxProduct - element.quantity;
|
|
894
|
+
offerAmount = singleItemCost * element.quantity;
|
|
895
|
+
costWithOutModifier = costWithOutModifier + (element.item_price * element.quantity);
|
|
896
|
+
} else {
|
|
897
|
+
offerAmount = singleItemCost * maxProduct;
|
|
898
|
+
costWithOutModifier = element.item_price * maxProduct;
|
|
899
|
+
maxProduct = 0;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
total = total + parseFloat(offerAmount);
|
|
903
|
+
|
|
904
|
+
carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
|
|
905
|
+
carts[ItemIndex].promotion_discount = parseFloat(costWithOutModifier).toFixed(2);
|
|
906
|
+
carts[ItemIndex].remove = true;
|
|
907
|
+
|
|
908
|
+
if (offerAmount > 0) {
|
|
909
|
+
carts[ItemIndex].redeem_promotion_id = offer.offer_id;
|
|
910
|
+
} else {
|
|
911
|
+
carts[ItemIndex].item_modifiers.map((res) => {
|
|
912
|
+
res.promotion_discount = 0;
|
|
913
|
+
res.redeem_promotion_id = 0;
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
count++;
|
|
917
|
+
} else {
|
|
918
|
+
count++;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
if (count == carts.length && carts) {
|
|
922
|
+
cartObject.item_details = carts
|
|
923
|
+
cartObject.discount = total
|
|
924
|
+
resolve(cartObject);
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
});
|
|
928
|
+
} else {
|
|
929
|
+
return new Promise(function (resolve, reject) {
|
|
930
|
+
resolve(cartObject);
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
} catch (error) {
|
|
934
|
+
reject(error)
|
|
935
|
+
}
|
|
936
|
+
};
|
|
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
|
+
|
|
963
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
964
|
+
val.toString().length > 10 ? val : Number(val)
|
|
965
|
+
);
|
|
966
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
967
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
968
|
+
);
|
|
969
|
+
|
|
970
|
+
var cartItem = carts;
|
|
971
|
+
|
|
972
|
+
cartItem.forEach((element, index) => {
|
|
973
|
+
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
974
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
975
|
+
if (offer.offer_products.offer_discount_products.includes(itemId) || offer.offer_products.package_items.includes(itemId)) {
|
|
976
|
+
var offerAmount = 0;
|
|
977
|
+
var singleItemCost = element.item_price;
|
|
978
|
+
|
|
979
|
+
if(singleItemCost >= fixedPrice){
|
|
980
|
+
offerAmount = (singleItemCost * element.quantity) - (fixedPrice * element.quantity);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
total = total + parseFloat(offerAmount);
|
|
984
|
+
|
|
985
|
+
carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
|
|
986
|
+
carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
|
|
987
|
+
carts[ItemIndex].remove = true;
|
|
988
|
+
|
|
989
|
+
if (offerAmount > 0) {
|
|
990
|
+
carts[ItemIndex].redeem_promotion_id = offer.offer_id;
|
|
991
|
+
} else {
|
|
992
|
+
carts[ItemIndex].item_modifiers.map((res) => {
|
|
993
|
+
res.promotion_discount = 0;
|
|
994
|
+
res.redeem_promotion_id = 0;
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
count++;
|
|
998
|
+
} else {
|
|
999
|
+
count++;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
if (count == carts.length && carts) {
|
|
1003
|
+
cartObject.item_details = carts
|
|
1004
|
+
cartObject.discount = total
|
|
1005
|
+
resolve(cartObject);
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
});
|
|
1009
|
+
} else {
|
|
1010
|
+
return new Promise(function (resolve, reject) {
|
|
1011
|
+
cartObject.discount = 0
|
|
1012
|
+
resolve(cartObject);
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
} catch (error) {
|
|
1016
|
+
reject(error)
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
|
|
1021
|
+
return new Promise((resolve, reject) => {
|
|
1022
|
+
try {
|
|
1023
|
+
let discountRoundOff = offer.discount_roundoff; // 1 = Round off, 0 = No rounding
|
|
1024
|
+
let deliveryDiscount = 0; // Default
|
|
1025
|
+
|
|
1026
|
+
if (cartObject.total >= offer.oo_min_amount && offer.oo_offer_type == 'percentage' && offer.oo_offer_value > 0) {
|
|
1027
|
+
// Calculate discount as a percentage of delivery cost
|
|
1028
|
+
deliveryDiscount = (deliveryCost * offer.oo_offer_value) / 100;
|
|
1029
|
+
|
|
1030
|
+
// Apply rounding if discountRoundOff is 1
|
|
1031
|
+
if (discountRoundOff == 1) {
|
|
1032
|
+
deliveryDiscount = Math.round(deliveryDiscount);
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
cartObject.discount = 0;
|
|
1036
|
+
cartObject.delivery_discount = deliveryDiscount;
|
|
1037
|
+
cartObject.is_delivery_discount = true;
|
|
1038
|
+
} else {
|
|
1039
|
+
cartObject.discount = 0;
|
|
1040
|
+
cartObject.delivery_discount = 0;
|
|
1041
|
+
cartObject.is_delivery_discount = false;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
resolve(cartObject); // Return updated cartObject
|
|
1045
|
+
} catch (error) {
|
|
1046
|
+
reject(error);
|
|
1047
|
+
}
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
function setPackageDefaultDiscount(cart) {
|
|
1052
|
+
return new Promise(async (resolve, reject) => {
|
|
1053
|
+
try {
|
|
1054
|
+
if (cart && cart.item_details) {
|
|
1055
|
+
cart.item_details.forEach(function (v) {
|
|
1056
|
+
if (v.isPackage == 1 && v.package_items)
|
|
1057
|
+
v.package_items.forEach(function (item) {
|
|
1058
|
+
item.default_combo_discount = 0;
|
|
1059
|
+
})
|
|
1060
|
+
});
|
|
1061
|
+
cart.item_details.forEach(async function (item, index) {
|
|
1062
|
+
if (item.isPackage == 1) {
|
|
1063
|
+
let itemTotal = 0;
|
|
1064
|
+
if (item.package_items && item.package_items.length > 0) {
|
|
1065
|
+
item.package_items.forEach(function (v) {
|
|
1066
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1067
|
+
// itemTotal += ((item.quantity * v.quantity) * v.price);
|
|
1068
|
+
itemTotal += (item.quantity * v.price);
|
|
1069
|
+
})
|
|
1070
|
+
let packagePrice = item.quantity * item.item_price;
|
|
1071
|
+
let totalDiscount = (packagePrice < itemTotal) ? (parseFloat(itemTotal) - parseFloat(packagePrice)).toFixed(2) : 0;
|
|
1072
|
+
await distributeDiscount(item.package_items, totalDiscount, itemTotal, item.quantity).then(ele => {
|
|
1073
|
+
item.package_items = ele;
|
|
1074
|
+
if (index == cart.item_details.length - 1) {
|
|
1075
|
+
resolve(cart);
|
|
1076
|
+
}
|
|
1077
|
+
}).catch(error => {
|
|
1078
|
+
reject(error);
|
|
1079
|
+
})
|
|
1080
|
+
} else {
|
|
1081
|
+
if (index == cart.item_details.length - 1) {
|
|
1082
|
+
resolve(cart);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
} else {
|
|
1086
|
+
if (index == cart.item_details.length - 1) {
|
|
1087
|
+
resolve(cart);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
})
|
|
1091
|
+
} else {
|
|
1092
|
+
resolve(cart);
|
|
1093
|
+
}
|
|
1094
|
+
} catch (error) {
|
|
1095
|
+
reject(error);
|
|
1096
|
+
}
|
|
1097
|
+
})
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
function distributeDiscount(packageItems, totalDiscount, itemTotal, quantity) {
|
|
1101
|
+
return new Promise(async (resolve, reject) => {
|
|
1102
|
+
try {
|
|
1103
|
+
let totalApplied = 0;
|
|
1104
|
+
let singleUnit = (quantity * parseFloat(totalDiscount) / parseFloat(itemTotal)).toFixed(2);
|
|
1105
|
+
for (const [index, item] of packageItems.entries()) {
|
|
1106
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1107
|
+
// let discountPerItem = (parseFloat(singleUnit) * (parseInt(item.quantity) * parseFloat(item.price))).toFixed(2);
|
|
1108
|
+
let discountPerItem = (parseFloat(singleUnit) * parseFloat(item.price)).toFixed(2);
|
|
1109
|
+
totalApplied = (parseFloat(totalApplied) + parseFloat(discountPerItem)).toFixed(2);
|
|
1110
|
+
item.default_combo_discount = parseFloat(discountPerItem).toFixed(2);
|
|
1111
|
+
if (index == packageItems.length - 1) {
|
|
1112
|
+
|
|
1113
|
+
let remaningOfferAmount = (parseFloat(totalDiscount) - parseFloat(totalApplied)).toFixed(2);
|
|
1114
|
+
for (const updateItem of packageItems) {
|
|
1115
|
+
let updateDisc = (parseFloat(updateItem.default_combo_discount) + parseFloat(remaningOfferAmount)).toFixed(2);
|
|
1116
|
+
if (updateDisc <= updateItem.price) {
|
|
1117
|
+
updateItem.default_combo_discount = updateDisc
|
|
1118
|
+
break;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
resolve(packageItems);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
reject(error);
|
|
1126
|
+
}
|
|
1127
|
+
})
|
|
1128
|
+
};
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
module.exports = {
|
|
1132
|
+
offerCalculation, setPackageDefaultDiscount
|
|
1133
|
+
}
|