@win2win/shared 1.0.286 → 1.0.288

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.
@@ -1,3 +1,4 @@
1
+ import { TipoIVA } from "../enums";
1
2
  interface PartialPedidoProducto {
2
3
  PRECIO: number;
3
4
  DESCUENTO: number;
@@ -5,9 +6,18 @@ interface PartialPedidoProducto {
5
6
  IVA: number;
6
7
  CANTIDAD: number;
7
8
  }
9
+ interface GastosEnvio {
10
+ TIPO_IVA?: TipoIVA;
11
+ PRECIO?: number;
12
+ DESCUENTO?: number;
13
+ }
8
14
  export declare class OrderPrice {
9
15
  private products;
10
- constructor(products: PartialPedidoProducto[]);
16
+ private gastosEnvio;
17
+ constructor(products: PartialPedidoProducto[], gastosEnvio?: GastosEnvio);
18
+ getNetShipping(): number;
19
+ getIVAShipping(): number;
20
+ getTotalShipping(): number;
11
21
  getPrice(): number;
12
22
  getDiscountPrice(): number;
13
23
  getNetPrice(): number;
@@ -20,6 +30,7 @@ export declare class OrderPrice {
20
30
  IMP_NETO_PED: number;
21
31
  IMP_IVA_PED: number;
22
32
  IMP_RE_PED: number;
33
+ IMP_TRANSPORTE_PED: number;
23
34
  IMP_TOTAL_PED: number;
24
35
  };
25
36
  getDiscountPercent(): number;
@@ -1,9 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OrderPrice = void 0;
4
+ const ProductPrice_1 = require("./ProductPrice");
4
5
  class OrderPrice {
5
- constructor(products) {
6
+ constructor(products, gastosEnvio) {
6
7
  this.products = products;
8
+ this.gastosEnvio = gastosEnvio;
9
+ }
10
+ getNetShipping() {
11
+ if (!this.gastosEnvio) {
12
+ return 0;
13
+ }
14
+ const productPrice = new ProductPrice_1.ProductPrice(this.gastosEnvio);
15
+ return productPrice.getNetPrice();
16
+ }
17
+ getIVAShipping() {
18
+ if (!this.gastosEnvio) {
19
+ return 0;
20
+ }
21
+ const productPrice = new ProductPrice_1.ProductPrice(this.gastosEnvio);
22
+ return productPrice.getIVAPrice();
23
+ }
24
+ getTotalShipping() {
25
+ if (!this.gastosEnvio) {
26
+ return 0;
27
+ }
28
+ const productPrice = new ProductPrice_1.ProductPrice(this.gastosEnvio);
29
+ return productPrice.getTotalPrice();
7
30
  }
8
31
  // ==== PRICES AMOUNTS ====
9
32
  getPrice() {
@@ -16,19 +39,25 @@ class OrderPrice {
16
39
  return this.roundToDecimals(this.getPrice() - this.getDiscountPrice());
17
40
  }
18
41
  getIVAPrice() {
19
- return this.roundToDecimals(this.products.reduce((acc, product) => acc + product.IVA * product.CANTIDAD, 0));
42
+ const IVAShipping = this.getIVAShipping();
43
+ const IVAProducts = this.products.reduce((acc, product) => acc + product.IVA * product.CANTIDAD, 0);
44
+ return this.roundToDecimals(IVAShipping + IVAProducts);
20
45
  }
21
46
  getREPrice() {
22
47
  return this.roundToDecimals(this.products.reduce((acc, product) => acc + product.RECARGO_EQUIVALENCIA * product.CANTIDAD, 0));
23
48
  }
24
49
  getTotalPrice() {
25
- return this.roundToDecimals(this.getNetPrice() + this.getIVAPrice() + this.getREPrice());
50
+ return this.roundToDecimals(this.getNetPrice() +
51
+ this.getNetShipping() +
52
+ this.getIVAPrice() +
53
+ this.getREPrice());
26
54
  }
27
55
  getTotalAmounts() {
28
56
  return {
29
57
  IMP_BRUTO_PED: this.getPrice(),
30
58
  IMP_DESC_PED: this.getDiscountPrice(),
31
59
  IMP_NETO_PED: this.getNetPrice(),
60
+ IMP_TRANSPORTE_PED: this.getNetShipping(),
32
61
  IMP_IVA_PED: this.getIVAPrice(),
33
62
  IMP_RE_PED: this.getREPrice(),
34
63
  IMP_TOTAL_PED: this.getTotalPrice(),
@@ -0,0 +1,43 @@
1
+ import { TipoIVA } from "../enums";
2
+ interface PayloadProductoPedido {
3
+ ID_PRODUCTO: number;
4
+ CANTIDAD: number;
5
+ PRECIO?: number;
6
+ PROPS?: Record<string, unknown>;
7
+ ID_PRECIO?: number;
8
+ ID_DIVISA?: number;
9
+ PRINCIPAL?: boolean;
10
+ DESCUENTO?: number;
11
+ IVA?: number;
12
+ RECARGO_EQUIVALENCIA?: number;
13
+ PRODUCTO: ProductoDetalle;
14
+ }
15
+ interface ProductoDetalle {
16
+ ID_PRODUCTO: number;
17
+ DETALLE_PACK?: PackDetalle[];
18
+ PACK?: boolean;
19
+ PRECIO: number;
20
+ TIPO_IVA: TipoIVA;
21
+ DESCUENTO?: number;
22
+ }
23
+ interface PackDetalle {
24
+ CANTIDAD: number;
25
+ DESCUENTO?: number;
26
+ DETALLE: {
27
+ PRECIO: number;
28
+ TIPO_IVA: TipoIVA;
29
+ };
30
+ }
31
+ export declare class OrderProductsBuilder {
32
+ private readonly orderProducts;
33
+ private readonly aplicaRecargoEquivalencia;
34
+ constructor(data: PayloadProductoPedido[], aplicaRecargoEquivalencia?: boolean);
35
+ build(): PayloadProductoPedido[];
36
+ private buildProduct;
37
+ private buildSimpleProducto;
38
+ private buildPackProducto;
39
+ private calculatePackTotals;
40
+ private getPrecioFinal;
41
+ private extractTotalsFromProductPrice;
42
+ }
43
+ export {};
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OrderProductsBuilder = void 0;
4
+ const enums_1 = require("../enums");
5
+ const ProductPrice_1 = require("./ProductPrice");
6
+ class OrderProductsBuilder {
7
+ constructor(data, aplicaRecargoEquivalencia = false) {
8
+ if (!Array.isArray(data)) {
9
+ throw new Error("Data must be an array of products");
10
+ }
11
+ this.orderProducts = data;
12
+ this.aplicaRecargoEquivalencia = aplicaRecargoEquivalencia;
13
+ }
14
+ build() {
15
+ return this.orderProducts.map((item) => this.buildProduct(item));
16
+ }
17
+ buildProduct(item) {
18
+ if (!item?.PRODUCTO) {
19
+ throw new Error(`Product data is missing for item with ID: ${item?.ID_PRODUCTO}`);
20
+ }
21
+ if (item.PRODUCTO.PACK) {
22
+ return this.buildPackProducto(item);
23
+ }
24
+ else {
25
+ return this.buildSimpleProducto(item);
26
+ }
27
+ }
28
+ buildSimpleProducto(item) {
29
+ const precioFinal = this.getPrecioFinal(item);
30
+ const cantidad = Math.max(1, item.CANTIDAD || 1);
31
+ const productPrice = new ProductPrice_1.ProductPrice({
32
+ PRECIO: precioFinal * cantidad,
33
+ TIPO_IVA: item.PRODUCTO.TIPO_IVA || enums_1.TipoIVA.EXENTO,
34
+ DESCUENTO: item.PRODUCTO.DESCUENTO || 0,
35
+ APLICA_RECARGO_EQUIVALENCIA: this.aplicaRecargoEquivalencia,
36
+ });
37
+ const totals = this.extractTotalsFromProductPrice(productPrice);
38
+ return {
39
+ ...item,
40
+ ...totals,
41
+ ID_PRECIO: item.ID_PRECIO || undefined,
42
+ };
43
+ }
44
+ buildPackProducto(item) {
45
+ const packDetails = item.PRODUCTO.DETALLE_PACK || [];
46
+ if (packDetails.length === 0) {
47
+ console.warn(`Pack product ${item.PRODUCTO.ID_PRODUCTO} has no pack details`);
48
+ }
49
+ const totals = this.calculatePackTotals(packDetails);
50
+ return {
51
+ ...item,
52
+ ...totals,
53
+ };
54
+ }
55
+ calculatePackTotals(packDetails) {
56
+ const initialTotals = {
57
+ PRECIO: 0,
58
+ IVA: 0,
59
+ DESCUENTO: 0,
60
+ RECARGO_EQUIVALENCIA: 0,
61
+ };
62
+ return packDetails.reduce((totals, packDetail) => {
63
+ const precio = Number(packDetail.DETALLE?.PRECIO || 0);
64
+ const cantidad = Math.max(1, packDetail.CANTIDAD || 1);
65
+ const descuento = Number(packDetail.DESCUENTO || 0);
66
+ if (precio <= 0) {
67
+ console.warn("Pack detail has invalid price:", packDetail);
68
+ return totals;
69
+ }
70
+ const productPrice = new ProductPrice_1.ProductPrice({
71
+ PRECIO: precio * cantidad,
72
+ TIPO_IVA: packDetail.DETALLE?.TIPO_IVA || enums_1.TipoIVA.EXENTO,
73
+ DESCUENTO: descuento,
74
+ APLICA_RECARGO_EQUIVALENCIA: this.aplicaRecargoEquivalencia,
75
+ });
76
+ const detailTotals = this.extractTotalsFromProductPrice(productPrice);
77
+ return {
78
+ PRECIO: totals.PRECIO + detailTotals.PRECIO,
79
+ IVA: totals.IVA + detailTotals.IVA,
80
+ DESCUENTO: totals.DESCUENTO + detailTotals.DESCUENTO,
81
+ RECARGO_EQUIVALENCIA: totals.RECARGO_EQUIVALENCIA + detailTotals.RECARGO_EQUIVALENCIA,
82
+ };
83
+ }, initialTotals);
84
+ }
85
+ getPrecioFinal(item) {
86
+ const precio = Number(item.PRECIO ?? item.PRODUCTO?.PRECIO ?? 0);
87
+ if (precio < 0) {
88
+ throw new Error(`Invalid negative price for product ${item.PRODUCTO.ID_PRODUCTO}`);
89
+ }
90
+ return precio;
91
+ }
92
+ extractTotalsFromProductPrice(productPrice) {
93
+ return {
94
+ PRECIO: productPrice.getPrice(),
95
+ IVA: productPrice.getIVAPrice(),
96
+ DESCUENTO: productPrice.getDiscountPrice(),
97
+ RECARGO_EQUIVALENCIA: productPrice.getREPrice(),
98
+ };
99
+ }
100
+ }
101
+ exports.OrderProductsBuilder = OrderProductsBuilder;
@@ -2,3 +2,4 @@ export * from './formatters';
2
2
  export * from './ProductPrice';
3
3
  export * from './validators';
4
4
  export * from './OrderPrice';
5
+ export * from './OrderProductsBuilder';
@@ -18,3 +18,4 @@ __exportStar(require("./formatters"), exports);
18
18
  __exportStar(require("./ProductPrice"), exports);
19
19
  __exportStar(require("./validators"), exports);
20
20
  __exportStar(require("./OrderPrice"), exports);
21
+ __exportStar(require("./OrderProductsBuilder"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@win2win/shared",
3
- "version": "1.0.286",
3
+ "version": "1.0.288",
4
4
  "description": "Tipos, interfaces, funciones, constantes, clases y enums compartidos por todos los proyectos de Win2Win",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",