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

204 lines
4.8 KiB
TypeScript
Raw Normal View History

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-06-11 15:13:44 +00:00
import { CustomerInvoiceCustomer, CustomerInvoiceItem, CustomerInvoiceItems } from "../entities";
import { CustomerInvoiceNumber, CustomerInvoiceSerie, CustomerInvoiceStatus } from "../value-objects";
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
export interface ICustomerInvoiceProps {
customerCustomerInvoiceNumber: CustomerInvoiceNumber;
customerCustomerInvoiceSeries: CustomerInvoiceSerie;
2025-04-01 14:26:15 +00:00
2025-06-11 15:13:44 +00:00
status: CustomerInvoiceStatus;
2025-03-18 08:05:00 +00:00
issueDate: UtcDate;
operationDate: UtcDate;
//dueDate: UtcDate; // ? --> depende de la forma de pago
//tax: Tax; // ? --> detalles?
2025-06-11 15:13:44 +00:00
customerCustomerInvoiceCurrency: 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-06-11 15:13:44 +00:00
customer?: CustomerInvoiceCustomer;
items?: CustomerInvoiceItems;
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
export interface ICustomerInvoice {
2025-03-18 08:05:00 +00:00
id: UniqueID;
2025-06-11 15:13:44 +00:00
customerCustomerInvoiceNumber: CustomerInvoiceNumber;
customerCustomerInvoiceSeries: CustomerInvoiceSerie;
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
status: CustomerInvoiceStatus;
2025-03-18 08:05:00 +00:00
issueDate: UtcDate;
operationDate: UtcDate;
//senderId: UniqueID;
2025-06-11 15:13:44 +00:00
customer?: CustomerInvoiceCustomer;
2025-03-18 08:05:00 +00:00
//dueDate
//tax: Tax;
2025-04-01 14:26:15 +00:00
//language: Language;
2025-06-11 15:13:44 +00:00
customerCustomerInvoiceCurrency: string;
2025-03-18 08:05:00 +00:00
//purchareOrderNumber: string;
//notes: Note;
//paymentInstructions: Note;
//paymentTerms: string;
2025-06-11 15:13:44 +00:00
items: CustomerInvoiceItems;
2025-03-18 08:05:00 +00:00
calculateSubtotal: () => MoneyValue;
calculateTaxTotal: () => MoneyValue;
calculateTotal: () => MoneyValue;
}
2025-06-11 15:13:44 +00:00
export class CustomerInvoice extends AggregateRoot<ICustomerInvoiceProps> implements ICustomerInvoice {
private _items!: Collection<CustomerInvoiceItem>;
//protected _status: CustomerInvoiceStatus;
2025-04-01 14:26:15 +00:00
2025-06-11 15:13:44 +00:00
protected constructor(props: ICustomerInvoiceProps, id?: UniqueID) {
2025-04-01 14:26:15 +00:00
super(props, id);
2025-06-11 15:13:44 +00:00
this._items = props.items || CustomerInvoiceItems.create();
2025-04-01 14:26:15 +00:00
}
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
static create(props: ICustomerInvoiceProps, id?: UniqueID): Result<CustomerInvoice, Error> {
const customerCustomerInvoice = new CustomerInvoice(props, id);
2025-03-18 08:05:00 +00:00
// Reglas de negocio / validaciones
// ...
// ...
2025-06-11 15:13:44 +00:00
// 🔹 Disparar evento de dominio "CustomerInvoiceAuthenticatedEvent"
//const { customerCustomerInvoice } = props;
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerCustomerInvoice.toString()));
2025-03-18 08:05:00 +00:00
2025-06-11 15:13:44 +00:00
return Result.ok(customerCustomerInvoice);
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
get customerCustomerInvoiceNumber() {
return this.props.customerCustomerInvoiceNumber;
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
get customerCustomerInvoiceSeries() {
return this.props.customerCustomerInvoiceSeries;
2025-03-18 08:05:00 +00:00
}
get issueDate() {
return this.props.issueDate;
}
/*get senderId(): UniqueID {
return this.props.senderId;
}*/
2025-06-11 15:13:44 +00:00
get customer(): CustomerInvoiceCustomer | undefined {
2025-04-01 14:26:15 +00:00
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-06-11 15:13:44 +00:00
get customerCustomerInvoiceCurrency() {
return this.props.customerCustomerInvoiceCurrency;
2025-03-18 08:05:00 +00:00
}
/*get notes() {
return this.props.notes;
}*/
// Method to get the complete list of line items
2025-06-11 15:13:44 +00:00
/*get lineItems(): CustomerInvoiceLineItem[] {
2025-03-18 08:05:00 +00:00
return this._lineItems;
}
2025-06-11 15:13:44 +00:00
addLineItem(lineItem: CustomerInvoiceLineItem, position?: number): void {
2025-03-18 08:05:00 +00:00
if (position === undefined) {
this._lineItems.push(lineItem);
} else {
this._lineItems.splice(position, 0, lineItem);
}
}*/
calculateSubtotal(): MoneyValue {
2025-06-11 15:13:44 +00:00
const customerCustomerInvoiceSubtotal = MoneyValue.create({
2025-04-01 14:26:15 +00:00
amount: 0,
2025-06-11 15:13:44 +00:00
currency_code: this.props.customerCustomerInvoiceCurrency,
2025-04-01 14:26:15 +00:00
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());
2025-06-11 15:13:44 +00:00
}, customerCustomerInvoiceSubtotal);
2025-03-18 08:05:00 +00:00
}
2025-06-11 15:13:44 +00:00
// Method to calculate the total tax in the customerCustomerInvoice
2025-03-18 08:05:00 +00:00
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-06-11 15:13:44 +00:00
currency_code: this.props.customerCustomerInvoiceCurrency,
2025-04-01 14:26:15 +00:00
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
}
2025-06-11 15:13:44 +00:00
// Method to calculate the total customerCustomerInvoice amount, including taxes
2025-03-18 08:05:00 +00:00
calculateTotal(): MoneyValue {
2025-04-01 14:26:15 +00:00
return this.calculateSubtotal().add(this.calculateTaxTotal());
2025-03-18 08:05:00 +00:00
}
}