washday-sdk 0.0.194 → 0.0.196

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.
@@ -2,28 +2,39 @@ import { DiscountCodeTypes } from "../../enum";
2
2
  import { calculateTotalTaxesIncluded } from "./calculateTotalTaxesIncluded";
3
3
  import { calculateTotalTaxesOverPrice } from "./calculateTotalTaxesOverPrice";
4
4
  import { getCreditApplied, getProductLineTotals, getShippingCost } from "./helpers";
5
- export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasShippingCost, storeDiscounts = [], discountCodeObj) => {
5
+ export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasShippingCost, storeDiscounts = [], discountCodeObj, redeemPointsDiscount = 0 // 💡 NUEVO parámetro
6
+ ) => {
6
7
  var _a;
7
8
  try {
8
9
  const appliedOrderDiscounts = {};
9
- const productTableCalculator = (storeSettings === null || storeSettings === void 0 ? void 0 : storeSettings.taxesType) === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
10
+ const productTableCalculator = (storeSettings === null || storeSettings === void 0 ? void 0 : storeSettings.taxesType) === 'over_price'
11
+ ? calculateTotalTaxesOverPrice
12
+ : calculateTotalTaxesIncluded;
10
13
  const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
11
- // PRODUCT LINE TOTALS
14
+ // === PRODUCT LINE TOTALS ===
12
15
  const { totalQuantity, totalImportWithDiscount, totalImportWithoutDiscount, totalImporTaxes, totalDiscountAmount, totalSubtotalAmount } = getProductLineTotals(productTableImports);
13
- // DISCOUNT TOTAL AND SHIPPING SERVICE COST
16
+ // === DISCOUNT CODE (monetario tipo NUMBER que se aplica una sola vez) ===
14
17
  let discountCodeAmount = 0;
15
- let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
16
- // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
17
- if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
18
- const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr) => discountCodeObj.products.includes(curr._id));
18
+ if (discountCodeObj &&
19
+ discountCodeObj.type === DiscountCodeTypes.NUMBER &&
20
+ discountCodeObj.applyOnceOnOrder) {
21
+ const includesProducts = discountCodeObj.applyToAllProducts ||
22
+ order.products.some((curr) => discountCodeObj.products.includes(curr._id));
19
23
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
20
24
  }
21
- // ORDER TOTAL CALCULATION AND CREDIT APPLIED
22
- let orderTotalWithOutCredit = +(totalImportWithDiscount + shippingCost - discountCodeAmount).toFixed(2);
23
- let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
24
- let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
25
- return Object.assign(Object.assign({}, order), { totalQuantity, customerDiscount: order.discountCode ? 0 : (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount, productTotal: totalImportWithDiscount, taxesTotal: totalImporTaxes, total: orderTotal, // Total final (ya sea sumando o no el IVA)
26
- creditApplied, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts, subtotal: totalSubtotalAmount });
25
+ // === SHIPPING COST ===
26
+ const shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
27
+ // === TOTAL ANTES DE CRÉDITOS/PUNTOS ===
28
+ const orderTotalWithOutCredit = +(totalImportWithDiscount +
29
+ shippingCost -
30
+ discountCodeAmount).toFixed(2);
31
+ // === APLICAR CRÉDITO Y PUNTOS REDIMIDOS ===
32
+ const creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
33
+ const orderTotal = +(orderTotalWithOutCredit -
34
+ creditApplied -
35
+ redeemPointsDiscount).toFixed(2);
36
+ // === RETURN FINAL OBJECT ===
37
+ return Object.assign(Object.assign({}, order), { totalQuantity, customerDiscount: order.discountCode ? 0 : (_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount, productTotal: totalImportWithDiscount, taxesTotal: totalImporTaxes, total: orderTotal, creditApplied, redeemPointsApplied: redeemPointsDiscount, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts, subtotal: totalSubtotalAmount });
27
38
  }
28
39
  catch (error) {
29
40
  throw error;
@@ -1,3 +1,4 @@
1
+ import { DiscountCodeTypes } from "../../enum";
1
2
  import { getProductTaxesPercentage } from "./helpers";
2
3
  export const calculateTotalTaxesIncluded = (order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj) => {
3
4
  const productTableImports = [...order.products, ...order.buyAndGetProducts].map((current) => {
@@ -52,6 +53,9 @@ export const calculateTotalTaxesIncluded = (order, selectedCustomer, storeSettin
52
53
  discPercentageInteger = 1;
53
54
  }
54
55
  }
56
+ else if (discountCodeObj.type === DiscountCodeTypes.FREE_ITEM && (current.isFreeItem)) {
57
+ discPercentageInteger = 1;
58
+ }
55
59
  }
56
60
  // === NUEVO: Aplicar descuento sobre el precio total ajustado (producto base + extra prorrateado) ===
57
61
  // Si hay cantidad, repartir el extra neto entre cada unidad
@@ -51,6 +51,9 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
51
51
  discPercentageInteger = 1;
52
52
  }
53
53
  }
54
+ else if (discountCodeObj.type === DiscountCodeTypes.FREE_ITEM && current.isFreeItem) {
55
+ discPercentageInteger = 1;
56
+ }
54
57
  }
55
58
  // Calcular el descuento por unidad sobre el precio ajustado
56
59
  const productDiscount = adjustedPrice * discPercentageInteger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "washday-sdk",
3
- "version": "0.0.194",
3
+ "version": "0.0.196",
4
4
  "description": "Washday utilities functions and API",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -11,14 +11,27 @@ export const calculateOrderTotal = (
11
11
  storeSettings: IStore,
12
12
  hasShippingCost: boolean,
13
13
  storeDiscounts: any[] = [],
14
- discountCodeObj: any
14
+ discountCodeObj: any,
15
+ redeemPointsDiscount: number = 0 // 💡 NUEVO parámetro
15
16
  ): any => {
16
17
  try {
17
18
  const appliedOrderDiscounts: any = {};
18
- const productTableCalculator = storeSettings?.taxesType === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
19
- const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
20
19
 
21
- // PRODUCT LINE TOTALS
20
+ const productTableCalculator =
21
+ storeSettings?.taxesType === 'over_price'
22
+ ? calculateTotalTaxesOverPrice
23
+ : calculateTotalTaxesIncluded;
24
+
25
+ const productTableImports = productTableCalculator(
26
+ order,
27
+ selectedCustomer,
28
+ storeSettings,
29
+ storeDiscounts,
30
+ appliedOrderDiscounts,
31
+ discountCodeObj
32
+ );
33
+
34
+ // === PRODUCT LINE TOTALS ===
22
35
  const {
23
36
  totalQuantity,
24
37
  totalImportWithDiscount,
@@ -28,30 +41,59 @@ export const calculateOrderTotal = (
28
41
  totalSubtotalAmount
29
42
  } = getProductLineTotals(productTableImports);
30
43
 
31
- // DISCOUNT TOTAL AND SHIPPING SERVICE COST
44
+ // === DISCOUNT CODE (monetario tipo NUMBER que se aplica una sola vez) ===
32
45
  let discountCodeAmount = 0;
33
- let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
46
+ if (
47
+ discountCodeObj &&
48
+ discountCodeObj.type === DiscountCodeTypes.NUMBER &&
49
+ discountCodeObj.applyOnceOnOrder
50
+ ) {
51
+ const includesProducts =
52
+ discountCodeObj.applyToAllProducts ||
53
+ order.products.some((curr: any) =>
54
+ discountCodeObj.products.includes(curr._id)
55
+ );
34
56
 
35
- // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
36
- if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
37
- const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr: any) => discountCodeObj.products.includes(curr._id));
38
57
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
39
58
  }
40
59
 
41
- // ORDER TOTAL CALCULATION AND CREDIT APPLIED
42
- let orderTotalWithOutCredit = +(totalImportWithDiscount + shippingCost - discountCodeAmount).toFixed(2);
43
- let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
44
- let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
60
+ // === SHIPPING COST ===
61
+ const shippingCost = getShippingCost(
62
+ discountCodeObj,
63
+ hasShippingCost,
64
+ storeSettings
65
+ );
66
+
67
+ // === TOTAL ANTES DE CRÉDITOS/PUNTOS ===
68
+ const orderTotalWithOutCredit = +(
69
+ totalImportWithDiscount +
70
+ shippingCost -
71
+ discountCodeAmount
72
+ ).toFixed(2);
73
+
74
+ // === APLICAR CRÉDITO Y PUNTOS REDIMIDOS ===
75
+ const creditApplied = getCreditApplied(
76
+ selectedCustomer,
77
+ orderTotalWithOutCredit
78
+ );
79
+
80
+ const orderTotal = +(
81
+ orderTotalWithOutCredit -
82
+ creditApplied -
83
+ redeemPointsDiscount
84
+ ).toFixed(2);
45
85
 
86
+ // === RETURN FINAL OBJECT ===
46
87
  return {
47
88
  ...order,
48
89
  totalQuantity,
49
90
  customerDiscount: order.discountCode ? 0 : selectedCustomer?.customer?.discount,
50
- productTotal: totalImportWithDiscount, // Total neto con descuento, que luego se convierte a bruto según el método
51
- taxesTotal: totalImporTaxes, // Monto de impuestos aplicado
52
- total: orderTotal, // Total final (ya sea sumando o no el IVA)
91
+ productTotal: totalImportWithDiscount,
92
+ taxesTotal: totalImporTaxes,
93
+ total: orderTotal,
53
94
  creditApplied,
54
- productTotalWithoutDiscount: totalImportWithoutDiscount, // Precio base (neto)
95
+ redeemPointsApplied: redeemPointsDiscount, // NUEVO CAMPO
96
+ productTotalWithoutDiscount: totalImportWithoutDiscount,
55
97
  totalDiscountAmount: totalDiscountAmount + discountCodeAmount,
56
98
  shippingServiceTotal: shippingCost,
57
99
  appliedOrderDiscounts,
@@ -60,4 +102,4 @@ export const calculateOrderTotal = (
60
102
  } catch (error) {
61
103
  throw error;
62
104
  }
63
- };
105
+ };
@@ -1,3 +1,4 @@
1
+ import { DiscountCodeTypes } from "../../enum";
1
2
  import { IOrderProduct } from "../../interfaces/Product";
2
3
  import { getProductTaxesPercentage } from "./helpers";
3
4
 
@@ -62,6 +63,8 @@ export const calculateTotalTaxesIncluded = (
62
63
  if (discountType === 'free' && current.isBuyAndGetProduct) {
63
64
  discPercentageInteger = 1;
64
65
  }
66
+ } else if (discountCodeObj.type === DiscountCodeTypes.FREE_ITEM && ((current as any).isFreeItem)) {
67
+ discPercentageInteger = 1;
65
68
  }
66
69
  }
67
70
 
@@ -29,9 +29,9 @@ export const calculateTotalTaxesOverPrice = (
29
29
  if (!order.discountCode) {
30
30
  const discountsToApply = storeDiscounts.filter(
31
31
  (discount: any) => discount.isActive && discount.products.some((p: any) => {
32
- if(typeof p === 'string') {
32
+ if (typeof p === 'string') {
33
33
  return p === current._id
34
- } else if(typeof p === 'object') {
34
+ } else if (typeof p === 'object') {
35
35
  return p._id === current._id
36
36
  }
37
37
  return false
@@ -59,6 +59,8 @@ export const calculateTotalTaxesOverPrice = (
59
59
  if (condition.getDiscountType === BuyAndGetConditionsTypes.FREE && current.isBuyAndGetProduct) {
60
60
  discPercentageInteger = 1;
61
61
  }
62
+ } else if (discountCodeObj.type === DiscountCodeTypes.FREE_ITEM && current.isFreeItem) {
63
+ discPercentageInteger = 1;
62
64
  }
63
65
  }
64
66