2025-09-03 10:41:12 +00:00
|
|
|
import { CurrencyCode, DomainEntity, LanguageCode, UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
|
|
|
|
import {
|
|
|
|
|
CustomerInvoiceItemDescription,
|
|
|
|
|
CustomerInvoiceItemDiscount,
|
|
|
|
|
CustomerInvoiceItemQuantity,
|
2025-09-03 18:04:09 +00:00
|
|
|
CustomerInvoiceItemSubtotalAmount,
|
|
|
|
|
CustomerInvoiceItemTotalAmount,
|
|
|
|
|
CustomerInvoiceItemUnitAmount,
|
2025-09-03 10:41:12 +00:00
|
|
|
} from "../../value-objects";
|
|
|
|
|
|
|
|
|
|
export interface CustomerInvoiceItemProps {
|
|
|
|
|
description: Maybe<CustomerInvoiceItemDescription>;
|
|
|
|
|
quantity: Maybe<CustomerInvoiceItemQuantity>; // Cantidad de unidades
|
2025-09-03 18:04:09 +00:00
|
|
|
unitPrice: Maybe<CustomerInvoiceItemUnitAmount>; // Precio unitario en la moneda de la factura
|
2025-09-03 10:41:12 +00:00
|
|
|
discount: Maybe<CustomerInvoiceItemDiscount>; // % descuento
|
|
|
|
|
|
|
|
|
|
languageCode: LanguageCode;
|
|
|
|
|
currencyCode: CurrencyCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class CustomerInvoiceItem extends DomainEntity<CustomerInvoiceItemProps> {
|
2025-09-03 18:04:09 +00:00
|
|
|
private _subtotalAmount!: CustomerInvoiceItemSubtotalAmount;
|
|
|
|
|
private _totalAmount!: CustomerInvoiceItemTotalAmount;
|
2025-09-03 10:41:12 +00:00
|
|
|
|
|
|
|
|
public static create(
|
|
|
|
|
props: CustomerInvoiceItemProps,
|
|
|
|
|
id?: UniqueID
|
|
|
|
|
): Result<CustomerInvoiceItem, Error> {
|
|
|
|
|
const item = new CustomerInvoiceItem(props, id);
|
|
|
|
|
|
|
|
|
|
// Reglas de negocio / validaciones
|
|
|
|
|
// ...
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
// 🔹 Disparar evento de dominio "CustomerInvoiceItemCreatedEvent"
|
|
|
|
|
//const { customerInvoice } = props;
|
|
|
|
|
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerInvoice.toString()));
|
|
|
|
|
|
|
|
|
|
return Result.ok(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get description(): Maybe<CustomerInvoiceItemDescription> {
|
|
|
|
|
return this.props.description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get quantity(): Maybe<CustomerInvoiceItemQuantity> {
|
|
|
|
|
return this.props.quantity;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
get unitAmount(): Maybe<CustomerInvoiceItemUnitAmount> {
|
2025-09-03 10:41:12 +00:00
|
|
|
return this.props.unitPrice;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
get subtotalAmount(): CustomerInvoiceItemSubtotalAmount {
|
|
|
|
|
if (!this._subtotalAmount) {
|
|
|
|
|
this._subtotalAmount = this.calculateSubtotal();
|
2025-09-03 10:41:12 +00:00
|
|
|
}
|
2025-09-03 18:04:09 +00:00
|
|
|
return this._subtotalAmount;
|
2025-09-03 10:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-04 10:02:24 +00:00
|
|
|
get discountPercentage(): Maybe<CustomerInvoiceItemDiscount> {
|
2025-09-03 10:41:12 +00:00
|
|
|
return this.props.discount;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 10:02:24 +00:00
|
|
|
get totalAmount(): CustomerInvoiceItemTotalAmount {
|
2025-09-03 18:04:09 +00:00
|
|
|
if (!this._totalAmount) {
|
|
|
|
|
this._totalAmount = this.calculateTotal();
|
2025-09-03 10:41:12 +00:00
|
|
|
}
|
2025-09-03 18:04:09 +00:00
|
|
|
return this._totalAmount;
|
2025-09-03 10:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get languageCode(): LanguageCode {
|
|
|
|
|
return this.props.languageCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get currencyCode(): CurrencyCode {
|
|
|
|
|
return this.props.currencyCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getValue(): CustomerInvoiceItemProps {
|
|
|
|
|
return this.props;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toPrimitive() {
|
|
|
|
|
return this.getValue();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
calculateSubtotal(): CustomerInvoiceItemSubtotalAmount {
|
2025-09-03 10:41:12 +00:00
|
|
|
throw new Error("Not implemented");
|
|
|
|
|
|
|
|
|
|
/*const unitPrice = this.unitPrice.isSome()
|
|
|
|
|
? this.unitPrice.unwrap()
|
|
|
|
|
: CustomerInvoiceItemUnitPrice.zero();
|
|
|
|
|
return this.unitPrice.multiply(this.quantity.toNumber()); // Precio unitario * Cantidad*/
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
calculateTotal(): CustomerInvoiceItemTotalAmount {
|
2025-09-03 10:41:12 +00:00
|
|
|
throw new Error("Not implemented");
|
|
|
|
|
//return this.subtotalPrice.subtract(this.subtotalPrice.percentage(this.discount.toNumber()));
|
|
|
|
|
}
|
|
|
|
|
}
|