washday-sdk 0.0.164 → 0.0.166

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.
@@ -13,6 +13,7 @@ export const calculateOrderTotal = (order, selectedCustomer, storeSettings, hasS
13
13
  // DISCOUNT TOTAL AND SHIPPING SERVICE COST
14
14
  let discountCodeAmount = 0;
15
15
  let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
16
+ // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
16
17
  if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
17
18
  const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr) => discountCodeObj.products.includes(curr._id));
18
19
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
@@ -5,14 +5,14 @@ export const calculateTotalTaxesIncluded = (order, selectedCustomer, storeSettin
5
5
  const qty = current.qty || 0;
6
6
  const taxPercentage = getProductTaxesPercentage(current, storeSettings);
7
7
  const taxFactor = 1 + taxPercentage / 100;
8
- // Convert extraAmount from gross to net (assume extraAmount is not discounted)
8
+ // Convertir extraAmount de bruto a neto
9
9
  const extraAmountGross = current.extraAmount || 0;
10
10
  const extraAmountNet = extraAmountGross / taxFactor;
11
- // Get the product’s gross price (excluding extraAmount)
11
+ // Precio bruto del producto (sin extraAmount)
12
12
  const productGrossPrice = order.express ? current.expressPrice : current.price;
13
- // Convert product gross price to net
13
+ // Convertir precio bruto a neto
14
14
  const unitNetPrice = productGrossPrice / taxFactor;
15
- // Determine discount percentage (if any) – discount is applied only on product net price.
15
+ // Determinar el porcentaje de descuento (si aplica)
16
16
  let discPercentageInteger = 0;
17
17
  let productPercentageDiscount = 0;
18
18
  let customerDiscount = 0;
@@ -42,32 +42,34 @@ export const calculateTotalTaxesIncluded = (order, selectedCustomer, storeSettin
42
42
  }
43
43
  }
44
44
  }
45
- // Calculate discount on product net price only.
46
- const discountAmountPerUnit = discPercentageInteger ? unitNetPrice * discPercentageInteger : 0;
47
- const discountedUnitNetPrice = unitNetPrice - discountAmountPerUnit;
48
- // Reapply VAT to get discounted product gross price.
49
- const discountedUnitGrossPrice = discountedUnitNetPrice * taxFactor;
50
- // Now, calculate the final totals:
51
- // For the product part:
52
- const productNetTotalWithoutDiscount = unitNetPrice * qty;
45
+ // === NUEVO: Aplicar descuento sobre el precio total ajustado (producto base + extra prorrateado) ===
46
+ // Si hay cantidad, repartir el extra neto entre cada unidad
47
+ const unitExtraNet = qty > 0 ? extraAmountNet / qty : 0;
48
+ // Precio neto ajustado: precio base + extra (por unidad)
49
+ const adjustedUnitNetPrice = unitNetPrice + unitExtraNet;
50
+ // Calcular el descuento sobre el precio ajustado
51
+ const discountAmountPerUnit = discPercentageInteger ? adjustedUnitNetPrice * discPercentageInteger : 0;
52
+ // Precio neto con descuento aplicado por unidad
53
+ const discountedUnitNetPrice = adjustedUnitNetPrice - discountAmountPerUnit;
54
+ // === Totales de la línea ===
55
+ // Total neto sin descuento: precio base total + extra (ya prorrateado en el total)
56
+ const productNetTotalWithoutDiscount = (unitNetPrice * qty) + extraAmountNet;
57
+ // Total neto con descuento: se aplica el descuento sobre el precio ajustado por unidad
53
58
  const productNetTotalWithDiscount = discountedUnitNetPrice * qty;
54
- // Add the extraAmount net (since it is not discounted):
55
- const totalNetWithoutDiscount = productNetTotalWithoutDiscount + extraAmountNet;
56
- const totalNetWithDiscount = productNetTotalWithDiscount + extraAmountNet;
57
- // Reapply VAT to both totals:
58
- const productLineImportTotal = +(totalNetWithoutDiscount * taxFactor).toFixed(2);
59
- const productLineTotalWithDiscount = +(totalNetWithDiscount * taxFactor).toFixed(2);
60
- // Calculate the taxes as the difference between gross and net:
61
- const totalTaxesApplied = +(productLineImportTotal - totalNetWithoutDiscount).toFixed(2);
59
+ // Reaplicar IVA a ambos totales
60
+ const productLineImportTotal = +(productNetTotalWithoutDiscount * taxFactor).toFixed(2);
61
+ const productLineTotalWithDiscount = +(productNetTotalWithDiscount * taxFactor).toFixed(2);
62
+ // Calcular el total de impuestos como la diferencia entre bruto y neto con descuento
63
+ const totalTaxesApplied = +(productLineTotalWithDiscount - productNetTotalWithDiscount).toFixed(2);
62
64
  const lineDiscount = +(discountAmountPerUnit * qty).toFixed(2);
63
65
  return {
64
66
  product: current,
65
67
  qty,
66
- productLineImportTotal, // original gross total (no discount)
67
- productLineTotalWithDiscount, // gross total after discount on product (extra not discounted)
68
+ productLineImportTotal, // Total bruto sin descuento (incluye extra y IVA)
69
+ productLineTotalWithDiscount, // Total bruto con descuento aplicado sobre el precio ajustado
68
70
  productLineTaxesTotal: totalTaxesApplied,
69
71
  lineDiscountAmount: lineDiscount,
70
- productLineSubtotal: totalNetWithoutDiscount // All line subtotal (extra amount considered, without discounts and taxes)
72
+ productLineSubtotal: productNetTotalWithoutDiscount // Subtotal neto (sin IVA)
71
73
  };
72
74
  });
73
75
  return productTableImports;
@@ -6,10 +6,16 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
6
6
  let discPercentageInteger = 0;
7
7
  let productPercentageDiscount = 0;
8
8
  let customerDiscount = 0;
9
- const qty = current.qty; // cantidad
9
+ const qty = current.qty || current.quantity; // cantidad
10
10
  // Precio base sin impuestos (en modo over_price el precio es neto)
11
11
  const productBasePrice = order.express ? current.expressPrice : current.price;
12
- // Cálculo de descuento sobre el producto (aplicable solo sobre el precio base)
12
+ // === NUEVO: Considerar extraAmount en el precio a descontar ===
13
+ // extraAmount es el ajuste total para la línea; se reparte por unidad
14
+ const extraAmount = current.extraAmount || 0;
15
+ const unitExtra = qty > 0 ? extraAmount / qty : 0;
16
+ // Precio ajustado: suma del precio base y el extra por unidad
17
+ const adjustedPrice = productBasePrice + unitExtra;
18
+ // Cálculo de descuento sobre el precio ajustado
13
19
  if (!order.discountCode) {
14
20
  const discountsToApply = storeDiscounts.filter((discount) => discount.products.includes(current._id) && discount.isActive);
15
21
  customerDiscount = ((_a = selectedCustomer === null || selectedCustomer === void 0 ? void 0 : selectedCustomer.customer) === null || _a === void 0 ? void 0 : _a.discount) || 0;
@@ -35,29 +41,29 @@ export const calculateTotalTaxesOverPrice = (order, selectedCustomer, storeSetti
35
41
  }
36
42
  }
37
43
  }
38
- // Calcular descuento por unidad y precio descontado
39
- const productDiscount = productBasePrice * discPercentageInteger;
40
- const discountedProductPrice = productBasePrice - productDiscount;
41
- // Subtotal para la línea (precio con descuento * cantidad + extraAmount) SIN impuestos
42
- const subtotal = (discountedProductPrice * qty) + (current.extraAmount || 0);
44
+ // Calcular el descuento por unidad sobre el precio ajustado
45
+ const productDiscount = adjustedPrice * discPercentageInteger;
46
+ const discountedAdjustedPrice = adjustedPrice - productDiscount;
47
+ // Subtotal para la línea (precio descontado por unidad * cantidad) SIN impuestos
48
+ const subtotal = discountedAdjustedPrice * qty;
43
49
  // Calcular impuestos sobre el subtotal
44
50
  const taxPercentage = getProductTaxesPercentage(current, storeSettings) / 100;
45
51
  const taxes = subtotal * taxPercentage;
46
52
  // Resultados finales:
47
53
  const totalTaxesApplied = +taxes.toFixed(2);
48
54
  const lineDiscount = +(productDiscount * qty).toFixed(2);
49
- // Precio total con descuento, reaplicando el IVA (es el total final a cobrar)
55
+ // Precio total con descuento, reaplicando impuestos
50
56
  const prodLinePriceWithDiscount = +((subtotal * (1 + taxPercentage))).toFixed(2);
51
57
  return {
52
58
  product: current,
53
59
  qty,
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
60
+ // Total bruto original sin descuento: (precio base * cantidad) + extraAmount
61
+ productLineImportTotal: (productBasePrice * qty) + extraAmount,
62
+ // Total final con descuento, con impuestos incluidos
57
63
  productLineTotalWithDiscount: prodLinePriceWithDiscount,
58
64
  productLineTaxesTotal: totalTaxesApplied,
59
65
  lineDiscountAmount: lineDiscount,
60
- // Subtotal: precio base (neto, con descuento) sin aplicar IVA
66
+ // Subtotal neto (sin impuestos) con descuento aplicado
61
67
  productLineSubtotal: subtotal
62
68
  };
63
69
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "washday-sdk",
3
- "version": "0.0.164",
3
+ "version": "0.0.166",
4
4
  "description": "Washday utilities functions and API",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -32,6 +32,7 @@ export const calculateOrderTotal = (
32
32
  let discountCodeAmount = 0;
33
33
  let shippingCost = getShippingCost(discountCodeObj, hasShippingCost, storeSettings);
34
34
 
35
+ // TODO: MAYBE THIS KIND OF DISCOUNT IS WRONG BECAUSE TAXES ARE CALCULATED BEFORE THIS POINT;
35
36
  if (discountCodeObj && discountCodeObj.type === DiscountCodeTypes.NUMBER && discountCodeObj.applyOnceOnOrder) {
36
37
  const includesProducts = discountCodeObj.applyToAllProducts || order.products.some((curr: any) => discountCodeObj.products.includes(curr._id));
37
38
  discountCodeAmount = includesProducts ? discountCodeObj.value : 0;
@@ -14,16 +14,16 @@ export const calculateTotalTaxesIncluded = (
14
14
  const taxPercentage = getProductTaxesPercentage(current, storeSettings);
15
15
  const taxFactor = 1 + taxPercentage / 100;
16
16
 
17
- // Convert extraAmount from gross to net (assume extraAmount is not discounted)
17
+ // Convertir extraAmount de bruto a neto
18
18
  const extraAmountGross = current.extraAmount || 0;
19
19
  const extraAmountNet = extraAmountGross / taxFactor;
20
20
 
21
- // Get the product’s gross price (excluding extraAmount)
21
+ // Precio bruto del producto (sin extraAmount)
22
22
  const productGrossPrice = order.express ? current.expressPrice : current.price;
23
- // Convert product gross price to net
23
+ // Convertir precio bruto a neto
24
24
  const unitNetPrice = productGrossPrice / taxFactor;
25
25
 
26
- // Determine discount percentage (if any) – discount is applied only on product net price.
26
+ // Determinar el porcentaje de descuento (si aplica)
27
27
  let discPercentageInteger = 0;
28
28
  let productPercentageDiscount = 0;
29
29
  let customerDiscount = 0;
@@ -32,7 +32,7 @@ export const calculateTotalTaxesIncluded = (
32
32
  (discount) => discount.products.includes(current._id) && discount.isActive
33
33
  );
34
34
  customerDiscount = selectedCustomer?.customer?.discount || 0;
35
- productPercentageDiscount = discountsToApply.reduce((prev: number, next: any) => {
35
+ productPercentageDiscount = discountsToApply.reduce((prev, next) => {
36
36
  return next.type === 'percentage' ? prev + next.value : prev;
37
37
  }, 0);
38
38
  discPercentageInteger = +((productPercentageDiscount + customerDiscount) / 100).toFixed(2);
@@ -55,37 +55,39 @@ export const calculateTotalTaxesIncluded = (
55
55
  }
56
56
  }
57
57
 
58
- // Calculate discount on product net price only.
59
- const discountAmountPerUnit = discPercentageInteger ? unitNetPrice * discPercentageInteger : 0;
60
- const discountedUnitNetPrice = unitNetPrice - discountAmountPerUnit;
61
- // Reapply VAT to get discounted product gross price.
62
- const discountedUnitGrossPrice = discountedUnitNetPrice * taxFactor;
58
+ // === NUEVO: Aplicar descuento sobre el precio total ajustado (producto base + extra prorrateado) ===
59
+ // Si hay cantidad, repartir el extra neto entre cada unidad
60
+ const unitExtraNet = qty > 0 ? extraAmountNet / qty : 0;
61
+ // Precio neto ajustado: precio base + extra (por unidad)
62
+ const adjustedUnitNetPrice = unitNetPrice + unitExtraNet;
63
+ // Calcular el descuento sobre el precio ajustado
64
+ const discountAmountPerUnit = discPercentageInteger ? adjustedUnitNetPrice * discPercentageInteger : 0;
65
+ // Precio neto con descuento aplicado por unidad
66
+ const discountedUnitNetPrice = adjustedUnitNetPrice - discountAmountPerUnit;
63
67
 
64
- // Now, calculate the final totals:
65
- // For the product part:
66
- const productNetTotalWithoutDiscount = unitNetPrice * qty;
68
+ // === Totales de la línea ===
69
+ // Total neto sin descuento: precio base total + extra (ya prorrateado en el total)
70
+ const productNetTotalWithoutDiscount = (unitNetPrice * qty) + extraAmountNet;
71
+ // Total neto con descuento: se aplica el descuento sobre el precio ajustado por unidad
67
72
  const productNetTotalWithDiscount = discountedUnitNetPrice * qty;
68
- // Add the extraAmount net (since it is not discounted):
69
- const totalNetWithoutDiscount = productNetTotalWithoutDiscount + extraAmountNet;
70
- const totalNetWithDiscount = productNetTotalWithDiscount + extraAmountNet;
71
73
 
72
- // Reapply VAT to both totals:
73
- const productLineImportTotal = + (totalNetWithoutDiscount * taxFactor).toFixed(2);
74
- const productLineTotalWithDiscount = + (totalNetWithDiscount * taxFactor).toFixed(2);
74
+ // Reaplicar IVA a ambos totales
75
+ const productLineImportTotal = +(productNetTotalWithoutDiscount * taxFactor).toFixed(2);
76
+ const productLineTotalWithDiscount = +(productNetTotalWithDiscount * taxFactor).toFixed(2);
75
77
 
76
- // Calculate the taxes as the difference between gross and net:
77
- const totalTaxesApplied = + (productLineImportTotal - totalNetWithoutDiscount).toFixed(2);
78
+ // Calcular el total de impuestos como la diferencia entre bruto y neto con descuento
79
+ const totalTaxesApplied = +(productLineTotalWithDiscount - productNetTotalWithDiscount).toFixed(2);
78
80
 
79
- const lineDiscount = + (discountAmountPerUnit * qty).toFixed(2);
81
+ const lineDiscount = +(discountAmountPerUnit * qty).toFixed(2);
80
82
 
81
83
  return {
82
84
  product: current,
83
85
  qty,
84
- productLineImportTotal, // original gross total (no discount)
85
- productLineTotalWithDiscount, // gross total after discount on product (extra not discounted)
86
+ productLineImportTotal, // Total bruto sin descuento (incluye extra y IVA)
87
+ productLineTotalWithDiscount, // Total bruto con descuento aplicado sobre el precio ajustado
86
88
  productLineTaxesTotal: totalTaxesApplied,
87
89
  lineDiscountAmount: lineDiscount,
88
- productLineSubtotal: totalNetWithoutDiscount // All line subtotal (extra amount considered, without discounts and taxes)
90
+ productLineSubtotal: productNetTotalWithoutDiscount // Subtotal neto (sin IVA)
89
91
  };
90
92
  });
91
93
  return productTableImports;
@@ -13,12 +13,19 @@ export const calculateTotalTaxesOverPrice = (
13
13
  let discPercentageInteger = 0;
14
14
  let productPercentageDiscount = 0;
15
15
  let customerDiscount = 0;
16
- const qty = current.qty; // cantidad
16
+ const qty = current.qty || current.quantity; // cantidad
17
17
 
18
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
- // Cálculo de descuento sobre el producto (aplicable solo sobre el precio base)
21
+ // === NUEVO: Considerar extraAmount en el precio a descontar ===
22
+ // extraAmount es el ajuste total para la línea; se reparte por unidad
23
+ const extraAmount = current.extraAmount || 0;
24
+ const unitExtra = qty > 0 ? extraAmount / qty : 0;
25
+ // Precio ajustado: suma del precio base y el extra por unidad
26
+ const adjustedPrice = productBasePrice + unitExtra;
27
+
28
+ // Cálculo de descuento sobre el precio ajustado
22
29
  if (!order.discountCode) {
23
30
  const discountsToApply = storeDiscounts.filter(
24
31
  (discount) => discount.products.includes(current._id) && discount.isActive
@@ -45,12 +52,12 @@ export const calculateTotalTaxesOverPrice = (
45
52
  }
46
53
  }
47
54
 
48
- // Calcular descuento por unidad y precio descontado
49
- const productDiscount = productBasePrice * discPercentageInteger;
50
- const discountedProductPrice = productBasePrice - productDiscount;
55
+ // Calcular el descuento por unidad sobre el precio ajustado
56
+ const productDiscount = adjustedPrice * discPercentageInteger;
57
+ const discountedAdjustedPrice = adjustedPrice - productDiscount;
51
58
 
52
- // Subtotal para la línea (precio con descuento * cantidad + extraAmount) SIN impuestos
53
- const subtotal = (discountedProductPrice * qty) + (current.extraAmount || 0);
59
+ // Subtotal para la línea (precio descontado por unidad * cantidad) SIN impuestos
60
+ const subtotal = discountedAdjustedPrice * qty;
54
61
 
55
62
  // Calcular impuestos sobre el subtotal
56
63
  const taxPercentage = getProductTaxesPercentage(current, storeSettings) / 100;
@@ -60,19 +67,19 @@ export const calculateTotalTaxesOverPrice = (
60
67
  const totalTaxesApplied = +taxes.toFixed(2);
61
68
  const lineDiscount = +(productDiscount * qty).toFixed(2);
62
69
 
63
- // Precio total con descuento, reaplicando el IVA (es el total final a cobrar)
70
+ // Precio total con descuento, reaplicando impuestos
64
71
  const prodLinePriceWithDiscount = +((subtotal * (1 + taxPercentage))).toFixed(2);
65
72
 
66
73
  return {
67
74
  product: current,
68
75
  qty,
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
76
+ // Total bruto original sin descuento: (precio base * cantidad) + extraAmount
77
+ productLineImportTotal: (productBasePrice * qty) + extraAmount,
78
+ // Total final con descuento, con impuestos incluidos
72
79
  productLineTotalWithDiscount: prodLinePriceWithDiscount,
73
80
  productLineTaxesTotal: totalTaxesApplied,
74
81
  lineDiscountAmount: lineDiscount,
75
- // Subtotal: precio base (neto, con descuento) sin aplicar IVA
82
+ // Subtotal neto (sin impuestos) con descuento aplicado
76
83
  productLineSubtotal: subtotal
77
84
  };
78
85
  });