foodbot-cart-calculations 1.0.66 → 1.0.67

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
@@ -61,7 +61,7 @@ function applyDistribution(body) {
61
61
  // let message = i18n.__("kiosk_point_message_redeemed").replace(/{totalAmount}/g, currency + "" + totalStoreValue).replace(/{paidPoints}/g, currency + "" + final.store_value)
62
62
  // final.points_redeem_message = message
63
63
  }
64
- taxCalculation.calculateTax2(final, taxSettings, taxType).then(taxData => {
64
+ taxCalculation.calculateTax2(final, taxSettings, taxType, body.isSplit).then(taxData => {
65
65
  taxData.sub_total = taxData.cartTotal;
66
66
  mainCart.final_amount = parseFloat(taxData.cartTotal);
67
67
  mainCart.cash_expected = parseFloat(taxData.cartTotal) ;
@@ -1,6 +1,8 @@
1
- function calculateTax2(cart, texSettings, taxType) {
1
+ var toFix = 2;
2
+ function calculateTax2(cart, texSettings, taxType, isSplit = false) {
2
3
  return new Promise(function (resolve, reject) {
3
4
  try {
5
+ toFix = isSplit ? 6 : 2;
4
6
  cart.cartTotal = 0;
5
7
  cart.tax_amount = 0;
6
8
  cart.item_details.forEach(function (v) {
@@ -44,15 +46,15 @@ function calculateTax2(cart, texSettings, taxType) {
44
46
  }
45
47
  promoDiscount = (promoDiscount > element.item_price * element.quantity) ? element.item_price * element.quantity : promoDiscount;
46
48
  element.promotion_discount = JSON.parse(JSON.stringify(promoDiscount));
47
- promoDiscount = parseFloat(promoDiscount).toFixed(6);
49
+ promoDiscount = parseFloat(promoDiscount).toFixed(toFix);
48
50
 
49
51
  let afterDiscount = element.item_price * element.quantity - promoDiscount;
50
- afterDiscount = parseFloat(afterDiscount).toFixed(6);
52
+ afterDiscount = parseFloat(afterDiscount).toFixed(toFix);
51
53
  afterDiscount = afterDiscount - pointsDiscount;
52
- afterDiscount = parseFloat(afterDiscount).toFixed(6);
54
+ afterDiscount = parseFloat(afterDiscount).toFixed(toFix);
53
55
  afterDiscount = (afterDiscount < 0) ? 0 : afterDiscount;
54
56
  cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDiscount);
55
- cart.cartTotal = parseFloat(cart.cartTotal).toFixed(6);
57
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(toFix);
56
58
  total = +element.item_price;
57
59
  if (element.isPackage == 1) {
58
60
  let result = await calculatePackageItemTax(cart, itemIndex, element, texSettings, afterDiscount, taxType);
@@ -75,7 +77,7 @@ function calculateTax2(cart, texSettings, taxType) {
75
77
  }
76
78
  if (itemIndex == cart.item_details.length - 1) {
77
79
  cart.cart_total_with_tax = parseFloat(cart.cartTotal) + parseFloat(cart.tax_amount);
78
- cart.cart_total_with_tax = parseFloat(cart.cart_total_with_tax).toFixed(6);
80
+ cart.cart_total_with_tax = parseFloat(cart.cart_total_with_tax).toFixed(toFix);
79
81
 
80
82
  //delay
81
83
  await new Promise((resolve) => {
@@ -113,6 +115,7 @@ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,
113
115
  let iepsTaxRateQuantity = +resp.iepsTaxRateQuantity;
114
116
  let itemTotalTax = 0;
115
117
  let tax_array = []
118
+ console.log("isSplit", toFix)
116
119
  if (itemTaxs && itemTaxs.length > 0) {
117
120
  itemTaxs.forEach(async (itemTaxArray, itemTaxIndex) => {
118
121
  // let startItemTax = JSON.parse(JSON.stringify(itemTaxArray))
@@ -125,13 +128,13 @@ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,
125
128
  // } else {
126
129
  // taxAmount = (lastPrice * startItemTax.tax_rate) / 100;
127
130
  // }
128
- // taxAmount = parseFloat(taxAmount).toFixed(6)
129
- // itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(6)
131
+ // taxAmount = parseFloat(taxAmount).toFixed(toFix)
132
+ // itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(toFix)
130
133
 
131
134
  // cart.tax_amount = parseFloat(cart.tax_amount) + parseFloat(taxAmount)
132
- // cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
133
- // startItemTax.base_price = parseFloat(lastPrice).toFixed(6)
134
- // startItemTax.tax_amount = parseFloat(taxAmount).toFixed(6)
135
+ // cart.tax_amount = parseFloat(cart.tax_amount).toFixed(toFix)
136
+ // startItemTax.base_price = parseFloat(lastPrice).toFixed(toFix)
137
+ // startItemTax.tax_amount = parseFloat(taxAmount).toFixed(toFix)
135
138
  // element.tax_array.push(JSON.parse(JSON.stringify(startItemTax)))
136
139
 
137
140
 
@@ -186,13 +189,13 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
186
189
  let defaultComboDiscount = packageItem.default_combo_discount ? packageItem.default_combo_discount : 0
187
190
  let afterDicsountPackage = packageItem.price * packageItem.quantity - promoDiscountPackage
188
191
 
189
- afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(6)
192
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(toFix)
190
193
  afterDicsountPackage = afterDicsountPackage - pointsDisouuntPackage
191
194
 
192
- afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(6)
195
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(toFix)
193
196
  afterDicsountPackage = afterDicsountPackage - defaultComboDiscount
194
197
 
195
- afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(6)
198
+ afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(toFix)
196
199
 
197
200
  getTaxSettings2(packageItem.tax_id, tax_settings, packageItem.ieps_tax).then(async (itemTaxs) => {
198
201
 
@@ -292,15 +295,15 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
292
295
  modi.tax_id = taxId
293
296
  let promoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0
294
297
  let pointsDisouunt = (modi.points_discount) ? modi.points_discount : 0
295
- promoDiscount = parseFloat(promoDiscount).toFixed(6)
298
+ promoDiscount = parseFloat(promoDiscount).toFixed(toFix)
296
299
  let afterDicsount = (modi.modifier_item_price * modi.quantity) * element.quantity - promoDiscount
297
300
  afterDicsount = afterDicsount - pointsDisouunt
298
- afterDicsount = parseFloat(afterDicsount).toFixed(6)
301
+ afterDicsount = parseFloat(afterDicsount).toFixed(toFix)
299
302
 
300
303
  afterDicsount = (afterDicsount < 0) ? 0 : afterDicsount
301
304
 
302
305
  cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDicsount)
303
- cart.cartTotal = parseFloat(cart.cartTotal).toFixed(6)
306
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(toFix)
304
307
 
305
308
  getTaxSettings2(taxId, tax_settings, iepsTax).then(async (modiItemTaxs) => {
306
309
 
@@ -412,12 +415,12 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
412
415
  let pointsDisouunt = (modi.points_discount) ? modi.points_discount : 0
413
416
  let afterDicsount = (modi.modifier_item_price * modi.quantity) * element.quantity - promoDiscount
414
417
  afterDicsount = afterDicsount - pointsDisouunt
415
- afterDicsount = parseFloat(afterDicsount).toFixed(6)
418
+ afterDicsount = parseFloat(afterDicsount).toFixed(toFix)
416
419
 
417
420
  afterDicsount = (afterDicsount < 0) ? 0 : afterDicsount
418
421
 
419
422
  cart.cartTotal = parseFloat(cart.cartTotal) + parseFloat(afterDicsount)
420
- cart.cartTotal = parseFloat(cart.cartTotal).toFixed(6)
423
+ cart.cartTotal = parseFloat(cart.cartTotal).toFixed(toFix)
421
424
 
422
425
  getTaxSettings2(taxId, tax_settings, iepsTax).then(async (modiItemTaxs) => {
423
426
 
@@ -457,7 +460,7 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
457
460
  cart.tax_amount = parseFloat(cart.tax_amount).toFixed(6)
458
461
 
459
462
 
460
- // modiItemTax.base_price = that.taxType == 1 ? parseFloat(afterDicsount).toFixed(6) : (parseFloat(afterDicsount) - parseFloat(taxAmount)).toFixed(6)
463
+ // modiItemTax.base_price = that.taxType == 1 ? parseFloat(afterDicsount).toFixed(toFix) : (parseFloat(afterDicsount) - parseFloat(taxAmount)).toFixed(toFix)
461
464
  modiItemTaxArray.tax_amount = parseFloat(taxAmount).toFixed(6);
462
465
  modiItemTaxArray.base_price = modiItemTaxArray.ieps_type ? lastPriceIeps : lastPrice;
463
466
  modiItemTaxArray.tax_rate = modiItemTaxArray.ieps_type ? iepsTaxRate : modiItemTaxArray.tax_rate;
@@ -627,7 +630,7 @@ async function getBeforeTaxPriceOfItem(taxes, price, taxType, quantity) {
627
630
 
628
631
  // Calculate taxAmount based on tax type 1 = exclusive and 2 = inclusive
629
632
  if (taxType == 2) {
630
- beforeTax = (beforeTax / (1 + totalTax / 100)).toFixed(6);
633
+ beforeTax = (beforeTax / (1 + totalTax / 100)).toFixed(toFix);
631
634
  }
632
635
 
633
636
  // ieps tax type = 1 precentage 2 absoulte
@@ -636,20 +639,20 @@ async function getBeforeTaxPriceOfItem(taxes, price, taxType, quantity) {
636
639
  // Adjust beforeTax based on tax type
637
640
  if (beforeTax >= tax.tax_rate) {
638
641
  beforeTaxIeps = taxType == 1 ? beforeTax : beforeTax - tax.tax_rate * quantity;
639
- beforeTaxIeps = parseFloat(beforeTaxIeps).toFixed(6)
642
+ beforeTaxIeps = parseFloat(beforeTaxIeps).toFixed(toFix)
640
643
  iepsTaxRate = tax.tax_rate * quantity / beforeTaxIeps
641
644
  iepsTaxRateQuantity = tax.tax_rate * quantity
642
645
  iepsTaxRate = parseFloat(iepsTaxRate)
643
- iepsTaxAmount = (tax.tax_rate * quantity).toFixed(6);
646
+ iepsTaxAmount = (tax.tax_rate * quantity).toFixed(toFix);
644
647
  }
645
648
  }
646
649
  else if (tax.ieps_type && tax.ieps_type == 1) {
647
650
  beforeTaxIeps = beforeTax;
648
651
  iepsTaxRate = tax.tax_rate
649
- iepsTaxAmount = (firstAmount - beforeTax - taxAmount).toFixed(6);
652
+ iepsTaxAmount = (firstAmount - beforeTax - taxAmount).toFixed(toFix);
650
653
  }
651
654
  else {
652
- taxAmount = ((beforeTax * tax.tax_rate) / 100).toFixed(6);
655
+ taxAmount = ((beforeTax * tax.tax_rate) / 100).toFixed(toFix);
653
656
  }
654
657
  });
655
658
  resolve({ beforeTax, taxAmount, beforeTaxIeps, iepsTaxRate, iepsTaxAmount, iepsTaxRateQuantity });
@@ -787,9 +790,9 @@ async function calculateItemWiseSplit(cart, itemWiseItems) {
787
790
  }));
788
791
 
789
792
  // FINAL VALUES
790
- p.tax = totalTax.toFixed(6);
791
- p.amount = subtotal.toFixed(6);
792
- p.due = subtotal.toFixed(6);
793
+ p.tax = totalTax.toFixed(toFix);
794
+ p.amount = subtotal.toFixed(toFix);
795
+ p.due = subtotal.toFixed(toFix);
793
796
  }
794
797
  });
795
798
  resolve(cart);
@@ -956,17 +959,17 @@ async function calculateHalfSplit(cart, options = {}) {
956
959
  const remainingTax = (unpaidTotalTax - accumulatedTax);
957
960
  const remainingAmount = (unpaidTotalAmount - accumulatedAmount);
958
961
 
959
- p.tax = Number(remainingTax).toFixed(6);
960
- p.amount = Number(remainingAmount).toFixed(6);
962
+ p.tax = Number(remainingTax).toFixed(toFix);
963
+ p.amount = Number(remainingAmount).toFixed(toFix);
961
964
  p.due = p.amount;
962
965
  } else {
963
966
  // Non-last unpaid: assign precomputed per-person values (rounded to 2 decimals)
964
- const taxAmount = Number(Number(halfSplitTaxPerPerson).toFixed(6));
965
- const dueAmount = Number(Number(halfSplitTotalPerPerson).toFixed(6));
967
+ const taxAmount = Number(Number(halfSplitTaxPerPerson).toFixed(toFix));
968
+ const dueAmount = Number(Number(halfSplitTotalPerPerson).toFixed(toFix));
966
969
 
967
- p.tax = taxAmount.toFixed(6);
968
- p.amount = dueAmount.toFixed(6);
969
- p.due = dueAmount.toFixed(6);
970
+ p.tax = taxAmount.toFixed(toFix);
971
+ p.amount = dueAmount.toFixed(toFix);
972
+ p.due = dueAmount.toFixed(toFix);
970
973
 
971
974
  accumulatedTax += taxAmount;
972
975
  accumulatedAmount += dueAmount;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.66",
3
+ "version": "1.0.67",
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": {