2025-09-03 10:41:12 +00:00
|
|
|
import {
|
|
|
|
|
AggregateRoot,
|
|
|
|
|
CurrencyCode,
|
|
|
|
|
LanguageCode,
|
|
|
|
|
TextValue,
|
|
|
|
|
UniqueID,
|
|
|
|
|
UtcDate,
|
|
|
|
|
} from "@repo/rdx-ddd";
|
|
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
|
|
|
|
import { 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-24 18:38:57 +00:00
|
|
|
export interface CustomerInvoiceProps {
|
2025-09-03 10:41:12 +00:00
|
|
|
companyId: UniqueID;
|
2025-06-11 15:13:44 +00:00
|
|
|
status: CustomerInvoiceStatus;
|
2025-09-03 10:41:12 +00:00
|
|
|
series: Maybe<CustomerInvoiceSerie>;
|
|
|
|
|
invoiceNumber: CustomerInvoiceNumber;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
issueDate: UtcDate;
|
2025-09-03 10:41:12 +00:00
|
|
|
operationDate: Maybe<UtcDate>;
|
|
|
|
|
|
|
|
|
|
notes: Maybe<TextValue>;
|
2025-03-18 08:05:00 +00:00
|
|
|
|
|
|
|
|
//dueDate: UtcDate; // ? --> depende de la forma de pago
|
|
|
|
|
|
|
|
|
|
//tax: Tax; // ? --> detalles?
|
|
|
|
|
|
|
|
|
|
//purchareOrderNumber: string;
|
|
|
|
|
//notes: Note;
|
|
|
|
|
|
|
|
|
|
//senderId: UniqueID;
|
|
|
|
|
|
|
|
|
|
//paymentInstructions: Note;
|
|
|
|
|
//paymentTerms: string;
|
|
|
|
|
|
2025-09-03 10:41:12 +00:00
|
|
|
languageCode: LanguageCode;
|
|
|
|
|
currencyCode: CurrencyCode;
|
|
|
|
|
|
|
|
|
|
//customer?: CustomerInvoiceCustomer;
|
2025-06-11 15:13:44 +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-06-11 15:13:44 +00:00
|
|
|
//protected _status: CustomerInvoiceStatus;
|
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-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-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-03 10:41:12 +00:00
|
|
|
public get issueDate(): UtcDate {
|
|
|
|
|
return this.props.issueDate;
|
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-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 10:41:12 +00:00
|
|
|
// Method to get the complete list of line items
|
|
|
|
|
get lineItems(): CustomerInvoiceItems {
|
2025-03-18 08:05:00 +00:00
|
|
|
return this._items;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|