2025-09-03 10:41:12 +00:00
|
|
|
import {
|
|
|
|
|
ISequelizeMapper,
|
|
|
|
|
MapperParamsType,
|
|
|
|
|
SequelizeMapper,
|
|
|
|
|
ValidationErrorCollection,
|
|
|
|
|
ValidationErrorDetail,
|
|
|
|
|
extractOrPushError,
|
|
|
|
|
} from "@erp/core/api";
|
2025-09-04 17:57:04 +00:00
|
|
|
import {
|
|
|
|
|
CurrencyCode,
|
|
|
|
|
LanguageCode,
|
|
|
|
|
Percentage,
|
|
|
|
|
TextValue,
|
|
|
|
|
UniqueID,
|
|
|
|
|
UtcDate,
|
|
|
|
|
maybeFromNullableVO,
|
2025-09-05 11:23:45 +00:00
|
|
|
toNullable,
|
2025-09-04 17:57:04 +00:00
|
|
|
} from "@repo/rdx-ddd";
|
2025-06-11 15:13:44 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
2025-06-12 06:55:17 +00:00
|
|
|
import {
|
|
|
|
|
CustomerInvoice,
|
2025-09-05 11:23:45 +00:00
|
|
|
CustomerInvoiceItems,
|
2025-06-12 06:55:17 +00:00
|
|
|
CustomerInvoiceNumber,
|
2025-09-03 10:41:12 +00:00
|
|
|
CustomerInvoiceProps,
|
2025-06-12 06:55:17 +00:00
|
|
|
CustomerInvoiceSerie,
|
|
|
|
|
CustomerInvoiceStatus,
|
|
|
|
|
} from "../../domain";
|
2025-06-11 15:13:44 +00:00
|
|
|
import { CustomerInvoiceCreationAttributes, CustomerInvoiceModel } from "../sequelize";
|
2025-06-12 06:55:17 +00:00
|
|
|
import { CustomerInvoiceItemMapper } from "./customer-invoice-item.mapper";
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
export interface ICustomerInvoiceMapper
|
2025-06-12 06:55:17 +00:00
|
|
|
extends ISequelizeMapper<
|
|
|
|
|
CustomerInvoiceModel,
|
|
|
|
|
CustomerInvoiceCreationAttributes,
|
|
|
|
|
CustomerInvoice
|
|
|
|
|
> {}
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
export class CustomerInvoiceMapper
|
|
|
|
|
extends SequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice>
|
|
|
|
|
implements ICustomerInvoiceMapper
|
|
|
|
|
{
|
2025-09-04 17:57:04 +00:00
|
|
|
private _itemsMapper: CustomerInvoiceItemMapper;
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2025-09-04 17:57:04 +00:00
|
|
|
this._itemsMapper = new CustomerInvoiceItemMapper(); // Instanciar el mapper de items
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
public mapToDomain(
|
|
|
|
|
source: CustomerInvoiceModel,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): Result<CustomerInvoice, Error> {
|
2025-09-04 17:57:04 +00:00
|
|
|
try {
|
|
|
|
|
const errors: ValidationErrorDetail[] = [];
|
|
|
|
|
|
|
|
|
|
const invoiceId = extractOrPushError(UniqueID.create(source.id), "id", errors);
|
|
|
|
|
const companyId = extractOrPushError(
|
|
|
|
|
UniqueID.create(source.company_id),
|
|
|
|
|
"company_id",
|
|
|
|
|
errors
|
2025-09-03 10:41:12 +00:00
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
const status = extractOrPushError(
|
|
|
|
|
CustomerInvoiceStatus.create(source.status),
|
|
|
|
|
"status",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const series = extractOrPushError(
|
|
|
|
|
maybeFromNullableVO(source.series, (value) => CustomerInvoiceSerie.create(value)),
|
|
|
|
|
"serie",
|
|
|
|
|
errors
|
|
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
const invoiceNumber = extractOrPushError(
|
|
|
|
|
CustomerInvoiceNumber.create(source.invoice_number),
|
|
|
|
|
"invoice_number",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const invoiceDate = extractOrPushError(
|
|
|
|
|
UtcDate.createFromISO(source.invoice_date),
|
|
|
|
|
"invoice_date",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const operationDate = extractOrPushError(
|
|
|
|
|
maybeFromNullableVO(source.operation_date, (value) => UtcDate.createFromISO(value)),
|
|
|
|
|
"operation_date",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const notes = extractOrPushError(
|
|
|
|
|
maybeFromNullableVO(source.notes, (value) => TextValue.create(value)),
|
|
|
|
|
"notes",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const languageCode = extractOrPushError(
|
|
|
|
|
LanguageCode.create(source.language_code),
|
|
|
|
|
"language_code",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const currencyCode = extractOrPushError(
|
|
|
|
|
CurrencyCode.create(source.currency_code),
|
|
|
|
|
"currency_code",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const discountPercentage = extractOrPushError(
|
|
|
|
|
Percentage.create({
|
|
|
|
|
value: source.discount_percentage_value,
|
|
|
|
|
scale: source.discount_percentage_scale,
|
|
|
|
|
}),
|
|
|
|
|
"discount_percentage",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
|
return Result.fail(
|
|
|
|
|
new ValidationErrorCollection("Customer invoice props mapping failed", errors)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mapear los items de la factura
|
|
|
|
|
const items = this._itemsMapper.mapArrayToDomain(source.items, {
|
|
|
|
|
sourceParent: source,
|
|
|
|
|
errors,
|
|
|
|
|
...params,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (errors.length > 0) {
|
|
|
|
|
return Result.fail(
|
|
|
|
|
new ValidationErrorCollection("Customer invoice item props mapping failed", errors)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const invoiceProps: CustomerInvoiceProps = {
|
|
|
|
|
companyId: companyId!,
|
|
|
|
|
status: status!,
|
|
|
|
|
series: series!,
|
|
|
|
|
invoiceNumber: invoiceNumber!,
|
|
|
|
|
invoiceDate: invoiceDate!,
|
|
|
|
|
operationDate: operationDate!,
|
|
|
|
|
|
|
|
|
|
notes: notes!,
|
|
|
|
|
|
|
|
|
|
languageCode: languageCode!,
|
|
|
|
|
currencyCode: currencyCode!,
|
|
|
|
|
|
|
|
|
|
discountPercentage: discountPercentage!,
|
|
|
|
|
|
2025-09-05 11:23:45 +00:00
|
|
|
items: CustomerInvoiceItems.create({
|
|
|
|
|
languageCode: languageCode!,
|
|
|
|
|
currencyCode: currencyCode!,
|
|
|
|
|
}),
|
2025-09-04 17:57:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return CustomerInvoice.create(invoiceProps, invoiceId);
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
return Result.fail(err as Error);
|
|
|
|
|
}
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
public mapToPersistence(
|
|
|
|
|
source: CustomerInvoice,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): CustomerInvoiceCreationAttributes {
|
2025-09-05 11:23:45 +00:00
|
|
|
//const subtotal = source.calculateSubtotal();
|
|
|
|
|
//const total = source.calculateTotal();
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
const items = this._itemsMapper.mapCollectionToPersistence(source.items, params);
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
return {
|
2025-09-05 11:23:45 +00:00
|
|
|
id: source.id.toPrimitive(),
|
|
|
|
|
company_id: source.companyId.toPrimitive(),
|
|
|
|
|
|
|
|
|
|
status: source.status.toPrimitive(),
|
|
|
|
|
series: toNullable(source.series, (series) => series.toPrimitive()),
|
|
|
|
|
|
2025-06-26 11:32:55 +00:00
|
|
|
invoice_number: source.invoiceNumber.toPrimitive(),
|
2025-09-04 17:57:04 +00:00
|
|
|
invoice_date: source.invoiceDate.toPrimitive(),
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-05 11:23:45 +00:00
|
|
|
operation_date: toNullable(source.operationDate, (operationDate) =>
|
|
|
|
|
operationDate.toPrimitive()
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
notes: toNullable(source.notes, (notes) => notes.toPrimitive()),
|
|
|
|
|
|
|
|
|
|
language_code: source.languageCode.code,
|
|
|
|
|
currency_code: source.currencyCode.code,
|
|
|
|
|
|
|
|
|
|
subtotal_amount_value: 0, //subtotal.amount,
|
|
|
|
|
subtotal_amount_scale: 2, //subtotal.scale,
|
|
|
|
|
|
|
|
|
|
/*discount_percentage_value: source.discountPercentage.value,
|
|
|
|
|
discount_percentage_scale: source.discountPercentage.scale,
|
|
|
|
|
|
|
|
|
|
discount_amount_value: source.discountAmount.value,
|
|
|
|
|
discount_amount_scale: source.discountAmount.scale,*/
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-05 11:23:45 +00:00
|
|
|
total_amount_value: 0, //total.amount,
|
|
|
|
|
total_amount_scale: 2, //total.scale,
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
items,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|