Uecko_ERP/modules/customer-invoices/src/api/application/presenters/domain/customer-invoice-items.full.presenter.ts

55 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-09-12 16:23:36 +00:00
import { Presenter } from "@erp/core/api";
import { toEmptyString } from "@repo/rdx-ddd";
import { ArrayElement } from "@repo/rdx-utils";
import { GetCustomerInvoiceByIdResponseDTO } from "../../../../common/dto";
import { CustomerInvoiceItem, CustomerInvoiceItems } from "../../../domain";
type GetCustomerInvoiceItemByInvoiceIdResponseDTO = ArrayElement<
GetCustomerInvoiceByIdResponseDTO["items"]
>;
export class CustomerInvoiceItemsFullPresenter extends Presenter {
2025-09-18 11:17:18 +00:00
private _mapItem(
2025-09-12 16:23:36 +00:00
invoiceItem: CustomerInvoiceItem,
index: number
): GetCustomerInvoiceItemByInvoiceIdResponseDTO {
const allAmounts = invoiceItem.getAllAmounts();
return {
id: invoiceItem.id.toPrimitive(),
2025-10-12 09:14:33 +00:00
is_non_valued: String(invoiceItem.isNonValued),
2025-09-12 16:23:36 +00:00
position: String(index),
description: toEmptyString(invoiceItem.description, (value) => value.toPrimitive()),
quantity: invoiceItem.quantity.match(
(quantity) => quantity.toObjectString(),
() => ({ value: "", scale: "" })
),
unit_amount: invoiceItem.unitAmount.match(
(unitAmount) => unitAmount.toObjectString(),
() => ({ value: "", scale: "", currency_code: "" })
),
subtotal_amount: allAmounts.subtotalAmount.toObjectString(),
discount_percentage: invoiceItem.discountPercentage.match(
(discountPercentage) => discountPercentage.toObjectString(),
() => ({ value: "", scale: "" })
),
discount_amount: allAmounts.discountAmount.toObjectString(),
2025-09-17 17:37:41 +00:00
2025-09-12 16:23:36 +00:00
taxable_amount: allAmounts.taxableAmount.toObjectString(),
2025-09-29 18:22:59 +00:00
tax_codes: invoiceItem.taxes.getCodesToString().split(","),
2025-09-12 16:23:36 +00:00
taxes_amount: allAmounts.taxesAmount.toObjectString(),
2025-09-17 17:37:41 +00:00
2025-09-12 16:23:36 +00:00
total_amount: allAmounts.totalAmount.toObjectString(),
};
}
toOutput(invoiceItems: CustomerInvoiceItems): GetCustomerInvoiceByIdResponseDTO["items"] {
2025-09-18 11:17:18 +00:00
return invoiceItems.map(this._mapItem);
2025-09-12 16:23:36 +00:00
}
}