foodbot-cart-calculations 1.0.66 → 1.0.68
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 +1 -1
- package/functions/taxCalculation.js +141 -108
- package/package.json +1 -1
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
|
-
|
|
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(
|
|
49
|
+
promoDiscount = parseFloat(promoDiscount).toFixed(toFix);
|
|
48
50
|
|
|
49
51
|
let afterDiscount = element.item_price * element.quantity - promoDiscount;
|
|
50
|
-
afterDiscount = parseFloat(afterDiscount).toFixed(
|
|
52
|
+
afterDiscount = parseFloat(afterDiscount).toFixed(toFix);
|
|
51
53
|
afterDiscount = afterDiscount - pointsDiscount;
|
|
52
|
-
afterDiscount = parseFloat(afterDiscount).toFixed(
|
|
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(
|
|
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(
|
|
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(
|
|
129
|
-
// itemTotalTax = (parseFloat(itemTotalTax) + parseFloat(taxAmount)).toFixed(
|
|
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(
|
|
133
|
-
// startItemTax.base_price = parseFloat(lastPrice).toFixed(
|
|
134
|
-
// startItemTax.tax_amount = parseFloat(taxAmount).toFixed(
|
|
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(
|
|
192
|
+
afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(toFix)
|
|
190
193
|
afterDicsountPackage = afterDicsountPackage - pointsDisouuntPackage
|
|
191
194
|
|
|
192
|
-
afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(
|
|
195
|
+
afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(toFix)
|
|
193
196
|
afterDicsountPackage = afterDicsountPackage - defaultComboDiscount
|
|
194
197
|
|
|
195
|
-
afterDicsountPackage = parseFloat(afterDicsountPackage).toFixed(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
652
|
+
iepsTaxAmount = (firstAmount - beforeTax - taxAmount).toFixed(toFix);
|
|
650
653
|
}
|
|
651
654
|
else {
|
|
652
|
-
taxAmount = ((beforeTax * tax.tax_rate) / 100).toFixed(
|
|
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(
|
|
791
|
-
p.amount = subtotal.toFixed(
|
|
792
|
-
p.due = subtotal.toFixed(
|
|
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);
|
|
@@ -826,172 +829,202 @@ async function calculateItemWiseSplit(cart, itemWiseItems) {
|
|
|
826
829
|
async function calculateHalfSplit(cart, options = {}) {
|
|
827
830
|
return new Promise(async (resolve, reject) => {
|
|
828
831
|
try {
|
|
829
|
-
// Safe getters / defaults
|
|
830
832
|
const itemWiseItems = options.itemWiseItems || [];
|
|
831
|
-
const allItems = options.allItems ||
|
|
833
|
+
const allItems = options.allItems || cart.items || [];
|
|
832
834
|
const splits = cart.split_payments || [];
|
|
833
835
|
|
|
834
836
|
// Totals
|
|
835
|
-
const totalAmount = parseFloat(
|
|
836
|
-
const totalTax = parseFloat(
|
|
837
|
+
const totalAmount = parseFloat(cart.after_discount ?? 0) || 0;
|
|
838
|
+
const totalTax = parseFloat(cart.tax_amount ?? 0) || 0;
|
|
837
839
|
|
|
838
|
-
//
|
|
840
|
+
// Paid amounts
|
|
839
841
|
const paidSplits = splits.filter((p) => p.paid_status);
|
|
840
|
-
const paidAmount = paidSplits.reduce((sum, p) => sum + (parseFloat(p.amount ||
|
|
841
|
-
const paidTax = paidSplits.reduce((sum, p) => sum + (parseFloat(p.tax ||
|
|
842
|
+
const paidAmount = paidSplits.reduce((sum, p) => sum + (parseFloat(p.amount || 0)), 0);
|
|
843
|
+
const paidTax = paidSplits.reduce((sum, p) => sum + (parseFloat(p.tax || 0)), 0);
|
|
842
844
|
|
|
843
845
|
// Unpaid totals
|
|
844
846
|
const unpaidTotalAmount = totalAmount - paidAmount;
|
|
845
847
|
const unpaidTotalTax = totalTax - paidTax;
|
|
846
848
|
|
|
847
|
-
// Unpaid split count
|
|
848
849
|
const unpaidSplits = splits.filter((p) => !p.paid_status);
|
|
849
|
-
const unpaidCount = unpaidSplits.length || 1;
|
|
850
|
+
const unpaidCount = unpaidSplits.length || 1;
|
|
850
851
|
|
|
851
|
-
//
|
|
852
|
-
let halfSplitTaxPerPerson =
|
|
853
|
-
|
|
854
|
-
|
|
852
|
+
// per-person distribution
|
|
853
|
+
let halfSplitTaxPerPerson =
|
|
854
|
+
typeof options.halfSplitTaxPerPerson == "number"
|
|
855
|
+
? options.halfSplitTaxPerPerson
|
|
856
|
+
: unpaidTotalTax / unpaidCount;
|
|
855
857
|
|
|
856
|
-
let halfSplitTotalPerPerson =
|
|
857
|
-
|
|
858
|
-
|
|
858
|
+
let halfSplitTotalPerPerson =
|
|
859
|
+
typeof options.halfSplitTotalPerPerson == "number"
|
|
860
|
+
? options.halfSplitTotalPerPerson
|
|
861
|
+
: unpaidTotalAmount / unpaidCount;
|
|
859
862
|
|
|
860
|
-
// Keep accumulators for rounding remainder
|
|
861
863
|
let accumulatedAmount = 0;
|
|
862
864
|
let accumulatedTax = 0;
|
|
863
865
|
let unpaidIndex = 0;
|
|
864
866
|
|
|
865
|
-
// For each split payment: assign items, compute item-level taxes & subtotal, then apply split rounding rules
|
|
866
867
|
for (let p of splits) {
|
|
867
868
|
if (!p.paid_status) {
|
|
868
|
-
const isLastUnpaid = unpaidIndex
|
|
869
|
+
const isLastUnpaid = unpaidIndex === unpaidCount - 1;
|
|
869
870
|
|
|
870
|
-
//
|
|
871
|
+
// Clone all items for this split user
|
|
871
872
|
p.items = JSON.parse(JSON.stringify(allItems));
|
|
872
873
|
|
|
873
|
-
//
|
|
874
|
+
// --------------------------------------------------------
|
|
875
|
+
// 🔥 ADDED LOGIC — Adjust quantities for the LAST UNPAID customer
|
|
876
|
+
// --------------------------------------------------------
|
|
877
|
+
if (isLastUnpaid) {
|
|
878
|
+
p.items.forEach((item) => {
|
|
879
|
+
const origin = itemWiseItems.find((it) => it._temp_id === item._temp_id);
|
|
880
|
+
if (!origin) return;
|
|
881
|
+
|
|
882
|
+
// 1. Compute how many qty PAID customers already took
|
|
883
|
+
let paidQty = 0;
|
|
884
|
+
splits.forEach((sp) => {
|
|
885
|
+
if (sp.paid_status && sp.items) {
|
|
886
|
+
const paidItem = sp.items.find((pi) => pi._temp_id === item._temp_id);
|
|
887
|
+
if (paidItem) paidQty += parseFloat(paidItem.quantity || 0);
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
// 2. Remaining qty for unpaid
|
|
892
|
+
const remainingQty = origin.quantity - paidQty;
|
|
893
|
+
|
|
894
|
+
// 3. Quantity each unpaid gets
|
|
895
|
+
const qtyPerUnpaid = parseFloat((remainingQty / unpaidCount).toFixed(6));
|
|
896
|
+
|
|
897
|
+
// 4. All other unpaid customers qty allocated
|
|
898
|
+
const allocatedToOthers = qtyPerUnpaid * (unpaidCount - 1);
|
|
899
|
+
|
|
900
|
+
// 5. FINAL remainder → assign to *last unpaid customer*
|
|
901
|
+
const finalQty = parseFloat((remainingQty - allocatedToOthers).toFixed(6));
|
|
902
|
+
|
|
903
|
+
item.quantity = finalQty;
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// --------------------------------------------------------
|
|
908
|
+
// RECOMPUTE subtotal + taxes with UPDATED quantities
|
|
909
|
+
// --------------------------------------------------------
|
|
874
910
|
let subtotal = 0;
|
|
875
911
|
let tax = 0;
|
|
876
912
|
|
|
877
913
|
for (let pi of p.items) {
|
|
878
|
-
const item = itemWiseItems.find((i) => i._temp_id
|
|
914
|
+
const item = itemWiseItems.find((i) => i._temp_id === pi._temp_id);
|
|
879
915
|
if (!item) continue;
|
|
880
916
|
|
|
881
917
|
const qty = Number(pi.quantity || 0);
|
|
882
918
|
const price = Number(item.per_item_cost || 0);
|
|
883
919
|
const itemTaxTotal = Number(item.total_tax_amount || 0);
|
|
884
920
|
|
|
885
|
-
// quantity-wise tax array
|
|
886
921
|
const quantityWiseTaxArray = (item.tax_array || []).map((taxObj) => {
|
|
887
|
-
const unitBase = Number(taxObj.base_price
|
|
888
|
-
const
|
|
922
|
+
const unitBase = Number(taxObj.base_price) / (item.quantity || 1);
|
|
923
|
+
const unitTax = Number(taxObj.tax_amount) / (item.quantity || 1);
|
|
924
|
+
|
|
889
925
|
return {
|
|
890
926
|
...taxObj,
|
|
891
|
-
base_price: (unitBase * qty).toFixed(
|
|
892
|
-
tax_amount: (
|
|
927
|
+
base_price: (unitBase * qty).toFixed(toFix),
|
|
928
|
+
tax_amount: (unitTax * qty).toFixed(toFix),
|
|
893
929
|
};
|
|
894
930
|
});
|
|
895
931
|
|
|
896
|
-
// modifiers
|
|
932
|
+
// modifiers tax
|
|
897
933
|
let modifierTaxArray = [];
|
|
898
934
|
const mods = item.item_modifiers || item.modifiers || [];
|
|
899
|
-
|
|
935
|
+
|
|
936
|
+
mods.forEach((m) => {
|
|
900
937
|
const mq = Number(m.quantity || 0) || 1;
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
const
|
|
938
|
+
|
|
939
|
+
const modTax = m.tax_array.map((tx) => {
|
|
940
|
+
const unitBase = Number(tx.base_price) / (item.quantity * mq);
|
|
941
|
+
const unitTax = Number(tx.tax_amount) / (item.quantity * mq);
|
|
942
|
+
|
|
904
943
|
return {
|
|
905
944
|
...tx,
|
|
906
|
-
base_price: (unitBase * (mq * qty)).toFixed(
|
|
907
|
-
tax_amount: (unitTax * (mq * qty)).toFixed(
|
|
945
|
+
base_price: (unitBase * (mq * qty)).toFixed(toFix),
|
|
946
|
+
tax_amount: (unitTax * (mq * qty)).toFixed(toFix),
|
|
908
947
|
};
|
|
909
948
|
});
|
|
949
|
+
|
|
910
950
|
modifierTaxArray.push(...modTax);
|
|
911
|
-
}
|
|
951
|
+
});
|
|
912
952
|
|
|
913
|
-
//
|
|
953
|
+
// Merge item taxes
|
|
914
954
|
const mergedItemTaxMap = {};
|
|
915
|
-
|
|
955
|
+
[...quantityWiseTaxArray, ...modifierTaxArray].forEach((t) => {
|
|
916
956
|
if (!mergedItemTaxMap[t.tax_id]) {
|
|
917
957
|
mergedItemTaxMap[t.tax_id] = { ...t, base_price: 0, tax_amount: 0 };
|
|
918
958
|
}
|
|
919
|
-
mergedItemTaxMap[t.tax_id].base_price += Number(t.base_price
|
|
920
|
-
mergedItemTaxMap[t.tax_id].tax_amount += Number(t.tax_amount
|
|
921
|
-
}
|
|
959
|
+
mergedItemTaxMap[t.tax_id].base_price += Number(t.base_price);
|
|
960
|
+
mergedItemTaxMap[t.tax_id].tax_amount += Number(t.tax_amount);
|
|
961
|
+
});
|
|
922
962
|
|
|
923
963
|
pi.tax_array = Object.values(mergedItemTaxMap).map((i) => ({
|
|
924
964
|
...i,
|
|
925
|
-
base_price: Number(i.base_price).toFixed(
|
|
926
|
-
tax_amount: Number(i.tax_amount).toFixed(
|
|
965
|
+
base_price: Number(i.base_price).toFixed(toFix),
|
|
966
|
+
tax_amount: Number(i.tax_amount).toFixed(toFix),
|
|
927
967
|
}));
|
|
928
968
|
|
|
929
|
-
// subtotal and tax contribution for this item in this split
|
|
930
969
|
subtotal += price * qty;
|
|
931
|
-
tax += (itemTaxTotal /
|
|
932
|
-
}
|
|
970
|
+
tax += (itemTaxTotal / item.quantity) * qty;
|
|
971
|
+
}
|
|
933
972
|
|
|
934
|
-
// Merge
|
|
973
|
+
// Merge taxes for whole split
|
|
935
974
|
const mergedTaxMap = {};
|
|
936
|
-
|
|
975
|
+
p.items.forEach((pi) => {
|
|
937
976
|
(pi.tax_array || []).forEach((t) => {
|
|
938
977
|
if (!mergedTaxMap[t.tax_id]) {
|
|
939
978
|
mergedTaxMap[t.tax_id] = { ...t, base_price: 0, tax_amount: 0 };
|
|
940
979
|
}
|
|
941
|
-
mergedTaxMap[t.tax_id].base_price += Number(t.base_price
|
|
942
|
-
mergedTaxMap[t.tax_id].tax_amount += Number(t.tax_amount
|
|
980
|
+
mergedTaxMap[t.tax_id].base_price += Number(t.base_price);
|
|
981
|
+
mergedTaxMap[t.tax_id].tax_amount += Number(t.tax_amount);
|
|
943
982
|
});
|
|
944
|
-
}
|
|
983
|
+
});
|
|
945
984
|
|
|
946
985
|
p.merge_tax_array = Object.values(mergedTaxMap).map((t) => ({
|
|
947
986
|
...t,
|
|
948
|
-
base_price: Number(t.base_price).toFixed(
|
|
949
|
-
tax_amount: Number(t.tax_amount).toFixed(
|
|
987
|
+
base_price: Number(t.base_price).toFixed(toFix),
|
|
988
|
+
tax_amount: Number(t.tax_amount).toFixed(toFix),
|
|
950
989
|
}));
|
|
951
990
|
|
|
952
|
-
//
|
|
991
|
+
// --------------------------------------------------------
|
|
992
|
+
// APPLY SPLIT AMOUNT + REMAINDER HANDLING
|
|
993
|
+
// --------------------------------------------------------
|
|
953
994
|
if (isLastUnpaid) {
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
const remainingTax = (unpaidTotalTax - accumulatedTax);
|
|
957
|
-
const remainingAmount = (unpaidTotalAmount - accumulatedAmount);
|
|
958
|
-
|
|
959
|
-
p.tax = Number(remainingTax).toFixed(6);
|
|
960
|
-
p.amount = Number(remainingAmount).toFixed(6);
|
|
995
|
+
p.tax = Number(unpaidTotalTax - accumulatedTax).toFixed(toFix);
|
|
996
|
+
p.amount = Number(unpaidTotalAmount - accumulatedAmount).toFixed(toFix);
|
|
961
997
|
p.due = p.amount;
|
|
962
998
|
} else {
|
|
963
|
-
|
|
964
|
-
const
|
|
965
|
-
const dueAmount = Number(Number(halfSplitTotalPerPerson).toFixed(6));
|
|
999
|
+
const taxAmount = Number(halfSplitTaxPerPerson).toFixed(toFix);
|
|
1000
|
+
const amountPer = Number(halfSplitTotalPerPerson).toFixed(toFix);
|
|
966
1001
|
|
|
967
|
-
p.tax = taxAmount
|
|
968
|
-
p.amount =
|
|
969
|
-
p.due =
|
|
1002
|
+
p.tax = taxAmount;
|
|
1003
|
+
p.amount = amountPer;
|
|
1004
|
+
p.due = amountPer;
|
|
970
1005
|
|
|
971
|
-
accumulatedTax += taxAmount;
|
|
972
|
-
accumulatedAmount +=
|
|
1006
|
+
accumulatedTax += Number(taxAmount);
|
|
1007
|
+
accumulatedAmount += Number(amountPer);
|
|
973
1008
|
}
|
|
974
1009
|
|
|
975
1010
|
unpaidIndex++;
|
|
976
|
-
}
|
|
977
|
-
} // end for splits loop
|
|
978
|
-
|
|
979
|
-
// Attach updated splits back to cart (if different structure)
|
|
980
|
-
if (cart.split_payments) {
|
|
981
|
-
cart.split_payments = splits;
|
|
1011
|
+
}
|
|
982
1012
|
}
|
|
1013
|
+
|
|
1014
|
+
cart.split_payments = splits;
|
|
983
1015
|
resolve(cart);
|
|
1016
|
+
|
|
984
1017
|
} catch (err) {
|
|
985
|
-
|
|
986
|
-
console.error("❌ Split Payment Calculation Failed:", err);
|
|
1018
|
+
console.error("❌ HalfSplit Calculation Error:", err);
|
|
987
1019
|
resolve({
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1020
|
+
status: "error",
|
|
1021
|
+
status_code: 500,
|
|
1022
|
+
message: "Internal server error",
|
|
1023
|
+
error: err.stack,
|
|
992
1024
|
});
|
|
993
1025
|
}
|
|
994
1026
|
});
|
|
995
1027
|
}
|
|
996
1028
|
|
|
1029
|
+
|
|
997
1030
|
module.exports = { calculateTax2, calculateItemWiseSplit, calculateHalfSplit }
|
package/package.json
CHANGED