foodbot-cart-calculations 1.0.40 → 1.0.42

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
@@ -7,8 +7,14 @@ 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;
11
+
12
+ if(body?.order_items?.delivery_discount)
13
+ body.order_items.delivery_discount = 0
14
+ body.order_items.is_delivery_discount = false
15
+
10
16
  promoCalculation.setPackageDefaultDiscount(body.order_items).then(res => {
11
- promoCalculation.offerCalculation(res, promotion).then(final => {
17
+ promoCalculation.offerCalculation(res, promotion, deliveryCost).then(final => {
12
18
 
13
19
  let taxSettings = (body.tax_settings && body.tax_settings.length > 0) ? body.tax_settings : []
14
20
  let taxType = (body.tax_type) ? body.tax_type : 1
@@ -67,8 +73,14 @@ function applyDistribution(body) {
67
73
  mainCart.cash_expected = parseFloat(taxData.cartTotal) + parseFloat(taxData.tax_amount);
68
74
  }
69
75
  mainCart.order_items = taxData
70
-
71
- let delivery_cost = body.delivery_cost?body.delivery_cost:0;
76
+
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
82
+ }
83
+
72
84
  let service_amount = body.service_amount?body.service_amount:0;
73
85
 
74
86
  //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
@@ -88,6 +88,12 @@ function offerCalculation(carts, offer) {
88
88
  }).catch(error => {
89
89
  reject(error);
90
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
+ })
91
97
  } else {
92
98
  carts.discount = 0
93
99
  resolve(carts)
@@ -1011,6 +1017,37 @@ function getFixedPriceProductOffer(cartObject, offer) {
1011
1017
  }
1012
1018
  };
1013
1019
 
1020
+ function getFreeDeliveryOffer(cartObject, offer, deliveryCost = 0) {
1021
+ return new Promise((resolve, reject) => {
1022
+ try {
1023
+ let discountRoundOff = offer.discount_roundoff; // 1 = Round off, 0 = No rounding
1024
+ let deliveryDiscount = 0; // Default
1025
+
1026
+ if (cartObject.total >= offer.oo_min_amount && offer.oo_offer_type == 'percentage' && offer.oo_offer_value > 0) {
1027
+ // Calculate discount as a percentage of delivery cost
1028
+ deliveryDiscount = (deliveryCost * offer.oo_offer_value) / 100;
1029
+
1030
+ // Apply rounding if discountRoundOff is 1
1031
+ if (discountRoundOff == 1) {
1032
+ deliveryDiscount = Math.round(deliveryDiscount);
1033
+ }
1034
+
1035
+ cartObject.discount = 0;
1036
+ cartObject.delivery_discount = deliveryDiscount;
1037
+ cartObject.is_delivery_discount = true;
1038
+ } else {
1039
+ cartObject.discount = 0;
1040
+ cartObject.delivery_discount = 0;
1041
+ cartObject.is_delivery_discount = false;
1042
+ }
1043
+
1044
+ resolve(cartObject); // Return updated cartObject
1045
+ } catch (error) {
1046
+ reject(error);
1047
+ }
1048
+ });
1049
+ }
1050
+
1014
1051
  function setPackageDefaultDiscount(cart) {
1015
1052
  return new Promise(async (resolve, reject) => {
1016
1053
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foodbot-cart-calculations",
3
- "version": "1.0.40",
3
+ "version": "1.0.42",
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": {