Uecko_ERP/modules/customer-invoices/src/api/domain/entities/customer-invoice-items/customer-invoice-item.ts

103 lines
2.9 KiB
TypeScript
Raw Normal View History

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
}
get discount(): Maybe<CustomerInvoiceItemDiscount> {
return this.props.discount;
}
2025-09-03 18:04:09 +00:00
get totalPrice(): CustomerInvoiceItemTotalAmount {
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()));
}
}