103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
|
|
import { CurrencyCode, DomainEntity, LanguageCode, UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
||
|
|
import {
|
||
|
|
CustomerInvoiceItemDescription,
|
||
|
|
CustomerInvoiceItemDiscount,
|
||
|
|
CustomerInvoiceItemQuantity,
|
||
|
|
CustomerInvoiceItemSubtotalPrice,
|
||
|
|
CustomerInvoiceItemTotalPrice,
|
||
|
|
CustomerInvoiceItemUnitPrice,
|
||
|
|
} from "../../value-objects";
|
||
|
|
|
||
|
|
export interface CustomerInvoiceItemProps {
|
||
|
|
description: Maybe<CustomerInvoiceItemDescription>;
|
||
|
|
quantity: Maybe<CustomerInvoiceItemQuantity>; // Cantidad de unidades
|
||
|
|
unitPrice: Maybe<CustomerInvoiceItemUnitPrice>; // Precio unitario en la moneda de la factura
|
||
|
|
discount: Maybe<CustomerInvoiceItemDiscount>; // % descuento
|
||
|
|
|
||
|
|
languageCode: LanguageCode;
|
||
|
|
currencyCode: CurrencyCode;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class CustomerInvoiceItem extends DomainEntity<CustomerInvoiceItemProps> {
|
||
|
|
private _subtotalPrice!: CustomerInvoiceItemSubtotalPrice;
|
||
|
|
private _totalPrice!: CustomerInvoiceItemTotalPrice;
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
get unitPrice(): Maybe<CustomerInvoiceItemUnitPrice> {
|
||
|
|
return this.props.unitPrice;
|
||
|
|
}
|
||
|
|
|
||
|
|
get subtotalPrice(): CustomerInvoiceItemSubtotalPrice {
|
||
|
|
if (!this._subtotalPrice) {
|
||
|
|
this._subtotalPrice = this.calculateSubtotal();
|
||
|
|
}
|
||
|
|
return this._subtotalPrice;
|
||
|
|
}
|
||
|
|
|
||
|
|
get discount(): Maybe<CustomerInvoiceItemDiscount> {
|
||
|
|
return this.props.discount;
|
||
|
|
}
|
||
|
|
|
||
|
|
get totalPrice(): CustomerInvoiceItemTotalPrice {
|
||
|
|
if (!this._totalPrice) {
|
||
|
|
this._totalPrice = this.calculateTotal();
|
||
|
|
}
|
||
|
|
return this._totalPrice;
|
||
|
|
}
|
||
|
|
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
|
||
|
|
calculateSubtotal(): CustomerInvoiceItemSubtotalPrice {
|
||
|
|
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*/
|
||
|
|
}
|
||
|
|
|
||
|
|
calculateTotal(): CustomerInvoiceItemTotalPrice {
|
||
|
|
throw new Error("Not implemented");
|
||
|
|
//return this.subtotalPrice.subtract(this.subtotalPrice.percentage(this.discount.toNumber()));
|
||
|
|
}
|
||
|
|
}
|