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

211 lines
4.6 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";
2025-06-12 06:55:17 +00:00
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 {
2025-06-12 06:55:17 +00:00
customerInvoiceNumber: CustomerInvoiceNumber;
customerInvoiceSeries: 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-12 06:55:17 +00:00
customerInvoiceCurrency: 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-12 06:55:17 +00:00
customerInvoiceNumber: CustomerInvoiceNumber;
customerInvoiceSeries: 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-12 06:55:17 +00:00
customerInvoiceCurrency: 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-12 06:55:17 +00:00
export class CustomerInvoice
extends AggregateRoot<ICustomerInvoiceProps>
implements ICustomerInvoice
{
2025-06-11 15:13:44 +00:00
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> {
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-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-06-12 06:55:17 +00:00
get customerInvoiceNumber() {
return this.props.customerInvoiceNumber;
2025-03-18 08:05:00 +00:00
}
2025-06-12 06:55:17 +00:00
get customerInvoiceSeries() {
return this.props.customerInvoiceSeries;
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-12 06:55:17 +00:00
get customerInvoiceCurrency() {
return this.props.customerInvoiceCurrency;
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-12 06:55:17 +00:00
const customerInvoiceSubtotal = MoneyValue.create({
2025-04-01 14:26:15 +00:00
amount: 0,
2025-06-12 06:55:17 +00:00
currency_code: this.props.customerInvoiceCurrency,
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-12 06:55:17 +00:00
}, customerInvoiceSubtotal);
2025-03-18 08:05:00 +00:00
}
2025-06-12 06:55:17 +00:00
// Method to calculate the total tax in the customerInvoice
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-12 06:55:17 +00:00
currency_code: this.props.customerInvoiceCurrency,
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-12 06:55:17 +00:00
// Method to calculate the total customerInvoice 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
}
}