Uecko_ERP/modules/customer-invoices/src/api/infrastructure/mappers/domain/item-taxes.mapper.ts

107 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-09-26 15:00:11 +00:00
import {
ISequelizeDomainMapper,
MapperParamsType,
SequelizeDomainMapper,
Tax,
} from "@erp/core/api";
2025-09-26 18:09:14 +00:00
2025-10-28 17:52:30 +00:00
import { JsonTaxCatalogProvider } from "@erp/core";
2025-09-10 16:06:29 +00:00
import {
2025-09-26 18:09:14 +00:00
UniqueID,
2025-09-10 16:06:29 +00:00
ValidationErrorCollection,
ValidationErrorDetail,
extractOrPushError,
2025-09-16 11:29:45 +00:00
} from "@repo/rdx-ddd";
2025-09-10 16:06:29 +00:00
import { Result } from "@repo/rdx-utils";
2025-09-26 18:09:14 +00:00
import { CustomerInvoiceItem } from "../../../domain";
2025-09-11 12:05:50 +00:00
import {
2025-09-26 15:00:11 +00:00
CustomerInvoiceItemTaxCreationAttributes,
2025-09-11 12:05:50 +00:00
CustomerInvoiceItemTaxModel,
} from "../../sequelize";
2025-09-10 16:06:29 +00:00
2025-09-26 15:00:11 +00:00
export interface IItemTaxesDomainMapper
extends ISequelizeDomainMapper<
CustomerInvoiceItemTaxModel,
CustomerInvoiceItemTaxCreationAttributes,
2025-09-26 18:09:14 +00:00
Tax
2025-09-26 15:00:11 +00:00
> {}
export class ItemTaxesDomainMapper
extends SequelizeDomainMapper<
CustomerInvoiceItemTaxModel,
CustomerInvoiceItemTaxCreationAttributes,
2025-09-26 18:09:14 +00:00
Tax
2025-09-26 15:00:11 +00:00
>
implements IItemTaxesDomainMapper
{
2025-09-10 16:06:29 +00:00
private _taxCatalog!: JsonTaxCatalogProvider;
constructor(params: MapperParamsType) {
super();
const { taxCatalog } = params as {
taxCatalog: JsonTaxCatalogProvider;
};
if (!taxCatalog) {
throw new Error('taxCatalog not defined ("ItemTaxesMapper")');
}
this._taxCatalog = taxCatalog;
2025-09-09 15:48:12 +00:00
}
2025-09-10 16:06:29 +00:00
public mapToDomain(
source: CustomerInvoiceItemTaxModel,
2025-09-09 15:48:12 +00:00
params?: MapperParamsType
2025-09-26 18:09:14 +00:00
): Result<Tax, Error> {
2025-09-10 16:06:29 +00:00
const { errors, index } = params as {
2025-09-09 15:48:12 +00:00
index: number;
2025-09-10 16:06:29 +00:00
errors: ValidationErrorDetail[];
};
const tax = extractOrPushError(
Tax.createFromCode(source.tax_code, this._taxCatalog),
`taxes[${index}].tax_code`,
errors
);
// Creación del objeto de dominio
2025-09-26 18:09:14 +00:00
const createResult = Tax.create(tax!);
2025-09-10 16:06:29 +00:00
if (createResult.isFailure) {
return Result.fail(
2025-10-18 19:57:52 +00:00
new ValidationErrorCollection("Invoice item tax creation failed", [
2025-09-10 16:06:29 +00:00
{ path: `taxes[${index}]`, message: createResult.error.message },
])
);
}
return createResult;
}
2025-09-26 15:00:11 +00:00
public mapToPersistence(
2025-09-26 18:09:14 +00:00
source: Tax,
2025-09-10 16:06:29 +00:00
params?: MapperParamsType
2025-09-26 15:00:11 +00:00
): Result<CustomerInvoiceItemTaxCreationAttributes, Error> {
const { errors, parent } = params as {
parent: CustomerInvoiceItem;
errors: ValidationErrorDetail[];
};
const taxableAmount = parent.getTaxableAmount();
2025-09-26 18:09:14 +00:00
const taxAmount = taxableAmount.percentage(source.percentage);
2025-09-26 15:00:11 +00:00
return Result.ok({
2025-09-26 18:09:14 +00:00
tax_id: UniqueID.generateNewID().toPrimitive(),
2025-09-26 15:00:11 +00:00
item_id: parent.id.toPrimitive(),
2025-09-26 18:09:14 +00:00
tax_code: source.code,
2025-09-26 15:00:11 +00:00
taxable_amount_value: taxableAmount.value,
taxable_amount_scale: taxableAmount.scale,
taxes_amount_value: taxAmount.value,
taxes_amount_scale: taxAmount.scale,
});
}
2025-09-09 15:48:12 +00:00
}