2025-05-20 10:08:24 +00:00
|
|
|
import { AggregateRoot, MoneyValue, UniqueID, UtcDate } from "@repo/rdx-ddd";
|
2025-05-09 10:45:32 +00:00
|
|
|
import { Collection, Result } from "@repo/rdx-utils";
|
2025-04-01 14:26:15 +00:00
|
|
|
import { InvoiceCustomer, InvoiceItem, InvoiceItems } from "../entities";
|
|
|
|
|
import { InvoiceNumber, InvoiceSerie, InvoiceStatus } from "../value-objects";
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
export interface IInvoiceProps {
|
|
|
|
|
invoiceNumber: InvoiceNumber;
|
2025-04-01 14:26:15 +00:00
|
|
|
invoiceSeries: InvoiceSerie;
|
|
|
|
|
|
|
|
|
|
status: InvoiceStatus;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
issueDate: UtcDate;
|
|
|
|
|
operationDate: UtcDate;
|
|
|
|
|
|
|
|
|
|
//dueDate: UtcDate; // ? --> depende de la forma de pago
|
|
|
|
|
|
|
|
|
|
//tax: Tax; // ? --> detalles?
|
2025-04-01 14:26:15 +00:00
|
|
|
invoiceCurrency: string;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
//language: Language;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
//purchareOrderNumber: string;
|
|
|
|
|
//notes: Note;
|
|
|
|
|
|
|
|
|
|
//senderId: UniqueID;
|
|
|
|
|
|
|
|
|
|
//paymentInstructions: Note;
|
|
|
|
|
//paymentTerms: string;
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
customer?: InvoiceCustomer;
|
2025-04-22 08:11:50 +00:00
|
|
|
items?: InvoiceItems;
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IInvoice {
|
|
|
|
|
id: UniqueID;
|
|
|
|
|
invoiceNumber: InvoiceNumber;
|
2025-04-01 14:26:15 +00:00
|
|
|
invoiceSeries: InvoiceSerie;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
status: InvoiceStatus;
|
|
|
|
|
|
|
|
|
|
issueDate: UtcDate;
|
|
|
|
|
operationDate: UtcDate;
|
|
|
|
|
|
|
|
|
|
//senderId: UniqueID;
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
customer?: InvoiceCustomer;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
//dueDate
|
|
|
|
|
|
|
|
|
|
//tax: Tax;
|
2025-04-01 14:26:15 +00:00
|
|
|
//language: Language;
|
|
|
|
|
invoiceCurrency: string;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
//purchareOrderNumber: string;
|
|
|
|
|
//notes: Note;
|
|
|
|
|
|
|
|
|
|
//paymentInstructions: Note;
|
|
|
|
|
//paymentTerms: string;
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
items: InvoiceItems;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
calculateSubtotal: () => MoneyValue;
|
|
|
|
|
calculateTaxTotal: () => MoneyValue;
|
|
|
|
|
calculateTotal: () => MoneyValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Invoice extends AggregateRoot<IInvoiceProps> implements IInvoice {
|
2025-04-01 14:26:15 +00:00
|
|
|
private _items!: Collection<InvoiceItem>;
|
|
|
|
|
//protected _status: InvoiceStatus;
|
|
|
|
|
|
|
|
|
|
protected constructor(props: IInvoiceProps, id?: UniqueID) {
|
|
|
|
|
super(props, id);
|
|
|
|
|
|
|
|
|
|
this._items = props.items || InvoiceItems.create();
|
|
|
|
|
}
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
static create(props: IInvoiceProps, id?: UniqueID): Result<Invoice, Error> {
|
|
|
|
|
const invoice = new Invoice(props, id);
|
|
|
|
|
|
|
|
|
|
// Reglas de negocio / validaciones
|
|
|
|
|
// ...
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
// 🔹 Disparar evento de dominio "InvoiceAuthenticatedEvent"
|
|
|
|
|
//const { invoice } = props;
|
|
|
|
|
//user.addDomainEvent(new InvoiceAuthenticatedEvent(id, invoice.toString()));
|
|
|
|
|
|
|
|
|
|
return Result.ok(invoice);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get invoiceNumber() {
|
|
|
|
|
return this.props.invoiceNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get invoiceSeries() {
|
|
|
|
|
return this.props.invoiceSeries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get issueDate() {
|
|
|
|
|
return this.props.issueDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*get senderId(): UniqueID {
|
|
|
|
|
return this.props.senderId;
|
|
|
|
|
}*/
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
get customer(): InvoiceCustomer | undefined {
|
|
|
|
|
return this.props.customer;
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get operationDate() {
|
|
|
|
|
return this.props.operationDate;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
/*get language() {
|
2025-03-18 08:05:00 +00:00
|
|
|
return this.props.language;
|
2025-04-01 14:26:15 +00:00
|
|
|
}*/
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
get dueDate() {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get tax() {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get status() {
|
2025-04-01 14:26:15 +00:00
|
|
|
return this.props.status;
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get items() {
|
|
|
|
|
return this._items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*get purchareOrderNumber() {
|
|
|
|
|
return this.props.purchareOrderNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get paymentInstructions() {
|
|
|
|
|
return this.props.paymentInstructions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get paymentTerms() {
|
|
|
|
|
return this.props.paymentTerms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get billTo() {
|
|
|
|
|
return this.props.billTo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get shipTo() {
|
|
|
|
|
return this.props.shipTo;
|
|
|
|
|
}*/
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
get invoiceCurrency() {
|
2025-03-18 08:05:00 +00:00
|
|
|
return this.props.invoiceCurrency;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*get notes() {
|
|
|
|
|
return this.props.notes;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
// Method to get the complete list of line items
|
|
|
|
|
/*get lineItems(): InvoiceLineItem[] {
|
|
|
|
|
return this._lineItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addLineItem(lineItem: InvoiceLineItem, position?: number): void {
|
|
|
|
|
if (position === undefined) {
|
|
|
|
|
this._lineItems.push(lineItem);
|
|
|
|
|
} else {
|
|
|
|
|
this._lineItems.splice(position, 0, lineItem);
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
calculateSubtotal(): MoneyValue {
|
2025-04-01 14:26:15 +00:00
|
|
|
const invoiceSubtotal = MoneyValue.create({
|
|
|
|
|
amount: 0,
|
|
|
|
|
currency_code: this.props.invoiceCurrency,
|
|
|
|
|
scale: 2,
|
|
|
|
|
}).data;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
return this._items.getAll().reduce((subtotal, item) => {
|
|
|
|
|
return subtotal.add(item.calculateTotal());
|
|
|
|
|
}, invoiceSubtotal);
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to calculate the total tax in the invoice
|
|
|
|
|
calculateTaxTotal(): MoneyValue {
|
2025-04-01 14:26:15 +00:00
|
|
|
const taxTotal = MoneyValue.create({
|
2025-03-18 08:05:00 +00:00
|
|
|
amount: 0,
|
2025-04-01 14:26:15 +00:00
|
|
|
currency_code: this.props.invoiceCurrency,
|
|
|
|
|
scale: 2,
|
|
|
|
|
}).data;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
return taxTotal;
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to calculate the total invoice amount, including taxes
|
|
|
|
|
calculateTotal(): MoneyValue {
|
2025-04-01 14:26:15 +00:00
|
|
|
return this.calculateSubtotal().add(this.calculateTaxTotal());
|
2025-03-18 08:05:00 +00:00
|
|
|
}
|
|
|
|
|
}
|