foodbot-cart-calculations 1.0.0

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.
@@ -0,0 +1,542 @@
1
+ function calculateTax2(cart, texSettings, taxType) {
2
+ return new Promise(function (resolve, reject) {
3
+ try {
4
+ cart.cartTotal = 0;
5
+ cart.tax_amount = 0;
6
+ cart.item_details.forEach(function (v) {
7
+ v.tax_amount = 0;
8
+ });
9
+ cart.item_details.forEach(function (v) {
10
+ if (!v.event && v.item_modifiers) {
11
+ v.item_modifiers.forEach(function (w) {
12
+ w.tax_amount = 0;
13
+ w.tax_array = [];
14
+ });
15
+ v.tax_array = [];
16
+ }
17
+ });
18
+ cart.item_details.forEach(function (v) {
19
+ if (v.isPackage == 1) {
20
+ v.package_items.forEach(function (w) {
21
+ w.modifiers.forEach(function (x) {
22
+ x.tax_amount = 0;
23
+ x.tax_array = [];
24
+ });
25
+ v.tax_array = [];
26
+ });
27
+ v.tax_array = [];
28
+ }
29
+ });
30
+ cart.merged_tax_array = [];
31
+
32
+ cart.item_details.forEach(async (element, itemIndex) => {
33
+ let total = 0;
34
+ if (!cart.item_details[itemIndex].tax_amount_modifier) {
35
+ cart.item_details[itemIndex].tax_amount_modifier = 0;
36
+ }
37
+ cart.item_details[itemIndex].total_tax_amount = 0;
38
+
39
+ let promoDiscount = (element.promotion_discount) ? element.promotion_discount : 0;
40
+ let pointsDiscount = (element.points_discount) ? element.points_discount : 0;
41
+
42
+ if (promoDiscount > 0) {
43
+ cart.item_details[itemIndex].item_event = 'update';
44
+ }
45
+ promoDiscount = (promoDiscount > element.item_price * element.quantity) ? element.item_price * element.quantity : promoDiscount;
46
+ element.promotion_discount = JSON.parse(JSON.stringify(promoDiscount));
47
+ promoDiscount = parseFloat(promoDiscount).toFixed(2);
48
+
49
+ let afterDiscount = element.item_price * element.quantity - promoDiscount;
50
+ afterDiscount = parseFloat(afterDiscount).toFixed(2);
51
+ afterDiscount = afterDiscount - pointsDiscount;
52
+ afterDiscount = parseFloat(afterDiscount).toFixed(2);
53
+ afterDiscount = (afterDiscount < 0) ? 0 : afterDiscount;
54
+ cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDiscount);
55
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(2);
56
+ total = +element.item_price;
57
+
58
+ if (element.isPackage == 1) {
59
+ let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount);
60
+ cart = result.cart;
61
+ element = result.element;
62
+ } else {
63
+ let result = await calculateItemTax(cart, itemIndex, element, texSettings, afterDiscount);
64
+ cart = result.cart;
65
+ element = result.element;
66
+ }
67
+
68
+ if (element.isPackage == 1) {
69
+ let result = await calculatePackageModiTax(cart, itemIndex, element, texSettings, afterDiscount);
70
+ cart = result.cart;
71
+ element = result.element;
72
+ } else {
73
+ let result = await calculateModiTax(cart, itemIndex, element, texSettings, afterDiscount);
74
+ cart = result.cart;
75
+ element = result.element;
76
+ }
77
+ if (itemIndex == cart.item_details.length - 1) {
78
+ cart.cart_total_with_tax = parseFloat(cart.cartTotal) + parseFloat(cart.tax_amount);
79
+ cart.cart_total_with_tax = parseFloat(cart.cart_total_with_tax).toFixed(2);
80
+ resolve(cart);
81
+ }
82
+
83
+ });
84
+ } catch (error) {
85
+ reject(error);
86
+ }
87
+ });
88
+ }
89
+
90
+ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount) {
91
+ return new Promise(async (resolve, reject) => {
92
+ getTaxSettings2(element.tax_id, tax_settings, element.ieps_tax).then(async (itemTaxs) => {
93
+ let lastPrice = afterDicsount
94
+ getBeforeTaxPriceOfItem(itemTaxs, lastPrice).then((resp) => {
95
+ lastPrice = +resp.beforeTax;
96
+ let itemTotalTax = 0;
97
+
98
+ itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
99
+ let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
100
+ if (!element.tax_array) {
101
+ element.tax_array = []
102
+ }
103
+ let taxAmount = 0
104
+ if (startItemTax.ieps_type && startItemTax.ieps_type == 2) {
105
+ taxAmount = lastPrice >= startItemTax.tax_rate ? startItemTax.tax_rate : 0;
106
+ } else {
107
+ taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
108
+ }
109
+ taxAmount = parseFloat(taxAmount).toFixed(2)
110
+ itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(2)
111
+
112
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
113
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
114
+ startItemTax.base_price = parseFloat(lastPrice).toFixed(2)
115
+ startItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
116
+ element.tax_array.push(JSON.parse(JSON.stringify(startItemTax)))
117
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
118
+
119
+ if (itemTaxIndex == itemTaxs.length - 1) {
120
+ element.tax_amount = itemTotalTax;
121
+ element.tax_amount = parseFloat(element.tax_amount).toFixed(2);
122
+ element.total_tax_amount = itemTotalTax;
123
+ element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(2);
124
+ element.tax_amount_modifier = parseFloat(itemTotalTax).toFixed(2);
125
+ resolve({ 'cart': cart, "element": element });
126
+ }
127
+ });
128
+ });
129
+ })
130
+ })
131
+ }
132
+ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount) {
133
+ return new Promise(async (resolve, reject) => {
134
+ let itemTotalTax = 0;
135
+ element.package_items.forEach((packageItem, packageItemIndex) => {
136
+ element.package_items[packageItemIndex].tax_amount = 0
137
+ let promoDiscountPackage = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0
138
+ let pointsDisouuntPackage = (packageItem.points_discount) ? packageItem.points_discount : 0
139
+
140
+ let defaultComboDiscount = packageItem.default_combo_discount ? packageItem.default_combo_discount : 0
141
+ let afterDicsountPackage = packageItem.price * element.quantity - promoDiscountPackage
142
+
143
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(2)
144
+ afterDicsountPackage = afterDicsountPackage - pointsDisouuntPackage
145
+
146
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(2)
147
+ afterDicsountPackage = afterDicsountPackage - defaultComboDiscount
148
+
149
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(2)
150
+
151
+ getTaxSettings2(packageItem.tax_id, tax_settings, packageItem.ieps_tax).then(async (itemTaxs) => {
152
+
153
+ if (!element.package_items[packageItemIndex].tax_amount_modifier)
154
+ element.package_items[packageItemIndex].tax_amount_modifier = 0
155
+
156
+ element.package_items[packageItemIndex].total_tax_amount = 0
157
+
158
+ let lastPrice = afterDicsountPackage
159
+ let itemtaxAmount = 0;
160
+
161
+ await getBeforeTaxPriceOfItem(itemTaxs, lastPrice).then(async (resp) => {
162
+ lastPrice = +resp.beforeTax;
163
+
164
+ itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
165
+
166
+ let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
167
+
168
+ if (!element.package_items[packageItemIndex].tax_array) {
169
+ element.package_items[packageItemIndex].tax_array = []
170
+ }
171
+
172
+
173
+ let taxAmount = 0
174
+
175
+ if (startItemTax.ieps_type && startItemTax.ieps_type == 2) {
176
+ taxAmount = lastPrice >= startItemTax.tax_rate ? startItemTax.tax_rate : 0;
177
+ } else {
178
+ taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
179
+ }
180
+
181
+ taxAmount = parseFloat(taxAmount).toFixed(2)
182
+ itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(2)
183
+ itemtaxAmount = (parseFloat(itemtaxAmount) + parseFloat(taxAmount)).toFixed(2);
184
+
185
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
186
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
187
+ startItemTax.base_price = parseFloat(lastPrice).toFixed(2)
188
+ startItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
189
+
190
+ element.package_items[packageItemIndex].tax_array.push(JSON.parse(JSON.stringify(startItemTax)));
191
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
192
+
193
+ //push array yo package
194
+ element = await setPackageTaxes(element, JSON.parse(JSON.stringify(startItemTax)));
195
+ element.package_items[packageItemIndex].tax_amount = parseFloat(itemtaxAmount).toFixed(2);
196
+ element.package_items[packageItemIndex].total_tax_amount = parseFloat(itemtaxAmount).toFixed(2);
197
+ // element.package_items[packageItemIndex].tax_amount_modifier = itemTotalTax
198
+ });
199
+ });
200
+
201
+ element.tax_amount = (parseFloat(element.tax_amount) + parseFloat(itemtaxAmount)).toFixed(2)
202
+ element.total_tax_amount = (parseFloat(element.total_tax_amount) + parseFloat(element.package_items[packageItemIndex].tax_amount)).toFixed(2)
203
+ })
204
+ if (packageItemIndex == element.package_items.length - 1) {
205
+ element.tax_amount_modifier = 0;
206
+ resolve({ 'cart': cart, "element": element });
207
+ }
208
+ })
209
+ })
210
+ }
211
+ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount) {
212
+ return new Promise(async (resolve, reject) => {
213
+ if (element && element.item_modifiers && element.item_modifiers.length > 0) {
214
+ let itemModiTotalTax = 0
215
+ element.item_modifiers.forEach((modi, modiIndex) => {
216
+ let taxId = []
217
+ let iepsTax;
218
+
219
+ if (modi.is_tax_rate_same == 1) {
220
+ if (modi.linked_product && modi.linked_product.product_tax_id) {
221
+ taxId = modi.linked_product.product_tax_id
222
+ iepsTax = modi.linked_product.ieps_tax
223
+ } else {
224
+ taxId = element.tax_id
225
+ iepsTax = []
226
+ }
227
+ }
228
+ else {
229
+ if (modi.linked_product && modi.linked_product.product_tax_id) {
230
+ taxId = modi.linked_product.product_tax_id
231
+ } else {
232
+ taxId = modi.tax_id
233
+ }
234
+ }
235
+
236
+ modi.tax_id = taxId
237
+ let promoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0
238
+ let pointsDisouunt = (modi.points_discount) ? modi.points_discount : 0
239
+ promoDiscount = parseFloat(promoDiscount).toFixed(2)
240
+ let afterDicsount = (modi.modifier_item_price * modi.quantity) * element.quantity - promoDiscount
241
+ afterDicsount = afterDicsount - pointsDisouunt
242
+ afterDicsount = parseFloat(afterDicsount).toFixed(2)
243
+
244
+ afterDicsount = (afterDicsount < 0) ? 0 : afterDicsount
245
+
246
+ cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDicsount)
247
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(2)
248
+
249
+ getTaxSettings2(taxId, tax_settings, iepsTax).then(async (modiItemTaxs) => {
250
+
251
+ let lastPrice = afterDicsount
252
+ if (modiItemTaxs.length > 0) {
253
+ await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice).then((resp) => {
254
+ lastPrice = +resp.beforeTax;
255
+
256
+ modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
257
+
258
+ let modiItemTaxArray = JSON.parse(JSON.stringify(modiItemTax))
259
+
260
+ if (!element.item_modifiers[modiIndex].tax_array)
261
+ element.item_modifiers[modiIndex].tax_array = []
262
+ if (!element.item_modifiers[modiIndex].tax_amount)
263
+ element.item_modifiers[modiIndex].tax_amount = 0
264
+
265
+ let taxAmount = 0
266
+
267
+ if (modiItemTaxArray.ieps_type && modiItemTaxArray.ieps_type == 2) {
268
+ taxAmount = lastPrice >= modiItemTaxArray.tax_rate ? modiItemTaxArray.tax_rate : 0;
269
+ } else {
270
+ taxAmount = (lastPrice * modiItemTaxArray.tax_rate) / 100;
271
+ }
272
+
273
+ taxAmount = parseFloat(taxAmount).toFixed(2)
274
+ itemModiTotalTax = itemModiTotalTax + taxAmount
275
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
276
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
277
+
278
+ modiItemTaxArray.base_price = parseFloat(lastPrice).toFixed(2)
279
+ modiItemTaxArray.tax_amount = parseFloat(taxAmount).toFixed(2)
280
+
281
+ element.item_modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTaxArray)))
282
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTaxArray)));
283
+
284
+ element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount) + parseFloat(taxAmount);
285
+ element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount).toFixed(2)
286
+ element.tax_amount_modifier = parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)
287
+ element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(2)
288
+ element.total_tax_amount = parseFloat(element.total_tax_amount) + parseFloat(taxAmount)
289
+ element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(2)
290
+
291
+ if (modiItemTaxs.length - 1 == modiTaxItemIndex) {
292
+ resolve({ 'cart': cart, "element": element });
293
+ }
294
+
295
+
296
+ });
297
+ })
298
+ }
299
+ else {
300
+ resolve({ 'cart': cart, "element": element });
301
+ }
302
+ })
303
+ })
304
+ }
305
+ else {
306
+ resolve({ 'cart': cart, "element": element });
307
+ }
308
+ })
309
+ }
310
+ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount) {
311
+ return new Promise(async (resolve, reject) => {
312
+ element.package_items.forEach((packageItem, packageItemIndex) => {
313
+ element.package_items[packageItemIndex].tax_amount_modifier = 0
314
+ if (packageItem.modifiers.length > 0) {
315
+ let itemModiTotalTax = 0
316
+ packageItem.modifiers.forEach((modi, modiIndex) => {
317
+ let taxId = []
318
+ let iepsTax;
319
+ if (modi.is_tax_rate_same == 1) {
320
+ if (modi.linked_product && modi.linked_product.product_tax_id) {
321
+ taxId = modi.linked_product.product_tax_id
322
+ iepsTax = modi.linked_product.ieps_tax
323
+ } else {
324
+ taxId = packageItem.tax_id
325
+ iepsTax = []
326
+ }
327
+ }
328
+ else {
329
+ if (modi.linked_product && modi.linked_product.product_tax_id) {
330
+ taxId = modi.linked_product.product_tax_id
331
+ } else {
332
+ taxId = modi.tax_id
333
+ }
334
+ }
335
+
336
+ modi.tax_id = taxId
337
+
338
+ let promoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0
339
+ let pointsDisouunt = (modi.points_discount) ? modi.points_discount : 0
340
+ let afterDicsount = (modi.modifier_item_price * modi.quantity) * element.quantity - promoDiscount
341
+ afterDicsount = afterDicsount - pointsDisouunt
342
+ afterDicsount = parseFloat(afterDicsount).toFixed(2)
343
+
344
+ afterDicsount = (afterDicsount < 0) ? 0 : afterDicsount
345
+
346
+ cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDicsount)
347
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(2)
348
+
349
+ getTaxSettings2(taxId, tax_settings, iepsTax).then(async (modiItemTaxs) => {
350
+
351
+ let lastPrice = afterDicsount
352
+
353
+ if (modiItemTaxs.length > 0) {
354
+ await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice).then((resp) => {
355
+ lastPrice = +resp.beforeTax;
356
+ modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
357
+
358
+ if (!element.package_items[packageItemIndex].modifiers[modiIndex].tax_array) {
359
+ element.package_items[packageItemIndex].modifiers[modiIndex].tax_array = []
360
+ }
361
+
362
+ if (!element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount) {
363
+ element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount = 0
364
+ }
365
+
366
+ let taxAmount = 0
367
+
368
+ if (modiItemTax.ieps_type && modiItemTax.ieps_type == 2) {
369
+ taxAmount = lastPrice >= modiItemTax.tax_rate ? modiItemTax.tax_rate : 0;
370
+ } else {
371
+ taxAmount = (lastPrice * modiItemTax.tax_rate) / 100;
372
+ }
373
+ taxAmount = parseFloat(taxAmount).toFixed(2)
374
+
375
+ itemModiTotalTax = itemModiTotalTax + taxAmount;
376
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
377
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
378
+
379
+
380
+ // modiItemTax.base_price = that.taxType == 1 ? parseFloat(afterDicsount).toFixed(2) : (parseFloat(afterDicsount) - parseFloat(taxAmount)).toFixed(2)
381
+ modiItemTax.base_price = lastPrice
382
+ modiItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
383
+
384
+ // cart[itemIndex].item_modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTax)))
385
+ element.package_items[packageItemIndex].modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTax)))
386
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTax)))
387
+
388
+ element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount = parseFloat(element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount) + parseFloat(taxAmount)
389
+ element.package_items[packageItemIndex].tax_amount_modifier = (parseFloat(element.package_items[packageItemIndex].tax_amount_modifier) + parseFloat(taxAmount)).toFixed(2)
390
+ element.package_items[packageItemIndex].total_tax_amount = (parseFloat(element.package_items[packageItemIndex].total_tax_amount) + parseFloat(taxAmount)).toFixed(2)
391
+ element.tax_amount_modifier = (parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)).toFixed(2)
392
+ element.total_tax_amount = (parseFloat(element.total_tax_amount) + parseFloat(taxAmount)).toFixed(2)
393
+
394
+ if (modiItemTaxs.length - 1 == modiTaxItemIndex)
395
+ resolve({ 'cart': cart, "element": element });
396
+
397
+ });
398
+ })
399
+ }
400
+ else {
401
+ resolve({ 'cart': cart, "element": element });
402
+ }
403
+
404
+
405
+ })
406
+
407
+ })
408
+ }
409
+ else {
410
+ resolve({ 'cart': cart, "element": element });
411
+ }
412
+ });
413
+ })
414
+ }
415
+ function setPackageTaxes(item, tax_array) {
416
+ return new Promise(async (resolve, reject) => {
417
+ let index = -1;
418
+
419
+ if (tax_array.ieps_type) {
420
+ index = item.tax_array.findIndex((res) => res.tax_id == tax_array.tax_id && res.ieps_type == tax_array.ieps_type);
421
+ } else {
422
+ index = item.tax_array.findIndex((res) => !res.ieps_type && res.tax_id == tax_array.tax_id);
423
+ }
424
+
425
+ if (index !== -1) {
426
+ item.tax_array[index].tax_amount = (parseFloat(item.tax_array[index].tax_amount) + parseFloat(tax_array.tax_amount)).toFixed(2);
427
+ item.tax_array[index].base_price = (parseFloat(item.tax_array[index].base_price) + parseFloat(tax_array.base_price)).toFixed(2);
428
+ } else {
429
+ item.tax_array.push(tax_array);
430
+ }
431
+ resolve(item);
432
+ })
433
+ }
434
+
435
+ function getTaxSettings2(itemTax, taxSettings, iepsTax = '') {
436
+ return new Promise(function (resolve, reject) {
437
+ let count = 0;
438
+ let settings = [];
439
+ let taxes = [];
440
+
441
+ if (itemTax && itemTax !== undefined) {
442
+ itemTax = !isJson(itemTax) ? itemTax : JSON.parse(itemTax);
443
+ taxes = [...itemTax];
444
+
445
+ // Handle ieps tax
446
+ if (iepsTax && iepsTax.status && iepsTax.status == 1 && iepsTax.ieps_details) {
447
+ if (iepsTax.ieps_details.type == 1) {
448
+ taxes.push(iepsTax.ieps_details);
449
+ } else {
450
+ const absoluteTax = {
451
+ base_price_effected: 0,
452
+ tax_sequence: 1,
453
+ tax_label: 'IEPS',
454
+ ieps_type: 2,
455
+ tax_id: iepsTax.ieps_details.tax_id,
456
+ tax_rate: iepsTax.ieps_details.tax_id,
457
+ };
458
+ settings.push(absoluteTax);
459
+ }
460
+ }
461
+
462
+ taxes.forEach((element) => {
463
+ count++;
464
+ let index = taxSettings.findIndex((res) => res.tax_id == element.tax_id);
465
+
466
+ if (index >= 0) {
467
+ const taxDetail = { ...taxSettings[index] };
468
+ if (element.type) {
469
+ taxDetail.tax_label = 'IEPS';
470
+ taxDetail.ieps_type = 1;
471
+ }
472
+ settings.push(taxDetail);
473
+ }
474
+
475
+ if (count == taxes.length) {
476
+ settings = settings.sort((a, b) => (Number(b.tax_sequence) < Number(a.tax_sequence) ? 1 : -1));
477
+ resolve(settings);
478
+ }
479
+ });
480
+ } else {
481
+ resolve([]);
482
+ }
483
+ });
484
+ }
485
+
486
+ function isJson(str) {
487
+ try {
488
+ JSON.parse(str);
489
+ } catch (e) {
490
+ return false;
491
+ }
492
+ return true;
493
+ }
494
+
495
+ async function getBeforeTaxPriceOfItem(taxes, price) {
496
+ return new Promise((resolve, reject) => {
497
+ let taxType = this.taxType;
498
+ let beforeTax = parseFloat(price);
499
+ let taxRate = 0;
500
+ let taxAmount = 0;
501
+ let itemPriceForTaxCal = 0;
502
+
503
+ taxes.forEach((tax) => {
504
+ if (tax.ieps_type && tax.ieps_type == 2) {
505
+ // Adjust beforeTax based on tax type
506
+ if (beforeTax >= tax.tax_rate) {
507
+ itemPriceForTaxCal = (taxType == 1) ? beforeTax + tax.tax_rate : beforeTax - tax.tax_rate;
508
+ }
509
+ } else {
510
+ taxRate += parseFloat(tax.tax_rate);
511
+ }
512
+ });
513
+
514
+ // Calculate taxAmount based on tax type 1 = exclusive and 2 = inclusive
515
+ if (taxType == 1) {
516
+ taxAmount = itemPriceForTaxCal == 0 ? ((beforeTax * taxRate) / 100).toFixed(6) : ((itemPriceForTaxCal * taxRate) / 100).toFixed(6);
517
+ } else {
518
+ beforeTax = itemPriceForTaxCal == 0 ? (beforeTax / parseFloat(1 + taxRate / 100)).toFixed(6) : (itemPriceForTaxCal / (1 + taxRate / 100)).toFixed(6);
519
+ taxAmount = itemPriceForTaxCal == 0 ? (price - beforeTax).toFixed(6) : (itemPriceForTaxCal - beforeTax).toFixed(6);
520
+ }
521
+ resolve({ beforeTax, taxAmount });
522
+ });
523
+ }
524
+ function setTaxes(cart, tax_array) {
525
+ return new Promise(async (resolve, reject) => {
526
+ let index = cart.merged_tax_array.findIndex((res) => res.tax_id == tax_array.tax_id)
527
+ if (index != -1) {
528
+ cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount) + parseFloat(tax_array.tax_amount)
529
+ cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount).toFixed(2)
530
+
531
+ cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price) + parseFloat(tax_array.base_price)
532
+ cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price).toFixed(2)
533
+
534
+ }
535
+ else {
536
+ cart.merged_tax_array.push(tax_array)
537
+ }
538
+ resolve(cart);
539
+ })
540
+ }
541
+
542
+ module.exports = { calculateTax2 }
package/main.js ADDED
@@ -0,0 +1,77 @@
1
+ const calculation = require('./calculations');
2
+ async function calculateTax(inputJSON) {
3
+ return new Promise(async (resolve, reject) => {
4
+ try {
5
+ const requiredFields = ['tax_type', 'order_items', 'tax_settings'];
6
+ const missingFields = [];
7
+ for (const field of requiredFields) {
8
+ if (!inputJSON || !inputJSON[field]) {
9
+ missingFields.push(field);
10
+ }
11
+ }
12
+ if (missingFields.length > 0) {
13
+ resolve({
14
+ 'status': 'fail',
15
+ 'status_code': 400,
16
+ 'message': 'Field required',
17
+ 'missing_fields': missingFields,
18
+ });
19
+ } else {
20
+ let continueFlag = true;
21
+ if (inputJSON.promotion_applied) {
22
+ const promotionKeys = ['offer_discount_type', 'oo_offer_value', 'offer_id'];
23
+ const missingPromotionKeys = promotionKeys.filter(key => !inputJSON.promotion_applied[key]);
24
+ if (missingPromotionKeys.length > 0) {
25
+ continueFlag = false;
26
+ resolve({
27
+ 'status': 'fail',
28
+ 'status_code': 400,
29
+ 'message': 'Field required for promotion_applied',
30
+ 'missing_fields': missingPromotionKeys,
31
+ });
32
+ }
33
+ }
34
+ if (continueFlag) {
35
+ calculation.applyDistribution(inputJSON).then(result => {
36
+ const optionalFields = ['order_type', 'restaurant_id', 'branch_id', 'request_id'];
37
+ const missingOptionalFields = optionalFields.filter(field => !inputJSON[field]);
38
+ if (missingOptionalFields.length > 0) {
39
+ resolve({
40
+ 'status': 'success',
41
+ 'status_code': 201,
42
+ 'message': 'Tax calculated successfully with few details missing',
43
+ 'missing_optional_fields': missingOptionalFields,
44
+ 'data': result
45
+ });
46
+ } else {
47
+ resolve({
48
+ 'status': 'success',
49
+ 'status_code': 200,
50
+ 'message': 'Tax calculated successfully',
51
+ 'data': result
52
+ });
53
+ }
54
+ }).catch(err => {
55
+ resolve({
56
+ "status": 'fail',
57
+ "status_code": 500,
58
+ "message": 'Error during tax calculation',
59
+ "error": err.stack,
60
+ });
61
+ })
62
+ }
63
+ }
64
+ } catch (error) {
65
+ resolve({
66
+ 'status': 'error',
67
+ 'status_code': 500,
68
+ 'message': 'Internal server error',
69
+ 'error': error.stack,
70
+ });
71
+ }
72
+ })
73
+ }
74
+
75
+ module.exports = { calculateTax }
76
+
77
+
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "foodbot-cart-calculations",
3
+ "version": "1.0.0",
4
+ "description": "Package for cart calculations in which it performs discount distribution and tax distribution on order",
5
+ "main": "main.js",
6
+ "scripts": {
7
+ "test": "npm start"
8
+ },
9
+ "author": "Foodbot",
10
+ "license": "ISC"
11
+ }