foodbot-cart-calculations 1.0.0
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/README.MD +41 -0
- package/calculations.js +94 -0
- package/functions/pointsCalculation.js +195 -0
- package/functions/promotionCalculation.js +1030 -0
- package/functions/taxCalculation.js +542 -0
- package/main.js +77 -0
- package/package.json +11 -0
package/README.MD
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# foodbot-cart-calculations
|
|
2
|
+
|
|
3
|
+
foodbot-cart-calculations package for cart calculations in which it performs discount distribution or loyalty points distibution and tax calculations distribution on order items. There are some required keys which will be used for distibute discount , loyalty points and calculate tax. This package will return promise so we have to wait for this , so we have to use await for this.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install foodbot-cart-calculations
|
|
9
|
+
|
|
10
|
+
Usage
|
|
11
|
+
|
|
12
|
+
const cartPackage = require('foodbot-cart-calculations');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
let json = {
|
|
16
|
+
restaurant_id: 'xxxx',
|
|
17
|
+
branch_id: 'xxxx',
|
|
18
|
+
tax_type: 2,
|
|
19
|
+
/*
|
|
20
|
+
.
|
|
21
|
+
.
|
|
22
|
+
.
|
|
23
|
+
.
|
|
24
|
+
*/
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Request Example
|
|
28
|
+
|
|
29
|
+
let finalJson= await cartPackage.calculateTax(json);
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// Response Example
|
|
34
|
+
|
|
35
|
+
console.log("final json " , finaJson);
|
|
36
|
+
{
|
|
37
|
+
"status":"success",
|
|
38
|
+
"status_code":"200",
|
|
39
|
+
"message":"Tax calculated successfully",
|
|
40
|
+
"data":/* json*/
|
|
41
|
+
}
|
package/calculations.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const taxCalculation = require('./functions/taxCalculation');
|
|
2
|
+
const promoCalculation = require('./functions/promotionCalculation');
|
|
3
|
+
const pointsCalculation = require('./functions/pointsCalculation');
|
|
4
|
+
function applyDistribution(body) {
|
|
5
|
+
return new Promise(async (resolve, reject) => {
|
|
6
|
+
try {
|
|
7
|
+
body.discount = 0
|
|
8
|
+
let mainCart = body
|
|
9
|
+
let promotion = (body.promotion_applied && body.promotion_applied) ? body.promotion_applied : {};
|
|
10
|
+
promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
|
|
11
|
+
promoCalculation.offerCalculation(res, promotion).then(final => {
|
|
12
|
+
|
|
13
|
+
let taxSettings = (body.tax_settings && body.tax_settings.length > 0) ? body.tax_settings : []
|
|
14
|
+
let taxType = (body.tax_type) ? body.tax_type : 1
|
|
15
|
+
let currency = (body.currency) ? body.currency : "$"
|
|
16
|
+
final.points_redeem_message = ""
|
|
17
|
+
|
|
18
|
+
if (body.promotions_applied && body.promotions_applied) {
|
|
19
|
+
body.promotions_applied.discount = final.discount
|
|
20
|
+
body.promotions_applied.promotion_id = body.promotions_applied.offer_id
|
|
21
|
+
body.promotions_applied.promotion_type = body.promotions_applied.offer_discount_type
|
|
22
|
+
body.promotions_applied.discount_type = body.promotions_applied.oo_offer_type
|
|
23
|
+
body.promotion_applied = body.promotions_applied
|
|
24
|
+
}
|
|
25
|
+
mainCart.store_value = 0;
|
|
26
|
+
|
|
27
|
+
pointsCalculation.getPointsDiscount(final, body.loyalty_points_details, body.user_detais).then(pointsCal => {
|
|
28
|
+
|
|
29
|
+
if (typeof (final.store_value) == "undefined" || isNaN(final.store_value))
|
|
30
|
+
final.store_value = 0
|
|
31
|
+
if (body.loyalty_points_details && parseInt(body.loyalty_points_details.points_redeemed) == 0) {
|
|
32
|
+
// let message = i18n.__("kiosk_point_message_not_redeemed")
|
|
33
|
+
// message = message.replace(/{totalAmount}/g, currency + "" +body.loyalty_points_details.store_value);
|
|
34
|
+
// message = message.replace(/{payPoints}/g, currency + "" + final.point_discount_amount);
|
|
35
|
+
// final.points_redeem_message = message
|
|
36
|
+
|
|
37
|
+
final.item_details.forEach(function (v) {
|
|
38
|
+
delete v.points_discount;
|
|
39
|
+
if (v.item_modifiers) {
|
|
40
|
+
v.item_modifiers.forEach((mod) => {
|
|
41
|
+
delete mod.points_discount;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
} else {
|
|
47
|
+
let totalStoreValue = (body.loyalty_points_details && body.loyalty_points_details.store_value) ? body.loyalty_points_details.store_value : 0
|
|
48
|
+
mainCart.store_value = final.store_value;
|
|
49
|
+
final.points_discount = final.store_value;
|
|
50
|
+
// let message = i18n.__("kiosk_point_message_redeemed").replace(/{totalAmount}/g, currency + "" + totalStoreValue).replace(/{paidPoints}/g, currency + "" + final.store_value)
|
|
51
|
+
// final.points_redeem_message = message
|
|
52
|
+
}
|
|
53
|
+
taxCalculation.calculateTax2(final, taxSettings, taxType).then(taxData => {
|
|
54
|
+
taxData.sub_total = taxData.cartTotal;
|
|
55
|
+
taxData.total_after_tax = taxData.cartTotal
|
|
56
|
+
if (body.tax_type == 2) {
|
|
57
|
+
taxData.sub_total = taxData.cartTotal - taxData.tax_amount
|
|
58
|
+
taxData.sub_total = parseFloat(taxData.sub_total).toFixed(2)
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
taxData.total_after_tax = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount)
|
|
62
|
+
taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
mainCart.order_items = taxData
|
|
67
|
+
mainCart.final_amount = taxData.cartTotal
|
|
68
|
+
mainCart.cash_expected = taxData.cartTotal
|
|
69
|
+
|
|
70
|
+
mainCart.order_items.discount = parseFloat(mainCart.order_items.discount) + parseFloat(mainCart.store_value)
|
|
71
|
+
mainCart.order_items.discount = parseFloat(mainCart.order_items.discount).toFixed(2)
|
|
72
|
+
|
|
73
|
+
mainCart.discount = mainCart.order_items.discount;
|
|
74
|
+
resolve(mainCart);
|
|
75
|
+
}).catch(err => {
|
|
76
|
+
reject(err);
|
|
77
|
+
})
|
|
78
|
+
}).catch(err => {
|
|
79
|
+
reject(err);
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
}).catch(err => {
|
|
83
|
+
reject(err);
|
|
84
|
+
})
|
|
85
|
+
}).catch(err => {
|
|
86
|
+
reject(err);
|
|
87
|
+
})
|
|
88
|
+
} catch (error) {
|
|
89
|
+
reject(error)
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = { applyDistribution }
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
function getPointsDiscount(cartObject, points, userDetails) {
|
|
2
|
+
return new Promise(function (resolve, reject) {
|
|
3
|
+
try {
|
|
4
|
+
let itemsTotal = 0
|
|
5
|
+
let carts = cartObject.item_details
|
|
6
|
+
|
|
7
|
+
carts.forEach(function (v) {
|
|
8
|
+
delete v.points_discount_modifier;
|
|
9
|
+
delete v.points_discount;
|
|
10
|
+
delete v.point_discount_status;
|
|
11
|
+
|
|
12
|
+
if (v.item_modifiers)
|
|
13
|
+
v.item_modifiers.forEach((mod) => {
|
|
14
|
+
delete mod.points_discount;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
if (v.isPackage == 1) {
|
|
18
|
+
v.package_items.forEach(function (p) {
|
|
19
|
+
delete p.points_discount_modifier;
|
|
20
|
+
delete p.points_discount;
|
|
21
|
+
delete p.point_discount_status;
|
|
22
|
+
|
|
23
|
+
if (p.item_modifiers)
|
|
24
|
+
p.item_modifiers.forEach((pMod) => {
|
|
25
|
+
delete pMod.points_discount;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (p.modifiers)
|
|
29
|
+
p.modifiers.forEach((pMod) => {
|
|
30
|
+
delete pMod.points_discount;
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
if (!points) {
|
|
36
|
+
resolve(0);
|
|
37
|
+
} else {
|
|
38
|
+
let pointsAmount = '0';
|
|
39
|
+
let count = 0;
|
|
40
|
+
let storeAmount = points.store_value;
|
|
41
|
+
let indexes = [];
|
|
42
|
+
|
|
43
|
+
if (points.redeem_categories && points.redeem_categories != '') {
|
|
44
|
+
itemsTotal = 0;
|
|
45
|
+
|
|
46
|
+
carts.forEach((element, index) => {
|
|
47
|
+
var ItemIndex = points.redeem_categories.findIndex((x) => x.category_id == element.category_id);
|
|
48
|
+
if (ItemIndex >= 0 || (points.redeem_categories && points.redeem_categories[0] && points.redeem_categories[0].category_id.toLowerCase() == 'all') && !element.event) {
|
|
49
|
+
let priceToal = parseFloat(element.item_price_with_modifier) * element.quantity;
|
|
50
|
+
let itemPrice = parseFloat(priceToal).toFixed(2);
|
|
51
|
+
|
|
52
|
+
if (element.promotion_discount) {
|
|
53
|
+
itemPrice = parseFloat(priceToal) - parseFloat(element.promotion_discount_modifier);
|
|
54
|
+
itemPrice = parseFloat(itemPrice).toFixed(2);
|
|
55
|
+
itemsTotal = parseFloat(itemPrice) + parseFloat(itemsTotal);
|
|
56
|
+
} else {
|
|
57
|
+
itemsTotal = parseFloat(itemsTotal) + parseFloat(priceToal);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (parseFloat(storeAmount) > parseFloat(itemPrice)) {
|
|
61
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(itemPrice);
|
|
62
|
+
pointsAmount = parseFloat(pointsAmount).toFixed(2);
|
|
63
|
+
storeAmount = parseFloat(storeAmount) - parseFloat(itemPrice);
|
|
64
|
+
} else {
|
|
65
|
+
let storeValue = JSON.parse(JSON.stringify(storeAmount));
|
|
66
|
+
storeAmount = parseFloat(storeAmount) - parseFloat(storeAmount);
|
|
67
|
+
pointsAmount = parseFloat(pointsAmount) + parseFloat(storeValue);
|
|
68
|
+
pointsAmount = parseFloat(pointsAmount).toFixed(2);
|
|
69
|
+
}
|
|
70
|
+
if (points && points.points_redeemed && userDetails)
|
|
71
|
+
element.point_discount_status = true;
|
|
72
|
+
}
|
|
73
|
+
count++;
|
|
74
|
+
|
|
75
|
+
if (count == carts.length) {
|
|
76
|
+
let singlePriceDiscount = pointsAmount / itemsTotal;
|
|
77
|
+
singlePriceDiscount = parseFloat(singlePriceDiscount).toFixed(6);
|
|
78
|
+
|
|
79
|
+
let storeValue = 0
|
|
80
|
+
let itemDiscount = 0;
|
|
81
|
+
let itemTotalDicount = 0;
|
|
82
|
+
|
|
83
|
+
carts.forEach((item, index) => {
|
|
84
|
+
item.points_discount = item.points_discount ? item.points_discount : 0;
|
|
85
|
+
item.points_discount_modifier = item.points_discount_modifier ? item.points_discount_modifier : 0;
|
|
86
|
+
|
|
87
|
+
if (item.isPackage == 1) {
|
|
88
|
+
if (item.point_discount_status) {
|
|
89
|
+
let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
|
|
90
|
+
itemDiscount = ((item.item_price * item.quantity) - promoDiscount) * singlePriceDiscount;
|
|
91
|
+
itemDiscount = parseFloat(itemDiscount).toFixed(2);
|
|
92
|
+
itemTotalDicount = parseFloat(itemDiscount) + parseFloat(itemTotalDicount);
|
|
93
|
+
|
|
94
|
+
if (itemDiscount > 0)
|
|
95
|
+
indexes.push(index);
|
|
96
|
+
|
|
97
|
+
item.points_discount = itemDiscount;
|
|
98
|
+
item.points_discount_modifier = 0;
|
|
99
|
+
|
|
100
|
+
let packDis = 0;
|
|
101
|
+
|
|
102
|
+
item.package_items.forEach((packageItem, packageItemIndex) => {
|
|
103
|
+
let packageItemPromoDiscount = (packageItem.promotion_discount) ? packageItem.promotion_discount : 0;
|
|
104
|
+
packageItem.points_discount_modifier = 0;
|
|
105
|
+
|
|
106
|
+
packageItem.points_discount = ((((packageItem.price * parseInt(item.quantity)) - parseFloat((packageItem.default_combo_discount)) - parseFloat(packageItemPromoDiscount))) * parseFloat(singlePriceDiscount)).toFixed(2);
|
|
107
|
+
packDis = (parseFloat(packDis) + parseFloat(packageItem.points_discount)).toFixed(2);
|
|
108
|
+
|
|
109
|
+
packageItem.modifiers.forEach((modi, modiIndex) => {
|
|
110
|
+
let modiItemAmount = 0;
|
|
111
|
+
let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
|
|
112
|
+
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity) - modiPromoDiscount) * parseFloat(singlePriceDiscount);
|
|
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
|
+
});
|
|
121
|
+
|
|
122
|
+
if (itemDiscount != packDis && item.package_items) {
|
|
123
|
+
if (packDis > itemDiscount) {
|
|
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
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
if (item.point_discount_status) {
|
|
134
|
+
let promoDiscount = (item.promotion_discount) ? item.promotion_discount : 0;
|
|
135
|
+
itemDiscount = ((item.item_price * item.quantity) - promoDiscount) * singlePriceDiscount;
|
|
136
|
+
itemDiscount = parseFloat(itemDiscount).toFixed(2);
|
|
137
|
+
itemTotalDicount = parseFloat(itemDiscount) + parseFloat(itemTotalDicount);
|
|
138
|
+
|
|
139
|
+
if (itemDiscount > 0)
|
|
140
|
+
indexes.push(index);
|
|
141
|
+
|
|
142
|
+
item.points_discount = itemDiscount;
|
|
143
|
+
|
|
144
|
+
item.item_modifiers.forEach((modi, modiIndex) => {
|
|
145
|
+
let modiItemAmount = 0;
|
|
146
|
+
let modiPromoDiscount = (modi.promotion_discount) ? modi.promotion_discount : 0;
|
|
147
|
+
modiItemAmount = (((parseFloat(modi.modifier_item_price) * parseInt(modi.quantity)) * item.quantity) - modiPromoDiscount) * parseFloat(singlePriceDiscount);
|
|
148
|
+
modiItemAmount = parseFloat(modiItemAmount).toFixed(2);
|
|
149
|
+
itemTotalDicount = parseFloat(itemTotalDicount) + parseFloat(modiItemAmount);
|
|
150
|
+
itemTotalDicount = parseFloat(itemTotalDicount).toFixed(2);
|
|
151
|
+
modi.points_discount = modiItemAmount;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
item.points_discount_modifier = itemTotalDicount;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
if ((Number(itemTotalDicount) < Number(pointsAmount) || Number(itemTotalDicount) > Number(pointsAmount)) && pointsAmount > 0 && itemTotalDicount > 0) {
|
|
160
|
+
let difference = Number(pointsAmount) - Number(itemTotalDicount);
|
|
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);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
cartObject.item_details = carts
|
|
171
|
+
cartObject.store_value = pointsAmount
|
|
172
|
+
cartObject.point_discount_amount = pointsAmount
|
|
173
|
+
|
|
174
|
+
resolve(cartObject);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
cartObject.item_details = carts
|
|
179
|
+
cartObject.store_value = pointsAmount
|
|
180
|
+
cartObject.point_discount_amount = pointsAmount
|
|
181
|
+
|
|
182
|
+
resolve(cartObject);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
reject(error);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
module.exports = {
|
|
194
|
+
getPointsDiscount
|
|
195
|
+
}
|