Uecko_ERP/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts
2025-06-26 13:32:55 +02:00

210 lines
4.5 KiB
TypeScript

import { AggregateRoot, MoneyValue, UniqueID, UtcDate } from "@repo/rdx-ddd";
import { Collection, Result } from "@repo/rdx-utils";
import { CustomerInvoiceCustomer, CustomerInvoiceItem, CustomerInvoiceItems } from "../entities";
import {
CustomerInvoiceNumber,
CustomerInvoiceSerie,
CustomerInvoiceStatus,
} from "../value-objects";
export interface CustomerInvoiceProps {
invoiceNumber: CustomerInvoiceNumber;
invoiceSeries: CustomerInvoiceSerie;
status: CustomerInvoiceStatus;
issueDate: UtcDate;
operationDate: UtcDate;
//dueDate: UtcDate; // ? --> depende de la forma de pago
//tax: Tax; // ? --> detalles?
currency: string;
//language: Language;
//purchareOrderNumber: string;
//notes: Note;
//senderId: UniqueID;
//paymentInstructions: Note;
//paymentTerms: string;
customer?: CustomerInvoiceCustomer;
items?: CustomerInvoiceItems;
}
export interface ICustomerInvoice {
id: UniqueID;
invoiceNumber: CustomerInvoiceNumber;
invoiceSeries: CustomerInvoiceSerie;
status: CustomerInvoiceStatus;
issueDate: UtcDate;
operationDate: UtcDate;
//senderId: UniqueID;
customer?: CustomerInvoiceCustomer;
//dueDate
//tax: Tax;
//language: Language;
currency: string;
//purchareOrderNumber: string;
//notes: Note;
//paymentInstructions: Note;
//paymentTerms: string;
items: CustomerInvoiceItems;
calculateSubtotal: () => MoneyValue;
calculateTaxTotal: () => MoneyValue;
calculateTotal: () => MoneyValue;
}
export class CustomerInvoice
extends AggregateRoot<CustomerInvoiceProps>
implements ICustomerInvoice
{
private _items!: Collection<CustomerInvoiceItem>;
//protected _status: CustomerInvoiceStatus;
protected constructor(props: CustomerInvoiceProps, id?: UniqueID) {
super(props, id);
this._items = props.items || CustomerInvoiceItems.create();
}
static create(props: CustomerInvoiceProps, id?: UniqueID): Result<CustomerInvoice, Error> {
const customerInvoice = new CustomerInvoice(props, id);
// Reglas de negocio / validaciones
// ...
// ...
// 🔹 Disparar evento de dominio "CustomerInvoiceAuthenticatedEvent"
//const { customerInvoice } = props;
//user.addDomainEvent(new CustomerInvoiceAuthenticatedEvent(id, customerInvoice.toString()));
return Result.ok(customerInvoice);
}
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;
}*/
get customer(): CustomerInvoiceCustomer | undefined {
return this.props.customer;
}
get operationDate() {
return this.props.operationDate;
}
/*get language() {
return this.props.language;
}*/
get dueDate() {
return undefined;
}
get tax() {
return undefined;
}
get status() {
return this.props.status;
}
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;
}*/
get currency() {
return this.props.currency;
}
/*get notes() {
return this.props.notes;
}*/
// Method to get the complete list of line items
/*get lineItems(): CustomerInvoiceLineItem[] {
return this._lineItems;
}
addLineItem(lineItem: CustomerInvoiceLineItem, position?: number): void {
if (position === undefined) {
this._lineItems.push(lineItem);
} else {
this._lineItems.splice(position, 0, lineItem);
}
}*/
calculateSubtotal(): MoneyValue {
const customerInvoiceSubtotal = MoneyValue.create({
amount: 0,
currency_code: this.props.currency,
scale: 2,
}).data;
return this._items.getAll().reduce((subtotal, item) => {
return subtotal.add(item.calculateTotal());
}, customerInvoiceSubtotal);
}
// Method to calculate the total tax in the customerInvoice
calculateTaxTotal(): MoneyValue {
const taxTotal = MoneyValue.create({
amount: 0,
currency_code: this.props.currency,
scale: 2,
}).data;
return taxTotal;
}
// Method to calculate the total customerInvoice amount, including taxes
calculateTotal(): MoneyValue {
return this.calculateSubtotal().add(this.calculateTaxTotal());
}
}