FactuGES_ERP/modules/customer-invoices/src/web/hooks/calcs/use-invoice-auto-recalc.ts

239 lines
7.2 KiB
TypeScript
Raw Normal View History

2025-10-12 18:36:33 +00:00
import { TaxCatalogProvider } from "@erp/core";
2025-10-13 17:41:21 +00:00
import React from "react";
2025-10-11 16:44:38 +00:00
import { UseFormReturn } from "react-hook-form";
2025-10-12 18:36:33 +00:00
import {
InvoiceItemCalcResult,
calculateInvoiceHeaderAmounts,
calculateInvoiceItemAmounts,
} from "../../domain";
2025-10-12 10:43:06 +00:00
import { InvoiceFormData, InvoiceItemFormData } from "../../schemas";
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
export type UseInvoiceAutoRecalcParams = {
currency_code: string;
taxCatalog: TaxCatalogProvider;
};
2025-10-11 16:44:38 +00:00
/**
* Hook que recalcula automáticamente los totales de cada línea
* y los totales generales de la factura cuando cambian los valores relevantes.
2025-10-12 18:36:33 +00:00
* Adaptado a formulario con números planos (no DTOs).
2025-10-11 16:44:38 +00:00
*/
2025-10-12 18:36:33 +00:00
export function useInvoiceAutoRecalc(
form: UseFormReturn<InvoiceFormData>,
params: UseInvoiceAutoRecalcParams
) {
2025-10-11 16:44:38 +00:00
const {
watch,
2025-10-12 18:36:33 +00:00
trigger,
2025-10-11 16:44:38 +00:00
formState: { isDirty, isLoading, isSubmitting },
} = form;
2025-10-12 18:36:33 +00:00
const { currency_code, taxCatalog } = params;
// Cache de los totales de cada línea de factura
const itemCache = React.useRef<Map<number, ReturnType<typeof calculateInvoiceItemAmounts>>>(
new Map()
);
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
// Cálculo de una línea (usa dominio puro)
2025-10-11 16:44:38 +00:00
const calculateItemTotals = React.useCallback(
2025-10-13 17:41:21 +00:00
(item: InvoiceItemFormData, header_discount_percentage: number) => {
2025-10-12 18:36:33 +00:00
const sanitizeString = (v?: number | string) =>
v && !Number.isNaN(Number(v)) ? String(v) : "0";
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
return calculateInvoiceItemAmounts(
{
quantity: sanitizeString(item.quantity),
unit_amount: sanitizeString(item.unit_amount),
discount_percentage: sanitizeString(item.discount_percentage),
2025-10-13 17:41:21 +00:00
header_discount_percentage: sanitizeString(header_discount_percentage),
2025-10-12 18:36:33 +00:00
tax_codes: item.tax_codes,
},
currency_code,
taxCatalog
2025-10-11 16:44:38 +00:00
);
2025-10-12 18:36:33 +00:00
},
[taxCatalog, currency_code]
);
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
// Totales globales (usa funciones del dominio)
2025-10-11 16:44:38 +00:00
const calculateInvoiceTotals = React.useCallback(
2025-10-13 17:41:21 +00:00
(items: InvoiceItemFormData[], header_discount_percentage: number) => {
2025-10-12 18:36:33 +00:00
const lines = items
.filter((i) => !i.is_non_valued)
.map((i) => {
2025-10-13 17:41:21 +00:00
const itemTotals = calculateItemTotals(i, header_discount_percentage);
2025-10-12 18:36:33 +00:00
return {
2025-10-13 17:41:21 +00:00
subtotal_amount: itemTotals.subtotal_amount,
discount_amount: itemTotals.discount_amount,
header_discount_amount: itemTotals.header_discount_amount,
taxable_amount: itemTotals.taxable_amount,
taxes_amount: itemTotals.taxes_amount,
total_amount: itemTotals.total_amount,
taxes_summary: itemTotals.taxes_summary,
2025-10-12 18:36:33 +00:00
};
});
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
return calculateInvoiceHeaderAmounts(lines, currency_code);
2025-10-11 16:44:38 +00:00
},
2025-10-12 18:36:33 +00:00
[calculateItemTotals, currency_code]
2025-10-11 16:44:38 +00:00
);
2025-10-13 17:41:21 +00:00
// Observamos el formulario esperando cualquier cambio
2025-10-11 16:44:38 +00:00
React.useEffect(() => {
2025-10-12 18:36:33 +00:00
if (!isDirty || isLoading || isSubmitting) return;
2025-10-12 19:09:56 +00:00
const subscription = watch((formData, { name, type }) => {
2025-10-12 18:36:33 +00:00
const items = (formData?.items || []) as InvoiceItemFormData[];
2025-10-13 17:41:21 +00:00
const header_discount_percentage = formData?.discount_percentage || 0;
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
if (items.length === 0) return;
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
// Detectar cambios en la cabecera
if (name === "discount_percentage") {
// Recalcular totales de factura
2025-10-13 17:41:21 +00:00
const invoiceTotals = calculateInvoiceTotals(items, header_discount_percentage);
2025-10-12 18:36:33 +00:00
2025-10-13 17:41:21 +00:00
// Estableer valores en cabecera
2025-10-12 18:36:33 +00:00
setInvoiceTotals(form, invoiceTotals);
2025-10-13 17:41:21 +00:00
// Forzar actualización de todas las líneas
items.forEach((item, idx) => {
const newTotals = calculateItemTotals(item, header_discount_percentage);
itemCache.current.set(idx, newTotals);
setInvoiceItemTotals(form, idx, newTotals);
});
2025-10-12 19:09:56 +00:00
trigger([
2025-10-12 18:36:33 +00:00
"subtotal_amount",
"discount_amount",
"taxable_amount",
"taxes_amount",
"total_amount",
]);
2025-10-11 16:44:38 +00:00
}
2025-10-12 18:36:33 +00:00
// 2. Cambio puntual de una línea
2025-10-11 16:44:38 +00:00
if (name?.startsWith("items.") && type === "change") {
const index = Number(name.split(".")[1]);
2025-10-12 18:36:33 +00:00
const field = name.split(".")[2];
2025-10-11 16:44:38 +00:00
2025-10-12 18:36:33 +00:00
if (["quantity", "unit_amount", "discount_percentage", "tax_codes"].includes(field)) {
const item = items[index] as InvoiceItemFormData;
const prevTotals = itemCache.current.get(index);
2025-10-13 17:41:21 +00:00
const newTotals = calculateItemTotals(item, header_discount_percentage);
2025-10-12 19:09:56 +00:00
2025-10-12 18:36:33 +00:00
// Si no hay cambios en los totales, no tocamos nada
const itemHasChanges =
2025-10-13 17:41:21 +00:00
!prevTotals || JSON.stringify(prevTotals) !== JSON.stringify(newTotals);
2025-10-12 18:36:33 +00:00
if (!itemHasChanges) {
return;
2025-10-11 16:44:38 +00:00
}
2025-10-12 18:36:33 +00:00
// El total de esta línea ha cambiado => actualizamos la cache
itemCache.current.set(index, newTotals);
// Actualizar los campos de esta línea
setInvoiceItemTotals(form, index, newTotals);
// Recalcular totales de factura
2025-10-13 17:41:21 +00:00
const invoiceTotals = calculateInvoiceTotals(items, header_discount_percentage);
2025-10-11 16:44:38 +00:00
2025-10-13 17:41:21 +00:00
// Estableer valores en cabecera
2025-10-12 18:36:33 +00:00
setInvoiceTotals(form, invoiceTotals);
2025-10-12 19:09:56 +00:00
trigger([
2025-10-12 18:36:33 +00:00
"items",
"subtotal_amount",
"discount_amount",
"taxable_amount",
"taxes_amount",
"total_amount",
]);
2025-10-11 16:44:38 +00:00
}
}
});
return () => subscription.unsubscribe();
}, [
2025-10-13 17:41:21 +00:00
form,
2025-10-11 16:44:38 +00:00
watch,
2025-10-12 19:09:56 +00:00
trigger,
isDirty,
2025-10-12 18:36:33 +00:00
isLoading,
isSubmitting,
2025-10-11 16:44:38 +00:00
calculateItemTotals,
calculateInvoiceTotals,
]);
}
2025-10-12 18:36:33 +00:00
// Ayudante para rellenar los importes de una línea
function setInvoiceItemTotals(
form: UseFormReturn<InvoiceFormData>,
index: number,
newTotals: InvoiceItemCalcResult
) {
const { setValue } = form;
setValue(`items.${index}.subtotal_amount`, newTotals.subtotal_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue(`items.${index}.discount_amount`, newTotals.discount_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue(`items.${index}.taxable_amount`, newTotals.taxable_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue(`items.${index}.taxes_amount`, newTotals.taxes_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue(`items.${index}.total_amount`, newTotals.total_amount, {
shouldDirty: true,
shouldValidate: false,
});
}
// Ayudante para actualizar los importes de la cabecera
function setInvoiceTotals(
form: UseFormReturn<InvoiceFormData>,
invoiceTotals: ReturnType<typeof calculateInvoiceHeaderAmounts>
) {
const { setValue } = form;
setValue("subtotal_amount", invoiceTotals.subtotal_amount, {
shouldDirty: true,
shouldValidate: false,
});
2025-10-13 17:41:21 +00:00
setValue("discount_amount", invoiceTotals.header_discount_amount, {
2025-10-12 18:36:33 +00:00
shouldDirty: true,
shouldValidate: false,
});
setValue("taxable_amount", invoiceTotals.taxable_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue("taxes_amount", invoiceTotals.taxes_amount, {
shouldDirty: true,
shouldValidate: false,
});
setValue("total_amount", invoiceTotals.total_amount, {
shouldDirty: true,
shouldValidate: false,
});
2025-10-13 17:41:21 +00:00
setValue(
"taxes",
invoiceTotals.taxes_summary.map((tax_item) => ({
tax_code: tax_item.code,
tax_label: tax_item.name,
taxable_amount: tax_item.taxable_amount,
taxes_amount: tax_item.taxes_amount,
}))
);
2025-10-12 18:36:33 +00:00
}