Uecko_ERP/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts

211 lines
5.2 KiB
TypeScript
Raw Normal View History

2025-09-09 18:13:54 +00:00
import { DomainValidationError } from "@erp/core/api";
2025-09-03 10:41:12 +00:00
import {
AggregateRoot,
CurrencyCode,
LanguageCode,
2025-09-03 18:04:09 +00:00
Percentage,
2025-09-03 10:41:12 +00:00
TextValue,
UniqueID,
UtcDate,
} from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
2025-09-10 16:06:29 +00:00
import { CustomerInvoiceItems } from "../entities";
2025-09-09 18:13:54 +00:00
import { InvoiceTaxes } from "../entities/invoice-taxes";
2025-06-12 06:55:17 +00:00
import {
CustomerInvoiceNumber,
CustomerInvoiceSerie,
CustomerInvoiceStatus,
2025-09-10 18:14:19 +00:00
InvoiceAmount,
2025-09-10 16:06:29 +00:00
InvoiceRecipient,
2025-06-12 06:55:17 +00:00
} from "../value-objects";
2025-03-18 08:05:00 +00:00
2025-06-24 18:38:57 +00:00
export interface CustomerInvoiceProps {
2025-09-03 10:41:12 +00:00
companyId: UniqueID;
2025-09-03 18:04:09 +00:00
2025-09-09 15:48:12 +00:00
isProforma: boolean;
2025-09-03 18:04:09 +00:00
invoiceNumber: CustomerInvoiceNumber;
2025-06-11 15:13:44 +00:00
status: CustomerInvoiceStatus;
2025-09-03 10:41:12 +00:00
series: Maybe<CustomerInvoiceSerie>;
2025-03-18 08:05:00 +00:00
2025-09-04 17:57:04 +00:00
invoiceDate: UtcDate;
2025-09-03 10:41:12 +00:00
operationDate: Maybe<UtcDate>;
2025-09-08 17:24:38 +00:00
customerId: UniqueID;
recipient: Maybe<InvoiceRecipient>;
2025-09-03 10:41:12 +00:00
notes: Maybe<TextValue>;
2025-03-18 08:05:00 +00:00
2025-09-03 10:41:12 +00:00
languageCode: LanguageCode;
currencyCode: CurrencyCode;
2025-09-10 18:14:19 +00:00
taxes: Maybe<InvoiceTaxes>;
items: CustomerInvoiceItems;
2025-09-03 18:04:09 +00:00
discountPercentage: Percentage;
2025-09-10 18:14:19 +00:00
}
2025-09-03 18:04:09 +00:00
2025-09-10 18:14:19 +00:00
export interface ICustomerInvoice {
getSubtotalAmount(): InvoiceAmount;
getDiscountAmount(): InvoiceAmount;
2025-09-03 18:04:09 +00:00
2025-09-10 18:14:19 +00:00
getTaxableAmount(): InvoiceAmount;
getTaxesAmount(): InvoiceAmount;
getTotalAmount(): InvoiceAmount;
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
export type CustomerInvoicePatchProps = Partial<Omit<CustomerInvoiceProps, "companyId">>;
2025-03-18 08:05:00 +00:00
2025-09-10 18:14:19 +00:00
export class CustomerInvoice
extends AggregateRoot<CustomerInvoiceProps>
implements ICustomerInvoice
{
2025-09-03 10:41:12 +00:00
private _items!: CustomerInvoiceItems;
2025-09-10 18:14:19 +00:00
private _taxes!: InvoiceTaxes;
2025-04-01 14:26:15 +00:00
2025-06-24 18:38:57 +00:00
protected constructor(props: CustomerInvoiceProps, id?: UniqueID) {
2025-04-01 14:26:15 +00:00
super(props, id);
2025-09-03 10:41:12 +00:00
this._items =
props.items ||
CustomerInvoiceItems.create({
languageCode: props.languageCode,
currencyCode: props.currencyCode,
});
2025-09-10 18:14:19 +00:00
this._taxes = props.taxes.match(
(taxes) => taxes,
() => InvoiceTaxes.create({})
);
2025-04-01 14:26:15 +00:00
}
2025-03-18 08:05:00 +00:00
2025-06-24 18:38:57 +00:00
static create(props: CustomerInvoiceProps, id?: UniqueID): Result<CustomerInvoice, Error> {
2025-06-12 06:55:17 +00:00
const customerInvoice = new CustomerInvoice(props, id);
2025-03-18 08:05:00 +00:00
// Reglas de negocio / validaciones
2025-09-09 15:48:12 +00:00
if (!customerInvoice.isProforma && !customerInvoice.hasRecipient) {
return Result.fail(
new DomainValidationError(
"MISSING_CUSTOMER_DATA",
"recipient",
"Customer data must be provided for non-proforma invoices"
)
);
}
2025-06-11 15:13:44 +00:00
// 🔹 Disparar evento de dominio "CustomerInvoiceAuthenticatedEvent"
2025-06-12 06:55:17 +00:00
//const { customerInvoice } = props;
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerInvoice.toString()));
2025-03-18 08:05:00 +00:00
2025-06-12 06:55:17 +00:00
return Result.ok(customerInvoice);
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
public update(partialInvoice: CustomerInvoicePatchProps): Result<CustomerInvoice, Error> {
throw new Error("Not implemented");
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
public get companyId(): UniqueID {
return this.props.companyId;
2025-03-18 08:05:00 +00:00
}
2025-09-08 17:24:38 +00:00
public get customerId(): UniqueID {
return this.props.customerId;
}
2025-09-09 15:48:12 +00:00
public get isProforma(): boolean {
return this.props.isProforma;
}
2025-09-03 18:04:09 +00:00
public get status(): CustomerInvoiceStatus {
return this.props.status;
}
2025-09-03 10:41:12 +00:00
public get series(): Maybe<CustomerInvoiceSerie> {
return this.props.series;
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
public get invoiceNumber() {
return this.props.invoiceNumber;
}
2025-03-18 08:05:00 +00:00
2025-09-04 17:57:04 +00:00
public get invoiceDate(): UtcDate {
return this.props.invoiceDate;
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
public get operationDate(): Maybe<UtcDate> {
2025-03-18 08:05:00 +00:00
return this.props.operationDate;
}
2025-09-03 10:41:12 +00:00
public get notes(): Maybe<TextValue> {
return this.props.notes;
2025-03-18 08:05:00 +00:00
}
2025-09-08 17:24:38 +00:00
public get recipient(): Maybe<InvoiceRecipient> {
return this.props.recipient;
}
2025-09-03 10:41:12 +00:00
public get languageCode(): LanguageCode {
return this.props.languageCode;
2025-03-18 08:05:00 +00:00
}
2025-09-03 10:41:12 +00:00
public get currencyCode(): CurrencyCode {
return this.props.currencyCode;
2025-03-18 08:05:00 +00:00
}
2025-09-03 18:04:09 +00:00
public get discountPercentage(): Percentage {
return this.props.discountPercentage;
}
2025-09-10 18:14:19 +00:00
// Method to get the complete list of line items
public get items(): CustomerInvoiceItems {
return this._items;
2025-09-03 18:04:09 +00:00
}
2025-09-10 18:14:19 +00:00
public get taxes(): InvoiceTaxes {
return this._taxes;
2025-09-03 18:04:09 +00:00
}
2025-09-10 18:14:19 +00:00
public get hasRecipient() {
return this.recipient.isSome();
2025-09-03 18:04:09 +00:00
}
2025-09-10 18:14:19 +00:00
public getSubtotalAmount(): InvoiceAmount {
const itemsSubtotal = this.items.getTotalAmount().convertScale(2);
2025-09-03 18:04:09 +00:00
2025-09-10 18:14:19 +00:00
return InvoiceAmount.create({
value: itemsSubtotal.value,
currency_code: this.currencyCode.code,
}).data as InvoiceAmount;
2025-03-18 08:05:00 +00:00
}
2025-09-10 18:14:19 +00:00
public getDiscountAmount(): InvoiceAmount {
return this.getSubtotalAmount().percentage(this.discountPercentage) as InvoiceAmount;
2025-09-08 17:24:38 +00:00
}
2025-09-10 18:14:19 +00:00
public getTaxableAmount(): InvoiceAmount {
return this.getSubtotalAmount().subtract(this.getDiscountAmount()) as InvoiceAmount;
2025-03-18 08:05:00 +00:00
}
2025-09-10 18:14:19 +00:00
public getTaxesAmount(): InvoiceAmount {
return this._getTaxesAmount(this.getTaxableAmount());
2025-03-18 08:05:00 +00:00
}
2025-09-10 18:14:19 +00:00
public getTotalAmount(): InvoiceAmount {
const taxableAmount = this.getTaxableAmount();
return taxableAmount.add(this._getTaxesAmount(taxableAmount)) as InvoiceAmount;
2025-03-18 08:05:00 +00:00
}
2025-09-10 18:14:19 +00:00
public getAllAmounts() {
return {
subtotalAmount: this.getSubtotalAmount(),
discountAmount: this.getDiscountAmount(),
taxableAmount: this.getTaxableAmount(),
taxesAmount: this.getTaxesAmount(),
totalAmount: this.getTotalAmount(),
};
2025-03-18 08:05:00 +00:00
}
2025-09-10 18:14:19 +00:00
private _getTaxesAmount(_taxableAmount: InvoiceAmount): InvoiceAmount {
return this._taxes.getTaxesAmount(_taxableAmount);
}
2025-03-18 08:05:00 +00:00
}