washday-sdk 0.0.163 → 0.0.164

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.
@@ -9,7 +9,7 @@ export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasS
9
9
  const productTableCalculator = (storeSettings === null || storeSettings === void 0 ? void 0 : storeSettings.taxesType) === 'over_price' ? calculateTotalTaxesOverPrice : calculateTotalTaxesIncluded;
10
10
  const productTableImports = productTableCalculator(order, selectedCustomer, storeSettings, storeDiscounts, appliedOrderDiscounts, discountCodeObj);
11
11
  // PRODUCT LINE TOTALS
12
- const { totalQuantity, totalImportWithDiscount, totalImportWithoutDiscount, totalImporTaxes, totalDiscountAmount } = getProductLineTotals(productTableImports);
12
+ const { totalQuantity, totalImportWithDiscount, totalImportWithoutDiscount, totalImporTaxes, totalDiscountAmount, totalSubtotalAmount } = getProductLineTotals(productTableImports);
13
13
  // DISCOUNT TOTAL AND SHIPPING SERVICE COST
14
14
  let discountCodeAmount = 0;
15
15
  let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
@@ -21,7 +21,8 @@ export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasS
21
21
  let orderTotalWithOutCredit = +(totalImportWithDiscount + shippingCost - discountCodeAmount).toFixed(2);
22
22
  let creditApplied = getCreditApplied(selectedCustomer, orderTotalWithOutCredit);
23
23
  let orderTotal = +(orderTotalWithOutCredit - creditApplied).toFixed(2);
24
- 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, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts });
24
+ 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)
25
+ creditApplied, productTotalWithoutDiscount: totalImportWithoutDiscount, totalDiscountAmount: totalDiscountAmount + discountCodeAmount, shippingServiceTotal: shippingCost, appliedOrderDiscounts, subtotal: totalSubtotalAmount });
25
26
  }
26
27
  catch (error) {
27
28
  throw error;
@@ -66,7 +66,8 @@ export const calculateTotalTaxesIncluded = (order, selectedCustomer, storeSettin
66
66
  productLineImportTotal, // original gross total (no discount)
67
67
  productLineTotalWithDiscount, // gross total after discount on product (extra not discounted)
68
68
  productLineTaxesTotal: totalTaxesApplied,
69
- lineDiscountAmount: lineDiscount
69
+ lineDiscountAmount: lineDiscount,
70
+ productLineSubtotal: totalNetWithoutDiscount // All line subtotal (extra amount considered, without discounts and taxes)
70
71
  };
71
72
  });
72
73
  return productTableImports;
@@ -6,10 +6,10 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
6
6
  let discPercentageInteger = 0;
7
7
  let productPercentageDiscount = 0;
8
8
  let customerDiscount = 0;
9
- let qty = current.qty;
10
- // Precio base sin impuestos y sin extraAmount
9
+ const qty = current.qty; // cantidad
10
+ // Precio base sin impuestos (en modo over_price el precio es neto)
11
11
  const productBasePrice = order.express ? current.expressPrice : current.price;
12
- // Calcular descuentos solo sobre el producto
12
+ // Cálculo de descuento sobre el producto (aplicable solo sobre el precio base)
13
13
  if (!order.discountCode) {
14
14
  const discountsToApply = storeDiscounts.filter((discount) => discount.products.includes(current._id) && discount.isActive);
15
15
  customerDiscount = ((_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount) || 0;
@@ -17,6 +17,7 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
17
17
  return discount.type === 'percentage' ? acc + discount.value : acc;
18
18
  }, 0);
19
19
  discPercentageInteger = +((productPercentageDiscount + customerDiscount) / 100).toFixed(2);
20
+ discountsToApply.forEach((discount) => (appliedOrderDiscounts[discount._id] = discount));
20
21
  }
21
22
  else {
22
23
  if (discountCodeObj.type === DiscountCodeTypes.PERCENTAGE) {
@@ -34,27 +35,30 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
34
35
  }
35
36
  }
36
37
  }
37
- // Calcular descuento solo sobre el producto
38
+ // Calcular descuento por unidad y precio descontado
38
39
  const productDiscount = productBasePrice * discPercentageInteger;
39
40
  const discountedProductPrice = productBasePrice - productDiscount;
40
- // Calcular subtotal: se aplica el descuento al producto, y luego se suma el extraAmount (que se cobra completo)
41
- const subtotal = (discountedProductPrice * qty) + current.extraAmount;
42
- // Calcular impuestos sobre el subtotal final
41
+ // Subtotal para la línea (precio con descuento * cantidad + extraAmount) SIN impuestos
42
+ const subtotal = (discountedProductPrice * qty) + (current.extraAmount || 0);
43
+ // Calcular impuestos sobre el subtotal
43
44
  const taxPercentage = getProductTaxesPercentage(current, storeSettings) / 100;
44
45
  const taxes = subtotal * taxPercentage;
45
- // Resultados finales
46
- const totalTaxesApplied = +(taxes).toFixed(2);
46
+ // Resultados finales:
47
+ const totalTaxesApplied = +taxes.toFixed(2);
47
48
  const lineDiscount = +(productDiscount * qty).toFixed(2);
48
- // Se suma el extraAmount a la parte del producto descontado para obtener el valor total a cobrar de la línea
49
- //const prodLinePriceWithDiscount = +(((discountedProductPrice * qty) + current.extraAmount)).toFixed(2);
50
- const prodLinePriceWithDiscount = +((((discountedProductPrice * qty) + current.extraAmount) * (1 + taxPercentage))).toFixed(2);
49
+ // Precio total con descuento, reaplicando el IVA (es el total final a cobrar)
50
+ const prodLinePriceWithDiscount = +((subtotal * (1 + taxPercentage))).toFixed(2);
51
51
  return {
52
52
  product: current,
53
53
  qty,
54
- productLineImportTotal: (productBasePrice * qty) + current.extraAmount,
54
+ // Total bruto original sin descuento (precio base * cantidad + extraAmount)
55
+ productLineImportTotal: (productBasePrice * qty) + (current.extraAmount || 0),
56
+ // Total final con descuento, que se obtiene al sumar IVA al subtotal neto
55
57
  productLineTotalWithDiscount: prodLinePriceWithDiscount,
56
58
  productLineTaxesTotal: totalTaxesApplied,
57
- lineDiscountAmount: lineDiscount
59
+ lineDiscountAmount: lineDiscount,
60
+ // Subtotal: precio base (neto, con descuento) sin aplicar IVA
61
+ productLineSubtotal: subtotal
58
62
  };
59
63
  });
60
64
  return productTableImports;
@@ -31,12 +31,16 @@ export const getProductLineTotals = (productLinesTotals) => {
31
31
  const totalDiscountAmount = +productLinesTotals
32
32
  .reduce((acum, line) => acum + line.lineDiscountAmount, 0)
33
33
  .toFixed(2);
34
+ const totalSubtotalAmount = +productLinesTotals
35
+ .reduce((acum, line) => acum + line.productLineSubtotal, 0)
36
+ .toFixed(2);
34
37
  return {
35
38
  totalQuantity,
36
39
  totalImportWithDiscount,
37
40
  totalImportWithoutDiscount,
38
41
  totalImporTaxes,
39
- totalDiscountAmount
42
+ totalDiscountAmount,
43
+ totalSubtotalAmount
40
44
  };
41
45
  };
42
46
  export const getShippingCost = (discountObject, requireShippingService, store) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "washday-sdk",
3
- "version": "0.0.163",
3
+ "version": "0.0.164",
4
4
  "description": "Washday utilities functions and API",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -9,6 +9,7 @@ export interface ProductLineTotals {
9
9
  productLineTotalWithDiscount: number,
10
10
  productLineTaxesTotal: number,
11
11
  lineDiscountAmount: number
12
+ productLineSubtotal: number
12
13
  }
13
14
 
14
15
 
@@ -24,7 +24,8 @@ export const calculateOrderTotal = (
24
24
  totalImportWithDiscount,
25
25
  totalImportWithoutDiscount,
26
26
  totalImporTaxes,
27
- totalDiscountAmount
27
+ totalDiscountAmount,
28
+ totalSubtotalAmount
28
29
  } = getProductLineTotals(productTableImports);
29
30
 
30
31
  // DISCOUNT TOTAL AND SHIPPING SERVICE COST
@@ -45,14 +46,15 @@ export const calculateOrderTotal = (
45
46
  ...order,
46
47
  totalQuantity,
47
48
  customerDiscount: order.discountCode ? 0 : selectedCustomer?.customer?.discount,
48
- productTotal: totalImportWithDiscount,
49
- taxesTotal: totalImporTaxes,
50
- total: orderTotal,
49
+ productTotal: totalImportWithDiscount, // Total neto con descuento, que luego se convierte a bruto según el método
50
+ taxesTotal: totalImporTaxes, // Monto de impuestos aplicado
51
+ total: orderTotal, // Total final (ya sea sumando o no el IVA)
51
52
  creditApplied,
52
- productTotalWithoutDiscount: totalImportWithoutDiscount,
53
+ productTotalWithoutDiscount: totalImportWithoutDiscount, // Precio base (neto)
53
54
  totalDiscountAmount: totalDiscountAmount + discountCodeAmount,
54
55
  shippingServiceTotal: shippingCost,
55
- appliedOrderDiscounts
56
+ appliedOrderDiscounts,
57
+ subtotal: totalSubtotalAmount
56
58
  };
57
59
  } catch (error) {
58
60
  throw error;
@@ -13,16 +13,16 @@ export const calculateTotalTaxesIncluded = (
13
13
  const qty = current.qty || 0;
14
14
  const taxPercentage = getProductTaxesPercentage(current, storeSettings);
15
15
  const taxFactor = 1 + taxPercentage / 100;
16
-
16
+
17
17
  // Convert extraAmount from gross to net (assume extraAmount is not discounted)
18
18
  const extraAmountGross = current.extraAmount || 0;
19
19
  const extraAmountNet = extraAmountGross / taxFactor;
20
-
20
+
21
21
  // Get the product’s gross price (excluding extraAmount)
22
22
  const productGrossPrice = order.express ? current.expressPrice : current.price;
23
23
  // Convert product gross price to net
24
24
  const unitNetPrice = productGrossPrice / taxFactor;
25
-
25
+
26
26
  // Determine discount percentage (if any) – discount is applied only on product net price.
27
27
  let discPercentageInteger = 0;
28
28
  let productPercentageDiscount = 0;
@@ -54,13 +54,13 @@ export const calculateTotalTaxesIncluded = (
54
54
  }
55
55
  }
56
56
  }
57
-
57
+
58
58
  // Calculate discount on product net price only.
59
59
  const discountAmountPerUnit = discPercentageInteger ? unitNetPrice * discPercentageInteger : 0;
60
60
  const discountedUnitNetPrice = unitNetPrice - discountAmountPerUnit;
61
61
  // Reapply VAT to get discounted product gross price.
62
62
  const discountedUnitGrossPrice = discountedUnitNetPrice * taxFactor;
63
-
63
+
64
64
  // Now, calculate the final totals:
65
65
  // For the product part:
66
66
  const productNetTotalWithoutDiscount = unitNetPrice * qty;
@@ -68,23 +68,24 @@ export const calculateTotalTaxesIncluded = (
68
68
  // Add the extraAmount net (since it is not discounted):
69
69
  const totalNetWithoutDiscount = productNetTotalWithoutDiscount + extraAmountNet;
70
70
  const totalNetWithDiscount = productNetTotalWithDiscount + extraAmountNet;
71
-
71
+
72
72
  // Reapply VAT to both totals:
73
73
  const productLineImportTotal = + (totalNetWithoutDiscount * taxFactor).toFixed(2);
74
74
  const productLineTotalWithDiscount = + (totalNetWithDiscount * taxFactor).toFixed(2);
75
-
75
+
76
76
  // Calculate the taxes as the difference between gross and net:
77
77
  const totalTaxesApplied = + (productLineImportTotal - totalNetWithoutDiscount).toFixed(2);
78
-
78
+
79
79
  const lineDiscount = + (discountAmountPerUnit * qty).toFixed(2);
80
-
80
+
81
81
  return {
82
82
  product: current,
83
83
  qty,
84
84
  productLineImportTotal, // original gross total (no discount)
85
85
  productLineTotalWithDiscount, // gross total after discount on product (extra not discounted)
86
86
  productLineTaxesTotal: totalTaxesApplied,
87
- lineDiscountAmount: lineDiscount
87
+ lineDiscountAmount: lineDiscount,
88
+ productLineSubtotal: totalNetWithoutDiscount // All line subtotal (extra amount considered, without discounts and taxes)
88
89
  };
89
90
  });
90
91
  return productTableImports;
@@ -13,12 +13,12 @@ export const calculateTotalTaxesOverPrice = (
13
13
  let discPercentageInteger = 0;
14
14
  let productPercentageDiscount = 0;
15
15
  let customerDiscount = 0;
16
- let qty = current.qty;
16
+ const qty = current.qty; // cantidad
17
17
 
18
- // Precio base sin impuestos y sin extraAmount
18
+ // Precio base sin impuestos (en modo over_price el precio es neto)
19
19
  const productBasePrice = order.express ? current.expressPrice : current.price;
20
20
 
21
- // Calcular descuentos solo sobre el producto
21
+ // Cálculo de descuento sobre el producto (aplicable solo sobre el precio base)
22
22
  if (!order.discountCode) {
23
23
  const discountsToApply = storeDiscounts.filter(
24
24
  (discount) => discount.products.includes(current._id) && discount.isActive
@@ -28,6 +28,7 @@ export const calculateTotalTaxesOverPrice = (
28
28
  return discount.type === 'percentage' ? acc + discount.value : acc;
29
29
  }, 0);
30
30
  discPercentageInteger = +((productPercentageDiscount + customerDiscount) / 100).toFixed(2);
31
+ discountsToApply.forEach((discount) => (appliedOrderDiscounts[discount._id] = discount));
31
32
  } else {
32
33
  if (discountCodeObj.type === DiscountCodeTypes.PERCENTAGE) {
33
34
  discPercentageInteger = +(discountCodeObj.value / 100).toFixed(2);
@@ -44,31 +45,35 @@ export const calculateTotalTaxesOverPrice = (
44
45
  }
45
46
  }
46
47
 
47
- // Calcular descuento solo sobre el producto
48
+ // Calcular descuento por unidad y precio descontado
48
49
  const productDiscount = productBasePrice * discPercentageInteger;
49
50
  const discountedProductPrice = productBasePrice - productDiscount;
50
51
 
51
- // Calcular subtotal: se aplica el descuento al producto, y luego se suma el extraAmount (que se cobra completo)
52
- const subtotal = (discountedProductPrice * qty) + current.extraAmount;
52
+ // Subtotal para la línea (precio con descuento * cantidad + extraAmount) SIN impuestos
53
+ const subtotal = (discountedProductPrice * qty) + (current.extraAmount || 0);
53
54
 
54
- // Calcular impuestos sobre el subtotal final
55
+ // Calcular impuestos sobre el subtotal
55
56
  const taxPercentage = getProductTaxesPercentage(current, storeSettings) / 100;
56
57
  const taxes = subtotal * taxPercentage;
57
58
 
58
- // Resultados finales
59
- const totalTaxesApplied = +(taxes).toFixed(2);
59
+ // Resultados finales:
60
+ const totalTaxesApplied = +taxes.toFixed(2);
60
61
  const lineDiscount = +(productDiscount * qty).toFixed(2);
61
- // Se suma el extraAmount a la parte del producto descontado para obtener el valor total a cobrar de la línea
62
- //const prodLinePriceWithDiscount = +(((discountedProductPrice * qty) + current.extraAmount)).toFixed(2);
63
- const prodLinePriceWithDiscount = +((((discountedProductPrice * qty) + current.extraAmount) * (1 + taxPercentage))).toFixed(2);
62
+
63
+ // Precio total con descuento, reaplicando el IVA (es el total final a cobrar)
64
+ const prodLinePriceWithDiscount = +((subtotal * (1 + taxPercentage))).toFixed(2);
64
65
 
65
66
  return {
66
67
  product: current,
67
68
  qty,
68
- productLineImportTotal: (productBasePrice * qty) + current.extraAmount,
69
+ // Total bruto original sin descuento (precio base * cantidad + extraAmount)
70
+ productLineImportTotal: (productBasePrice * qty) + (current.extraAmount || 0),
71
+ // Total final con descuento, que se obtiene al sumar IVA al subtotal neto
69
72
  productLineTotalWithDiscount: prodLinePriceWithDiscount,
70
73
  productLineTaxesTotal: totalTaxesApplied,
71
- lineDiscountAmount: lineDiscount
74
+ lineDiscountAmount: lineDiscount,
75
+ // Subtotal: precio base (neto, con descuento) sin aplicar IVA
76
+ productLineSubtotal: subtotal
72
77
  };
73
78
  });
74
79
  return productTableImports;
@@ -4,70 +4,74 @@ import { IOrderProduct, ProductLineTotals } from "../../interfaces/Product";
4
4
  import { IStore, ITaxConfig } from "../../interfaces/Store";
5
5
 
6
6
  export const getProductTaxesPercentage = (productObj: IOrderProduct, store: IStore): number => {
7
- const getTaxValue = (tax: ITaxConfig | null | undefined, isTaxExempt: boolean): number => {
8
- if (!tax) {
9
- // If no store tax configured, always return 0
10
- return 0;
11
- }
7
+ const getTaxValue = (tax: ITaxConfig | null | undefined, isTaxExempt: boolean): number => {
8
+ if (!tax) {
9
+ // If no store tax configured, always return 0
10
+ return 0;
11
+ }
12
12
 
13
- if (isTaxExempt) {
14
- // If store tax is configured and taxExempt is true, return 0
15
- return 0;
16
- }
13
+ if (isTaxExempt) {
14
+ // If store tax is configured and taxExempt is true, return 0
15
+ return 0;
16
+ }
17
17
 
18
- // If store tax configured and taxExempt is false, return store tax
19
- return tax.value;
20
- };
18
+ // If store tax configured and taxExempt is false, return store tax
19
+ return tax.value;
20
+ };
21
21
 
22
- const tax1 = getTaxValue(store.taxOne, productObj.taxExemptOne);
23
- const tax2 = getTaxValue(store.taxTwo, productObj.taxExemptTwo);
24
- const tax3 = getTaxValue(store.taxThree, productObj.taxExemptThree);
22
+ const tax1 = getTaxValue(store.taxOne, productObj.taxExemptOne);
23
+ const tax2 = getTaxValue(store.taxTwo, productObj.taxExemptTwo);
24
+ const tax3 = getTaxValue(store.taxThree, productObj.taxExemptThree);
25
25
 
26
- return tax1 + tax2 + tax3;
26
+ return tax1 + tax2 + tax3;
27
27
  };
28
28
 
29
29
  export const getProductLineTotals = (productLinesTotals: ProductLineTotals[]) => {
30
- const totalQuantity = +productLinesTotals.reduce((acum, line) => acum + line.qty, 0).toFixed(2);
31
- const totalImportWithDiscount = +productLinesTotals
32
- .reduce((acum, line) => acum + line.productLineTotalWithDiscount, 0)
33
- .toFixed(2);
34
- const totalImportWithoutDiscount = +productLinesTotals
35
- .reduce((acum, line) => acum + line.productLineImportTotal, 0)
36
- .toFixed(2);
37
- const totalImporTaxes = +productLinesTotals
38
- .reduce((acum, line) => acum + line.productLineTaxesTotal, 0)
39
- .toFixed(2);
40
- const totalDiscountAmount = +productLinesTotals
41
- .reduce((acum, line) => acum + line.lineDiscountAmount, 0)
42
- .toFixed(2);
43
-
44
- return {
45
- totalQuantity,
46
- totalImportWithDiscount,
47
- totalImportWithoutDiscount,
48
- totalImporTaxes,
49
- totalDiscountAmount
50
- };
30
+ const totalQuantity = +productLinesTotals.reduce((acum, line) => acum + line.qty, 0).toFixed(2);
31
+ const totalImportWithDiscount = +productLinesTotals
32
+ .reduce((acum, line) => acum + line.productLineTotalWithDiscount, 0)
33
+ .toFixed(2);
34
+ const totalImportWithoutDiscount = +productLinesTotals
35
+ .reduce((acum, line) => acum + line.productLineImportTotal, 0)
36
+ .toFixed(2);
37
+ const totalImporTaxes = +productLinesTotals
38
+ .reduce((acum, line) => acum + line.productLineTaxesTotal, 0)
39
+ .toFixed(2);
40
+ const totalDiscountAmount = +productLinesTotals
41
+ .reduce((acum, line) => acum + line.lineDiscountAmount, 0)
42
+ .toFixed(2);
43
+ const totalSubtotalAmount = +productLinesTotals
44
+ .reduce((acum, line) => acum + line.productLineSubtotal, 0)
45
+ .toFixed(2);
46
+
47
+ return {
48
+ totalQuantity,
49
+ totalImportWithDiscount,
50
+ totalImportWithoutDiscount,
51
+ totalImporTaxes,
52
+ totalDiscountAmount,
53
+ totalSubtotalAmount
54
+ };
51
55
  };
52
56
 
53
57
  export const getShippingCost = (discountObject: any, requireShippingService: boolean, store: IStore): number => {
54
- const shippingCost = requireShippingService ? store?.orderPageConfig?.shippingServiceCost ?? 0 : 0;
58
+ const shippingCost = requireShippingService ? store?.orderPageConfig?.shippingServiceCost ?? 0 : 0;
55
59
 
56
- if (discountObject?.type === DiscountCodeTypes.FREE_DELIVERY) {
57
- return 0;
58
- }
60
+ if (discountObject?.type === DiscountCodeTypes.FREE_DELIVERY) {
61
+ return 0;
62
+ }
59
63
 
60
- return shippingCost;
64
+ return shippingCost;
61
65
  };
62
66
 
63
67
  export const getCreditApplied = (selectedCustomer: { customer: ICustomer } | undefined, orderTotal: number): number => {
64
- const customerCredit = selectedCustomer?.customer?.credit ?? 0;
68
+ const customerCredit = selectedCustomer?.customer?.credit ?? 0;
65
69
 
66
- if (customerCredit === 0 || !selectedCustomer) {
67
- return 0;
68
- }
70
+ if (customerCredit === 0 || !selectedCustomer) {
71
+ return 0;
72
+ }
69
73
 
70
- return Math.min(customerCredit, orderTotal);
74
+ return Math.min(customerCredit, orderTotal);
71
75
  };
72
76
 
73
77
  export const applyDiscountToProducts = (