foodbot-cart-calculations 1.0.39 → 1.0.41
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 +9 -2
- package/functions/promotionCalculation.js +68 -26
- package/package.json +1 -1
package/calculations.js
CHANGED
|
@@ -7,8 +7,9 @@ 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;
|
|
10
11
|
promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
|
|
11
|
-
promoCalculation.offerCalculation(res, promotion).then(final => {
|
|
12
|
+
promoCalculation.offerCalculation(res, promotion, deliveryCost).then(final => {
|
|
12
13
|
|
|
13
14
|
let taxSettings = (body.tax_settings && body.tax_settings.length > 0) ? body.tax_settings : []
|
|
14
15
|
let taxType = (body.tax_type) ? body.tax_type : 1
|
|
@@ -67,8 +68,14 @@ function applyDistribution(body) {
|
|
|
67
68
|
mainCart.cash_expected = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount);
|
|
68
69
|
}
|
|
69
70
|
mainCart.order_items = taxData
|
|
70
|
-
|
|
71
|
+
|
|
71
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 = 0
|
|
77
|
+
}
|
|
78
|
+
|
|
72
79
|
let service_amount = body.service_amount?body.service_amount:0;
|
|
73
80
|
|
|
74
81
|
//we took 2 value because in case of absolute we use tipValue
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function offerCalculation(carts, offer) {
|
|
1
|
+
function offerCalculation(carts, offer, deliveryCost = 0) {
|
|
2
2
|
return new Promise(function (resolve, reject) {
|
|
3
3
|
try {
|
|
4
4
|
carts.total = 0
|
|
@@ -55,6 +55,7 @@ function offerCalculation(carts, offer) {
|
|
|
55
55
|
reject(error);
|
|
56
56
|
})
|
|
57
57
|
} else if (offer && offer.offer_discount_type && (offer.offer_discount_type == 'discount' || offer.offer_discount_type == 'manual')) {
|
|
58
|
+
|
|
58
59
|
getCategorySpeificOffer(carts, offer).then(res => {
|
|
59
60
|
carts = res
|
|
60
61
|
|
|
@@ -87,6 +88,12 @@ function offerCalculation(carts, offer) {
|
|
|
87
88
|
}).catch(error => {
|
|
88
89
|
reject(error);
|
|
89
90
|
})
|
|
91
|
+
} else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'free_delivery') {
|
|
92
|
+
getFreeDeliveryOffer(carts, offer, deliveryCost).then(free => {
|
|
93
|
+
resolve(free)
|
|
94
|
+
}).catch(error => {
|
|
95
|
+
reject(error);
|
|
96
|
+
})
|
|
90
97
|
} else {
|
|
91
98
|
carts.discount = 0
|
|
92
99
|
resolve(carts)
|
|
@@ -120,13 +127,15 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
120
127
|
if (parseFloat(offer.oo_min_amount) <= parseFloat(afterTotal)) {
|
|
121
128
|
|
|
122
129
|
if (offer.offer_discount_type != 'manual')
|
|
123
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
130
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
131
|
+
val.toString().length > 10 ? val : Number(val)
|
|
132
|
+
);
|
|
124
133
|
|
|
125
134
|
if (offer.offer_discount_type != 'manual')
|
|
126
135
|
if (offer.offer_products.package_items) {
|
|
127
|
-
offer.offer_products.package_items = offer.offer_products.package_items.map(
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
137
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
138
|
+
);
|
|
130
139
|
}
|
|
131
140
|
else {
|
|
132
141
|
offer.offer_products.package_items = []
|
|
@@ -134,7 +143,8 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
134
143
|
|
|
135
144
|
let foundItemsSum = 0
|
|
136
145
|
carts.item_details.filter((res) => {
|
|
137
|
-
|
|
146
|
+
let itemId = res.item_id.toString().length > 10 ? res.item_id : parseInt(res.item_id);
|
|
147
|
+
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))) {
|
|
138
148
|
|
|
139
149
|
foundItemsSum = foundItemsSum + res.item_price_with_modifier * res.quantity
|
|
140
150
|
return foundItemsSum
|
|
@@ -144,8 +154,8 @@ function getCategorySpeificOffer(carts, offer) {
|
|
|
144
154
|
let offerAmount = (offer.oo_offer_value <= foundItemsSum) ? offer.oo_offer_value : foundItemsSum;
|
|
145
155
|
let remaningOfferAmount = offerAmount
|
|
146
156
|
carts.item_details.forEach((element, index) => {
|
|
147
|
-
|
|
148
|
-
if (!element.event && (offer.offer_discount_type == 'manual' || (offer.offer_products.offer_discount_products.includes(
|
|
157
|
+
let itemId = element.item_id.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
158
|
+
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))) {
|
|
149
159
|
var itemAmount = 0;
|
|
150
160
|
|
|
151
161
|
// Apply discount on Packages
|
|
@@ -732,10 +742,15 @@ function addItemQuantity(items, offer) {
|
|
|
732
742
|
try {
|
|
733
743
|
var finalItems = [];
|
|
734
744
|
var count = 0;
|
|
735
|
-
offer.offer_products.offer_buy_products=offer.offer_products.offer_buy_products.map(item =>
|
|
736
|
-
|
|
745
|
+
offer.offer_products.offer_buy_products=offer.offer_products.offer_buy_products.map(item =>
|
|
746
|
+
item.toString().length > 10 ? item : parseInt(item, 10)
|
|
747
|
+
);
|
|
748
|
+
offer.offer_products.offer_get_products=offer.offer_products.offer_get_products.map(item =>
|
|
749
|
+
item.toString().length > 10 ? item : parseInt(item, 10)
|
|
750
|
+
);
|
|
737
751
|
items.forEach(function (element) {
|
|
738
|
-
|
|
752
|
+
let itemId = element.item_id.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
753
|
+
if (offer.offer_products.offer_buy_products.includes(itemId) || offer.offer_products.offer_get_products.includes(itemId)) {
|
|
739
754
|
for (var i = 1; i <= element.quantity; i++) {
|
|
740
755
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
741
756
|
var amount = element.item_price;
|
|
@@ -743,9 +758,9 @@ function addItemQuantity(items, offer) {
|
|
|
743
758
|
if (offer.offer_products.modifier_category.includes('all') || offer.offer_products.modifier_category.includes('All')) {
|
|
744
759
|
amount = element.item_price_with_modifier;
|
|
745
760
|
} else {
|
|
746
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
747
|
-
|
|
748
|
-
|
|
761
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
762
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
763
|
+
);
|
|
749
764
|
var modifiers = [];
|
|
750
765
|
element.item_modifiers.forEach(function (modifier) {
|
|
751
766
|
|
|
@@ -761,8 +776,8 @@ function addItemQuantity(items, offer) {
|
|
|
761
776
|
'item_id': element.item_id,
|
|
762
777
|
'amount': amount,
|
|
763
778
|
'item': item,
|
|
764
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
765
|
-
'get': offer.offer_products.offer_get_products.includes(
|
|
779
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
780
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
766
781
|
});
|
|
767
782
|
} else {
|
|
768
783
|
finalItems.push({
|
|
@@ -770,8 +785,8 @@ function addItemQuantity(items, offer) {
|
|
|
770
785
|
'item_id': element.item_id,
|
|
771
786
|
// / element.quantity
|
|
772
787
|
'amount': element.item_price,
|
|
773
|
-
'buy': offer.offer_products.offer_buy_products.includes(
|
|
774
|
-
'get': offer.offer_products.offer_get_products.includes(
|
|
788
|
+
'buy': offer.offer_products.offer_buy_products.includes(itemId),
|
|
789
|
+
'get': offer.offer_products.offer_get_products.includes(itemId)
|
|
775
790
|
});
|
|
776
791
|
}
|
|
777
792
|
}
|
|
@@ -812,7 +827,9 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
812
827
|
var total = 0;
|
|
813
828
|
var count = 0;
|
|
814
829
|
var offerAmount = offer.oo_offer_value;
|
|
815
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
830
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
831
|
+
val.toString().length > 10 ? val : Number(val)
|
|
832
|
+
);
|
|
816
833
|
|
|
817
834
|
var cartItem = carts;
|
|
818
835
|
if (offer.offer_products.order_modifiercost_use == 1) {
|
|
@@ -832,7 +849,8 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
832
849
|
cartItem.forEach((element, index) => {
|
|
833
850
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
834
851
|
|
|
835
|
-
|
|
852
|
+
let itemId = element.item_id.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
853
|
+
if (offer.offer_products.offer_discount_products.includes(itemId)) {
|
|
836
854
|
var offerAmount = 0;
|
|
837
855
|
let costWithOutModifier = 0;
|
|
838
856
|
var singleItemCost = element.item_price;
|
|
@@ -851,9 +869,9 @@ function getFreeProductOffer(cartObject, offer) {
|
|
|
851
869
|
singleItemCost = singleItemCost + singModPrice;
|
|
852
870
|
});
|
|
853
871
|
} else {
|
|
854
|
-
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(
|
|
855
|
-
|
|
856
|
-
|
|
872
|
+
offer.offer_products.modifier_category = offer.offer_products.modifier_category.map(x =>
|
|
873
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
874
|
+
);
|
|
857
875
|
|
|
858
876
|
element.item_modifiers.forEach((modifier, modiIndex) => {
|
|
859
877
|
if (offer.offer_products.modifier_category.includes(modifier.modifier_category_id)) {
|
|
@@ -942,14 +960,19 @@ function getFixedPriceProductOffer(cartObject, offer) {
|
|
|
942
960
|
var count = 0;
|
|
943
961
|
var fixedPrice = offer.oo_offer_value;
|
|
944
962
|
|
|
945
|
-
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(
|
|
946
|
-
|
|
963
|
+
offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(val =>
|
|
964
|
+
val.toString().length > 10 ? val : Number(val)
|
|
965
|
+
);
|
|
966
|
+
offer.offer_products.package_items = offer.offer_products.package_items.map(x =>
|
|
967
|
+
x.toString().length > 10 ? x : parseInt(x, 10)
|
|
968
|
+
);
|
|
947
969
|
|
|
948
970
|
var cartItem = carts;
|
|
949
971
|
|
|
950
972
|
cartItem.forEach((element, index) => {
|
|
951
973
|
var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
|
|
952
|
-
|
|
974
|
+
let itemId = element.item_id.toString().length > 10 ? element.item_id : parseInt(element.item_id);
|
|
975
|
+
if (offer.offer_products.offer_discount_products.includes(itemId) || offer.offer_products.package_items.includes(itemId)) {
|
|
953
976
|
var offerAmount = 0;
|
|
954
977
|
var singleItemCost = element.item_price;
|
|
955
978
|
|
|
@@ -994,6 +1017,25 @@ function getFixedPriceProductOffer(cartObject, offer) {
|
|
|
994
1017
|
}
|
|
995
1018
|
};
|
|
996
1019
|
|
|
1020
|
+
function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
|
|
1021
|
+
return new Promise(function (resolve, reject) {
|
|
1022
|
+
try {
|
|
1023
|
+
if(cartObject.total >= offer.oo_min_amount){
|
|
1024
|
+
cartObject.discount = 0
|
|
1025
|
+
cartObject.delivery_discount = deliveryCost;
|
|
1026
|
+
cartObject.is_delivery_discount = true;
|
|
1027
|
+
}else{
|
|
1028
|
+
cartObject.discount = 0
|
|
1029
|
+
cartObject.delivery_discount = 0;
|
|
1030
|
+
cartObject.is_delivery_discount = false;
|
|
1031
|
+
}
|
|
1032
|
+
resolve(cartObject); // Resolve the promise with updated cartObject
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
reject(error)
|
|
1035
|
+
}
|
|
1036
|
+
})
|
|
1037
|
+
};
|
|
1038
|
+
|
|
997
1039
|
function setPackageDefaultDiscount(cart) {
|
|
998
1040
|
return new Promise(async (resolve, reject) => {
|
|
999
1041
|
try {
|
package/package.json
CHANGED