From 198137d426fdc6804ca9680a322a0cb31defcd97 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 29 Sep 2025 09:32:13 +0200 Subject: [PATCH] Facturas de cliente --- .../domain/customer-invoice.full.presenter.ts | 8 +++- .../api/domain/aggregates/customer-invoice.ts | 2 +- .../customer-invoice-items.ts | 38 ++++++++++--------- ...get-customer-invoice-by-id.response.dto.ts | 8 +++- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/modules/customer-invoices/src/api/application/presenters/domain/customer-invoice.full.presenter.ts b/modules/customer-invoices/src/api/application/presenters/domain/customer-invoice.full.presenter.ts index 965c544a..0a9de682 100644 --- a/modules/customer-invoices/src/api/application/presenters/domain/customer-invoice.full.presenter.ts +++ b/modules/customer-invoices/src/api/application/presenters/domain/customer-invoice.full.presenter.ts @@ -54,7 +54,13 @@ export class CustomerInvoiceFullPresenter extends Presenter< customer_id: invoice.customerId.toString(), recipient, - taxes: invoice.taxes.map((tax) => tax.tax.code).join(), + taxes: invoice.taxes.map((taxItem) => { + return { + tax_code: taxItem.tax.code, + taxable_amount: taxItem.taxableAmount.toObjectString(), + taxes_amount: taxItem.taxesAmount.toObjectString(), + }; + }), payment_method: payment, diff --git a/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts b/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts index ab7b21fc..5c8d5aeb 100644 --- a/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts +++ b/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts @@ -25,7 +25,7 @@ export interface CustomerInvoiceProps { status: CustomerInvoiceStatus; series: Maybe; - invoiceNumber: Maybe; + invoiceNumber: Maybe; invoiceDate: UtcDate; operationDate: Maybe; diff --git a/modules/customer-invoices/src/api/domain/entities/customer-invoice-items/customer-invoice-items.ts b/modules/customer-invoices/src/api/domain/entities/customer-invoice-items/customer-invoice-items.ts index 32cbdb54..8cdb5e13 100644 --- a/modules/customer-invoices/src/api/domain/entities/customer-invoice-items/customer-invoice-items.ts +++ b/modules/customer-invoices/src/api/domain/entities/customer-invoice-items/customer-invoice-items.ts @@ -72,33 +72,37 @@ export class CustomerInvoiceItems extends Collection { ); } - public getTaxesAmountByTaxes() { - const resultMap = new Map(); - + public getTaxesAmountByTaxes(): Array<{ + tax: Tax; + taxableAmount: ItemAmount; + taxesAmount: ItemAmount; + }> { + const getTaxCode = (tax: Tax): string => tax.code; // clave estable para Map const currencyCode = this._currencyCode.code; + // Mapeamos por clave (tax code), pero tambiƩn guardamos el Tax original + const resultMap = new Map< + string, + { tax: Tax; taxableAmount: ItemAmount; taxesAmount: ItemAmount } + >(); + for (const item of this.getAll()) { for (const { taxableAmount, tax, taxesAmount } of item.getTaxesAmountByTaxes()) { - const { taxableAmount: taxableCurrent, taxesAmount: taxesCurrent } = resultMap.get(tax) ?? { + const key = getTaxCode(tax); + const current = resultMap.get(key) ?? { + tax, taxableAmount: ItemAmount.zero(currencyCode), taxesAmount: ItemAmount.zero(currencyCode), }; - resultMap.set(tax, { - taxableAmount: taxableCurrent.add(taxableAmount), - taxesAmount: taxesCurrent.add(taxesAmount), + + resultMap.set(key, { + tax: current.tax, + taxableAmount: current.taxableAmount.add(taxableAmount), + taxesAmount: current.taxesAmount.add(taxesAmount), }); } } - const items = []; - for (const [tax, { taxableAmount, taxesAmount }] of resultMap) { - items.push({ - taxableAmount, - tax, - taxesAmount, - }); - } - - return items; + return Array.from(resultMap.values()); } } diff --git a/modules/customer-invoices/src/common/dto/response/get-customer-invoice-by-id.response.dto.ts b/modules/customer-invoices/src/common/dto/response/get-customer-invoice-by-id.response.dto.ts index 0d994cc0..6e64f939 100644 --- a/modules/customer-invoices/src/common/dto/response/get-customer-invoice-by-id.response.dto.ts +++ b/modules/customer-invoices/src/common/dto/response/get-customer-invoice-by-id.response.dto.ts @@ -30,7 +30,13 @@ export const GetCustomerInvoiceByIdResponseSchema = z.object({ country: z.string(), }), - taxes: z.string(), + taxes: z.array( + z.object({ + tax_code: z.string(), + taxable_amount: MoneySchema, + taxes_amount: MoneySchema, + }) + ), payment_method: z .object({