foodbot-cart-calculations 1.0.27 → 1.0.29

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 CHANGED
@@ -68,13 +68,9 @@ function applyDistribution(body) {
68
68
  }
69
69
  mainCart.order_items = taxData
70
70
 
71
-
72
71
  let delivery_cost = body.delivery_cost?body.delivery_cost:0;
73
72
  let service_amount = body.service_amount?body.service_amount:0;
74
73
 
75
- mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(delivery_cost) + parseFloat(service_amount);
76
- mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(delivery_cost) + parseFloat(service_amount);
77
-
78
74
  let tipValue = body.tip_value?body.tip_value:0;
79
75
 
80
76
  if (body.tip_type && body.tip_type == 'percentage') {
@@ -82,6 +78,11 @@ function applyDistribution(body) {
82
78
  tipValue = parseFloat(tipValue).toFixed(2)
83
79
  }
84
80
 
81
+ mainCart.tip_amount = parseFloat(tipValue).toFixed(2);
82
+
83
+ mainCart.final_amount = parseFloat(mainCart.final_amount) + parseFloat(delivery_cost) + parseFloat(service_amount);
84
+ mainCart.cash_expected = parseFloat(mainCart.cash_expected) + parseFloat(delivery_cost) + parseFloat(service_amount);
85
+
85
86
  taxData.total_after_tax = parseFloat(taxData.total_after_tax) + parseFloat(tipValue);
86
87
  taxData.total_after_tax = parseFloat(taxData.total_after_tax).toFixed(2);
87
88
 
@@ -96,7 +97,6 @@ function applyDistribution(body) {
96
97
 
97
98
  mainCart.discount = mainCart.order_items.discount;
98
99
  mainCart.order_items.total=parseFloat(mainCart.order_items.total).toFixed(2)
99
-
100
100
 
101
101
  resolve(mainCart);
102
102
  }).catch(err => {
@@ -28,6 +28,7 @@ function offerCalculation(carts, offer) {
28
28
  });
29
29
  }
30
30
  });
31
+
31
32
  if (offer && offer.offer_discount_type && offer.offer_discount_type == 'buy_x_get_y') {
32
33
  getOfferDetails(carts, offer).then(res => {
33
34
  carts = res
@@ -77,6 +78,12 @@ function offerCalculation(carts, offer) {
77
78
  }).catch(error => {
78
79
  reject(error);
79
80
  })
81
+ } else if (offer && offer.offer_discount_type && offer.offer_discount_type == 'fixed_price') {
82
+ getFixedPriceProductOffer(carts, offer).then(free => {
83
+ resolve(free)
84
+ }).catch(error => {
85
+ reject(error);
86
+ })
80
87
  } else {
81
88
  carts.discount = 0
82
89
  resolve(carts)
@@ -439,7 +446,7 @@ function getOfferDetails(carts, offer = []) {
439
446
  const offerBuyProducts = offer.offer_products.offer_buy_products.map(Number);
440
447
  const offerGetProducts = offer.offer_products.offer_get_products.map(Number);
441
448
  addItemQuantity(carts.item_details, offer).then((data) => {
442
- const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? -1 : 1);
449
+ const descending = data.sort((a, b) => (parseInt(b.amount) > parseInt(a.amount)) ? 1 : -1);
443
450
  let total = 0
444
451
  let buyItemAmount=0;
445
452
  let buyItemss =[]
@@ -462,8 +469,6 @@ function getOfferDetails(carts, offer = []) {
462
469
  };
463
470
 
464
471
 
465
-
466
-
467
472
  function getBuyxGetYOnCondition(descending, cartData, offer,total,buyItemAmount,buyItemss) {
468
473
  return new Promise((resolve, reject) => {
469
474
  let carts = cartData.item_details;
@@ -532,7 +537,7 @@ function getBuyxGetYOnCondition(descending, cartData, offer,total,buyItemAmount,
532
537
  // Handle absolute discount type
533
538
 
534
539
 
535
- buyItemAmount = buyItemAmount + desc[i].amount;
540
+ buyItemAmount = parseFloat(buyItemAmount) + desc[i].amount;
536
541
  desc[i].item.indexss = i;
537
542
  buyItemss.push(desc[i].item);
538
543
  }
@@ -907,6 +912,83 @@ function getFreeProductOffer(cartObject, offer) {
907
912
  }
908
913
  };
909
914
 
915
+ function getFixedPriceProductOffer(cartObject, offer) {
916
+ try {
917
+ let carts = cartObject.item_details;
918
+ carts.forEach(function (v) {
919
+ v.promotion_discount_modifier = 0;
920
+ v.promotion_discount = 0;
921
+ v.redeem_promotion_id = 0;
922
+ if (v.item_modifiers) v.item_modifiers.forEach((mod) => {
923
+ mod.promotion_discount = 0;
924
+ mod.redeem_promotion_id = 0;
925
+ });
926
+ });
927
+
928
+ if (carts && offer.oo_offer_type == "absolute") {
929
+ carts.map((a) => {
930
+ a.promotion_discount = 0;
931
+ a.redeem_promotion_id = 0;
932
+ a.remove = false;
933
+ });
934
+
935
+ return new Promise(function (resolve, reject) {
936
+ var total = 0;
937
+ var count = 0;
938
+ var fixedPrice = offer.oo_offer_value;
939
+
940
+ offer.offer_products.offer_discount_products = offer.offer_products.offer_discount_products.map(Number);
941
+ offer.offer_products.package_items = offer.offer_products.package_items.map(Number);
942
+
943
+ var cartItem = carts;
944
+
945
+ cartItem.forEach((element, index) => {
946
+ var ItemIndex = carts.findIndex((x) => x.item_id == element.item_id && !x.remove);
947
+ if (offer.offer_products.offer_discount_products.includes(parseInt(element.item_id)) || offer.offer_products.package_items.includes(parseInt(element.item_id))) {
948
+ var offerAmount = 0;
949
+ var singleItemCost = element.item_price;
950
+
951
+ if(singleItemCost >= fixedPrice){
952
+ offerAmount = (singleItemCost * element.quantity) - (fixedPrice * element.quantity);
953
+ }
954
+
955
+ total = total + parseFloat(offerAmount);
956
+
957
+ carts[ItemIndex].promotion_discount_modifier = parseFloat(offerAmount).toFixed(2);
958
+ carts[ItemIndex].promotion_discount = parseFloat(offerAmount).toFixed(2);
959
+ carts[ItemIndex].remove = true;
960
+
961
+ if (offerAmount > 0) {
962
+ carts[ItemIndex].redeem_promotion_id = offer.offer_id;
963
+ } else {
964
+ carts[ItemIndex].item_modifiers.map((res) => {
965
+ res.promotion_discount = 0;
966
+ res.redeem_promotion_id = 0;
967
+ });
968
+ }
969
+ count++;
970
+ } else {
971
+ count++;
972
+ }
973
+
974
+ if (count == carts.length && carts) {
975
+ cartObject.item_details = carts
976
+ cartObject.discount = total
977
+ resolve(cartObject);
978
+ }
979
+ });
980
+ });
981
+ } else {
982
+ return new Promise(function (resolve, reject) {
983
+ cartObject.discount = 0
984
+ resolve(cartObject);
985
+ });
986
+ }
987
+ } catch (error) {
988
+ reject(error)
989
+ }
990
+ };
991
+
910
992
  function setPackageDefaultDiscount(cart) {
911
993
  return new Promise(async (resolve, reject) => {
912
994
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "Package for cart calculations in which it performs discount distribution and tax distribution on order",
5
5
  "main": "index.js",
6
6
  "scripts": {