foodbot-cart-calculations 1.0.1 → 1.0.2-dev.1
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 +53 -6
- package/functions/pointsCalculation.js +3 -3
- package/functions/promotionCalculation.js +522 -402
- package/functions/taxCalculation.js +334 -186
- package/index.js +794 -2
- package/package.json +1 -1
package/calculations.js
CHANGED
|
@@ -7,8 +7,14 @@ function applyDistribution(body) {
|
|
|
7
7
|
body.discount = 0
|
|
8
8
|
let mainCart = body
|
|
9
9
|
let promotion = (body.promotion_applied && body.promotion_applied) ? body.promotion_applied : {};
|
|
10
|
+
let deliveryCost = (body.delivery_cost && body.delivery_cost) ? body.delivery_cost : 0;
|
|
11
|
+
|
|
12
|
+
if(body?.order_items?.delivery_discount)
|
|
13
|
+
body.order_items.delivery_discount = 0
|
|
14
|
+
body.order_items.is_delivery_discount = false
|
|
15
|
+
|
|
10
16
|
promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
|
|
11
|
-
promoCalculation.offerCalculation(res, promotion).then(final => {
|
|
17
|
+
promoCalculation.offerCalculation(res, promotion, deliveryCost).then(final => {
|
|
12
18
|
|
|
13
19
|
let taxSettings = (body.tax_settings && body.tax_settings.length > 0) ? body.tax_settings : []
|
|
14
20
|
let taxType = (body.tax_type) ? body.tax_type : 1
|
|
@@ -52,6 +58,8 @@ function applyDistribution(body) {
|
|
|
52
58
|
}
|
|
53
59
|
taxCalculation.calculateTax2(final, taxSettings, taxType).then(taxData => {
|
|
54
60
|
taxData.sub_total = taxData.cartTotal;
|
|
61
|
+
mainCart.final_amount = parseFloat(taxData.cartTotal);
|
|
62
|
+
mainCart.cash_expected = parseFloat(taxData.cartTotal) ;
|
|
55
63
|
taxData.total_after_tax = taxData.cartTotal
|
|
56
64
|
if (body.tax_type == 2) {
|
|
57
65
|
taxData.sub_total = taxData.cartTotal - taxData.tax_amount
|
|
@@ -59,18 +67,57 @@ function applyDistribution(body) {
|
|
|
59
67
|
}
|
|
60
68
|
else {
|
|
61
69
|
taxData.total_after_tax = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount)
|
|
62
|
-
taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2)
|
|
70
|
+
taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2);
|
|
71
|
+
|
|
72
|
+
mainCart.final_amount = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount);
|
|
73
|
+
mainCart.cash_expected = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount);
|
|
74
|
+
}
|
|
75
|
+
mainCart.order_items = taxData
|
|
76
|
+
|
|
77
|
+
let delivery_cost = body.delivery_cost ? body.delivery_cost : 0;
|
|
78
|
+
// Adjust delivery cost based on delivery discount
|
|
79
|
+
if (final.is_delivery_discount) {
|
|
80
|
+
delivery_cost = Math.max(0, delivery_cost - final.delivery_discount);
|
|
81
|
+
body.delivery_cost = delivery_cost; // Update body accordingly
|
|
63
82
|
}
|
|
64
83
|
|
|
84
|
+
let service_amount = body.service_amount?body.service_amount:0;
|
|
85
|
+
|
|
86
|
+
//we took 2 value because in case of absolute we use tipValue
|
|
87
|
+
let tipValue = body.tip_value?body.tip_value:0;
|
|
88
|
+
let tipValueReq = body.tip_value?body.tip_value:0;
|
|
65
89
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
90
|
+
if (body.tip_type && body.tip_type == 'percentage') {
|
|
91
|
+
tipValue = (mainCart.final_amount * parseInt(tipValueReq)) / 100
|
|
92
|
+
tipValue = parseFloat(tipValue).toFixed(2)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
mainCart.tip_amount = parseFloat(tipValue).toFixed(2);
|
|
96
|
+
|
|
97
|
+
mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(delivery_cost) + parseFloat(service_amount);
|
|
98
|
+
mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(delivery_cost) + parseFloat(service_amount);
|
|
99
|
+
|
|
100
|
+
//In a case when calculation of order done from pos.
|
|
101
|
+
if (body.tip_type && body.tip_type == 'percentage' && body.isPos && body.isPos == 1) {
|
|
102
|
+
tipValue = (mainCart.final_amount * parseInt(tipValueReq)) / 100;
|
|
103
|
+
tipValue = parseFloat(tipValue).toFixed(2)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
taxData.total_after_tax = parseFloat(taxData.total_after_tax) + parseFloat(tipValue);
|
|
107
|
+
taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2);
|
|
108
|
+
|
|
109
|
+
mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(tipValue);
|
|
110
|
+
mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(tipValue);
|
|
111
|
+
|
|
112
|
+
taxData.final_amount = parseFloat(taxData.final_amount).toFixed(2);
|
|
113
|
+
taxData.cash_expected = parseFloat(taxData.cash_expected).toFixed(2);
|
|
69
114
|
|
|
70
|
-
mainCart.order_items.discount = parseFloat(mainCart.order_items.discount)
|
|
115
|
+
mainCart.order_items.discount = parseFloat(mainCart.order_items.discount) ;
|
|
71
116
|
mainCart.order_items.discount = parseFloat(mainCart.order_items.discount).toFixed(2)
|
|
72
117
|
|
|
73
118
|
mainCart.discount = mainCart.order_items.discount;
|
|
119
|
+
mainCart.order_items.total=parseFloat(mainCart.order_items.total).toFixed(2)
|
|
120
|
+
|
|
74
121
|
resolve(mainCart);
|
|
75
122
|
}).catch(err => {
|
|
76
123
|
reject(err);
|
|
@@ -35,9 +35,9 @@ function getPointsDiscount(cartObject, points, userDetails) {
|
|
|
35
35
|
if (!points) {
|
|
36
36
|
resolve(0);
|
|
37
37
|
} else {
|
|
38
|
-
let pointsAmount =
|
|
38
|
+
let pointsAmount = 0;
|
|
39
39
|
let count = 0;
|
|
40
|
-
let storeAmount = points.store_value;
|
|
40
|
+
let storeAmount = points.store_value ?? (points.original_store_value ?? 0);
|
|
41
41
|
let indexes = [];
|
|
42
42
|
|
|
43
43
|
if (points.redeem_categories && points.redeem_categories != '') {
|
|
@@ -74,7 +74,7 @@ function getPointsDiscount(cartObject, points, userDetails) {
|
|
|
74
74
|
|
|
75
75
|
if (count == carts.length) {
|
|
76
76
|
let singlePriceDiscount = pointsAmount / itemsTotal;
|
|
77
|
-
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
77
|
+
singlePriceDiscount = (itemsTotal && itemsTotal > 0)?parseFloat(singlePriceDiscount).toFixed(6):0;
|
|
78
78
|
|
|
79
79
|
let storeValue = 0
|
|
80
80
|
let itemDiscount = 0;
|