2025-09-04 17:57:04 +00:00
|
|
|
import {
|
|
|
|
|
ISequelizeMapper,
|
|
|
|
|
MapperParamsType,
|
|
|
|
|
SequelizeMapper,
|
|
|
|
|
ValidationErrorDetail,
|
|
|
|
|
extractOrPushError,
|
|
|
|
|
} from "@erp/core/api";
|
|
|
|
|
import { CurrencyCode, LanguageCode, Quantity, UniqueID, maybeFromNullableVO, toNullable } from "@repo/rdx-ddd";
|
2025-05-26 10:38:45 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
2025-04-22 08:11:50 +00:00
|
|
|
import { InferCreationAttributes } from "sequelize";
|
2025-05-26 10:38:45 +00:00
|
|
|
import {
|
2025-06-11 15:13:44 +00:00
|
|
|
CustomerInvoice,
|
|
|
|
|
CustomerInvoiceItem,
|
|
|
|
|
CustomerInvoiceItemDescription,
|
|
|
|
|
CustomerInvoiceItemDiscount,
|
|
|
|
|
CustomerInvoiceItemQuantity,
|
2025-09-03 18:04:09 +00:00
|
|
|
CustomerInvoiceItemUnitAmount,
|
2025-05-26 10:38:45 +00:00
|
|
|
} from "../../domain";
|
2025-06-12 06:55:17 +00:00
|
|
|
import {
|
|
|
|
|
CustomerInvoiceItemCreationAttributes,
|
|
|
|
|
CustomerInvoiceItemModel,
|
|
|
|
|
CustomerInvoiceModel,
|
|
|
|
|
} from "../sequelize";
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-06-11 15:13:44 +00:00
|
|
|
export interface ICustomerInvoiceItemMapper
|
2025-06-12 06:55:17 +00:00
|
|
|
extends ISequelizeMapper<
|
|
|
|
|
CustomerInvoiceItemModel,
|
|
|
|
|
CustomerInvoiceItemCreationAttributes,
|
|
|
|
|
CustomerInvoiceItem
|
|
|
|
|
> {}
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-06-11 15:13:44 +00:00
|
|
|
export class CustomerInvoiceItemMapper
|
2025-06-12 06:55:17 +00:00
|
|
|
extends SequelizeMapper<
|
|
|
|
|
CustomerInvoiceItemModel,
|
|
|
|
|
CustomerInvoiceItemCreationAttributes,
|
|
|
|
|
CustomerInvoiceItem
|
|
|
|
|
>
|
2025-06-11 15:13:44 +00:00
|
|
|
implements ICustomerInvoiceItemMapper
|
2025-03-18 08:05:00 +00:00
|
|
|
{
|
2025-04-22 08:11:50 +00:00
|
|
|
public mapToDomain(
|
2025-06-11 15:13:44 +00:00
|
|
|
source: CustomerInvoiceItemModel,
|
2025-04-22 08:11:50 +00:00
|
|
|
params?: MapperParamsType
|
2025-06-11 15:13:44 +00:00
|
|
|
): Result<CustomerInvoiceItem, Error> {
|
2025-09-04 17:57:04 +00:00
|
|
|
const { sourceParent, errors } = params as {
|
|
|
|
|
sourceParent: CustomerInvoiceModel;
|
|
|
|
|
errors: ValidationErrorDetail[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const itemId = extractOrPushError(UniqueID.create(source.item_id), "item_id", errors);
|
|
|
|
|
|
|
|
|
|
const languageCode = extractOrPushError(
|
|
|
|
|
LanguageCode.create(sourceParent.language_code),
|
|
|
|
|
"language_code",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const currencyCode = extractOrPushError(
|
|
|
|
|
CurrencyCode.create(sourceParent.currency_code),
|
|
|
|
|
"currency_code",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const description = extractOrPushError(
|
|
|
|
|
maybeFromNullableVO(source.description, (value) =>
|
|
|
|
|
CustomerInvoiceItemDescription.create(value)
|
|
|
|
|
),
|
|
|
|
|
"description",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const quantity = extractOrPushError(
|
|
|
|
|
maybeFromNullableVO(source.quantity_value, (value) =>
|
|
|
|
|
Quantity.create({ value, })
|
|
|
|
|
|
|
|
|
|
CustomerInvoiceItemQuantity.create({
|
|
|
|
|
value: source.quantity_amouwnt,
|
|
|
|
|
scale: source.quantity_scale,
|
|
|
|
|
}),
|
|
|
|
|
"discount_percentage",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const unitAmount = extractOrPushError(
|
|
|
|
|
CustomerInvoiceItemUnitAmount.create({
|
|
|
|
|
value: source.unit_price_amount,
|
|
|
|
|
scale: source.unit_price_scale,
|
|
|
|
|
}),
|
|
|
|
|
"discount_percentage",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const discountPercentage = extractOrPushError(
|
|
|
|
|
CustomerInvoiceItemDiscount.create({
|
|
|
|
|
value: source.discount_amount,
|
|
|
|
|
scale: source.discount_scale,
|
|
|
|
|
}),
|
|
|
|
|
"discount_percentage",
|
|
|
|
|
errors
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-05-26 10:38:45 +00:00
|
|
|
// Creación del objeto de dominio
|
2025-06-11 15:13:44 +00:00
|
|
|
return CustomerInvoiceItem.create(
|
2025-04-22 08:11:50 +00:00
|
|
|
{
|
2025-09-04 17:57:04 +00:00
|
|
|
languageCode: languageCode!,
|
|
|
|
|
currencyCode: currencyCode!,
|
|
|
|
|
description: description!,
|
|
|
|
|
quantity: quantity!,
|
|
|
|
|
unitAmount: unitAmount!,
|
|
|
|
|
discountPercentage: discountPercentage!,
|
|
|
|
|
},º
|
|
|
|
|
id
|
2025-04-22 08:11:50 +00:00
|
|
|
);
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-22 08:11:50 +00:00
|
|
|
public mapToPersistence(
|
2025-06-11 15:13:44 +00:00
|
|
|
source: CustomerInvoiceItem,
|
2025-04-22 08:11:50 +00:00
|
|
|
params?: MapperParamsType
|
2025-06-11 15:13:44 +00:00
|
|
|
): InferCreationAttributes<CustomerInvoiceItemModel, {}> {
|
2025-09-04 17:57:04 +00:00
|
|
|
1
|
2025-04-22 08:11:50 +00:00
|
|
|
const { index, sourceParent } = params as {
|
|
|
|
|
index: number;
|
2025-06-11 15:13:44 +00:00
|
|
|
sourceParent: CustomerInvoice;
|
2025-04-22 08:11:50 +00:00
|
|
|
};
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
item_id: source.id.toPrimitive(),
|
2025-06-12 06:55:17 +00:00
|
|
|
invoice_id: sourceParent.id.toPrimitive(),
|
2025-03-18 08:05:00 +00:00
|
|
|
position: index,
|
|
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
description: toNullable(source.description, (description) => description.toPrimitive()),
|
|
|
|
|
|
|
|
|
|
quantity_value: toNullable(source.quantity, (value) => value.toPrimitive().value),
|
|
|
|
|
quantity_scale: source.quantity.match(
|
|
|
|
|
(value) => value.toPrimitive().scale,
|
|
|
|
|
() => CustomerInvoiceItemQuantity.DEFAULT_SCALE),
|
2025-04-22 08:11:50 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
unit_amount_value: toNullable(source.unitAmount, (value) => value.toPrimitive().value),
|
|
|
|
|
unit_amount_scale: source.unitAmount.match(
|
|
|
|
|
(value) => value.toPrimitive().scale,
|
|
|
|
|
() => CustomerInvoiceItemUnitAmount.DEFAULT_SCALE),
|
2025-04-22 08:11:50 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
subtotal_amount_value: source.subtotalAmount.toPrimitive().value,
|
|
|
|
|
subtotal_amount_scale: source.subtotalAmount.toPrimitive().scale,
|
2025-04-22 08:11:50 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
discount_percentage_value: toNullable(source.discountPercentage, (value) => value.toPrimitive().value),
|
|
|
|
|
discount_percentage_scale: source.discountPercentage.match(
|
|
|
|
|
(value) => value.toPrimitive().scale,
|
|
|
|
|
() => CustomerInvoiceItemUnitAmount.DEFAULT_SCALE),
|
2025-04-22 08:11:50 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
discount_amount_value: source.subtotalAmount.toPrimitive().value,
|
|
|
|
|
discount_amount_scale: source.subtotalAmount.toPrimitive().scale,
|
2025-04-22 08:11:50 +00:00
|
|
|
|
2025-09-04 17:57:04 +00:00
|
|
|
total_amount_value: source.totalAmount.toPrimitive().value,
|
|
|
|
|
total_amount_scale: source.totalAmount.toPrimitive().scale,
|
2025-03-18 08:05:00 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|