Compare commits
2 Commits
a4615e8bc4
...
964565a6fe
| Author | SHA1 | Date | |
|---|---|---|---|
| 964565a6fe | |||
| 2ae118d1ff |
@ -60,7 +60,7 @@ export class ProformaFullSnapshotBuilder implements IProformaFullSnapshotBuilder
|
|||||||
payment_method: payment,
|
payment_method: payment,
|
||||||
|
|
||||||
subtotal_amount: allTotals.subtotalAmount.toObjectString(),
|
subtotal_amount: allTotals.subtotalAmount.toObjectString(),
|
||||||
items_discount_amount: allTotals.itemDiscountAmount.toObjectString(),
|
items_discount_amount: allTotals.itemsDiscountAmount.toObjectString(),
|
||||||
|
|
||||||
global_discount_percentage: proforma.globalDiscountPercentage.toObjectString(),
|
global_discount_percentage: proforma.globalDiscountPercentage.toObjectString(),
|
||||||
global_discount_amount: allTotals.globalDiscountAmount.toObjectString(),
|
global_discount_amount: allTotals.globalDiscountAmount.toObjectString(),
|
||||||
|
|||||||
@ -59,7 +59,7 @@ export interface IProformaCreateProps {
|
|||||||
export interface IProformaTotals {
|
export interface IProformaTotals {
|
||||||
subtotalAmount: InvoiceAmount;
|
subtotalAmount: InvoiceAmount;
|
||||||
|
|
||||||
itemDiscountAmount: InvoiceAmount;
|
itemsDiscountAmount: InvoiceAmount;
|
||||||
globalDiscountAmount: InvoiceAmount;
|
globalDiscountAmount: InvoiceAmount;
|
||||||
totalDiscountAmount: InvoiceAmount;
|
totalDiscountAmount: InvoiceAmount;
|
||||||
|
|
||||||
@ -276,7 +276,7 @@ export class Proforma extends AggregateRoot<InternalProformaProps> implements IP
|
|||||||
return {
|
return {
|
||||||
subtotalAmount: this.toInvoiceAmount(itemsTotals.subtotalAmount),
|
subtotalAmount: this.toInvoiceAmount(itemsTotals.subtotalAmount),
|
||||||
|
|
||||||
itemDiscountAmount: this.toInvoiceAmount(itemsTotals.itemDiscountAmount),
|
itemsDiscountAmount: this.toInvoiceAmount(itemsTotals.itemDiscountAmount),
|
||||||
globalDiscountAmount: this.toInvoiceAmount(itemsTotals.globalDiscountAmount),
|
globalDiscountAmount: this.toInvoiceAmount(itemsTotals.globalDiscountAmount),
|
||||||
totalDiscountAmount: this.toInvoiceAmount(itemsTotals.totalDiscountAmount),
|
totalDiscountAmount: this.toInvoiceAmount(itemsTotals.totalDiscountAmount),
|
||||||
|
|
||||||
|
|||||||
@ -170,7 +170,7 @@ export class ProformaItem extends DomainEntity<InternalProformaItemProps> implem
|
|||||||
// Todos a 4 decimales
|
// Todos a 4 decimales
|
||||||
|
|
||||||
public isValued(): boolean {
|
public isValued(): boolean {
|
||||||
return this.props.quantity.isSome() || this.props.unitAmount.isSome();
|
return this.props.quantity.isSome() && this.props.unitAmount.isSome();
|
||||||
}
|
}
|
||||||
|
|
||||||
public subtotalAmount(): ItemAmount {
|
public subtotalAmount(): ItemAmount {
|
||||||
|
|||||||
@ -339,8 +339,8 @@ export class SequelizeProformaDomainMapper extends SequelizeDomainMapper<
|
|||||||
subtotal_amount_value: allAmounts.subtotalAmount.value,
|
subtotal_amount_value: allAmounts.subtotalAmount.value,
|
||||||
subtotal_amount_scale: allAmounts.subtotalAmount.scale,
|
subtotal_amount_scale: allAmounts.subtotalAmount.scale,
|
||||||
|
|
||||||
items_discount_amount_value: allAmounts.itemDiscountAmount.value,
|
items_discount_amount_value: allAmounts.itemsDiscountAmount.value,
|
||||||
items_discount_amount_scale: allAmounts.itemDiscountAmount.scale,
|
items_discount_amount_scale: allAmounts.itemsDiscountAmount.scale,
|
||||||
|
|
||||||
global_discount_percentage_value: source.globalDiscountPercentage.toPrimitive().value,
|
global_discount_percentage_value: source.globalDiscountPercentage.toPrimitive().value,
|
||||||
global_discount_percentage_scale: source.globalDiscountPercentage.toPrimitive().scale,
|
global_discount_percentage_scale: source.globalDiscountPercentage.toPrimitive().scale,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import type { JsonTaxCatalogProvider } from "@erp/core";
|
import type { JsonTaxCatalogProvider } from "@erp/core";
|
||||||
import { DiscountPercentage, Tax } from "@erp/core/api";
|
import { DiscountPercentage, Tax } from "@erp/core/api";
|
||||||
import {
|
import {
|
||||||
type IProformaItemCreateProps,
|
InvoiceAmount,
|
||||||
InvoiceSerie,
|
InvoiceSerie,
|
||||||
ItemAmount,
|
ItemAmount,
|
||||||
ItemDescription,
|
ItemDescription,
|
||||||
@ -39,73 +39,86 @@ import type {
|
|||||||
CreateProformaItemFromFactugesRequestDTO,
|
CreateProformaItemFromFactugesRequestDTO,
|
||||||
} from "../../../common";
|
} from "../../../common";
|
||||||
|
|
||||||
export interface IProformaFromFactuGESProps {
|
export type ProformaCustomerLookup = {
|
||||||
customerLookup: {
|
tin: TINNumber;
|
||||||
tin: TINNumber;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
paymentLookup: {
|
export type ProformaPaymentLookup = {
|
||||||
factuges_id: string;
|
factuges_id: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
customerDraft: {
|
export type ProformaCustomerDraft = {
|
||||||
//reference: Maybe<Name>;
|
isCompany: boolean;
|
||||||
|
name: Name;
|
||||||
|
tin: TINNumber;
|
||||||
|
address: PostalAddressProps;
|
||||||
|
emailPrimary: Maybe<EmailAddress>;
|
||||||
|
emailSecondary: Maybe<EmailAddress>;
|
||||||
|
phonePrimary: Maybe<PhoneNumber>;
|
||||||
|
phoneSecondary: Maybe<PhoneNumber>;
|
||||||
|
mobilePrimary: Maybe<PhoneNumber>;
|
||||||
|
mobileSecondary: Maybe<PhoneNumber>;
|
||||||
|
website: Maybe<URLAddress>;
|
||||||
|
languageCode: LanguageCode;
|
||||||
|
currencyCode: CurrencyCode;
|
||||||
|
};
|
||||||
|
|
||||||
isCompany: boolean;
|
export type ProformaDraftItem = {
|
||||||
name: Name;
|
position: string;
|
||||||
//tradeName: Maybe<Name>;
|
description: Maybe<ItemDescription>;
|
||||||
tin: TINNumber;
|
quantity: Maybe<ItemQuantity>;
|
||||||
|
unitAmount: Maybe<ItemAmount>;
|
||||||
|
subtotalAmount: Maybe<ItemAmount>;
|
||||||
|
itemDiscountPercentage: Maybe<DiscountPercentage>;
|
||||||
|
itemDiscountAmount: Maybe<ItemAmount>;
|
||||||
|
globalDiscountPercentage: DiscountPercentage;
|
||||||
|
globalDiscountAmount: Maybe<ItemAmount>;
|
||||||
|
totalDiscountAmount: Maybe<ItemAmount>;
|
||||||
|
taxableAmount: Maybe<ItemAmount>;
|
||||||
|
taxes: ProformaItemTaxesProps;
|
||||||
|
taxesAmount: Maybe<ItemAmount>;
|
||||||
|
totalAmount: Maybe<ItemAmount>;
|
||||||
|
languageCode: LanguageCode;
|
||||||
|
currencyCode: CurrencyCode;
|
||||||
|
};
|
||||||
|
|
||||||
address: PostalAddressProps;
|
export type ProformaDraft = {
|
||||||
|
series: Maybe<InvoiceSerie>;
|
||||||
|
invoiceDate: UtcDate;
|
||||||
|
operationDate: Maybe<UtcDate>;
|
||||||
|
reference: Maybe<string>;
|
||||||
|
description: Maybe<string>;
|
||||||
|
notes: Maybe<TextValue>;
|
||||||
|
languageCode: LanguageCode;
|
||||||
|
currencyCode: CurrencyCode;
|
||||||
|
subtotalAmount: Maybe<InvoiceAmount>;
|
||||||
|
globalDiscountPercentage: DiscountPercentage;
|
||||||
|
itemsDiscountAmount: Maybe<InvoiceAmount>;
|
||||||
|
taxableAmount: Maybe<InvoiceAmount>;
|
||||||
|
taxes: ProformaItemTaxesProps;
|
||||||
|
taxesAmount: Maybe<InvoiceAmount>;
|
||||||
|
totalAmount: Maybe<InvoiceAmount>;
|
||||||
|
items: ProformaDraftItem[];
|
||||||
|
};
|
||||||
|
|
||||||
emailPrimary: Maybe<EmailAddress>;
|
export type ProformaPaymentDraft = {
|
||||||
emailSecondary: Maybe<EmailAddress>;
|
factuges_id: string;
|
||||||
|
description: string;
|
||||||
|
};
|
||||||
|
|
||||||
phonePrimary: Maybe<PhoneNumber>;
|
export type FactugesProformaPayload = {
|
||||||
phoneSecondary: Maybe<PhoneNumber>;
|
customerLookup: ProformaCustomerLookup;
|
||||||
|
paymentLookup: ProformaPaymentLookup;
|
||||||
mobilePrimary: Maybe<PhoneNumber>;
|
customerDraft: ProformaCustomerDraft;
|
||||||
mobileSecondary: Maybe<PhoneNumber>;
|
proformaDraft: ProformaDraft;
|
||||||
|
paymentDraft: ProformaPaymentDraft;
|
||||||
//fax: Maybe<PhoneNumber>;
|
};
|
||||||
website: Maybe<URLAddress>;
|
|
||||||
|
|
||||||
//legalRecord: Maybe<TextValue>;
|
|
||||||
|
|
||||||
//defaultTaxes: CustomerTaxesProps;
|
|
||||||
|
|
||||||
languageCode: LanguageCode;
|
|
||||||
currencyCode: CurrencyCode;
|
|
||||||
};
|
|
||||||
|
|
||||||
proformaDraft: {
|
|
||||||
series: Maybe<InvoiceSerie>;
|
|
||||||
|
|
||||||
invoiceDate: UtcDate;
|
|
||||||
operationDate: Maybe<UtcDate>;
|
|
||||||
|
|
||||||
reference: Maybe<string>;
|
|
||||||
description: Maybe<string>;
|
|
||||||
notes: Maybe<TextValue>;
|
|
||||||
|
|
||||||
languageCode: LanguageCode;
|
|
||||||
currencyCode: CurrencyCode;
|
|
||||||
|
|
||||||
items: IProformaItemCreateProps[];
|
|
||||||
globalDiscountPercentage: DiscountPercentage;
|
|
||||||
};
|
|
||||||
|
|
||||||
paymentDraft: {
|
|
||||||
factuges_id: string;
|
|
||||||
description: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICreateProformaFromFactugesInputMapper {
|
export interface ICreateProformaFromFactugesInputMapper {
|
||||||
map(
|
map(
|
||||||
dto: CreateProformaFromFactugesRequestDTO,
|
dto: CreateProformaFromFactugesRequestDTO,
|
||||||
params: { companyId: UniqueID }
|
params: { companyId: UniqueID }
|
||||||
): Result<IProformaFromFactuGESProps>;
|
): Result<FactugesProformaPayload>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateProformaFromFactugesInputMapper
|
export class CreateProformaFromFactugesInputMapper
|
||||||
@ -120,7 +133,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
public map(
|
public map(
|
||||||
dto: CreateProformaFromFactugesRequestDTO,
|
dto: CreateProformaFromFactugesRequestDTO,
|
||||||
params: { companyId: UniqueID }
|
params: { companyId: UniqueID }
|
||||||
): Result<IProformaFromFactuGESProps> {
|
): Result<FactugesProformaPayload> {
|
||||||
try {
|
try {
|
||||||
const errors: ValidationErrorDetail[] = [];
|
const errors: ValidationErrorDetail[] = [];
|
||||||
const { companyId } = params;
|
const { companyId } = params;
|
||||||
@ -172,7 +185,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
currencyCode: CurrencyCode;
|
currencyCode: CurrencyCode;
|
||||||
errors: ValidationErrorDetail[];
|
errors: ValidationErrorDetail[];
|
||||||
}
|
}
|
||||||
): IProformaFromFactuGESProps["paymentDraft"] {
|
): ProformaPaymentDraft {
|
||||||
const errors: ValidationErrorDetail[] = [];
|
const errors: ValidationErrorDetail[] = [];
|
||||||
const { companyId } = params;
|
const { companyId } = params;
|
||||||
|
|
||||||
@ -189,7 +202,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
currencyCode: CurrencyCode;
|
currencyCode: CurrencyCode;
|
||||||
errors: ValidationErrorDetail[];
|
errors: ValidationErrorDetail[];
|
||||||
}
|
}
|
||||||
): IProformaFromFactuGESProps["proformaDraft"] {
|
): ProformaDraft {
|
||||||
const errors: ValidationErrorDetail[] = [];
|
const errors: ValidationErrorDetail[] = [];
|
||||||
const { companyId } = params;
|
const { companyId } = params;
|
||||||
|
|
||||||
@ -230,7 +243,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
);
|
);
|
||||||
|
|
||||||
const description = extractOrPushError(
|
const description = extractOrPushError(
|
||||||
maybeFromNullableResult(dto.reference, (value) => Result.ok(String(value))),
|
maybeFromNullableResult(dto.description, (value) => Result.ok(String(value))),
|
||||||
"description",
|
"description",
|
||||||
errors
|
errors
|
||||||
);
|
);
|
||||||
@ -241,12 +254,6 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
errors
|
errors
|
||||||
);
|
);
|
||||||
|
|
||||||
const globalDiscountPercentage = extractOrPushError(
|
|
||||||
DiscountPercentage.create({ value: Number(dto.global_discount_percentage_value) }),
|
|
||||||
"global_discount_percentage_value",
|
|
||||||
params.errors
|
|
||||||
);
|
|
||||||
|
|
||||||
const languageCode = extractOrPushError(
|
const languageCode = extractOrPushError(
|
||||||
LanguageCode.create(dto.customer.language_code),
|
LanguageCode.create(dto.customer.language_code),
|
||||||
"language_code",
|
"language_code",
|
||||||
@ -255,6 +262,59 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
|
|
||||||
const currencyCode = CurrencyCode.fromEUR();
|
const currencyCode = CurrencyCode.fromEUR();
|
||||||
|
|
||||||
|
const subtotalAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(dto.subtotal_amount_value, (value) =>
|
||||||
|
InvoiceAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
"subtotal_amount_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const globalDiscountPercentage = extractOrPushError(
|
||||||
|
DiscountPercentage.create({ value: Number(dto.global_discount_percentage_value) }),
|
||||||
|
"global_discount_percentage_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const itemsDiscountAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(dto.discount_amount_value, (value) =>
|
||||||
|
InvoiceAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
"discount_amount_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const taxableAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(dto.taxable_amount_value, (value) =>
|
||||||
|
InvoiceAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
"taxable_amount_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: Determinar cómo calcular los impuestos de la cabecera de la proforma
|
||||||
|
const taxes: ProformaItemTaxesProps = {
|
||||||
|
iva: Maybe.none(),
|
||||||
|
retention: Maybe.none(),
|
||||||
|
rec: Maybe.none(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const taxesAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(dto.taxes_amount_value, (value) =>
|
||||||
|
InvoiceAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
"taxes_amount_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(dto.total_amount_value, (value) =>
|
||||||
|
InvoiceAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
"total_amount_value",
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
const itemsProps = this.mapItemsProps(dto, {
|
const itemsProps = this.mapItemsProps(dto, {
|
||||||
languageCode: languageCode!,
|
languageCode: languageCode!,
|
||||||
currencyCode: currencyCode,
|
currencyCode: currencyCode,
|
||||||
@ -262,7 +322,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
errors,
|
errors,
|
||||||
});
|
});
|
||||||
|
|
||||||
const props: IProformaFromFactuGESProps["proformaDraft"] = {
|
const props: ProformaDraft = {
|
||||||
//companyId,
|
//companyId,
|
||||||
//status: defaultStatus,
|
//status: defaultStatus,
|
||||||
|
|
||||||
@ -282,9 +342,16 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
languageCode: languageCode!,
|
languageCode: languageCode!,
|
||||||
currencyCode: currencyCode!,
|
currencyCode: currencyCode!,
|
||||||
|
|
||||||
|
subtotalAmount: subtotalAmount!,
|
||||||
globalDiscountPercentage: globalDiscountPercentage!,
|
globalDiscountPercentage: globalDiscountPercentage!,
|
||||||
|
itemsDiscountAmount: itemsDiscountAmount!,
|
||||||
|
|
||||||
items: itemsProps, // ← IProformaItemProps[]
|
taxableAmount: taxableAmount!,
|
||||||
|
taxes: taxes,
|
||||||
|
taxesAmount: taxesAmount!,
|
||||||
|
totalAmount: totalAmount!,
|
||||||
|
|
||||||
|
items: itemsProps,
|
||||||
};
|
};
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
@ -297,7 +364,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
currencyCode: CurrencyCode;
|
currencyCode: CurrencyCode;
|
||||||
errors: ValidationErrorDetail[];
|
errors: ValidationErrorDetail[];
|
||||||
}
|
}
|
||||||
): IProformaFromFactuGESProps["customerDraft"] {
|
): ProformaCustomerDraft {
|
||||||
const { errors, currencyCode } = params;
|
const { errors, currencyCode } = params;
|
||||||
|
|
||||||
const isCompany = dto.is_company === "1";
|
const isCompany = dto.is_company === "1";
|
||||||
@ -397,7 +464,7 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
errors,
|
errors,
|
||||||
});*/
|
});*/
|
||||||
|
|
||||||
const customerProps: IProformaFromFactuGESProps["customerDraft"] = {
|
const customerProps: ProformaCustomerDraft = {
|
||||||
//companyId,
|
//companyId,
|
||||||
//status: status!,
|
//status: status!,
|
||||||
|
|
||||||
@ -440,10 +507,12 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
globalDiscountPercentage: DiscountPercentage;
|
globalDiscountPercentage: DiscountPercentage;
|
||||||
errors: ValidationErrorDetail[];
|
errors: ValidationErrorDetail[];
|
||||||
}
|
}
|
||||||
): IProformaItemCreateProps[] {
|
): ProformaDraftItem[] {
|
||||||
const itemsProps: IProformaItemCreateProps[] = [];
|
const itemsProps: ProformaDraftItem[] = [];
|
||||||
|
|
||||||
dto.items.forEach((item, index) => {
|
dto.items.forEach((item, index) => {
|
||||||
|
const position = String(item.position);
|
||||||
|
|
||||||
const description = extractOrPushError(
|
const description = extractOrPushError(
|
||||||
maybeFromNullableResult(item.description, (value) => ItemDescription.create(value)),
|
maybeFromNullableResult(item.description, (value) => ItemDescription.create(value)),
|
||||||
`items[${index}].description`,
|
`items[${index}].description`,
|
||||||
@ -454,19 +523,27 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
maybeFromNullableResult(item.quantity_value, (value) =>
|
maybeFromNullableResult(item.quantity_value, (value) =>
|
||||||
ItemQuantity.create({ value: Number(value) })
|
ItemQuantity.create({ value: Number(value) })
|
||||||
),
|
),
|
||||||
"items[$index].quantity_value",
|
`items[${index}].quantity_value`,
|
||||||
params.errors
|
params.errors
|
||||||
);
|
);
|
||||||
|
|
||||||
const unitAmount = extractOrPushError(
|
const unitAmount = extractOrPushError(
|
||||||
maybeFromNullableResult(item.unit_value, (value) =>
|
maybeFromNullableResult(item.unit_amount_value, (value) =>
|
||||||
ItemAmount.create({ value: Number(value) })
|
ItemAmount.create({ value: Number(value) })
|
||||||
),
|
),
|
||||||
`items[${index}].unit_value`,
|
`items[${index}].unit_amount_value`,
|
||||||
params.errors
|
params.errors
|
||||||
);
|
);
|
||||||
|
|
||||||
const discountPercentage = extractOrPushError(
|
const subtotalAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.subtotal_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].subtotal_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const itemDiscountPercentage = extractOrPushError(
|
||||||
maybeFromNullableResult(item.item_discount_percentage_value, (value) =>
|
maybeFromNullableResult(item.item_discount_percentage_value, (value) =>
|
||||||
DiscountPercentage.create({ value: Number(value) })
|
DiscountPercentage.create({ value: Number(value) })
|
||||||
),
|
),
|
||||||
@ -474,35 +551,93 @@ export class CreateProformaFromFactugesInputMapper
|
|||||||
params.errors
|
params.errors
|
||||||
);
|
);
|
||||||
|
|
||||||
const taxes = this.mapItempTaxesProps(item, {
|
const itemDiscountAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.item_discount_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].item_discount_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const globalDiscountAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.global_discount_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].global_discount_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalDiscountAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.total_discount_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].total_discount_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const taxableAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.taxable_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].taxable_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const taxesAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.taxes_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].taxes_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const totalAmount = extractOrPushError(
|
||||||
|
maybeFromNullableResult(item.total_amount_value, (value) =>
|
||||||
|
ItemAmount.create({ value: Number(value) })
|
||||||
|
),
|
||||||
|
`items[${index}].total_amount_value`,
|
||||||
|
params.errors
|
||||||
|
);
|
||||||
|
|
||||||
|
const taxes = this.mapItemTaxesProps(item, {
|
||||||
itemIndex: index,
|
itemIndex: index,
|
||||||
errors: params.errors,
|
errors: params.errors,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.throwIfValidationErrors(params.errors);
|
const _item: ProformaDraftItem = {
|
||||||
|
position,
|
||||||
itemsProps.push({
|
|
||||||
description: description!,
|
description: description!,
|
||||||
|
|
||||||
quantity: quantity!,
|
quantity: quantity!,
|
||||||
unitAmount: unitAmount!,
|
unitAmount: unitAmount!,
|
||||||
|
subtotalAmount: subtotalAmount!,
|
||||||
|
|
||||||
itemDiscountPercentage: discountPercentage!,
|
itemDiscountPercentage: itemDiscountPercentage!,
|
||||||
|
itemDiscountAmount: itemDiscountAmount!,
|
||||||
taxes,
|
|
||||||
|
|
||||||
globalDiscountPercentage: params.globalDiscountPercentage,
|
globalDiscountPercentage: params.globalDiscountPercentage,
|
||||||
|
globalDiscountAmount: globalDiscountAmount!,
|
||||||
|
totalDiscountAmount: totalDiscountAmount!,
|
||||||
|
|
||||||
|
taxableAmount: taxableAmount!,
|
||||||
|
taxes,
|
||||||
|
taxesAmount: taxesAmount!,
|
||||||
|
totalAmount: totalAmount!,
|
||||||
|
|
||||||
languageCode: params.languageCode,
|
languageCode: params.languageCode,
|
||||||
currencyCode: params.currencyCode,
|
currencyCode: params.currencyCode,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
itemsProps.push(_item);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.throwIfValidationErrors(params.errors);
|
||||||
|
|
||||||
return itemsProps;
|
return itemsProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Devuelve las propiedades de los impustos de una línea de detalle */
|
/* Devuelve las propiedades de los impustos de una línea de detalle */
|
||||||
|
|
||||||
private mapItempTaxesProps(
|
private mapItemTaxesProps(
|
||||||
itemDTO: CreateProformaItemFromFactugesRequestDTO,
|
itemDTO: CreateProformaItemFromFactugesRequestDTO,
|
||||||
params: { itemIndex: number; errors: ValidationErrorDetail[] }
|
params: { itemIndex: number; errors: ValidationErrorDetail[] }
|
||||||
): ProformaItemTaxesProps {
|
): ProformaItemTaxesProps {
|
||||||
|
|||||||
@ -2,9 +2,12 @@ import type { JsonTaxCatalogProvider } from "@erp/core";
|
|||||||
import { type ITransactionManager, Tax, isEntityNotFoundError } from "@erp/core/api";
|
import { type ITransactionManager, Tax, isEntityNotFoundError } from "@erp/core/api";
|
||||||
import type { ProformaPublicServices } from "@erp/customer-invoices/api";
|
import type { ProformaPublicServices } from "@erp/customer-invoices/api";
|
||||||
import {
|
import {
|
||||||
|
type InvoiceAmount,
|
||||||
InvoicePaymentMethod,
|
InvoicePaymentMethod,
|
||||||
type InvoiceRecipient,
|
type InvoiceRecipient,
|
||||||
InvoiceStatus,
|
InvoiceStatus,
|
||||||
|
type ItemAmount,
|
||||||
|
type Proforma,
|
||||||
} from "@erp/customer-invoices/api/domain";
|
} from "@erp/customer-invoices/api/domain";
|
||||||
import type { CustomerPublicServices } from "@erp/customers/api";
|
import type { CustomerPublicServices } from "@erp/customers/api";
|
||||||
import {
|
import {
|
||||||
@ -13,15 +16,19 @@ import {
|
|||||||
CustomerTaxes,
|
CustomerTaxes,
|
||||||
type ICustomerCreateProps,
|
type ICustomerCreateProps,
|
||||||
} from "@erp/customers/api/domain";
|
} from "@erp/customers/api/domain";
|
||||||
import { type Name, type PhoneNumber, type TextValue, UniqueID } from "@repo/rdx-ddd";
|
import {
|
||||||
|
type Name,
|
||||||
|
type PhoneNumber,
|
||||||
|
type TextValue,
|
||||||
|
UniqueID,
|
||||||
|
ValidationErrorCollection,
|
||||||
|
type ValidationErrorDetail,
|
||||||
|
} from "@repo/rdx-ddd";
|
||||||
import { Maybe, Result } from "@repo/rdx-utils";
|
import { Maybe, Result } from "@repo/rdx-utils";
|
||||||
import type { Transaction } from "sequelize";
|
import type { Transaction } from "sequelize";
|
||||||
|
|
||||||
import type { CreateProformaFromFactugesRequestDTO } from "../../../common";
|
import type { CreateProformaFromFactugesRequestDTO } from "../../../common";
|
||||||
import type {
|
import type { FactugesProformaPayload, ICreateProformaFromFactugesInputMapper } from "../mappers";
|
||||||
ICreateProformaFromFactugesInputMapper,
|
|
||||||
IProformaFromFactuGESProps,
|
|
||||||
} from "../mappers";
|
|
||||||
|
|
||||||
import paymentsCatalog from "./payments.json";
|
import paymentsCatalog from "./payments.json";
|
||||||
|
|
||||||
@ -89,8 +96,8 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
companyId,
|
companyId,
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
if (customerResult.isFailure) {
|
if (paymentResult.isFailure) {
|
||||||
return Result.fail(customerResult.error);
|
return Result.fail(paymentResult.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const payment = paymentResult.data;
|
const payment = paymentResult.data;
|
||||||
@ -125,6 +132,13 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
return Result.fail(createResult.error);
|
return Result.fail(createResult.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valida que los datos de entrada coincidan con el snapshot
|
||||||
|
const proforma = createResult.data;
|
||||||
|
const validationResult = this.validateDraftAgainstProforma(proformaDraft, proforma);
|
||||||
|
if (validationResult.isFailure) {
|
||||||
|
return Result.fail(validationResult.error);
|
||||||
|
}
|
||||||
|
|
||||||
const readResult = await this.proformaServices.getProformaSnapshotById(
|
const readResult = await this.proformaServices.getProformaSnapshotById(
|
||||||
createResult.data.id,
|
createResult.data.id,
|
||||||
{
|
{
|
||||||
@ -151,8 +165,150 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valida que las magnitudes importadas del borrador coincidan con la proforma
|
||||||
|
* generada por el dominio.
|
||||||
|
*
|
||||||
|
* Motivo:
|
||||||
|
* - Detecta divergencias entre el payload legacy y los cálculos reales del dominio.
|
||||||
|
* - Actúa como validación de reconciliación, no como sustituto de las invariantes del agregado.
|
||||||
|
*/
|
||||||
|
private validateDraftAgainstProforma(
|
||||||
|
proformaDraft: FactugesProformaPayload["proformaDraft"],
|
||||||
|
proforma: Proforma
|
||||||
|
): Result<void, Error> {
|
||||||
|
const errors: ValidationErrorDetail[] = [];
|
||||||
|
const proformaTotals = proforma.totals();
|
||||||
|
|
||||||
|
console.log(proformaTotals);
|
||||||
|
|
||||||
|
if (proformaDraft.items.length !== proforma.items.size()) {
|
||||||
|
errors.push({
|
||||||
|
path: "items",
|
||||||
|
message: "La cantidad de ítems de la proforma no coincide con los datos de entrada.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.validateOptionalExpectedAmount({
|
||||||
|
expected: proformaDraft.subtotalAmount,
|
||||||
|
actual: proformaTotals.subtotalAmount,
|
||||||
|
path: "subtotalAmount",
|
||||||
|
message: "El subtotal de la proforma no coincide con los datos de entrada.",
|
||||||
|
errors,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.validateOptionalExpectedAmount({
|
||||||
|
expected: proformaDraft.taxableAmount,
|
||||||
|
actual: proformaTotals.taxableAmount,
|
||||||
|
path: "taxableAmount",
|
||||||
|
message: "La base imponible de la proforma no coincide con los datos de entrada.",
|
||||||
|
errors,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.validateOptionalExpectedAmount({
|
||||||
|
expected: proformaDraft.taxesAmount,
|
||||||
|
actual: proformaTotals.taxesAmount,
|
||||||
|
path: "taxesAmount",
|
||||||
|
message: "La suma de impuestos de la proforma no coincide con los datos de entrada.",
|
||||||
|
errors,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.validateOptionalExpectedAmount({
|
||||||
|
expected: proformaDraft.totalAmount,
|
||||||
|
actual: proformaTotals.totalAmount,
|
||||||
|
path: "totalAmount",
|
||||||
|
message: "El total de la proforma no coincide con los datos de entrada.",
|
||||||
|
errors,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (errors.length > 0) {
|
||||||
|
return Result.fail(
|
||||||
|
new ValidationErrorCollection(
|
||||||
|
"La proforma generada no coincide con las magnitudes validadas del borrador importado.",
|
||||||
|
errors
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
private validateOptionalExpectedAmount(params: {
|
||||||
|
expected: Maybe<InvoiceAmount | ItemAmount>;
|
||||||
|
actual: InvoiceAmount | ItemAmount;
|
||||||
|
path: string;
|
||||||
|
message: string;
|
||||||
|
errors: ValidationErrorDetail[];
|
||||||
|
}): void {
|
||||||
|
const { expected, actual, path, message, errors } = params;
|
||||||
|
|
||||||
|
if (expected.isNone()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const expectedAmount = expected.unwrap();
|
||||||
|
|
||||||
|
if (!actual.equalsTo(expectedAmount)) {
|
||||||
|
errors.push({
|
||||||
|
path,
|
||||||
|
message: this.buildAmountMismatchMessage({
|
||||||
|
baseMessage: message,
|
||||||
|
expected: expectedAmount,
|
||||||
|
actual,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private buildAmountMismatchMessage(params: {
|
||||||
|
baseMessage: string;
|
||||||
|
expected: InvoiceAmount | ItemAmount;
|
||||||
|
actual: InvoiceAmount | ItemAmount;
|
||||||
|
}): string {
|
||||||
|
const { baseMessage, expected, actual } = params;
|
||||||
|
|
||||||
|
return `${baseMessage} Esperado: ${expected.formattedValue}. Actual: ${actual.formattedValue}.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valida un importe opcional esperado contra un importe real también opcional.
|
||||||
|
*
|
||||||
|
* Motivo:
|
||||||
|
* - Algunos campos pueden faltar tanto en el payload importado como en
|
||||||
|
* la proyección o snapshot generado.
|
||||||
|
* - Si el esperado existe pero el real no, se considera discrepancia.
|
||||||
|
*/
|
||||||
|
private validateOptionalMaybeAmount(params: {
|
||||||
|
expected: Maybe<InvoiceAmount | ItemAmount>;
|
||||||
|
actual: Maybe<InvoiceAmount | ItemAmount>;
|
||||||
|
path: string;
|
||||||
|
message: string;
|
||||||
|
errors: ValidationErrorDetail[];
|
||||||
|
}): void {
|
||||||
|
const { expected, actual, path, message, errors } = params;
|
||||||
|
|
||||||
|
if (expected.isNone()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual.isNone()) {
|
||||||
|
errors.push({
|
||||||
|
path,
|
||||||
|
message,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!actual.unwrap().equals(expected.unwrap())) {
|
||||||
|
errors.push({
|
||||||
|
path,
|
||||||
|
message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private buildProformaCreateProps(deps: {
|
private buildProformaCreateProps(deps: {
|
||||||
proformaDraft: IProformaFromFactuGESProps["proformaDraft"];
|
proformaDraft: FactugesProformaPayload["proformaDraft"];
|
||||||
customerId: UniqueID;
|
customerId: UniqueID;
|
||||||
payment: FakePaymentMethod;
|
payment: FakePaymentMethod;
|
||||||
context: {
|
context: {
|
||||||
@ -193,8 +349,8 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
* @returns `Result` con el cliente resuelto o el error producido.
|
* @returns `Result` con el cliente resuelto o el error producido.
|
||||||
*/
|
*/
|
||||||
private async resolveCustomer(
|
private async resolveCustomer(
|
||||||
customerLookup: IProformaFromFactuGESProps["customerLookup"],
|
customerLookup: FactugesProformaPayload["customerLookup"],
|
||||||
customerDraft: IProformaFromFactuGESProps["customerDraft"],
|
customerDraft: FactugesProformaPayload["customerDraft"],
|
||||||
context: {
|
context: {
|
||||||
companyId: UniqueID;
|
companyId: UniqueID;
|
||||||
transaction: Transaction;
|
transaction: Transaction;
|
||||||
@ -227,8 +383,8 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async resolvePayment(
|
private async resolvePayment(
|
||||||
paymentLookup: IProformaFromFactuGESProps["paymentLookup"],
|
paymentLookup: FactugesProformaPayload["paymentLookup"],
|
||||||
paymentDraft: IProformaFromFactuGESProps["paymentDraft"],
|
paymentDraft: FactugesProformaPayload["paymentDraft"],
|
||||||
context: {
|
context: {
|
||||||
companyId: UniqueID;
|
companyId: UniqueID;
|
||||||
transaction: Transaction;
|
transaction: Transaction;
|
||||||
@ -254,7 +410,7 @@ export class CreateProformaFromFactugesUseCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private buildCustomerCreateProps(
|
private buildCustomerCreateProps(
|
||||||
customerDraft: IProformaFromFactuGESProps["customerDraft"],
|
customerDraft: FactugesProformaPayload["customerDraft"],
|
||||||
context: {
|
context: {
|
||||||
companyId: UniqueID;
|
companyId: UniqueID;
|
||||||
transaction: Transaction;
|
transaction: Transaction;
|
||||||
|
|||||||
@ -5,9 +5,9 @@ export const CreateProformaItemFromFactugesRequestSchema = z.object({
|
|||||||
position: z.string(),
|
position: z.string(),
|
||||||
description: z.string().default(""),
|
description: z.string().default(""),
|
||||||
quantity_value: NumericStringSchema.default(""), // Ya viene escalado
|
quantity_value: NumericStringSchema.default(""), // Ya viene escalado
|
||||||
unit_value: NumericStringSchema.default(""),
|
unit_amount_value: NumericStringSchema.default(""),
|
||||||
|
|
||||||
subtotal_amuount_value: NumericStringSchema.default(""),
|
subtotal_amount_value: NumericStringSchema.default(""),
|
||||||
item_discount_percentage_value: NumericStringSchema.default(""),
|
item_discount_percentage_value: NumericStringSchema.default(""),
|
||||||
item_discount_amount_value: NumericStringSchema.default(""),
|
item_discount_amount_value: NumericStringSchema.default(""),
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ export const CreateProformaItemFromFactugesRequestSchema = z.object({
|
|||||||
total_discount_amount_value: NumericStringSchema.default(""),
|
total_discount_amount_value: NumericStringSchema.default(""),
|
||||||
taxable_amount_value: NumericStringSchema.default(""),
|
taxable_amount_value: NumericStringSchema.default(""),
|
||||||
|
|
||||||
total_value: NumericStringSchema.default(""),
|
total_amount_value: NumericStringSchema.default(""),
|
||||||
|
|
||||||
iva_code: z.string().default(""),
|
iva_code: z.string().default(""),
|
||||||
iva_percentage_value: NumericStringSchema.default(""),
|
iva_percentage_value: NumericStringSchema.default(""),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user