2025-09-09 18:13:54 +00:00
|
|
|
import { JsonTaxCatalogProvider } from "@erp/core";
|
|
|
|
|
import {
|
|
|
|
|
MapperParamsType,
|
|
|
|
|
SequelizeMapper,
|
|
|
|
|
Tax,
|
|
|
|
|
Taxes,
|
|
|
|
|
ValidationErrorCollection,
|
|
|
|
|
ValidationErrorDetail,
|
|
|
|
|
extractOrPushError,
|
|
|
|
|
} from "@erp/core/api";
|
|
|
|
|
import { Result } from "@repo/rdx-utils";
|
2025-09-09 15:48:12 +00:00
|
|
|
import { InferCreationAttributes } from "sequelize";
|
2025-09-09 18:13:54 +00:00
|
|
|
import { CustomerInvoiceProps, InvoiceAmount } from "../../domain";
|
|
|
|
|
import { InvoiceTax } from "../../domain/entities/invoice-taxes";
|
|
|
|
|
import {
|
|
|
|
|
CustomerInvoiceItemTaxModel,
|
|
|
|
|
CustomerInvoiceTaxCreationAttributes,
|
|
|
|
|
CustomerInvoiceTaxModel,
|
|
|
|
|
} from "../sequelize";
|
2025-09-09 15:48:12 +00:00
|
|
|
|
2025-09-09 18:13:54 +00:00
|
|
|
export class TaxesMapper extends SequelizeMapper<
|
|
|
|
|
CustomerInvoiceTaxModel,
|
|
|
|
|
CustomerInvoiceTaxCreationAttributes,
|
|
|
|
|
InvoiceTax
|
|
|
|
|
> {
|
|
|
|
|
private _taxCatalog: JsonTaxCatalogProvider;
|
|
|
|
|
|
|
|
|
|
constructor(params: {
|
|
|
|
|
taxCatalog: JsonTaxCatalogProvider;
|
|
|
|
|
}) {
|
|
|
|
|
super();
|
|
|
|
|
const { taxCatalog } = params;
|
|
|
|
|
this._taxCatalog = taxCatalog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public mapToDomain(
|
|
|
|
|
source: CustomerInvoiceTaxModel,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): Result<InvoiceTax, Error> {
|
|
|
|
|
const { errors, index, attributes } = params as {
|
|
|
|
|
index: number;
|
|
|
|
|
requireIncludes: boolean;
|
|
|
|
|
errors: ValidationErrorDetail[];
|
|
|
|
|
attributes: Partial<CustomerInvoiceProps>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const tax = extractOrPushError(
|
|
|
|
|
Tax.createFromCode(source.tax_code, this._taxCatalog),
|
|
|
|
|
`taxes[${index}].tax_code`,
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const taxableAmount = extractOrPushError(
|
|
|
|
|
InvoiceAmount.create({
|
|
|
|
|
value: source.taxable_amount_value,
|
|
|
|
|
currency_code: attributes.currencyCode?.code,
|
|
|
|
|
}),
|
|
|
|
|
`taxes[${index}].taxable_amount_value`,
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (source.taxable_amount_scale !== InvoiceAmount.DEFAULT_SCALE) {
|
|
|
|
|
errors.push({
|
|
|
|
|
path: `taxes[${index}].taxable_amount_scale`,
|
|
|
|
|
message: "Invalid taxable amount scale",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const taxesAmount = extractOrPushError(
|
|
|
|
|
InvoiceAmount.create({
|
|
|
|
|
value: source.taxes_amount_value,
|
|
|
|
|
currency_code: attributes.currencyCode?.code,
|
|
|
|
|
}),
|
|
|
|
|
`taxes[${index}].taxes_amount_value`,
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (source.taxes_amount_scale !== InvoiceAmount.DEFAULT_SCALE) {
|
|
|
|
|
errors.push({
|
|
|
|
|
path: `taxes[${index}].taxes_amount_scale`,
|
|
|
|
|
message: "Invalid taxes amount scale",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creación del objeto de dominio
|
|
|
|
|
const createResult = InvoiceTax.create({
|
|
|
|
|
tax: tax!,
|
|
|
|
|
taxableAmount: taxableAmount!,
|
|
|
|
|
taxesAmount: taxesAmount!,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (createResult.isFailure) {
|
|
|
|
|
return Result.fail(
|
|
|
|
|
new ValidationErrorCollection("Invoice taxes creation failed", [
|
|
|
|
|
{ path: `taxes[${index}]`, message: createResult.error.message },
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return createResult;
|
|
|
|
|
}
|
2025-09-09 15:48:12 +00:00
|
|
|
|
|
|
|
|
public mapToPersistence(
|
|
|
|
|
source: Taxes,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): InferCreationAttributes<CustomerInvoiceItemTaxModel, {}> {
|
|
|
|
|
/*const { index, sourceParent } = params as {
|
|
|
|
|
index: number;
|
|
|
|
|
sourceParent: CustomerInvoice;
|
|
|
|
|
};*/
|
|
|
|
|
}
|
|
|
|
|
}
|