277 lines
7.9 KiB
TypeScript
277 lines
7.9 KiB
TypeScript
import {
|
|
ISequelizeMapper,
|
|
MapperParamsType,
|
|
SequelizeMapper,
|
|
ValidationErrorCollection,
|
|
ValidationErrorDetail,
|
|
extractOrPushError,
|
|
} from "@erp/core/api";
|
|
import {
|
|
CurrencyCode,
|
|
LanguageCode,
|
|
Percentage,
|
|
TextValue,
|
|
UniqueID,
|
|
UtcDate,
|
|
maybeFromNullableVO,
|
|
toNullable,
|
|
} from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
import {
|
|
CustomerInvoice,
|
|
CustomerInvoiceItems,
|
|
CustomerInvoiceNumber,
|
|
CustomerInvoiceProps,
|
|
CustomerInvoiceSerie,
|
|
CustomerInvoiceStatus,
|
|
} from "../../domain";
|
|
import { CustomerInvoiceCreationAttributes, CustomerInvoiceModel } from "../sequelize";
|
|
import { CustomerInvoiceItemMapper } from "./customer-invoice-item.mapper";
|
|
import { InvoiceRecipientMapper } from "./invoice-recipient.mapper";
|
|
import { TaxesMapper } from "./taxes.mapper";
|
|
|
|
export interface ICustomerInvoiceMapper
|
|
extends ISequelizeMapper<
|
|
CustomerInvoiceModel,
|
|
CustomerInvoiceCreationAttributes,
|
|
CustomerInvoice
|
|
> {}
|
|
|
|
export class CustomerInvoiceMapper
|
|
extends SequelizeMapper<CustomerInvoiceModel, CustomerInvoiceCreationAttributes, CustomerInvoice>
|
|
implements ICustomerInvoiceMapper
|
|
{
|
|
private _itemsMapper: CustomerInvoiceItemMapper;
|
|
private _recipientMapper: InvoiceRecipientMapper;
|
|
private _taxesMapper: TaxesMapper;
|
|
|
|
constructor() {
|
|
super();
|
|
this._itemsMapper = new CustomerInvoiceItemMapper(); // Instanciar el mapper de items
|
|
this._recipientMapper = new InvoiceRecipientMapper();
|
|
this._taxesMapper = new TaxesMapper();
|
|
}
|
|
|
|
public mapToDomain(
|
|
source: CustomerInvoiceModel,
|
|
params?: MapperParamsType
|
|
): Result<CustomerInvoice, Error> {
|
|
try {
|
|
const errors: ValidationErrorDetail[] = [];
|
|
|
|
const invoiceId = extractOrPushError(UniqueID.create(source.id), "id", errors);
|
|
const companyId = extractOrPushError(
|
|
UniqueID.create(source.company_id),
|
|
"company_id",
|
|
errors
|
|
);
|
|
|
|
const isProforma = Boolean(source.is_proforma);
|
|
|
|
const status = extractOrPushError(
|
|
CustomerInvoiceStatus.create(source.status),
|
|
"status",
|
|
errors
|
|
);
|
|
|
|
const series = extractOrPushError(
|
|
maybeFromNullableVO(source.series, (value) => CustomerInvoiceSerie.create(value)),
|
|
"serie",
|
|
errors
|
|
);
|
|
|
|
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
|
|
);
|
|
|
|
// Customer
|
|
const customerId = extractOrPushError(
|
|
UniqueID.create(source.customer_id),
|
|
"customer_id",
|
|
errors
|
|
);
|
|
|
|
// Recipient (customer data) (snapshot)
|
|
const recipient = this._recipientMapper.mapToDomain(source, {
|
|
errors,
|
|
...params,
|
|
});
|
|
|
|
// Mapear los items de la factura
|
|
const itemsOrResult = this._itemsMapper.mapArrayToDomain(source.items, {
|
|
parent: source,
|
|
errors,
|
|
...params,
|
|
});
|
|
|
|
// Mapear los impuestos
|
|
const taxesOrResult = this._taxesMapper.mapArrayToDomain(source.taxes, {
|
|
parent: source,
|
|
errors,
|
|
...params,
|
|
});
|
|
|
|
if (errors.length > 0) {
|
|
return Result.fail(
|
|
new ValidationErrorCollection("Customer invoice item props mapping failed", errors)
|
|
);
|
|
}
|
|
|
|
const invoiceProps: CustomerInvoiceProps = {
|
|
companyId: companyId!,
|
|
|
|
isProforma: isProforma,
|
|
status: status!,
|
|
series: series!,
|
|
invoiceNumber: invoiceNumber!,
|
|
invoiceDate: invoiceDate!,
|
|
operationDate: operationDate!,
|
|
|
|
customerId: customerId!,
|
|
recipient: recipient,
|
|
|
|
notes: notes!,
|
|
|
|
languageCode: languageCode!,
|
|
currencyCode: currencyCode!,
|
|
|
|
discountPercentage: discountPercentage!,
|
|
|
|
taxes: taxesOrResult,
|
|
|
|
items: CustomerInvoiceItems.create({
|
|
languageCode: languageCode!,
|
|
currencyCode: currencyCode!,
|
|
items: itemsOrResult.isSuccess ? itemsOrResult.data.getAll() : [],
|
|
}),
|
|
};
|
|
|
|
return CustomerInvoice.create(invoiceProps, invoiceId);
|
|
} catch (err: unknown) {
|
|
return Result.fail(err as Error);
|
|
}
|
|
}
|
|
|
|
public mapToPersistence(
|
|
source: CustomerInvoice,
|
|
params?: MapperParamsType
|
|
): CustomerInvoiceCreationAttributes {
|
|
const items = this._itemsMapper.mapCollectionToPersistence(source.items, params);
|
|
|
|
const customer = source.recipient.match(
|
|
(recipient) =>
|
|
({
|
|
customer_id: recipient.id.toPrimitive(),
|
|
customer_tin: recipient.tin.toPrimitive(),
|
|
customer_name: recipient.name.toPrimitive(),
|
|
customer_street: toNullable(recipient.address.street, (street) => street.toPrimitive()),
|
|
customer_street2: toNullable(recipient.address.street2, (street2) =>
|
|
street2.toPrimitive()
|
|
),
|
|
customer_city: toNullable(recipient.address.city, (city) => city.toPrimitive()),
|
|
customer_province: toNullable(recipient.address.province, (province) =>
|
|
province.toPrimitive()
|
|
),
|
|
customer_postal_code: toNullable(recipient.address.postalCode, (postalCode) =>
|
|
postalCode.toPrimitive()
|
|
),
|
|
customer_country: toNullable(recipient.address.country, (country) =>
|
|
country.toPrimitive()
|
|
),
|
|
}) as any,
|
|
() => ({
|
|
customer_id: source.customerId.toPrimitive(),
|
|
customer_tin: null,
|
|
customer_name: null,
|
|
customer_street: null,
|
|
customer_street2: null,
|
|
customer_city: null,
|
|
customer_province: null,
|
|
customer_postal_code: null,
|
|
customer_country: null,
|
|
})
|
|
) as any;
|
|
|
|
return {
|
|
id: source.id.toPrimitive(),
|
|
company_id: source.companyId.toPrimitive(),
|
|
|
|
status: source.status.toPrimitive(),
|
|
series: toNullable(source.series, (series) => series.toPrimitive()),
|
|
|
|
invoice_number: source.invoiceNumber.toPrimitive(),
|
|
invoice_date: source.invoiceDate.toPrimitive(),
|
|
|
|
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,
|
|
|
|
taxable_amount_value: source.taxableAmount.value,
|
|
taxable_amount_scale: source.taxableAmount.value,
|
|
tax_amount_value: source.taxAmount.value,
|
|
tax_amount_scale: source.taxAmount.value,
|
|
|
|
total_amount_value: 0, //total.amount,
|
|
total_amount_scale: 2, //total.scale,
|
|
|
|
items,
|
|
...customer,
|
|
};
|
|
}
|
|
}
|