Facturas de cliente

This commit is contained in:
David Arranz 2025-09-29 09:32:13 +02:00
parent b123ee4440
commit 198137d426
4 changed files with 36 additions and 20 deletions

View File

@ -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,

View File

@ -25,7 +25,7 @@ export interface CustomerInvoiceProps {
status: CustomerInvoiceStatus;
series: Maybe<CustomerInvoiceSerie>;
invoiceNumber: Maybe<DocNumber>;
invoiceNumber: Maybe<CustomerInvoiceNumber>;
invoiceDate: UtcDate;
operationDate: Maybe<UtcDate>;

View File

@ -72,33 +72,37 @@ export class CustomerInvoiceItems extends Collection<CustomerInvoiceItem> {
);
}
public getTaxesAmountByTaxes() {
const resultMap = new Map<Tax, { taxableAmount: ItemAmount; taxesAmount: ItemAmount }>();
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());
}
}

View File

@ -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({