foodbot-cart-calculations 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/calculations.js CHANGED
@@ -62,15 +62,27 @@ function applyDistribution(body) {
62
62
  taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2)
63
63
  }
64
64
 
65
+ let tipValue = body.tip_value?body.tip_value:0;
66
+
67
+ if (body.tip_type && body.tip_type == 'percentage') {
68
+ tipValue = (taxData.total_after_tax * parseInt(tipValue)) / 100
69
+ tipValue = parseFloat(tipValue).toFixed(2)
70
+ }
71
+
72
+ taxData.total_after_tax = parseFloat(taxData.total_after_tax) + parseFloat(tipValue);
73
+
65
74
 
66
75
  mainCart.order_items = taxData
67
76
  mainCart.final_amount = taxData.cartTotal
68
77
  mainCart.cash_expected = taxData.cartTotal
78
+ mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(tipValue);
79
+ mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(tipValue);
69
80
 
70
81
  mainCart.order_items.discount = parseFloat(mainCart.order_items.discount) + parseFloat(mainCart.store_value)
71
82
  mainCart.order_items.discount = parseFloat(mainCart.order_items.discount).toFixed(2)
72
83
 
73
84
  mainCart.discount = mainCart.order_items.discount;
85
+ mainCart.order_items.total=parseFloat(mainCart.order_items.total).toFixed(2)
74
86
  resolve(mainCart);
75
87
  }).catch(err => {
76
88
  reject(err);
@@ -3,12 +3,30 @@ function offerCalculation(carts, offer) {
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
+ let modTotal = (mod.modifier_item_price * mod.quantity) * element.quantity
13
+ carts.total += modTotal
14
+ });
15
+ }
16
+ if (element.isPackage == 1) {
17
+ element.package_items.forEach(function (p) {
18
+ delete p.promotion_discount;
19
+ if (p.item_modifiers)
20
+ p.item_modifiers.forEach((pMod) => {
21
+ delete pMod.points_discount;
22
+ });
23
+
24
+ if (p.modifiers)
25
+ p.modifiers.forEach((pMod) => {
26
+ delete pMod.points_discount;
27
+ });
28
+ });
29
+ }
12
30
  });
13
31
  if (offer && offer.offer_discount_type && offer.offer_discount_type == 'buy_x_get_y') {
14
32
  getOfferDetails(carts, offer).then(res => {
@@ -20,7 +38,9 @@ function offerCalculation(carts, offer) {
20
38
  offer.oo_offer_value = Math.round(carts.discount)
21
39
  getOfferDetails(carts, offer).then(res => {
22
40
  resolve(res)
23
- }).catch(error);
41
+ }).catch(error=>{
42
+ reject(error)
43
+ });
24
44
  }
25
45
  else {
26
46
  resolve(carts)
@@ -375,48 +395,47 @@ function getCategorySpeificOffer(carts, offer) {
375
395
  }
376
396
 
377
397
  function getOfferDetails(carts, offer = []) {
378
- try {
379
- buyItemAmount = 0
380
- buyItemss = []
381
- total = 0
382
-
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);
398
+ return new Promise(function (resolve, reject) {
399
+ try {
400
+ buyItemAmount = 0
401
+ buyItemss = []
402
+ total = 0
385
403
 
386
- // const buyItems = [];
387
- // let buyItemAmount = 0;
404
+ offer.offer_products.get_item_count = parseInt(offer.offer_products.get_item_count);
405
+ offer.offer_products.buy_item_count = parseInt(offer.offer_products.buy_item_count);
388
406
 
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
- });
407
+ // const buyItems = [];
408
+ // let buyItemAmount = 0;
394
409
 
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
- });
410
+ // carts.item_details.forEach(function (v) {
411
+ // delete v.promotion_discount_modifier;
412
+ // delete v.promotion_discount;
413
+ // delete v.redeem_promotion_id;
414
+ // });
406
415
 
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;
416
+ carts.item_details.forEach(function (v) {
417
+ delete v.promotion_discount_modifier;
418
+ delete v.promotion_discount;
419
+ delete v.redeem_promotion_id;
420
+ if (v.item_modifiers) {
421
+ v.item_modifiers.forEach((mod) => {
422
+ delete mod.promotion_discount;
423
+ delete mod.redeem_promotion_id;
424
+ });
425
+ }
413
426
  });
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
427
 
419
- return new Promise(function (resolve, reject) {
428
+ if (carts && carts.item_details) {
429
+ carts.item_details.map((a) => {
430
+ a.promotion_discount = 0;
431
+ a.redeem_promotion_id = 0;
432
+ a.promotion_discount_modifier = 0;
433
+ a.discount_applied = false;
434
+ });
435
+ let total = 0;
436
+ const loopArray = [];
437
+ const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
438
+ const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
420
439
  addItemQuantity(carts.item_details, offer).then((data) => {
421
440
  const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? -1 : 1);
422
441
  getBuyxGetYOnCondition(descending, carts, offer).then((total) => {
@@ -428,16 +447,13 @@ function getOfferDetails(carts, offer = []) {
428
447
  }).catch(error => {
429
448
  reject(error);
430
449
  })
431
- });
432
- } else {
433
- return new Promise(function (resolve, reject) {
434
- resolve({ 'offer_amount': 0, 'amount': '' });
435
- });
450
+ } else {
451
+ resolve({ 'offer_amount': 0, 'amount': '' });
452
+ }
453
+ } catch (error) {
454
+ reject(error)
436
455
  }
437
- } catch (error) {
438
- reject(error)
439
- }
440
-
456
+ })
441
457
  };
442
458
 
443
459
  function getBuyxGetYOnCondition(descending, cartData, offer) {
@@ -672,87 +688,90 @@ function getAbsoluteDiscount(cartObject, offer, buyItemss, buyItemAmount) {
672
688
  let remaningOfferAmount = offer.oo_offer_value;
673
689
  let count = 0;
674
690
  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);
706
-
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] = {};
691
+ if(buyItemss && buyItemss.length>0){
692
+ buyItemss.forEach((element, buyIndex) => {
693
+ let itemAmount = 0;
694
+ let itemTotalDiscount = 0;
695
+ let itemIndex = carts.findIndex((obj) => {
696
+ return obj.item_id == element.item_id && !obj.discount_applied;
697
+ });
698
+
699
+ if (itemIndex == -1) itemIndex = carts.findIndex((obj) => {
700
+ return obj.item_id == element.item_id;
701
+ });
702
+
703
+ if (!carts[itemIndex].promotion_discount_modifier) carts[itemIndex].promotion_discount_modifier = 0;
704
+
705
+ if (element.item_modifiers && element.item_modifiers.length > 0 && offer.offer_products.order_modifiercost_use == 1) {
706
+ let modiCount = 0;
707
+ element.item_modifiers.forEach((modi) => {
708
+ let modiItemAmount = 0;
709
+ let modOfferAmount = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * element.quantity * parseFloat(singlePriceDiscount);
710
+ modiItemAmount = (modi.modifier_item_price < modOfferAmount) ? modi.modifier_item_price : modOfferAmount;
711
+ modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
712
+ itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(modiItemAmount);
713
+ total = parseFloat(total) + parseFloat(modiItemAmount);
714
+ total = parseFloat(total).toFixed(2);
715
+
716
+ var modiIndex = carts[itemIndex].item_modifiers.findIndex((obj) => {
717
+ return obj.modifier_item_id == modi.modifier_item_id;
718
+ });
719
+
720
+ carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(modiItemAmount);
721
+ carts[itemIndex].promotion_discount_modifier = parseFloat(carts[itemIndex].promotion_discount_modifier).toFixed(2);
722
+
723
+ if (modiIndex >= 0) {
724
+ if (!carts[itemIndex].item_modifiers[modiIndex].promotion_discount) carts[itemIndex].item_modifiers[modiIndex].promotion_discount = 0;
725
+ if (!carts[itemIndex].item_modifiers[modiIndex]) {
726
+ carts[itemIndex].item_modifiers[modiIndex] = {};
727
+ carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
728
+ }
729
+
730
+ carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount) + parseFloat(modiItemAmount);
731
+ carts[itemIndex].item_modifiers[modiIndex].promotion_discount = parseFloat(carts[itemIndex].item_modifiers[modiIndex].promotion_discount).toFixed(2);
711
732
  carts[itemIndex].item_modifiers[modiIndex].redeem_promotion_id = offer.offer_id;
733
+ modiCount++;
734
+ remaningOfferAmount = remaningOfferAmount - modiItemAmount;
735
+ } else {
736
+ modiCount++;
712
737
  }
713
-
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
- });
738
+
739
+ if (modiCount == element.item_modifiers.length) count++;
740
+ });
741
+ } else {
742
+ count++;
743
+ }
744
+
745
+ carts[itemIndex].discount_applied = true;
746
+
747
+ if (buyItemss[buyItemss.length - 1].item_id == element.item_id) {
748
+
749
+ itemAmount = (element.item_price < remaningOfferAmount) ? element.item_price : parseFloat(remaningOfferAmount).toFixed(2);
750
+ } else {
751
+ itemAmount = element.item_price * singlePriceDiscount;
752
+ }
753
+
754
+ itemAmount = parseFloat(itemAmount);
755
+ total = parseFloat(total) + parseFloat(itemAmount);
756
+ total = parseFloat(total).toFixed(2);
757
+ itemTotalDiscount = parseFloat(itemTotalDiscount) + parseFloat(itemAmount);
758
+ remaningOfferAmount = remaningOfferAmount - itemAmount;
759
+
760
+ carts[itemIndex].promotion_discount = (parseFloat(carts[itemIndex].promotion_discount) + parseFloat(itemAmount)).toFixed(2);
761
+ carts[itemIndex].promotion_discount_modifier = (parseFloat(carts[itemIndex].promotion_discount_modifier) + parseFloat(itemAmount)).toFixed(2);
762
+ carts[itemIndex].redeem_promotion_id = offer.offer_id;
763
+
764
+ if (count == buyItemss.length) {
765
+ cartObject.item_details = carts
766
+ cartObject.discount = total
767
+
768
+
769
+ resolve(cartObject);
770
+ }
771
+ });
772
+ }else{
773
+ resolve(cartObject);
774
+ }
756
775
  } catch (error) {
757
776
  reject(error);
758
777
  }
@@ -54,23 +54,22 @@ function calculateTax2(cart, texSettings, taxType) {
54
54
  cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDiscount);
55
55
  cart.cartTotal = parseFloat(cart.cartTotal).toFixed(2);
56
56
  total = +element.item_price;
57
-
58
57
  if (element.isPackage == 1) {
59
- let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
58
+ let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
60
59
  cart = result.cart;
61
60
  element = result.element;
62
61
  } else {
63
- let result = await calculateItemTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
62
+ let result = await calculateItemTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
64
63
  cart = result.cart;
65
64
  element = result.element;
66
65
  }
67
66
 
68
67
  if (element.isPackage == 1) {
69
- let result = await calculatePackageModiTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
68
+ let result = await calculatePackageModiTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
70
69
  cart = result.cart;
71
70
  element = result.element;
72
71
  } else {
73
- let result = await calculateModiTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
72
+ let result = await calculateModiTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
74
73
  cart = result.cart;
75
74
  element = result.element;
76
75
  }
@@ -87,49 +86,86 @@ function calculateTax2(cart, texSettings, taxType) {
87
86
  });
88
87
  }
89
88
 
90
- function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
89
+ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
91
90
  return new Promise(async (resolve, reject) => {
91
+ if (element.event && element.tax_id && element.tax_id.length == 0) {
92
+ let no_tax = tax_settings.filter(ele => ele.tax_rate == 0);
93
+ element.tax_id = [{
94
+ "tax_id": no_tax[0].tax_id
95
+ }]
96
+ }
92
97
  getTaxSettings2(element.tax_id, tax_settings, element.ieps_tax).then(async (itemTaxs) => {
93
98
  let lastPrice = afterDicsount
94
- getBeforeTaxPriceOfItem(itemTaxs, lastPrice,taxType).then((resp) => {
95
- lastPrice = +resp.beforeTax;
99
+ getBeforeTaxPriceOfItem(itemTaxs, lastPrice, taxType, element.quantity).then((resp) => {
100
+ lastPrice = resp.beforeTax;
101
+ let lastPriceIeps = +resp.beforeTaxIeps;
102
+ let iepsTaxRate = +resp.iepsTaxRate;
103
+ let iepsTaxAmount = +resp.iepsTaxAmount;
104
+ let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
96
105
  let itemTotalTax = 0;
106
+ let tax_array = []
107
+ if (itemTaxs && itemTaxs.length > 0) {
108
+ itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
109
+ // let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
110
+ // if (!element.tax_array) {
111
+ // element.tax_array = []
112
+ // }
113
+ // let taxAmount = 0
114
+ // if (startItemTax.ieps_type && startItemTax.ieps_type == 2) {
115
+ // taxAmount = lastPrice >= startItemTax.tax_rate ? startItemTax.tax_rate : 0;
116
+ // } else {
117
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
118
+ // }
119
+ // taxAmount = parseFloat(taxAmount).toFixed(2)
120
+ // itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(2)
121
+
122
+ // cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
123
+ // cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
124
+ // startItemTax.base_price = parseFloat(lastPrice).toFixed(2)
125
+ // startItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
126
+ // element.tax_array.push(JSON.parse(JSON.stringify(startItemTax)))
97
127
 
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
+ let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
130
+ let taxAmount = 0;
131
+
132
+ if (startItemTax.ieps_type) {
133
+ taxAmount = iepsTaxAmount;
134
+ } else {
135
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
136
+ taxAmount = resp.taxAmount;
137
+ }
138
+ taxAmount = parseFloat(taxAmount).toFixed(6);
139
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
140
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
141
+ itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(6);
142
+ startItemTax.tax_amount = parseFloat(taxAmount).toFixed(6);
143
+ startItemTax.base_price = startItemTax.ieps_type ? lastPriceIeps : lastPrice;
144
+ startItemTax.tax_rate = startItemTax.ieps_type ? iepsTaxRate : startItemTax.tax_rate;
145
+ startItemTax.tax_rate_quantity = iepsTaxRateQuantity;
146
+ tax_array.push(JSON.parse(JSON.stringify(startItemTax)))
147
+
148
+
149
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
150
+
151
+ if (itemTaxIndex == itemTaxs.length - 1) {
152
+ element.tax_amount = itemTotalTax;
153
+ element.tax_array = tax_array;
154
+ element.tax_amount = parseFloat(element.tax_amount).toFixed(6);
155
+ element.total_tax_amount = itemTotalTax;
156
+ element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(6);
157
+ element.tax_amount_modifier = parseFloat(itemTotalTax).toFixed(6);
158
+ resolve({ 'cart': cart, "element": element });
159
+ }
160
+ });
161
+ } else {
162
+ resolve({ 'cart': cart, "element": element });
163
+ }
128
164
  });
129
165
  })
130
166
  })
131
167
  }
132
- function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
168
+ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
133
169
  return new Promise(async (resolve, reject) => {
134
170
  let itemTotalTax = 0;
135
171
  element.package_items.forEach((packageItem, packageItemIndex) => {
@@ -158,48 +194,52 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
158
194
  let lastPrice = afterDicsountPackage
159
195
  let itemtaxAmount = 0;
160
196
 
161
- await getBeforeTaxPriceOfItem(itemTaxs, lastPrice,taxType).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
- });
197
+ await getBeforeTaxPriceOfItem(itemTaxs, lastPrice, taxType, packageItem.quantity).then(async (resp) => {
198
+ lastPrice = resp.beforeTax;
199
+ let lastPriceIeps = +resp.beforeTaxIeps;
200
+ let iepsTaxRate = +resp.iepsTaxRate;
201
+ let iepsTaxAmount = +resp.iepsTaxAmount;
202
+ let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
203
+ let tax_array = []
204
+ if (itemTaxs && itemTaxs.length > 0) {
205
+ itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
206
+
207
+ let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
208
+
209
+ let taxAmount = 0
210
+
211
+ if (startItemTax.ieps_type) {
212
+ taxAmount = iepsTaxAmount;
213
+ } else {
214
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
215
+ taxAmount = resp.taxAmount;
216
+ }
217
+
218
+ taxAmount = parseFloat(taxAmount).toFixed(6);
219
+ cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
220
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
221
+ itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(6);
222
+ itemtaxAmount = (parseFloat(itemtaxAmount) + parseFloat(taxAmount)).toFixed(6);
223
+ startItemTax.tax_amount = parseFloat(taxAmount).toFixed(6);
224
+ startItemTax.base_price = startItemTax.ieps_type ? lastPriceIeps : lastPrice;
225
+ startItemTax.tax_rate = startItemTax.ieps_type ? iepsTaxRate : startItemTax.tax_rate;
226
+ startItemTax.tax_rate_quantity = iepsTaxRateQuantity;
227
+ tax_array.push(JSON.parse(JSON.stringify(startItemTax)));
228
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
229
+
230
+ //push array yo package
231
+ element = await setPackageTaxes(element, JSON.parse(JSON.stringify(startItemTax)));
232
+ element.package_items[packageItemIndex].tax_amount = parseFloat(itemtaxAmount).toFixed(6);
233
+ element.package_items[packageItemIndex].tax_array = tax_array;
234
+ element.package_items[packageItemIndex].total_tax_amount = parseFloat(itemtaxAmount).toFixed(6);
235
+
236
+ // element.package_items[packageItemIndex].tax_amount_modifier = itemTotalTax
237
+ });
238
+ }
199
239
  });
200
240
 
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)
241
+ element.tax_amount = (parseFloat(element.tax_amount) + parseFloat(itemTotalTax)).toFixed(6)
242
+ element.total_tax_amount = (parseFloat(element.total_tax_amount) + parseFloat(element.package_items[packageItemIndex].tax_amount)).toFixed(6)
203
243
  })
204
244
  if (packageItemIndex == element.package_items.length - 1) {
205
245
  element.tax_amount_modifier = 0;
@@ -208,21 +248,22 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
208
248
  })
209
249
  })
210
250
  }
211
- function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
251
+ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
212
252
  return new Promise(async (resolve, reject) => {
213
253
  if (element && element.item_modifiers && element.item_modifiers.length > 0) {
214
254
  let itemModiTotalTax = 0
215
255
  element.item_modifiers.forEach((modi, modiIndex) => {
216
256
  let taxId = []
217
- let iepsTax;
218
-
257
+ let iepsTax;
219
258
  if (modi.is_tax_rate_same == 1) {
220
259
  if (modi.linked_product && modi.linked_product.product_tax_id) {
221
260
  taxId = modi.linked_product.product_tax_id
222
- iepsTax = modi.linked_product.ieps_tax
261
+ if (modi.linked_product.ieps_tax && modi.linked_product.ieps_tax.ieps_details && modi.linked_product.ieps_tax.ieps_details.type == 1)
262
+ iepsTax = modi.linked_product.ieps_tax
223
263
  } else {
224
264
  taxId = element.tax_id
225
- iepsTax = []
265
+ if (element.ieps_tax && element.ieps_tax.ieps_details && element.ieps_tax.ieps_details.type == 1)
266
+ iepsTax = element.ieps_tax
226
267
  }
227
268
  }
228
269
  else {
@@ -250,8 +291,14 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
250
291
 
251
292
  let lastPrice = afterDicsount
252
293
  if (modiItemTaxs.length > 0) {
253
- await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice,taxType).then((resp) => {
254
- lastPrice = +resp.beforeTax;
294
+ await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice, taxType, modi.quantity * element.quantity).then((resp) => {
295
+ lastPrice = resp.beforeTax;
296
+ let lastPriceIeps = +resp.beforeTaxIeps;
297
+ let iepsTaxRate = +resp.iepsTaxRate;
298
+ let iepsTaxAmount = +resp.iepsTaxAmount;
299
+ let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
300
+ let itemTotalTax = 0;
301
+ let tax_array = []
255
302
 
256
303
  modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
257
304
 
@@ -264,31 +311,37 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
264
311
 
265
312
  let taxAmount = 0
266
313
 
267
- if (modiItemTaxArray.ieps_type && modiItemTaxArray.ieps_type == 2) {
268
- taxAmount = lastPrice >= modiItemTaxArray.tax_rate ? modiItemTaxArray.tax_rate : 0;
314
+ if (modiItemTaxArray.ieps_type) {
315
+ taxAmount = iepsTaxAmount;
269
316
  } else {
270
- taxAmount = (lastPrice * modiItemTaxArray.tax_rate) / 100;
317
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
318
+ taxAmount = resp.taxAmount;
271
319
  }
272
320
 
273
- taxAmount = parseFloat(taxAmount).toFixed(2)
321
+ taxAmount = parseFloat(taxAmount).toFixed(6)
274
322
  itemModiTotalTax = itemModiTotalTax + taxAmount
275
323
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
276
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
324
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
277
325
 
278
- modiItemTaxArray.base_price = parseFloat(lastPrice).toFixed(2)
279
- modiItemTaxArray.tax_amount = parseFloat(taxAmount).toFixed(2)
326
+ itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(6);
327
+ modiItemTaxArray.tax_amount = parseFloat(taxAmount).toFixed(6);
328
+ modiItemTaxArray.base_price = modiItemTaxArray.ieps_type ? lastPriceIeps : lastPrice;
329
+ modiItemTaxArray.tax_rate = modiItemTaxArray.ieps_type ? iepsTaxRate : modiItemTaxArray.tax_rate;
330
+ modiItemTaxArray.tax_rate_quantity = iepsTaxRateQuantity;
331
+ tax_array.push(JSON.parse(JSON.stringify(modiItemTaxArray)))
280
332
 
281
- element.item_modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTaxArray)))
333
+ element.item_modifiers[modiIndex].tax_array = tax_array;
282
334
  cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTaxArray)));
283
335
 
284
336
  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)
337
+ element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount).toFixed(6)
286
338
  element.tax_amount_modifier = parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)
287
- element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(2)
339
+ element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(6)
288
340
  element.total_tax_amount = parseFloat(element.total_tax_amount) + parseFloat(taxAmount)
289
- element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(2)
341
+ element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(6)
342
+
343
+ if (modiIndex == element.item_modifiers.length - 1) {
290
344
 
291
- if (modiItemTaxs.length - 1 == modiTaxItemIndex) {
292
345
  resolve({ 'cart': cart, "element": element });
293
346
  }
294
347
 
@@ -297,7 +350,10 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
297
350
  })
298
351
  }
299
352
  else {
300
- resolve({ 'cart': cart, "element": element });
353
+ if (modiIndex == element.item_modifiers.length - 1) {
354
+
355
+ resolve({ 'cart': cart, "element": element });
356
+ }
301
357
  }
302
358
  })
303
359
  })
@@ -307,7 +363,7 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
307
363
  }
308
364
  })
309
365
  }
310
- function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
366
+ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
311
367
  return new Promise(async (resolve, reject) => {
312
368
  element.package_items.forEach((packageItem, packageItemIndex) => {
313
369
  element.package_items[packageItemIndex].tax_amount_modifier = 0
@@ -351,10 +407,16 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
351
407
  let lastPrice = afterDicsount
352
408
 
353
409
  if (modiItemTaxs.length > 0) {
354
- await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice,taxType).then((resp) => {
355
- lastPrice = +resp.beforeTax;
410
+ await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice, taxType, modi.quantity * element.quantity).then((resp) => {
411
+ lastPrice = resp.beforeTax;
412
+ let lastPriceIeps = +resp.beforeTaxIeps;
413
+ let iepsTaxRate = +resp.iepsTaxRate;
414
+ let iepsTaxAmount = +resp.iepsTaxAmount;
415
+ let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
416
+ let itemTotalTax = 0;
417
+ let tax_array = []
356
418
  modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
357
-
419
+ let modiItemTaxArray = JSON.parse(JSON.stringify(modiItemTax))
358
420
  if (!element.package_items[packageItemIndex].modifiers[modiIndex].tax_array) {
359
421
  element.package_items[packageItemIndex].modifiers[modiIndex].tax_array = []
360
422
  }
@@ -365,40 +427,49 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
365
427
 
366
428
  let taxAmount = 0
367
429
 
368
- if (modiItemTax.ieps_type && modiItemTax.ieps_type == 2) {
369
- taxAmount = lastPrice >= modiItemTax.tax_rate ? modiItemTax.tax_rate : 0;
430
+ if (modiItemTaxArray.ieps_type) {
431
+ taxAmount = iepsTaxAmount;
370
432
  } else {
371
- taxAmount = (lastPrice * modiItemTax.tax_rate) / 100;
433
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
434
+ taxAmount = resp.taxAmount;
372
435
  }
373
- taxAmount = parseFloat(taxAmount).toFixed(2)
436
+ taxAmount = parseFloat(taxAmount).toFixed(6)
374
437
 
375
438
  itemModiTotalTax = itemModiTotalTax + taxAmount;
376
439
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
377
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
440
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
378
441
 
379
442
 
380
443
  // 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)
444
+ modiItemTaxArray.tax_amount = parseFloat(taxAmount).toFixed(6);
445
+ modiItemTaxArray.base_price = modiItemTaxArray.ieps_type ? lastPriceIeps : lastPrice;
446
+ modiItemTaxArray.tax_rate = modiItemTaxArray.ieps_type ? iepsTaxRate : modiItemTaxArray.tax_rate;
447
+ modiItemTaxArray.tax_rate_quantity = iepsTaxRateQuantity;
448
+ tax_array.push(JSON.parse(JSON.stringify(modiItemTaxArray)))
383
449
 
384
450
  // 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)))
451
+ element.package_items[packageItemIndex].modifiers[modiIndex].tax_array = tax_array;
452
+ cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTaxArray)))
387
453
 
388
454
  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)
455
+ element.package_items[packageItemIndex].tax_amount_modifier = (parseFloat(element.package_items[packageItemIndex].tax_amount_modifier) + parseFloat(taxAmount)).toFixed(6)
456
+ element.package_items[packageItemIndex].total_tax_amount = (parseFloat(element.package_items[packageItemIndex].total_tax_amount) + parseFloat(taxAmount)).toFixed(6)
457
+ element.tax_amount_modifier = (parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)).toFixed(6)
458
+ element.total_tax_amount = (parseFloat(element.total_tax_amount) + parseFloat(taxAmount)).toFixed(6)
459
+
460
+ if (modiIndex == packageItem.modifiers.length - 1) {
393
461
 
394
- if (modiItemTaxs.length - 1 == modiTaxItemIndex)
395
462
  resolve({ 'cart': cart, "element": element });
463
+ }
396
464
 
397
465
  });
398
466
  })
399
467
  }
400
468
  else {
401
- resolve({ 'cart': cart, "element": element });
469
+ if (modiIndex == packageItem.modifiers.length - 1) {
470
+
471
+ resolve({ 'cart': cart, "element": element });
472
+ }
402
473
  }
403
474
 
404
475
 
@@ -412,21 +483,39 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
412
483
  });
413
484
  })
414
485
  }
415
- function setPackageTaxes(item, tax_array) {
486
+ function setPackageTaxes(item, ele) {
416
487
  return new Promise(async (resolve, reject) => {
417
488
  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);
489
+ if (ele.ieps_type) {
490
+ index = item.tax_array.findIndex((res) => {
491
+ return res.tax_id == ele.tax_id && res.ieps_type == ele.ieps_type
492
+ });
421
493
  } else {
422
- index = item.tax_array.findIndex((res) => !res.ieps_type && res.tax_id == tax_array.tax_id);
494
+ index = item.tax_array.findIndex((res) => {
495
+ return !res.ieps_type && res.tax_id == ele.tax_id
496
+ });
423
497
  }
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);
498
+ if (index != -1) {
499
+ item.tax_array[index].tax_amount = parseFloat(item.tax_array[index].tax_amount) + parseFloat(ele.tax_amount)
500
+ item.tax_array[index].tax_amount = parseFloat(item.tax_array[index].tax_amount).toFixed(6)
501
+ if (item.tax_array[index].tax_rate_quantity)
502
+ item.tax_array[index].tax_rate_quantity = parseFloat(item.tax_array[index].tax_rate_quantity) + parseFloat(ele.tax_rate_quantity)
503
+ item.tax_array[index].base_price = parseFloat(item.tax_array[index].base_price) + parseFloat(ele.base_price)
504
+ item.tax_array[index].base_price = parseFloat(item.tax_array[index].base_price).toFixed(6)
505
+ }
506
+ else {
507
+ item.tax_array.push(ele)
508
+ }
509
+ item.tax_array.sort((a, b) => {
510
+ return a.tax_sequence - b.tax_sequence
511
+ })
512
+ if (item.tax_array[0] && !item.tax_array[0].ieps_type && item.tax_array[1] && item.tax_array[1].ieps_type && item.tax_array[1].ieps_type == 2) {
513
+ item.tax_array[1].base_price = (item.tax_array[0].base_price > 0) ? item.tax_array[0].base_price - item.tax_array[1].tax_rate_quantity : 0;
514
+ item.tax_array[1].base_price = parseFloat(item.tax_array[1].base_price).toFixed(6)
515
+ item.tax_array[1].tax_amount = item.tax_array[1].tax_rate_quantity
516
+ let iepsTaxRate = (item.tax_array[0].base_price > 0) ? item.tax_array[1].tax_rate_quantity / item.tax_array[1].base_price : 0;
517
+ iepsTaxRate = parseFloat(iepsTaxRate).toFixed(6)
518
+ item.tax_array[1].tax_rate = iepsTaxRate
430
519
  }
431
520
  resolve(item);
432
521
  })
@@ -438,33 +527,23 @@ function getTaxSettings2(itemTax, taxSettings, iepsTax = '') {
438
527
  let settings = [];
439
528
  let taxes = [];
440
529
 
441
- if (itemTax && itemTax !== undefined) {
442
- itemTax = !isJson(itemTax) ? itemTax : JSON.parse(itemTax);
530
+ if (itemTax && itemTax != undefined) {
443
531
  taxes = [...itemTax];
444
-
445
532
  // Handle ieps tax
446
533
  if (iepsTax && iepsTax.status && iepsTax.status == 1 && iepsTax.ieps_details) {
447
534
  if (iepsTax.ieps_details.type == 1) {
448
535
  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
536
  }
460
537
  }
461
538
 
462
- taxes.forEach((element) => {
539
+ taxes.forEach(function (element) {
463
540
  count++;
464
- let index = taxSettings.findIndex((res) => res.tax_id == element.tax_id);
541
+ let index = taxSettings.findIndex(function (res) {
542
+ return res.tax_id == element.tax_id;
543
+ });
465
544
 
466
545
  if (index >= 0) {
467
- const taxDetail = { ...taxSettings[index] };
546
+ const taxDetail = Object.assign({}, taxSettings[index]);
468
547
  if (element.type) {
469
548
  taxDetail.tax_label = 'IEPS';
470
549
  taxDetail.ieps_type = 1;
@@ -473,7 +552,21 @@ function getTaxSettings2(itemTax, taxSettings, iepsTax = '') {
473
552
  }
474
553
 
475
554
  if (count == taxes.length) {
476
- settings = settings.sort((a, b) => (Number(b.tax_sequence) < Number(a.tax_sequence) ? 1 : -1));
555
+ // Handle ieps tax
556
+ if (iepsTax && iepsTax.status && iepsTax.status == 1 && iepsTax.ieps_details) {
557
+ if (iepsTax.ieps_details.type == 2) {
558
+ const absoluteTax = {
559
+ base_price_effected: 0,
560
+ tax_sequence: 2,
561
+ tax_label: 'IEPS',
562
+ ieps_type: 2,
563
+ tax_id: 'absolute',
564
+ tax_rate: iepsTax.ieps_details.tax_id,
565
+ };
566
+ settings.push(absoluteTax);
567
+ }
568
+ }
569
+ // settings = settings.sort((a, b) => (Number(b.tax_sequence) > Number(a.tax_sequence)) ? 1 : -1);
477
570
  resolve(settings);
478
571
  }
479
572
  });
@@ -492,50 +585,86 @@ function isJson(str) {
492
585
  return true;
493
586
  }
494
587
 
495
- async function getBeforeTaxPriceOfItem(taxes, price,taxType) {
588
+ async function getBeforeTaxPriceOfItem(taxes, price, taxType, qty) {
496
589
  return new Promise((resolve, reject) => {
497
- let beforeTax = parseFloat(price);
590
+ let beforeTax = +price;
591
+ let beforeTaxIeps = 0;
498
592
  let taxRate = 0;
499
593
  let taxAmount = 0;
500
- let itemPriceForTaxCal = 0;
594
+ let iepsTaxRate = 0;
595
+ let iepsTaxAmount = 0;
596
+ let iepsTaxRateQuantity = 0;
501
597
 
502
598
  taxes.forEach((tax) => {
503
- if (tax.ieps_type && tax.ieps_type == 2) {
504
- // Adjust beforeTax based on tax type
505
- if (beforeTax >= tax.tax_rate) {
506
- itemPriceForTaxCal = (taxType == 1) ? beforeTax + tax.tax_rate : beforeTax - tax.tax_rate;
507
- }
508
- } else {
599
+ if (!tax.ieps_type) {
509
600
  taxRate += parseFloat(tax.tax_rate);
510
601
  }
511
602
  });
512
603
 
513
604
  // Calculate taxAmount based on tax type 1 = exclusive and 2 = inclusive
514
605
  if (taxType == 1) {
515
- taxAmount = itemPriceForTaxCal == 0 ? ((beforeTax * taxRate) / 100).toFixed(6) : ((itemPriceForTaxCal * taxRate) / 100).toFixed(6);
606
+ taxAmount = ((beforeTax * taxRate) / 100).toFixed(6);
516
607
  } else {
517
- beforeTax = itemPriceForTaxCal == 0 ? (beforeTax / parseFloat(1 + taxRate / 100)).toFixed(6) : (itemPriceForTaxCal / (1 + taxRate / 100)).toFixed(6);
518
- taxAmount = itemPriceForTaxCal == 0 ? (price - beforeTax).toFixed(6) : (itemPriceForTaxCal - beforeTax).toFixed(6);
608
+ beforeTax = (beforeTax / (1 + taxRate / 100)).toFixed(6);
609
+ taxAmount = (price - beforeTax).toFixed(6);
519
610
  }
520
- resolve({ beforeTax, taxAmount });
611
+
612
+ // ieps tax type = 1 precentage 2 absoulte
613
+ taxes.forEach((tax) => {
614
+ if (tax.ieps_type && tax.ieps_type == 2) {
615
+ // Adjust beforeTax based on tax type
616
+ if (beforeTax >= tax.tax_rate) {
617
+ beforeTaxIeps = taxType == 1 ? beforeTax : (beforeTax - (tax.tax_rate * qty));
618
+ beforeTaxIeps = (beforeTaxIeps).toFixed(6);
619
+ iepsTaxRate = (tax.tax_rate * qty) / beforeTaxIeps;
620
+ iepsTaxRate = parseFloat(iepsTaxRate).toFixed(6);
621
+ iepsTaxAmount = (tax.tax_rate * qty).toFixed(6);
622
+ iepsTaxRateQuantity = iepsTaxAmount;
623
+ }
624
+ } else if (tax.ieps_type && tax.ieps_type == 1) {
625
+ beforeTaxIeps = taxType == 1 ? (beforeTax).toFixed(6) : (beforeTax / (1 + tax.tax_rate / 100)).toFixed(6);
626
+ iepsTaxRate = tax.tax_rate;
627
+ iepsTaxAmount = beforeTax - beforeTaxIeps;
628
+ iepsTaxAmount = (iepsTaxAmount).toFixed(6)
629
+ }
630
+ });
631
+ resolve({ beforeTax, taxAmount, beforeTaxIeps, iepsTaxRate, iepsTaxAmount, iepsTaxRateQuantity });
521
632
  });
522
633
  }
523
- function setTaxes(cart, tax_array) {
524
- return new Promise(async (resolve, reject) => {
525
- let index = cart.merged_tax_array.findIndex((res) => res.tax_id == tax_array.tax_id)
526
- if (index != -1) {
527
- cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount) + parseFloat(tax_array.tax_amount)
528
- cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount).toFixed(2)
529
-
530
- cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price) + parseFloat(tax_array.base_price)
531
- cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price).toFixed(2)
532
-
533
- }
534
- else {
535
- cart.merged_tax_array.push(tax_array)
536
- }
537
- resolve(cart);
634
+ function setTaxes(cart, ele) {
635
+ let index = -1;
636
+ if (ele.ieps_type) {
637
+ index = cart.merged_tax_array.findIndex((res) => {
638
+ return res.tax_id == ele.tax_id && res.ieps_type == ele.ieps_type
639
+ });
640
+ } else {
641
+ index = cart.merged_tax_array.findIndex((res) => {
642
+ return !res.ieps_type && res.tax_id == ele.tax_id
643
+ });
644
+ }
645
+ if (index != -1) {
646
+ cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount) + parseFloat(ele.tax_amount)
647
+ cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount).toFixed(6)
648
+ if (cart.merged_tax_array[index].tax_rate_quantity)
649
+ cart.merged_tax_array[index].tax_rate_quantity = parseFloat(cart.merged_tax_array[index].tax_rate_quantity) + parseFloat(ele.tax_rate_quantity)
650
+ cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price) + parseFloat(ele.base_price)
651
+ cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price).toFixed(6)
652
+ }
653
+ else {
654
+ cart.merged_tax_array.push(ele)
655
+ }
656
+ cart.merged_tax_array.sort((a, b) => {
657
+ return a.tax_sequence - b.tax_sequence
538
658
  })
659
+ if (cart.merged_tax_array[0] && !cart.merged_tax_array[0].ieps_type && cart.merged_tax_array[1] && cart.merged_tax_array[1].ieps_type && cart.merged_tax_array[1].ieps_type == 2) {
660
+ cart.merged_tax_array[1].base_price = (cart.merged_tax_array[0].base_price > 0) ? cart.merged_tax_array[0].base_price - cart.merged_tax_array[1].tax_rate_quantity : 0;
661
+ cart.merged_tax_array[1].base_price = parseFloat(cart.merged_tax_array[1].base_price).toFixed(6)
662
+ cart.merged_tax_array[1].tax_amount = cart.merged_tax_array[1].tax_rate_quantity
663
+ let iepsTaxRate = (cart.merged_tax_array[0].base_price > 0) ? cart.merged_tax_array[1].tax_rate_quantity / cart.merged_tax_array[1].base_price : 0;
664
+ iepsTaxRate = parseFloat(iepsTaxRate).toFixed(6)
665
+ cart.merged_tax_array[1].tax_rate = iepsTaxRate
666
+ }
667
+ return cart;
539
668
  }
540
669
 
541
670
  module.exports = { calculateTax2 }
package/index.js CHANGED
@@ -72,6 +72,75 @@ async function calculateTax(inputJSON) {
72
72
  })
73
73
  }
74
74
 
75
+ let json ={
76
+ "order_items": {
77
+ "item_details": [
78
+ {
79
+ "purchase_points": 100,
80
+ "total_amount": 100,
81
+ "item_price": 100,
82
+ "event": "point_purchase",
83
+ "order_id": "pos_order_id",
84
+ "item_display_text": "Rupay coin",
85
+ "quantity": 1,
86
+ "tax_id": []
87
+ }
88
+ ]
89
+ },
90
+ "tax_settings": [
91
+ {
92
+ "tax_id": "4e4ed8a8-756f-11ed-a2b4-42010a93300d",
93
+ "tax_label": "16% IVA",
94
+ "tax_rate": 16,
95
+ "tax_sequence": 1,
96
+ "base_price_effected": 1
97
+ },
98
+ {
99
+ "tax_id": "e51939ce-bd95-472a-a5e7-6302f262711c",
100
+ "tax_label": "CGST-10%",
101
+ "tax_rate": 10,
102
+ "tax_sequence": 3,
103
+ "base_price_effected": 0
104
+ },
105
+ {
106
+ "tax_id": "591f969b-3dd6-46cf-820c-de82bf1601f8",
107
+ "tax_label": "SGST-16%",
108
+ "tax_rate": 16,
109
+ "tax_sequence": 4,
110
+ "base_price_effected": 0
111
+ },
112
+ {
113
+ "tax_id": "843394f0-d85b-41c6-b561-2239a0011595",
114
+ "tax_label": "ROAD TAX-(%",
115
+ "tax_rate": 9,
116
+ "tax_sequence": 5,
117
+ "base_price_effected": 1
118
+ },
119
+ {
120
+ "tax_id": "2c856900-756f-11ed-a2b4-42010a93300d",
121
+ "tax_label": "CVAT-10%",
122
+ "tax_rate": 10,
123
+ "tax_sequence": 1,
124
+ "base_price_effected": 1
125
+ },
126
+ {
127
+ "tax_id": "e47296aa-b5ae-449f-ab5f-ff50f43a648a",
128
+ "tax_label": "0% Tax",
129
+ "tax_rate": 0,
130
+ "tax_sequence": 3,
131
+ "base_price_effected": 1
132
+ }
133
+ ],
134
+ "tax_type": 2,
135
+ "store_value": 0
136
+ }
137
+
138
+ calculateTax(json).then(res=>{
139
+ console.log(res)
140
+ }).catch(err=>{
141
+ console.log(err)
142
+ })
143
+
75
144
  module.exports = { calculateTax }
76
145
 
77
146
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Package for cart calculations in which it performs discount distribution and tax distribution on order",
5
5
  "main": "index.js",
6
6
  "scripts": {