foodbot-cart-calculations 1.0.3 → 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,10 +62,21 @@ 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)
@@ -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
  }
@@ -55,21 +55,21 @@ function calculateTax2(cart, texSettings, taxType) {
55
55
  cart.cartTotal = parseFloat(cart.cartTotal).toFixed(2);
56
56
  total = +element.item_price;
57
57
  if (element.isPackage == 1) {
58
- let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
58
+ let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
59
59
  cart = result.cart;
60
60
  element = result.element;
61
61
  } else {
62
- let result = await calculateItemTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
62
+ let result = await calculateItemTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
63
63
  cart = result.cart;
64
64
  element = result.element;
65
65
  }
66
66
 
67
67
  if (element.isPackage == 1) {
68
- let result = await calculatePackageModiTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
68
+ let result = await calculatePackageModiTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
69
69
  cart = result.cart;
70
70
  element = result.element;
71
71
  } else {
72
- let result = await calculateModiTax(cart, itemIndex, element, texSettings, afterDiscount,taxType);
72
+ let result = await calculateModiTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
73
73
  cart = result.cart;
74
74
  element = result.element;
75
75
  }
@@ -86,52 +86,86 @@ function calculateTax2(cart, texSettings, taxType) {
86
86
  });
87
87
  }
88
88
 
89
- function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
89
+ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
90
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
+ }
91
97
  getTaxSettings2(element.tax_id, tax_settings, element.ieps_tax).then(async (itemTaxs) => {
92
98
  let lastPrice = afterDicsount
93
- getBeforeTaxPriceOfItem(itemTaxs, lastPrice,taxType).then((resp) => {
94
- 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;
95
105
  let itemTotalTax = 0;
96
- if(itemTaxs && itemTaxs.length > 0){
106
+ let tax_array = []
107
+ if (itemTaxs && itemTaxs.length > 0) {
97
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)))
127
+
128
+
98
129
  let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
99
- if (!element.tax_array) {
100
- element.tax_array = []
101
- }
102
- let taxAmount = 0
103
- if (startItemTax.ieps_type && startItemTax.ieps_type == 2) {
104
- taxAmount = lastPrice >= startItemTax.tax_rate ? startItemTax.tax_rate : 0;
130
+ let taxAmount = 0;
131
+
132
+ if (startItemTax.ieps_type) {
133
+ taxAmount = iepsTaxAmount;
105
134
  } else {
106
- taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
135
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
136
+ taxAmount = resp.taxAmount;
107
137
  }
108
- taxAmount = parseFloat(taxAmount).toFixed(2)
109
- itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(2)
110
-
138
+ taxAmount = parseFloat(taxAmount).toFixed(6);
111
139
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
112
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
113
- startItemTax.base_price = parseFloat(lastPrice).toFixed(2)
114
- startItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
115
- element.tax_array.push(JSON.parse(JSON.stringify(startItemTax)))
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
+
116
149
  cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
117
-
150
+
118
151
  if (itemTaxIndex == itemTaxs.length - 1) {
119
152
  element.tax_amount = itemTotalTax;
120
- element.tax_amount = parseFloat(element.tax_amount).toFixed(2);
153
+ element.tax_array = tax_array;
154
+ element.tax_amount = parseFloat(element.tax_amount).toFixed(6);
121
155
  element.total_tax_amount = itemTotalTax;
122
- element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(2);
123
- element.tax_amount_modifier = parseFloat(itemTotalTax).toFixed(2);
156
+ element.total_tax_amount = parseFloat(element.total_tax_amount).toFixed(6);
157
+ element.tax_amount_modifier = parseFloat(itemTotalTax).toFixed(6);
124
158
  resolve({ 'cart': cart, "element": element });
125
159
  }
126
160
  });
127
- }else{
161
+ } else {
128
162
  resolve({ 'cart': cart, "element": element });
129
163
  }
130
164
  });
131
165
  })
132
166
  })
133
167
  }
134
- function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
168
+ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
135
169
  return new Promise(async (resolve, reject) => {
136
170
  let itemTotalTax = 0;
137
171
  element.package_items.forEach((packageItem, packageItemIndex) => {
@@ -160,49 +194,52 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
160
194
  let lastPrice = afterDicsountPackage
161
195
  let itemtaxAmount = 0;
162
196
 
163
- await getBeforeTaxPriceOfItem(itemTaxs, lastPrice,taxType).then(async (resp) => {
164
- lastPrice = +resp.beforeTax;
165
- if(itemTaxs && itemTaxs.length > 0){
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) {
166
205
  itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
167
206
 
168
207
  let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
169
-
170
- if (!element.package_items[packageItemIndex].tax_array) {
171
- element.package_items[packageItemIndex].tax_array = []
172
- }
173
-
174
-
208
+
175
209
  let taxAmount = 0
176
-
177
- if (startItemTax.ieps_type && startItemTax.ieps_type == 2) {
178
- taxAmount = lastPrice >= startItemTax.tax_rate ? startItemTax.tax_rate : 0;
210
+
211
+ if (startItemTax.ieps_type) {
212
+ taxAmount = iepsTaxAmount;
179
213
  } else {
180
- taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
214
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
215
+ taxAmount = resp.taxAmount;
181
216
  }
182
-
183
- taxAmount = parseFloat(taxAmount).toFixed(2)
184
- itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(2)
185
- itemtaxAmount = (parseFloat(itemtaxAmount) + parseFloat(taxAmount)).toFixed(2);
186
-
217
+
218
+ taxAmount = parseFloat(taxAmount).toFixed(6);
187
219
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
188
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
189
- startItemTax.base_price = parseFloat(lastPrice).toFixed(2)
190
- startItemTax.tax_amount = parseFloat(taxAmount).toFixed(2)
191
-
192
- element.package_items[packageItemIndex].tax_array.push(JSON.parse(JSON.stringify(startItemTax)));
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)));
193
228
  cart = await setTaxes(cart, JSON.parse(JSON.stringify(startItemTax)))
194
-
229
+
195
230
  //push array yo package
196
231
  element = await setPackageTaxes(element, JSON.parse(JSON.stringify(startItemTax)));
197
- element.package_items[packageItemIndex].tax_amount = parseFloat(itemtaxAmount).toFixed(2);
198
- element.package_items[packageItemIndex].total_tax_amount = parseFloat(itemtaxAmount).toFixed(2);
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
+
199
236
  // element.package_items[packageItemIndex].tax_amount_modifier = itemTotalTax
200
237
  });
201
238
  }
202
239
  });
203
240
 
204
- element.tax_amount = (parseFloat(element.tax_amount) + parseFloat(itemtaxAmount)).toFixed(2)
205
- 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)
206
243
  })
207
244
  if (packageItemIndex == element.package_items.length - 1) {
208
245
  element.tax_amount_modifier = 0;
@@ -211,21 +248,22 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
211
248
  })
212
249
  })
213
250
  }
214
- function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
251
+ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
215
252
  return new Promise(async (resolve, reject) => {
216
253
  if (element && element.item_modifiers && element.item_modifiers.length > 0) {
217
254
  let itemModiTotalTax = 0
218
255
  element.item_modifiers.forEach((modi, modiIndex) => {
219
256
  let taxId = []
220
- let iepsTax;
221
-
257
+ let iepsTax;
222
258
  if (modi.is_tax_rate_same == 1) {
223
259
  if (modi.linked_product && modi.linked_product.product_tax_id) {
224
260
  taxId = modi.linked_product.product_tax_id
225
- 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
226
263
  } else {
227
264
  taxId = element.tax_id
228
- iepsTax = []
265
+ if (element.ieps_tax && element.ieps_tax.ieps_details && element.ieps_tax.ieps_details.type == 1)
266
+ iepsTax = element.ieps_tax
229
267
  }
230
268
  }
231
269
  else {
@@ -253,8 +291,14 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
253
291
 
254
292
  let lastPrice = afterDicsount
255
293
  if (modiItemTaxs.length > 0) {
256
- await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice,taxType).then((resp) => {
257
- 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 = []
258
302
 
259
303
  modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
260
304
 
@@ -267,31 +311,37 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
267
311
 
268
312
  let taxAmount = 0
269
313
 
270
- if (modiItemTaxArray.ieps_type && modiItemTaxArray.ieps_type == 2) {
271
- taxAmount = lastPrice >= modiItemTaxArray.tax_rate ? modiItemTaxArray.tax_rate : 0;
314
+ if (modiItemTaxArray.ieps_type) {
315
+ taxAmount = iepsTaxAmount;
272
316
  } else {
273
- taxAmount = (lastPrice * modiItemTaxArray.tax_rate) / 100;
317
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
318
+ taxAmount = resp.taxAmount;
274
319
  }
275
320
 
276
- taxAmount = parseFloat(taxAmount).toFixed(2)
321
+ taxAmount = parseFloat(taxAmount).toFixed(6)
277
322
  itemModiTotalTax = itemModiTotalTax + taxAmount
278
323
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
279
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
324
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
280
325
 
281
- modiItemTaxArray.base_price = parseFloat(lastPrice).toFixed(2)
282
- 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)))
283
332
 
284
- element.item_modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTaxArray)))
333
+ element.item_modifiers[modiIndex].tax_array = tax_array;
285
334
  cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTaxArray)));
286
335
 
287
336
  element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount) + parseFloat(taxAmount);
288
- 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)
289
338
  element.tax_amount_modifier = parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)
290
- element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(2)
339
+ element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(6)
291
340
  element.total_tax_amount = parseFloat(element.total_tax_amount) + parseFloat(taxAmount)
292
- 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) {
293
344
 
294
- if (modiItemTaxs.length - 1 == modiTaxItemIndex) {
295
345
  resolve({ 'cart': cart, "element": element });
296
346
  }
297
347
 
@@ -300,7 +350,10 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
300
350
  })
301
351
  }
302
352
  else {
303
- resolve({ 'cart': cart, "element": element });
353
+ if (modiIndex == element.item_modifiers.length - 1) {
354
+
355
+ resolve({ 'cart': cart, "element": element });
356
+ }
304
357
  }
305
358
  })
306
359
  })
@@ -310,7 +363,7 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
310
363
  }
311
364
  })
312
365
  }
313
- function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount,taxType) {
366
+ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
314
367
  return new Promise(async (resolve, reject) => {
315
368
  element.package_items.forEach((packageItem, packageItemIndex) => {
316
369
  element.package_items[packageItemIndex].tax_amount_modifier = 0
@@ -354,10 +407,16 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
354
407
  let lastPrice = afterDicsount
355
408
 
356
409
  if (modiItemTaxs.length > 0) {
357
- await getBeforeTaxPriceOfItem(modiItemTaxs, lastPrice,taxType).then((resp) => {
358
- 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 = []
359
418
  modiItemTaxs.forEach(async (modiItemTax, modiTaxItemIndex) => {
360
-
419
+ let modiItemTaxArray = JSON.parse(JSON.stringify(modiItemTax))
361
420
  if (!element.package_items[packageItemIndex].modifiers[modiIndex].tax_array) {
362
421
  element.package_items[packageItemIndex].modifiers[modiIndex].tax_array = []
363
422
  }
@@ -368,40 +427,49 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
368
427
 
369
428
  let taxAmount = 0
370
429
 
371
- if (modiItemTax.ieps_type && modiItemTax.ieps_type == 2) {
372
- taxAmount = lastPrice >= modiItemTax.tax_rate ? modiItemTax.tax_rate : 0;
430
+ if (modiItemTaxArray.ieps_type) {
431
+ taxAmount = iepsTaxAmount;
373
432
  } else {
374
- taxAmount = (lastPrice * modiItemTax.tax_rate) / 100;
433
+ // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
434
+ taxAmount = resp.taxAmount;
375
435
  }
376
- taxAmount = parseFloat(taxAmount).toFixed(2)
436
+ taxAmount = parseFloat(taxAmount).toFixed(6)
377
437
 
378
438
  itemModiTotalTax = itemModiTotalTax + taxAmount;
379
439
  cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
380
- cart.tax_amount = parseFloat(cart.tax_amount).toFixed(2)
440
+ cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
381
441
 
382
442
 
383
443
  // modiItemTax.base_price = that.taxType == 1 ? parseFloat(afterDicsount).toFixed(2) : (parseFloat(afterDicsount) - parseFloat(taxAmount)).toFixed(2)
384
- modiItemTax.base_price = lastPrice
385
- 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)))
386
449
 
387
450
  // cart[itemIndex].item_modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTax)))
388
- element.package_items[packageItemIndex].modifiers[modiIndex].tax_array.push(JSON.parse(JSON.stringify(modiItemTax)))
389
- 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)))
390
453
 
391
454
  element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount = parseFloat(element.package_items[packageItemIndex].modifiers[modiIndex].tax_amount) + parseFloat(taxAmount)
392
- element.package_items[packageItemIndex].tax_amount_modifier = (parseFloat(element.package_items[packageItemIndex].tax_amount_modifier) + parseFloat(taxAmount)).toFixed(2)
393
- element.package_items[packageItemIndex].total_tax_amount = (parseFloat(element.package_items[packageItemIndex].total_tax_amount) + parseFloat(taxAmount)).toFixed(2)
394
- element.tax_amount_modifier = (parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)).toFixed(2)
395
- 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) {
396
461
 
397
- if (modiItemTaxs.length - 1 == modiTaxItemIndex)
398
462
  resolve({ 'cart': cart, "element": element });
463
+ }
399
464
 
400
465
  });
401
466
  })
402
467
  }
403
468
  else {
404
- resolve({ 'cart': cart, "element": element });
469
+ if (modiIndex == packageItem.modifiers.length - 1) {
470
+
471
+ resolve({ 'cart': cart, "element": element });
472
+ }
405
473
  }
406
474
 
407
475
 
@@ -415,21 +483,39 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
415
483
  });
416
484
  })
417
485
  }
418
- function setPackageTaxes(item, tax_array) {
486
+ function setPackageTaxes(item, ele) {
419
487
  return new Promise(async (resolve, reject) => {
420
488
  let index = -1;
421
-
422
- if (tax_array.ieps_type) {
423
- 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
+ });
424
493
  } else {
425
- 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
+ });
426
497
  }
427
-
428
- if (index !== -1) {
429
- item.tax_array[index].tax_amount = (parseFloat(item.tax_array[index].tax_amount) + parseFloat(tax_array.tax_amount)).toFixed(2);
430
- item.tax_array[index].base_price = (parseFloat(item.tax_array[index].base_price) + parseFloat(tax_array.base_price)).toFixed(2);
431
- } else {
432
- 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
433
519
  }
434
520
  resolve(item);
435
521
  })
@@ -441,33 +527,23 @@ function getTaxSettings2(itemTax, taxSettings, iepsTax = '') {
441
527
  let settings = [];
442
528
  let taxes = [];
443
529
 
444
- if (itemTax && itemTax !== undefined) {
445
- itemTax = !isJson(itemTax) ? itemTax : JSON.parse(itemTax);
530
+ if (itemTax && itemTax != undefined) {
446
531
  taxes = [...itemTax];
447
-
448
532
  // Handle ieps tax
449
533
  if (iepsTax && iepsTax.status && iepsTax.status == 1 && iepsTax.ieps_details) {
450
534
  if (iepsTax.ieps_details.type == 1) {
451
535
  taxes.push(iepsTax.ieps_details);
452
- } else {
453
- const absoluteTax = {
454
- base_price_effected: 0,
455
- tax_sequence: 1,
456
- tax_label: 'IEPS',
457
- ieps_type: 2,
458
- tax_id: iepsTax.ieps_details.tax_id,
459
- tax_rate: iepsTax.ieps_details.tax_id,
460
- };
461
- settings.push(absoluteTax);
462
536
  }
463
537
  }
464
538
 
465
- taxes.forEach((element) => {
539
+ taxes.forEach(function (element) {
466
540
  count++;
467
- 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
+ });
468
544
 
469
545
  if (index >= 0) {
470
- const taxDetail = { ...taxSettings[index] };
546
+ const taxDetail = Object.assign({}, taxSettings[index]);
471
547
  if (element.type) {
472
548
  taxDetail.tax_label = 'IEPS';
473
549
  taxDetail.ieps_type = 1;
@@ -476,7 +552,21 @@ function getTaxSettings2(itemTax, taxSettings, iepsTax = '') {
476
552
  }
477
553
 
478
554
  if (count == taxes.length) {
479
- 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);
480
570
  resolve(settings);
481
571
  }
482
572
  });
@@ -495,50 +585,86 @@ function isJson(str) {
495
585
  return true;
496
586
  }
497
587
 
498
- async function getBeforeTaxPriceOfItem(taxes, price,taxType) {
588
+ async function getBeforeTaxPriceOfItem(taxes, price, taxType, qty) {
499
589
  return new Promise((resolve, reject) => {
500
- let beforeTax = parseFloat(price);
590
+ let beforeTax = +price;
591
+ let beforeTaxIeps = 0;
501
592
  let taxRate = 0;
502
593
  let taxAmount = 0;
503
- let itemPriceForTaxCal = 0;
594
+ let iepsTaxRate = 0;
595
+ let iepsTaxAmount = 0;
596
+ let iepsTaxRateQuantity = 0;
504
597
 
505
598
  taxes.forEach((tax) => {
506
- if (tax.ieps_type && tax.ieps_type == 2) {
507
- // Adjust beforeTax based on tax type
508
- if (beforeTax >= tax.tax_rate) {
509
- itemPriceForTaxCal = (taxType == 1) ? beforeTax + tax.tax_rate : beforeTax - tax.tax_rate;
510
- }
511
- } else {
599
+ if (!tax.ieps_type) {
512
600
  taxRate += parseFloat(tax.tax_rate);
513
601
  }
514
602
  });
515
603
 
516
604
  // Calculate taxAmount based on tax type 1 = exclusive and 2 = inclusive
517
605
  if (taxType == 1) {
518
- taxAmount = itemPriceForTaxCal == 0 ? ((beforeTax * taxRate) / 100).toFixed(6) : ((itemPriceForTaxCal * taxRate) / 100).toFixed(6);
606
+ taxAmount = ((beforeTax * taxRate) / 100).toFixed(6);
519
607
  } else {
520
- beforeTax = itemPriceForTaxCal == 0 ? (beforeTax / parseFloat(1 + taxRate / 100)).toFixed(6) : (itemPriceForTaxCal / (1 + taxRate / 100)).toFixed(6);
521
- 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);
522
610
  }
523
- 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 });
524
632
  });
525
633
  }
526
- function setTaxes(cart, tax_array) {
527
- return new Promise(async (resolve, reject) => {
528
- let index = cart.merged_tax_array.findIndex((res) => res.tax_id == tax_array.tax_id)
529
- if (index != -1) {
530
- cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount) + parseFloat(tax_array.tax_amount)
531
- cart.merged_tax_array[index].tax_amount = parseFloat(cart.merged_tax_array[index].tax_amount).toFixed(2)
532
-
533
- cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price) + parseFloat(tax_array.base_price)
534
- cart.merged_tax_array[index].base_price = parseFloat(cart.merged_tax_array[index].base_price).toFixed(2)
535
-
536
- }
537
- else {
538
- cart.merged_tax_array.push(tax_array)
539
- }
540
- 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
541
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;
542
668
  }
543
669
 
544
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.3",
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": {