foodbot-cart-calculations 1.0.48 → 1.0.50
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 +33 -15
- package/functions/pointsCalculation.js +222 -87
- package/index.js +3 -2
- package/package.json +1 -1
package/calculations.js
CHANGED
|
@@ -8,10 +8,15 @@ function applyDistribution(body) {
|
|
|
8
8
|
let mainCart = body
|
|
9
9
|
let promotion = (body.promotion_applied && body.promotion_applied) ? body.promotion_applied : {};
|
|
10
10
|
let deliveryCost = (body.delivery_cost && body.delivery_cost) ? body.delivery_cost : 0;
|
|
11
|
-
|
|
12
|
-
if(body?.order_items?.delivery_discount)
|
|
11
|
+
|
|
12
|
+
if(body?.order_items?.delivery_discount) {
|
|
13
13
|
body.order_items.delivery_discount = 0
|
|
14
14
|
body.order_items.is_delivery_discount = false
|
|
15
|
+
}
|
|
16
|
+
if (body?.order_items?.service_discount) {
|
|
17
|
+
body.order_items.service_discount = 0;
|
|
18
|
+
body.order_items.is_service_discount = false;
|
|
19
|
+
}
|
|
15
20
|
|
|
16
21
|
promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
|
|
17
22
|
promoCalculation.offerCalculation(res, promotion, deliveryCost).then(final => {
|
|
@@ -29,9 +34,9 @@ function applyDistribution(body) {
|
|
|
29
34
|
body.promotion_applied = body.promotions_applied
|
|
30
35
|
}
|
|
31
36
|
mainCart.store_value = 0;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
const originalDeliveryDiscount = final.delivery_discount;
|
|
38
|
+
const originalIsDeliveryDiscount = final.is_delivery_discount;
|
|
39
|
+
pointsCalculation.getPointsDiscount(final, body.loyalty_points_details, body.user_detais, body.delivery_cost, body.service_amount).then(pointsCal => {
|
|
35
40
|
if (typeof (final.store_value) == "undefined" || isNaN(final.store_value))
|
|
36
41
|
final.store_value = 0
|
|
37
42
|
if (body.loyalty_points_details && parseInt(body.loyalty_points_details.points_redeemed) == 0) {
|
|
@@ -73,23 +78,36 @@ function applyDistribution(body) {
|
|
|
73
78
|
mainCart.cash_expected = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount);
|
|
74
79
|
}
|
|
75
80
|
mainCart.order_items = taxData
|
|
76
|
-
|
|
81
|
+
|
|
77
82
|
let delivery_cost = body.delivery_cost ? body.delivery_cost : 0;
|
|
83
|
+
let service_amount = body.service_amount ? body.service_amount : 0;
|
|
84
|
+
|
|
78
85
|
// Adjust delivery cost based on delivery discount
|
|
79
|
-
if (
|
|
80
|
-
delivery_cost = Math.max(0, delivery_cost -
|
|
81
|
-
body.delivery_cost = delivery_cost;
|
|
86
|
+
if (originalIsDeliveryDiscount) {
|
|
87
|
+
delivery_cost = Math.max(0, delivery_cost - parseFloat(originalDeliveryDiscount));
|
|
88
|
+
body.delivery_cost = delivery_cost;
|
|
89
|
+
mainCart.order_items.delivery_discount = parseFloat(originalDeliveryDiscount);
|
|
90
|
+
mainCart.order_items.is_delivery_discount = true;
|
|
91
|
+
console.log("✅ Applied delivery discount:", { delivery_cost, body_delivery_cost: body.delivery_cost, order_items: mainCart.order_items });
|
|
82
92
|
}
|
|
83
93
|
|
|
84
|
-
|
|
85
|
-
|
|
94
|
+
if (final.is_delivery_discount) {
|
|
95
|
+
delivery_cost = Math.max(0, delivery_cost - final.delivery_discount);
|
|
96
|
+
body.delivery_cost = delivery_cost;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Adjust service amount based on service discount
|
|
100
|
+
if (final.is_service_discount) {
|
|
101
|
+
service_amount = Math.max(0, service_amount - final.service_discount);
|
|
102
|
+
body.service_amount = service_amount;
|
|
103
|
+
}
|
|
86
104
|
//we took 2 value because in case of absolute we use tipValue
|
|
87
105
|
let tipValue = body.tip_value?body.tip_value:0;
|
|
88
106
|
let tipValueReq = body.tip_value?body.tip_value:0;
|
|
89
107
|
|
|
90
108
|
if (body.tip_type && body.tip_type == 'percentage') {
|
|
91
|
-
|
|
92
|
-
|
|
109
|
+
tipValue = (mainCart.final_amount * parseInt(tipValueReq)) / 100
|
|
110
|
+
tipValue = parseFloat(tipValue).toFixed(2)
|
|
93
111
|
}
|
|
94
112
|
|
|
95
113
|
mainCart.tip_amount = parseFloat(tipValue).toFixed(2);
|
|
@@ -105,10 +123,10 @@ function applyDistribution(body) {
|
|
|
105
123
|
|
|
106
124
|
taxData.total_after_tax = parseFloat(taxData.total_after_tax) + parseFloat(tipValue);
|
|
107
125
|
taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2);
|
|
108
|
-
|
|
126
|
+
|
|
109
127
|
mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(tipValue);
|
|
110
128
|
mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(tipValue);
|
|
111
|
-
|
|
129
|
+
|
|
112
130
|
taxData.final_amount = parseFloat(taxData.final_amount).toFixed(2);
|
|
113
131
|
taxData.cash_expected = parseFloat(taxData.cash_expected).toFixed(2);
|
|
114
132
|
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
function getPointsDiscount(cartObject, points, userDetails) {
|
|
1
|
+
function getPointsDiscount(cartObject, points, userDetails, deliveryCost = 0, serviceAmount = 0) {
|
|
2
2
|
return new Promise(function (resolve, reject) {
|
|
3
3
|
try {
|
|
4
4
|
let itemsTotal = 0
|
|
5
5
|
let carts = cartObject.item_details
|
|
6
|
+
let deliveryDiscount = 0;
|
|
7
|
+
let serviceDiscount = 0;
|
|
6
8
|
|
|
9
|
+
// reset old discounts
|
|
7
10
|
carts.forEach(function (v) {
|
|
8
11
|
delete v.points_discount_modifier;
|
|
9
12
|
delete v.points_discount;
|
|
@@ -32,8 +35,11 @@ function getPointsDiscount(cartObject, points, userDetails) {
|
|
|
32
35
|
});
|
|
33
36
|
}
|
|
34
37
|
});
|
|
38
|
+
|
|
35
39
|
if (!points) {
|
|
36
|
-
|
|
40
|
+
cartObject.delivery_discount = 0;
|
|
41
|
+
cartObject.service_discount = 0;
|
|
42
|
+
resolve(cartObject);
|
|
37
43
|
} else {
|
|
38
44
|
let pointsAmount = 0;
|
|
39
45
|
let count = 0;
|
|
@@ -71,113 +77,242 @@ function getPointsDiscount(cartObject, points, userDetails) {
|
|
|
71
77
|
element.point_discount_status = true;
|
|
72
78
|
}
|
|
73
79
|
count++;
|
|
80
|
+
});
|
|
74
81
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
// Apply points to service charge if enabled
|
|
83
|
+
if (points.enable_service_charge_usage === 1 && serviceAmount > 0 && storeAmount > 0) {
|
|
84
|
+
// Check if service discount is already applied through promotions
|
|
85
|
+
let hasServicePromo = cartObject.is_service_discount === true;
|
|
86
|
+
|
|
87
|
+
if (!hasServicePromo) {
|
|
88
|
+
cartObject.is_service_discount = true;
|
|
89
|
+
if (parseFloat(storeAmount) >= parseFloat(serviceAmount)) {
|
|
90
|
+
serviceDiscount = parseFloat(serviceAmount);
|
|
91
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(serviceAmount);
|
|
92
|
+
storeAmount = parseFloat(storeAmount) - parseFloat(serviceAmount);
|
|
93
|
+
} else {
|
|
94
|
+
serviceDiscount = parseFloat(storeAmount);
|
|
95
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(storeAmount);
|
|
96
|
+
storeAmount = 0;
|
|
97
|
+
}
|
|
98
|
+
serviceDiscount = parseFloat(serviceDiscount).toFixed(2);
|
|
99
|
+
cartObject.service_discount = serviceDiscount;
|
|
100
|
+
} else {
|
|
101
|
+
cartObject.service_discount = cartObject.service_discount || 0;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
cartObject.service_discount = cartObject.service_discount || 0;
|
|
105
|
+
if (!cartObject.is_service_discount) {
|
|
106
|
+
cartObject.is_service_discount = false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
78
109
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
110
|
+
// Apply points to delivery if enabled and no delivery promotion is already applied
|
|
111
|
+
if (points.enable_delivery_points_usage === 1 && deliveryCost > 0 && storeAmount > 0) {
|
|
112
|
+
// Check if delivery discount is already applied through promotions
|
|
113
|
+
let hasDeliveryPromo = cartObject.is_delivery_discount === true;
|
|
82
114
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
115
|
+
if (!hasDeliveryPromo) {
|
|
116
|
+
cartObject.is_delivery_discount = true;
|
|
117
|
+
if (parseFloat(storeAmount) >= parseFloat(deliveryCost)) {
|
|
118
|
+
deliveryDiscount = parseFloat(deliveryCost);
|
|
119
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(deliveryCost);
|
|
120
|
+
storeAmount = parseFloat(storeAmount) - parseFloat(deliveryCost);
|
|
121
|
+
} else {
|
|
122
|
+
deliveryDiscount = parseFloat(storeAmount);
|
|
123
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(storeAmount);
|
|
124
|
+
storeAmount = 0;
|
|
125
|
+
}
|
|
126
|
+
deliveryDiscount = parseFloat(deliveryDiscount).toFixed(2);
|
|
127
|
+
cartObject.delivery_discount = deliveryDiscount;
|
|
128
|
+
} else {
|
|
129
|
+
// If delivery promotion is already applied, preserve existing discount
|
|
130
|
+
cartObject.delivery_discount = cartObject.delivery_discount || 0;
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
cartObject.delivery_discount = cartObject.delivery_discount || 0;
|
|
134
|
+
if (!cartObject.is_delivery_discount) {
|
|
135
|
+
cartObject.is_delivery_discount = false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
86
138
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
itemDiscount = ((item.item_price * item.quantity) - promoDiscount) * singlePriceDiscount;
|
|
91
|
-
itemDiscount = parseFloat(itemDiscount).toFixed(2);
|
|
92
|
-
itemTotalDicount = parseFloat(itemDiscount) + parseFloat(itemTotalDicount);
|
|
139
|
+
if (count == carts.length) {
|
|
140
|
+
let singlePriceDiscount = pointsAmount / itemsTotal;
|
|
141
|
+
singlePriceDiscount = (itemsTotal && itemsTotal > 0)?parseFloat(singlePriceDiscount).toFixed(6):0;
|
|
93
142
|
|
|
94
|
-
|
|
95
|
-
|
|
143
|
+
let indexes = [];
|
|
144
|
+
let itemTotalDicount = 0;
|
|
96
145
|
|
|
97
|
-
|
|
98
|
-
|
|
146
|
+
carts.forEach((item, index) => {
|
|
147
|
+
item.points_discount = item.points_discount ? item.points_discount : 0;
|
|
148
|
+
item.points_discount_modifier = item.points_discount_modifier ? item.points_discount_modifier : 0;
|
|
99
149
|
|
|
100
|
-
|
|
150
|
+
// Replace the package items section (around line 120-170) with this corrected version:
|
|
101
151
|
|
|
102
|
-
item.
|
|
103
|
-
|
|
104
|
-
|
|
152
|
+
if (item.isPackage == 1) {
|
|
153
|
+
if (item.point_discount_status) {
|
|
154
|
+
let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
|
|
155
|
+
|
|
156
|
+
// For package items, calculate discount based on actual item price
|
|
157
|
+
let mainItemPrice = parseFloat(item.item_price) * item.quantity;
|
|
158
|
+
let mainItemDiscount = ((mainItemPrice - promoDiscount) * parseFloat(singlePriceDiscount));
|
|
159
|
+
mainItemDiscount = Math.max(0, mainItemDiscount);
|
|
160
|
+
mainItemDiscount = parseFloat(mainItemDiscount).toFixed(2);
|
|
161
|
+
|
|
162
|
+
// Cap main item discount
|
|
163
|
+
if (parseFloat(mainItemDiscount) > (mainItemPrice - promoDiscount)) {
|
|
164
|
+
mainItemDiscount = (mainItemPrice - promoDiscount).toFixed(2);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (parseFloat(mainItemDiscount) > 0)
|
|
168
|
+
indexes.push(index);
|
|
169
|
+
|
|
170
|
+
let totalPackageItemsDiscount = 0;
|
|
171
|
+
let totalPackageModifiersDiscount = 0;
|
|
172
|
+
|
|
173
|
+
item.package_items.forEach((packageItem, packageItemIndex) => {
|
|
174
|
+
let packageItemPromoDiscount = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0;
|
|
175
|
+
packageItem.points_discount_modifier = 0;
|
|
176
|
+
|
|
177
|
+
// Calculate base package item price
|
|
178
|
+
let packageItemBasePrice = (packageItem.price * parseInt(item.quantity)) - parseFloat(packageItem.default_combo_discount || 0) - parseFloat(packageItemPromoDiscount);
|
|
179
|
+
|
|
180
|
+
// Ensure base price is not negative
|
|
181
|
+
if (packageItemBasePrice < 0) {
|
|
182
|
+
packageItemBasePrice = 0;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Calculate discount for package item
|
|
186
|
+
let packageItemDiscount = (packageItemBasePrice * parseFloat(singlePriceDiscount));
|
|
187
|
+
packageItemDiscount = parseFloat(packageItemDiscount).toFixed(2);
|
|
188
|
+
|
|
189
|
+
// Cap package item discount to not exceed its actual price
|
|
190
|
+
if (parseFloat(packageItemDiscount) > packageItemBasePrice) {
|
|
191
|
+
packageItemDiscount = parseFloat(packageItemBasePrice).toFixed(2);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Ensure discount is not negative
|
|
195
|
+
if (parseFloat(packageItemDiscount) < 0) {
|
|
196
|
+
packageItemDiscount = "0.00";
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
packageItem.points_discount = packageItemDiscount;
|
|
200
|
+
totalPackageItemsDiscount = parseFloat(totalPackageItemsDiscount) + parseFloat(packageItemDiscount);
|
|
201
|
+
|
|
202
|
+
// Handle modifiers
|
|
203
|
+
if (packageItem.modifiers && packageItem.modifiers.length > 0) {
|
|
204
|
+
packageItem.modifiers.forEach((modi, modiIndex) => {
|
|
205
|
+
let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
|
|
206
|
+
|
|
207
|
+
// Calculate modifier base price
|
|
208
|
+
let modiBasePrice = (parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity - modiPromoDiscount;
|
|
209
|
+
|
|
210
|
+
// Ensure modifier base price is not negative
|
|
211
|
+
if (modiBasePrice < 0) {
|
|
212
|
+
modiBasePrice = 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let modiItemAmount = (modiBasePrice * parseFloat(singlePriceDiscount));
|
|
216
|
+
modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
|
|
217
|
+
|
|
218
|
+
// Cap modifier discount
|
|
219
|
+
if (parseFloat(modiItemAmount) > modiBasePrice) {
|
|
220
|
+
modiItemAmount = parseFloat(modiBasePrice).toFixed(2);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Ensure discount is not negative
|
|
224
|
+
if (parseFloat(modiItemAmount) < 0) {
|
|
225
|
+
modiItemAmount = "0.00";
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
modi.points_discount = modiItemAmount;
|
|
229
|
+
packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount_modifier) + parseFloat(modi.points_discount)).toFixed(2);
|
|
230
|
+
totalPackageModifiersDiscount = parseFloat(totalPackageModifiersDiscount) + parseFloat(modiItemAmount);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// CRITICAL FIX: Set main package discount to match the actual main item price
|
|
236
|
+
// This ensures tax calculation gets the correct discount amount
|
|
237
|
+
item.points_discount = mainItemDiscount;
|
|
238
|
+
item.points_discount_modifier = "0.00";
|
|
239
|
+
|
|
240
|
+
// Add main item discount to cart total (not package items discount)
|
|
241
|
+
itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(mainItemDiscount);
|
|
242
|
+
itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
if (item.point_discount_status) {
|
|
246
|
+
let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
|
|
105
247
|
|
|
106
|
-
|
|
107
|
-
|
|
248
|
+
// --- Base item discount ---
|
|
249
|
+
let basePrice = parseFloat(item.item_price) * item.quantity;
|
|
250
|
+
let baseDiscount = ((basePrice - promoDiscount) * parseFloat(singlePriceDiscount));
|
|
251
|
+
baseDiscount = parseFloat(baseDiscount).toFixed(2);
|
|
108
252
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
|
|
114
|
-
itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(modiItemAmount);
|
|
115
|
-
itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
|
|
116
|
-
modi.points_discount = modiItemAmount;
|
|
117
|
-
packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount_modifier) + parseFloat(modi.points_discount)).toFixed(2);
|
|
118
|
-
});
|
|
119
|
-
packageItem.points_discount_modifier = (parseFloat(packageItem.points_discount) + parseFloat(packageItem.points_discount_modifier)).toFixed(2);
|
|
120
|
-
});
|
|
253
|
+
// cap base discount
|
|
254
|
+
if (parseFloat(baseDiscount) > basePrice) {
|
|
255
|
+
baseDiscount = basePrice.toFixed(2);
|
|
256
|
+
}
|
|
121
257
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
let diff = (parseFloat(packDis) - parseFloat(itemDiscount)).toFixed(2);
|
|
125
|
-
item.package_items[0].points_discount = (parseFloat(item.package_items[0].points_discount) - parseFloat(diff)).toFixed(2);
|
|
126
|
-
} else {
|
|
127
|
-
let diff = (parseFloat(itemDiscount) - parseFloat(packDis)).toFixed(2);
|
|
128
|
-
item.package_items[0].points_discount = (parseFloat(item.package_items[0].points_discount) - parseFloat(diff)).toFixed(2);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
258
|
+
if (parseFloat(baseDiscount) > 0) {
|
|
259
|
+
indexes.push(index);
|
|
131
260
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(modiItemAmount);
|
|
150
|
-
itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
|
|
151
|
-
modi.points_discount = modiItemAmount;
|
|
261
|
+
item.points_discount = baseDiscount;
|
|
262
|
+
|
|
263
|
+
// --- Modifiers ---
|
|
264
|
+
let modsTotalDiscount = 0;
|
|
265
|
+
if (item.item_modifiers && item.item_modifiers.length > 0) {
|
|
266
|
+
item.item_modifiers.forEach((modi) => {
|
|
267
|
+
let modiBase = parseFloat(modi.modifier_item_price) * modi.quantity * item.quantity;
|
|
268
|
+
let modiDiscount = (modiBase * parseFloat(singlePriceDiscount));
|
|
269
|
+
modiDiscount = parseFloat(modiDiscount).toFixed(2);
|
|
270
|
+
|
|
271
|
+
// cap modifier
|
|
272
|
+
if (parseFloat(modiDiscount) > modiBase) {
|
|
273
|
+
modiDiscount = modiBase.toFixed(2);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
modi.points_discount = modiDiscount;
|
|
277
|
+
modsTotalDiscount = (parseFloat(modsTotalDiscount) + parseFloat(modiDiscount)).toFixed(2);
|
|
152
278
|
});
|
|
279
|
+
}
|
|
280
|
+
item.points_discount_modifier = modsTotalDiscount;
|
|
153
281
|
|
|
154
|
-
|
|
282
|
+
// --- cap total item discount ---
|
|
283
|
+
let itemMax = parseFloat(item.item_price_with_modifier) * item.quantity;
|
|
284
|
+
let totalItemDiscount = parseFloat(item.points_discount) + parseFloat(item.points_discount_modifier);
|
|
285
|
+
if (totalItemDiscount > itemMax) {
|
|
286
|
+
let overflow = totalItemDiscount - itemMax;
|
|
287
|
+
if (parseFloat(item.points_discount_modifier) >= overflow) {
|
|
288
|
+
item.points_discount_modifier = (parseFloat(item.points_discount_modifier) - overflow).toFixed(2);
|
|
289
|
+
} else {
|
|
290
|
+
let leftover = overflow - parseFloat(item.points_discount_modifier);
|
|
291
|
+
item.points_discount_modifier = "0.00";
|
|
292
|
+
item.points_discount = (parseFloat(item.points_discount) - leftover).toFixed(2);
|
|
293
|
+
}
|
|
155
294
|
}
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
295
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
difference = parseFloat(difference).toFixed(2);
|
|
162
|
-
if (index > -1) {
|
|
163
|
-
carts[indexes[0]].points_discount = (difference > 0) ? parseFloat(carts[indexes[0]].points_discount) + parseFloat(difference) : parseFloat(carts[indexes[0]].points_discount) - Math.abs(difference);
|
|
164
|
-
carts[indexes[0]].points_discount = parseFloat(carts[indexes[0]].points_discount).toFixed(2);
|
|
165
|
-
itemTotalDicount = (difference < 0) ? itemTotalDicount + difference : itemTotalDicount - Math.abs(difference);
|
|
166
|
-
itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
|
|
296
|
+
// add to cart-level total
|
|
297
|
+
itemTotalDicount = (parseFloat(itemTotalDicount) + parseFloat(item.points_discount) + parseFloat(item.points_discount_modifier)).toFixed(2);
|
|
167
298
|
}
|
|
168
299
|
}
|
|
300
|
+
});
|
|
169
301
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
302
|
+
cartObject.item_details = carts
|
|
303
|
+
cartObject.store_value = pointsAmount
|
|
304
|
+
cartObject.point_discount_amount = pointsAmount
|
|
305
|
+
cartObject.delivery_discount = deliveryDiscount;
|
|
306
|
+
cartObject.service_discount = serviceDiscount;
|
|
173
307
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
});
|
|
308
|
+
resolve(cartObject);
|
|
309
|
+
}
|
|
177
310
|
} else {
|
|
178
311
|
cartObject.item_details = carts
|
|
179
|
-
cartObject.store_value =
|
|
180
|
-
cartObject.point_discount_amount =
|
|
312
|
+
cartObject.store_value = 0
|
|
313
|
+
cartObject.point_discount_amount = 0
|
|
314
|
+
cartObject.delivery_discount = 0;
|
|
315
|
+
cartObject.service_discount = 0;
|
|
181
316
|
|
|
182
317
|
resolve(cartObject);
|
|
183
318
|
}
|
package/index.js
CHANGED
|
@@ -848,8 +848,9 @@ async function calculateTax(inputJSON) {
|
|
|
848
848
|
// }
|
|
849
849
|
// };
|
|
850
850
|
|
|
851
|
+
// let json = JSON.parse(fs.readFileSync('./sampleInput.json', 'utf8'));
|
|
851
852
|
// calculateTax(json).then(res=>{
|
|
852
|
-
//
|
|
853
|
+
// console.log(JSON.stringify(res))
|
|
853
854
|
|
|
854
855
|
// const jsonResponse = JSON.stringify(res, null, 2); // Format the JSON with indentation
|
|
855
856
|
// const filePath = path.join(__dirname, 'response.json'); // File path in the same directory
|
|
@@ -865,5 +866,5 @@ async function calculateTax(inputJSON) {
|
|
|
865
866
|
// }).catch(err=>{
|
|
866
867
|
// console.log(err)
|
|
867
868
|
// })
|
|
868
|
-
|
|
869
|
+
|
|
869
870
|
module.exports = { calculateTax }
|
package/package.json
CHANGED