foodbot-cart-calculations 1.0.61 → 1.0.62
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/functions/promotionCalculation.js +0 -1
- package/functions/taxCalculation.js +161 -42
- package/index.js +21 -1
- package/package.json +1 -1
|
@@ -147,7 +147,6 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
147
147
|
if(!res.purchase_points){
|
|
148
148
|
let itemId = res.item_id?.toString().length > 10 ? res.item_id : parseInt(res.item_id);
|
|
149
149
|
if (!res.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(itemId) && res.isPackage == 0) || (offer.offer_products.package_items.includes(itemId) && res.isPackage == 1))) {
|
|
150
|
-
// let ItemPriceWithMod = res.item_price_with_modifier ? res.item_price_with_modifier : (res.item_price || res.item_cost)
|
|
151
150
|
let ItemPriceWithMod = res.item_price_with_modifier ?? res.item_price ?? res.item_cost;
|
|
152
151
|
foundItemsSum = foundItemsSum + ItemPriceWithMod * res.quantity
|
|
153
152
|
return foundItemsSum
|
|
@@ -76,17 +76,18 @@ function calculateTax2(cart, texSettings, taxType) {
|
|
|
76
76
|
if (itemIndex == cart.item_details.length - 1) {
|
|
77
77
|
cart.cart_total_with_tax = parseFloat(cart.cartTotal) + parseFloat(cart.tax_amount);
|
|
78
78
|
cart.cart_total_with_tax = parseFloat(cart.cart_total_with_tax).toFixed(2);
|
|
79
|
-
|
|
79
|
+
|
|
80
|
+
//delay
|
|
80
81
|
await new Promise((resolve) => {
|
|
81
82
|
setTimeout(async () => {
|
|
82
83
|
resolve(); // Resolve after processing all items
|
|
83
|
-
}, 300)
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
}, 300)
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
calculateItemWiseSplit(cart).then((cart) => {
|
|
86
88
|
resolve(cart);
|
|
87
|
-
|
|
89
|
+
});
|
|
88
90
|
}
|
|
89
|
-
|
|
90
91
|
});
|
|
91
92
|
} catch (error) {
|
|
92
93
|
reject(error);
|
|
@@ -173,6 +174,7 @@ function calculateItemTax(cart, itemIndex, element, tax_settings, afterDicsount,
|
|
|
173
174
|
})
|
|
174
175
|
})
|
|
175
176
|
}
|
|
177
|
+
|
|
176
178
|
function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
|
|
177
179
|
return new Promise(async (resolve, reject) => {
|
|
178
180
|
let itemTotalTax = 0;
|
|
@@ -255,14 +257,15 @@ function calculatePackageItemTax(cart, itemIndex, element, tax_settings, afterDi
|
|
|
255
257
|
})
|
|
256
258
|
})
|
|
257
259
|
}
|
|
260
|
+
|
|
258
261
|
function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
|
|
259
262
|
return new Promise(async (resolve, reject) => {
|
|
260
263
|
if (element && element.item_modifiers && element.item_modifiers.length > 0) {
|
|
261
|
-
|
|
264
|
+
|
|
262
265
|
element.item_modifiers.forEach((modi, modiIndex) => {
|
|
263
266
|
let itemModiTotalTax = 0
|
|
264
267
|
let taxId = []
|
|
265
|
-
let iepsTax;
|
|
268
|
+
let iepsTax;
|
|
266
269
|
//code commented for test
|
|
267
270
|
|
|
268
271
|
//code gain commented for push
|
|
@@ -345,7 +348,7 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
|
|
|
345
348
|
element.item_modifiers[modiIndex].tax_array = tax_array;
|
|
346
349
|
cart = await setTaxes(cart, JSON.parse(JSON.stringify(modiItemTaxArray)));
|
|
347
350
|
|
|
348
|
-
element.item_modifiers[modiIndex].tax_amount =
|
|
351
|
+
element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount) + parseFloat(taxAmount);
|
|
349
352
|
element.item_modifiers[modiIndex].tax_amount = parseFloat(element.item_modifiers[modiIndex].tax_amount).toFixed(6)
|
|
350
353
|
element.tax_amount_modifier = parseFloat(element.tax_amount_modifier) + parseFloat(taxAmount)
|
|
351
354
|
element.tax_amount_modifier = parseFloat(element.tax_amount_modifier).toFixed(6)
|
|
@@ -375,12 +378,13 @@ function calculateModiTax(cart, itemIndex, element, tax_settings, afterDicsount,
|
|
|
375
378
|
}
|
|
376
379
|
})
|
|
377
380
|
}
|
|
381
|
+
|
|
378
382
|
function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDicsount, taxType) {
|
|
379
383
|
return new Promise(async (resolve, reject) => {
|
|
380
384
|
element.package_items.forEach((packageItem, packageItemIndex) => {
|
|
381
385
|
element.package_items[packageItemIndex].tax_amount_modifier = 0
|
|
382
386
|
if (packageItem.modifiers.length > 0) {
|
|
383
|
-
|
|
387
|
+
|
|
384
388
|
packageItem.modifiers.forEach((modi, modiIndex) => {
|
|
385
389
|
let itemModiTotalTax = 0;
|
|
386
390
|
let taxId = []
|
|
@@ -496,6 +500,7 @@ function calculatePackageModiTax(cart, itemIndex, element, tax_settings, afterDi
|
|
|
496
500
|
});
|
|
497
501
|
})
|
|
498
502
|
}
|
|
503
|
+
|
|
499
504
|
function setPackageTaxes(item, ele) {
|
|
500
505
|
return new Promise(async (resolve, reject) => {
|
|
501
506
|
let index = -1;
|
|
@@ -605,49 +610,49 @@ async function getBeforeTaxPriceOfItem(taxes, price, taxType, quantity) {
|
|
|
605
610
|
let taxRate = 0;
|
|
606
611
|
let taxAmount = 0;
|
|
607
612
|
let iepsTaxRate = 0;
|
|
608
|
-
let iepsTaxAmount=0;
|
|
613
|
+
let iepsTaxAmount = 0;
|
|
609
614
|
let iepsTaxRateQuantity = 0;
|
|
610
|
-
let totalTax
|
|
611
|
-
let firstAmount = beforeTax
|
|
615
|
+
let totalTax = 0;
|
|
616
|
+
let firstAmount = beforeTax;
|
|
612
617
|
|
|
613
618
|
taxes.forEach((tax) => {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
619
|
+
if (!tax.ieps_type) {
|
|
620
|
+
taxRate += parseFloat(tax.tax_rate);
|
|
621
|
+
totalTax += parseFloat(tax.tax_rate);
|
|
622
|
+
}
|
|
623
|
+
if (tax.ieps_type && tax.ieps_type == 1) {
|
|
624
|
+
totalTax += parseFloat(tax.tax_rate);
|
|
625
|
+
}
|
|
621
626
|
});
|
|
622
627
|
|
|
623
628
|
// Calculate taxAmount based on tax type 1 = exclusive and 2 = inclusive
|
|
624
629
|
if (taxType == 2) {
|
|
625
|
-
|
|
630
|
+
beforeTax = (beforeTax / (1 + totalTax / 100)).toFixed(2);
|
|
626
631
|
}
|
|
627
632
|
|
|
628
633
|
// ieps tax type = 1 precentage 2 absoulte
|
|
629
634
|
taxes.forEach((tax) => {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
else if (tax.ieps_type && tax.ieps_type == 1) {
|
|
642
|
-
beforeTaxIeps = beforeTax ;
|
|
643
|
-
iepsTaxRate = tax.tax_rate
|
|
644
|
-
iepsTaxAmount = (firstAmount - beforeTax - taxAmount).toFixed(2);
|
|
635
|
+
if (tax.ieps_type && tax.ieps_type == 2) {
|
|
636
|
+
// Adjust beforeTax based on tax type
|
|
637
|
+
if (beforeTax >= tax.tax_rate) {
|
|
638
|
+
beforeTaxIeps = taxType == 1 ? beforeTax : beforeTax - tax.tax_rate * quantity;
|
|
639
|
+
beforeTaxIeps = parseFloat(beforeTaxIeps).toFixed(2)
|
|
640
|
+
iepsTaxRate = tax.tax_rate * quantity / beforeTaxIeps
|
|
641
|
+
iepsTaxRateQuantity = tax.tax_rate * quantity
|
|
642
|
+
iepsTaxRate = parseFloat(iepsTaxRate)
|
|
643
|
+
iepsTaxAmount = (tax.tax_rate * quantity).toFixed(2);
|
|
645
644
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
645
|
+
}
|
|
646
|
+
else if (tax.ieps_type && tax.ieps_type == 1) {
|
|
647
|
+
beforeTaxIeps = beforeTax;
|
|
648
|
+
iepsTaxRate = tax.tax_rate
|
|
649
|
+
iepsTaxAmount = (firstAmount - beforeTax - taxAmount).toFixed(2);
|
|
650
|
+
}
|
|
651
|
+
else {
|
|
652
|
+
taxAmount = ((beforeTax * tax.tax_rate) / 100).toFixed(2);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
resolve({ beforeTax, taxAmount, beforeTaxIeps, iepsTaxRate, iepsTaxAmount, iepsTaxRateQuantity });
|
|
651
656
|
});
|
|
652
657
|
}
|
|
653
658
|
|
|
@@ -687,4 +692,118 @@ function setTaxes(cart, ele) {
|
|
|
687
692
|
return cart;
|
|
688
693
|
}
|
|
689
694
|
|
|
690
|
-
|
|
695
|
+
// 📌 calculateItemWiseSplit.js
|
|
696
|
+
|
|
697
|
+
async function calculateItemWiseSplit(cart, itemWiseItems) {
|
|
698
|
+
return new Promise(async (resolve, reject) => {
|
|
699
|
+
try {
|
|
700
|
+
cart.split_payments.forEach((p) => {
|
|
701
|
+
if (!p.paid_status) {
|
|
702
|
+
let subtotal = 0;
|
|
703
|
+
let totalTax = 0;
|
|
704
|
+
|
|
705
|
+
p.items.forEach((pi) => {
|
|
706
|
+
const item = itemWiseItems.find((it) => it._temp_id == pi._temp_id);
|
|
707
|
+
if (!item) return;
|
|
708
|
+
|
|
709
|
+
const qty = pi.quantity || 0;
|
|
710
|
+
const price = item.per_item_cost || 0;
|
|
711
|
+
const taxTotal = item.total_tax_amount || 0;
|
|
712
|
+
|
|
713
|
+
// ---------------------------------------
|
|
714
|
+
// TAX SPLIT PER QTY (Normal Item Level)
|
|
715
|
+
// ---------------------------------------
|
|
716
|
+
const quantityWiseTaxArray = (item.tax_array ?? []).map((taxObj) => {
|
|
717
|
+
const unitBase = parseFloat(taxObj.base_price) / item.quantity;
|
|
718
|
+
const unitTax = parseFloat(taxObj.tax_amount) / item.quantity;
|
|
719
|
+
|
|
720
|
+
return {
|
|
721
|
+
...taxObj,
|
|
722
|
+
base_price: (unitBase * qty).toFixed(6),
|
|
723
|
+
tax_amount: (unitTax * qty).toFixed(6)
|
|
724
|
+
};
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
// ---------------------------------------
|
|
728
|
+
// TAX SPLIT FOR **MODIFIERS**
|
|
729
|
+
// ---------------------------------------
|
|
730
|
+
let modifierTaxArray = [];
|
|
731
|
+
let modifiers = item.item_modifiers ?? item.modifiers ?? [];
|
|
732
|
+
|
|
733
|
+
modifiers.forEach((mod) => {
|
|
734
|
+
const mqty = mod.quantity || 0;
|
|
735
|
+
|
|
736
|
+
const modTaxArr = (mod.tax_array ?? []).map((t) => {
|
|
737
|
+
const unitBase = parseFloat(t.base_price) / (item.quantity * mod.quantity);
|
|
738
|
+
const unitTax = parseFloat(t.tax_amount) / (item.quantity * mod.quantity);
|
|
739
|
+
|
|
740
|
+
return {
|
|
741
|
+
...t,
|
|
742
|
+
base_price: (unitBase * (qty * mqty)).toFixed(6),
|
|
743
|
+
tax_amount: (unitTax * (qty * mqty)).toFixed(6)
|
|
744
|
+
};
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
modifierTaxArray.push(...modTaxArr);
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// MERGE FINAL ITEM LEVEL TAX
|
|
751
|
+
const mergedItemTax = {};
|
|
752
|
+
[...quantityWiseTaxArray, ...modifierTaxArray].forEach(t => {
|
|
753
|
+
if (!mergedItemTax[t.tax_id]) {
|
|
754
|
+
mergedItemTax[t.tax_id] = { ...t, base_price: 0, tax_amount: 0 };
|
|
755
|
+
}
|
|
756
|
+
mergedItemTax[t.tax_id].base_price += parseFloat(t.base_price);
|
|
757
|
+
mergedItemTax[t.tax_id].tax_amount += parseFloat(t.tax_amount);
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
pi.tax_array = Object.values(mergedItemTax).map(t => ({
|
|
761
|
+
...t,
|
|
762
|
+
base_price: t.base_price.toFixed(6),
|
|
763
|
+
tax_amount: t.tax_amount.toFixed(6)
|
|
764
|
+
}));
|
|
765
|
+
|
|
766
|
+
// CALCULATE NORMAL SUBTOTAL + TAX
|
|
767
|
+
subtotal += (price * qty);
|
|
768
|
+
totalTax += (parseFloat(taxTotal) / item.quantity) * qty;
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// MERGE SPLIT WISE TAX
|
|
772
|
+
const mergedTaxMap = {};
|
|
773
|
+
p.items.forEach(pi => {
|
|
774
|
+
(pi.tax_array ?? []).forEach(t => {
|
|
775
|
+
if (!mergedTaxMap[t.tax_id]) {
|
|
776
|
+
mergedTaxMap[t.tax_id] = { ...t, base_price: 0, tax_amount: 0 };
|
|
777
|
+
}
|
|
778
|
+
mergedTaxMap[t.tax_id].base_price += parseFloat(t.base_price);
|
|
779
|
+
mergedTaxMap[t.tax_id].tax_amount += parseFloat(t.tax_amount);
|
|
780
|
+
});
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
p.merge_tax_array = Object.values(mergedTaxMap).map(t => ({
|
|
784
|
+
...t,
|
|
785
|
+
base_price: t.base_price.toFixed(6),
|
|
786
|
+
tax_amount: t.tax_amount.toFixed(6)
|
|
787
|
+
}));
|
|
788
|
+
|
|
789
|
+
// FINAL VALUES
|
|
790
|
+
p.tax = totalTax.toFixed(2);
|
|
791
|
+
p.amount = subtotal.toFixed(2);
|
|
792
|
+
p.due = subtotal.toFixed(2);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
resolve(cart);
|
|
796
|
+
}
|
|
797
|
+
catch (err) {
|
|
798
|
+
console.error("❌ Split Payment Calculation Failed:", err);
|
|
799
|
+
resolve({
|
|
800
|
+
'status': 'error',
|
|
801
|
+
'status_code': 500,
|
|
802
|
+
'message': 'Internal server error',
|
|
803
|
+
'error': error.stack,
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
module.exports = { calculateTax2, calculateItemWiseSplit }
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const calculation = require('./calculations');
|
|
2
|
+
const { calculateItemWiseSplit } = require('./functions/taxCalculation');
|
|
2
3
|
// const fs = require('fs');
|
|
3
4
|
// const path = require('path')
|
|
4
5
|
|
|
@@ -76,6 +77,7 @@ async function calculateTax(inputJSON) {
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
// let json = JSON.parse(fs.readFileSync('./sample.json', 'utf8'));
|
|
80
|
+
|
|
79
81
|
// calculateTax(json).then(res=>{
|
|
80
82
|
// // console.log(JSON.stringify(res))
|
|
81
83
|
|
|
@@ -90,8 +92,26 @@ async function calculateTax(inputJSON) {
|
|
|
90
92
|
// }
|
|
91
93
|
// });
|
|
92
94
|
|
|
95
|
+
// }).catch(err=>{
|
|
96
|
+
// console.log(err)
|
|
97
|
+
// })
|
|
98
|
+
|
|
99
|
+
// calculateItemWiseSplit(json).then(res=>{
|
|
100
|
+
// // console.log(JSON.stringify(res))
|
|
101
|
+
|
|
102
|
+
// const jsonResponse = JSON.stringify(res, null, 2); // Format the JSON with indentation
|
|
103
|
+
// const filePath = path.join(__dirname, 'response.json'); // File path in the same directory
|
|
104
|
+
|
|
105
|
+
// fs.writeFile(filePath, jsonResponse, 'utf8', err => {
|
|
106
|
+
// if (err) {
|
|
107
|
+
// console.error('Error writing to file:', err);
|
|
108
|
+
// } else {
|
|
109
|
+
// console.log('Response successfully written to response.json');
|
|
110
|
+
// }
|
|
111
|
+
// });
|
|
112
|
+
|
|
93
113
|
// }).catch(err=>{
|
|
94
114
|
// console.log(err)
|
|
95
115
|
// })
|
|
96
116
|
|
|
97
|
-
module.exports = { calculateTax }
|
|
117
|
+
module.exports = { calculateTax , calculateItemWiseSplit}
|
package/package.json
CHANGED