Uecko_ERP/apps/server/src/contexts/customer-billing/domain/events/customer-invoice-item.ts
2025-02-24 20:00:28 +01:00

62 lines
1.8 KiB
TypeScript

import { DomainEntity, MoneyValue, Percentage, UniqueID } from "@common/domain";
import { Quantity } from "@common/domain/value-objects/quantity";
import { Maybe, Result } from "@common/helpers";
export interface ICustomerInvoiceItemProps {
description: Maybe<string>; // Descripción del artículo o servicio
quantity: Quantity; // Cantidad de unidades
unitPrice: MoneyValue; // Precio unitario en la moneda de la factura
// subtotalPrice: MoneyValue; // Precio unitario * Cantidad
discount: Percentage; // % descuento
// totalPrice: MoneyValue;
}
export interface ICustomerInvoiceItem {
description: Maybe<string>;
quantity: Quantity;
unitPrice: MoneyValue;
subtotalPrice: MoneyValue;
discount: Percentage;
totalPrice: MoneyValue;
}
export class CustomerInvoiceItem
extends DomainEntity<ICustomerInvoiceItemProps>
implements ICustomerInvoiceItem
{
public static create(
props: ICustomerInvoiceItemProps,
id?: UniqueID
): Result<CustomerInvoiceItem, Error> {
return Result.ok(new CustomerInvoiceItem(props, id));
}
get description(): Maybe<string> {
return this.props.description;
}
get quantity(): Quantity {
return this.props.quantity;
}
get unitPrice(): MoneyValue {
return this.props.unitPrice;
}
get subtotalPrice(): MoneyValue {
return this.quantity.isNull() || this.unitPrice.isNull()
? MoneyValue.create({ amount: null, scale: 2 }).object
: this.unitPrice.multiply(this.quantity.toNumber());
}
get discount(): Percentage {
return this.props.discount;
}
get totalPrice(): MoneyValue {
return this.subtotalPrice.isNull()
? MoneyValue.create({ amount: null, scale: 2 }).object
: this.subtotalPrice.subtract(this.subtotalPrice.percentage(this.discount.toNumber()));
}
}