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

240 lines
5.6 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
MoneyValue,
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 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-04 17:57:04 +00:00
//subtotalAmount: MoneyValue;
2025-09-03 18:04:09 +00:00
discountPercentage: Percentage;
//discountAmount: MoneyValue;
2025-09-09 18:13:54 +00:00
taxes: InvoiceTaxes;
2025-09-03 18:04:09 +00:00
2025-09-04 17:57:04 +00:00
//totalAmount: MoneyValue;
2025-09-03 18:04:09 +00:00
2025-09-04 17:57:04 +00:00
items: CustomerInvoiceItems;
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-03 10:41:12 +00:00
export class CustomerInvoice extends AggregateRoot<CustomerInvoiceProps> {
private _items!: CustomerInvoiceItems;
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-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 subtotalAmount(): MoneyValue {
2025-09-04 17:57:04 +00:00
throw new Error("discountAmount not implemented");
2025-09-03 18:04:09 +00:00
}
public get discountPercentage(): Percentage {
return this.props.discountPercentage;
}
public get discountAmount(): MoneyValue {
throw new Error("discountAmount not implemented");
}
public get taxableAmount(): MoneyValue {
throw new Error("taxableAmount not implemented");
}
public get taxAmount(): MoneyValue {
2025-09-04 17:57:04 +00:00
throw new Error("discountAmount not implemented");
2025-09-03 18:04:09 +00:00
}
public get totalAmount(): MoneyValue {
throw new Error("totalAmount not implemented");
}
2025-09-03 10:41:12 +00:00
// Method to get the complete list of line items
2025-09-03 18:04:09 +00:00
get items(): CustomerInvoiceItems {
2025-03-18 08:05:00 +00:00
return this._items;
}
2025-09-09 15:48:12 +00:00
get hasRecipient() {
return this.recipient.isSome();
2025-09-08 17:24:38 +00:00
}
2025-09-03 10:41:12 +00:00
/*get senderId(): UniqueID {
return this.props.senderId;
}*/
/* get customer(): CustomerInvoiceCustomer | undefined {
return this.props.customer;
}*/
2025-03-18 08:05:00 +00:00
/*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-09-03 10:41:12 +00:00
/*
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);
}
}*/
2025-09-03 10:41:12 +00:00
/*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-26 11:32:55 +00:00
currency_code: this.props.currency,
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-09-03 10:41:12 +00:00
}*/
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-09-03 10:41:12 +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-26 11:32:55 +00:00
currency_code: this.props.currency,
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-09-03 10:41:12 +00:00
}*/
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-09-03 10:41:12 +00:00
/*calculateTotal(): MoneyValue {
2025-04-01 14:26:15 +00:00
return this.calculateSubtotal().add(this.calculateTaxTotal());
2025-09-03 10:41:12 +00:00
}*/
2025-03-18 08:05:00 +00:00
}