foodbot-cart-calculations 1.0.41 → 1.0.43
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 +10 -5
- package/functions/promotionCalculation.js +37 -21
- package/package.json +1 -1
package/calculations.js
CHANGED
|
@@ -8,6 +8,11 @@ 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)
|
|
13
|
+
body.order_items.delivery_discount = 0
|
|
14
|
+
body.order_items.is_delivery_discount = false
|
|
15
|
+
|
|
11
16
|
promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
|
|
12
17
|
promoCalculation.offerCalculation(res, promotion, deliveryCost).then(final => {
|
|
13
18
|
|
|
@@ -69,11 +74,11 @@ function applyDistribution(body) {
|
|
|
69
74
|
}
|
|
70
75
|
mainCart.order_items = taxData
|
|
71
76
|
|
|
72
|
-
let delivery_cost = body.delivery_cost?body.delivery_cost:0;
|
|
73
|
-
|
|
74
|
-
if(final.is_delivery_discount){
|
|
75
|
-
delivery_cost = 0;
|
|
76
|
-
body.delivery_cost =
|
|
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
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
let service_amount = body.service_amount?body.service_amount:0;
|
|
@@ -143,18 +143,19 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
143
143
|
|
|
144
144
|
let foundItemsSum = 0
|
|
145
145
|
carts.item_details.filter((res) => {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
if(!res.purchase_points){
|
|
147
|
+
let itemId = res.item_id?.toString().length > 10 ? res.item_id : parseInt(res.item_id);
|
|
148
|
+
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))) {
|
|
149
|
+
foundItemsSum = foundItemsSum + res.item_price_with_modifier * res.quantity
|
|
150
|
+
return foundItemsSum
|
|
151
|
+
}
|
|
151
152
|
}
|
|
152
153
|
})
|
|
153
154
|
|
|
154
155
|
let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
|
|
155
156
|
let remaningOfferAmount = offerAmount
|
|
156
157
|
carts.item_details.forEach((element, index) => {
|
|
157
|
-
let itemId = element.item_id
|
|
158
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
158
159
|
if (!element.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(itemId) && element.isPackage == 0) || (offer.offer_products.package_items.includes(itemId) && element.isPackage == 1))) {
|
|
159
160
|
var itemAmount = 0;
|
|
160
161
|
|
|
@@ -749,7 +750,7 @@ function addItemQuantity(items, offer) {
|
|
|
749
750
|
item.toString().length > 10 ? item : parseInt(item, 10)
|
|
750
751
|
);
|
|
751
752
|
items.forEach(function (element) {
|
|
752
|
-
let itemId = element.item_id
|
|
753
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
753
754
|
if (offer.offer_products.offer_buy_products.includes(itemId) || offer.offer_products.offer_get_products.includes(itemId)) {
|
|
754
755
|
for (var i = 1; i <= element.quantity; i++) {
|
|
755
756
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
@@ -849,7 +850,7 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
849
850
|
cartItem.forEach((element, index) => {
|
|
850
851
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
851
852
|
|
|
852
|
-
let itemId = element.item_id
|
|
853
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
853
854
|
if (offer.offer_products.offer_discount_products.includes(itemId)) {
|
|
854
855
|
var offerAmount = 0;
|
|
855
856
|
let costWithOutModifier = 0;
|
|
@@ -971,7 +972,7 @@ function getFixedPriceProductOffer(cartObject, offer) {
|
|
|
971
972
|
|
|
972
973
|
cartItem.forEach((element, index) => {
|
|
973
974
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
974
|
-
let itemId = element.item_id
|
|
975
|
+
let itemId = element.item_id?.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
975
976
|
if (offer.offer_products.offer_discount_products.includes(itemId) || offer.offer_products.package_items.includes(itemId)) {
|
|
976
977
|
var offerAmount = 0;
|
|
977
978
|
var singleItemCost = element.item_price;
|
|
@@ -1018,23 +1019,35 @@ function getFixedPriceProductOffer(cartObject, offer) {
|
|
|
1018
1019
|
};
|
|
1019
1020
|
|
|
1020
1021
|
function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
|
|
1021
|
-
return new Promise(
|
|
1022
|
+
return new Promise((resolve, reject) => {
|
|
1022
1023
|
try {
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1024
|
+
let discountRoundOff = offer.discount_roundoff; // 1 = Round off, 0 = No rounding
|
|
1025
|
+
let deliveryDiscount = 0; // Default
|
|
1026
|
+
|
|
1027
|
+
if (cartObject.total >= offer.oo_min_amount && offer.oo_offer_type == 'percentage' && offer.oo_offer_value > 0) {
|
|
1028
|
+
// Calculate discount as a percentage of delivery cost
|
|
1029
|
+
deliveryDiscount = (deliveryCost * offer.oo_offer_value) / 100;
|
|
1030
|
+
|
|
1031
|
+
// Apply rounding if discountRoundOff is 1
|
|
1032
|
+
if (discountRoundOff == 1) {
|
|
1033
|
+
deliveryDiscount = Math.round(deliveryDiscount);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
cartObject.discount = 0;
|
|
1037
|
+
cartObject.delivery_discount = deliveryDiscount;
|
|
1026
1038
|
cartObject.is_delivery_discount = true;
|
|
1027
|
-
}else{
|
|
1028
|
-
cartObject.discount = 0
|
|
1039
|
+
} else {
|
|
1040
|
+
cartObject.discount = 0;
|
|
1029
1041
|
cartObject.delivery_discount = 0;
|
|
1030
1042
|
cartObject.is_delivery_discount = false;
|
|
1031
1043
|
}
|
|
1032
|
-
|
|
1044
|
+
|
|
1045
|
+
resolve(cartObject); // Return updated cartObject
|
|
1033
1046
|
} catch (error) {
|
|
1034
|
-
reject(error)
|
|
1047
|
+
reject(error);
|
|
1035
1048
|
}
|
|
1036
|
-
})
|
|
1037
|
-
}
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1038
1051
|
|
|
1039
1052
|
function setPackageDefaultDiscount(cart) {
|
|
1040
1053
|
return new Promise(async (resolve, reject) => {
|
|
@@ -1051,7 +1064,9 @@ function setPackageDefaultDiscount(cart) {
|
|
|
1051
1064
|
let itemTotal = 0;
|
|
1052
1065
|
if (item.package_items && item.package_items.length > 0) {
|
|
1053
1066
|
item.package_items.forEach(function (v) {
|
|
1054
|
-
|
|
1067
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1068
|
+
// itemTotal += ((item.quantity * v.quantity) * v.price);
|
|
1069
|
+
itemTotal += (item.quantity * v.price);
|
|
1055
1070
|
})
|
|
1056
1071
|
let packagePrice = item.quantity * item.item_price;
|
|
1057
1072
|
let totalDiscount = (packagePrice < itemTotal) ? (parseFloat(itemTotal) - parseFloat(packagePrice)).toFixed(2) : 0;
|
|
@@ -1089,10 +1104,11 @@ function distributeDiscount(packageItems, totalDiscount, itemTotal, quantity) {
|
|
|
1089
1104
|
let totalApplied = 0;
|
|
1090
1105
|
let singleUnit = (quantity * parseFloat(totalDiscount) / parseFloat(itemTotal)).toFixed(2);
|
|
1091
1106
|
for (const [index, item] of packageItems.entries()) {
|
|
1107
|
+
//this commemted code is sceniore when user can also increas item quantity i think which is wrong
|
|
1108
|
+
// let discountPerItem = (parseFloat(singleUnit) * (parseInt(item.quantity) * parseFloat(item.price))).toFixed(2);
|
|
1092
1109
|
let discountPerItem = (parseFloat(singleUnit) * parseFloat(item.price)).toFixed(2);
|
|
1093
1110
|
totalApplied = (parseFloat(totalApplied) + parseFloat(discountPerItem)).toFixed(2);
|
|
1094
1111
|
item.default_combo_discount = parseFloat(discountPerItem).toFixed(2);
|
|
1095
|
-
|
|
1096
1112
|
if (index == packageItems.length - 1) {
|
|
1097
1113
|
|
|
1098
1114
|
let remaningOfferAmount = (parseFloat(totalDiscount) - parseFloat(totalApplied)).toFixed(2);
|
package/package.json
CHANGED