.
This commit is contained in:
parent
07e71e70a5
commit
ff036a7d81
@ -11,6 +11,7 @@ import { InvoiceFormData, InvoiceItemFormData } from "../../schemas";
|
|||||||
export type UseInvoiceAutoRecalcParams = {
|
export type UseInvoiceAutoRecalcParams = {
|
||||||
currency_code: string;
|
currency_code: string;
|
||||||
taxCatalog: TaxCatalogProvider;
|
taxCatalog: TaxCatalogProvider;
|
||||||
|
debug?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,7 +22,7 @@ export type UseInvoiceAutoRecalcParams = {
|
|||||||
*/
|
*/
|
||||||
export function useInvoiceAutoRecalc(
|
export function useInvoiceAutoRecalc(
|
||||||
form: UseFormReturn<InvoiceFormData>,
|
form: UseFormReturn<InvoiceFormData>,
|
||||||
{ currency_code, taxCatalog }: UseInvoiceAutoRecalcParams
|
{ currency_code, taxCatalog, debug = false }: UseInvoiceAutoRecalcParams
|
||||||
) {
|
) {
|
||||||
const { setValue, trigger, control } = form;
|
const { setValue, trigger, control } = form;
|
||||||
|
|
||||||
@ -99,12 +100,14 @@ export function useInvoiceAutoRecalc(
|
|||||||
shouldUpdateHeader = true;
|
shouldUpdateHeader = true;
|
||||||
itemCache.current.set(idx, next);
|
itemCache.current.set(idx, next);
|
||||||
setInvoiceItemTotals(form, idx, next);
|
setInvoiceItemTotals(form, idx, next);
|
||||||
|
if (debug) console.log(`💡 Recalc line ${idx + 1}`, next);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (shouldUpdateHeader) {
|
if (shouldUpdateHeader) {
|
||||||
const totals = calculateInvoiceTotals(deferredItems, deferredDiscount);
|
const totals = calculateInvoiceTotals(deferredItems, deferredDiscount);
|
||||||
setInvoiceTotals(form, totals);
|
setInvoiceTotals(form, totals);
|
||||||
|
if (debug) console.log("📊 Recalc invoice totals", totals);
|
||||||
|
|
||||||
void trigger([
|
void trigger([
|
||||||
"subtotal_amount",
|
"subtotal_amount",
|
||||||
@ -119,7 +122,15 @@ export function useInvoiceAutoRecalc(
|
|||||||
return () => {
|
return () => {
|
||||||
if (debounceTimer.current) clearTimeout(debounceTimer.current);
|
if (debounceTimer.current) clearTimeout(debounceTimer.current);
|
||||||
};
|
};
|
||||||
}, [deferredItems, deferredDiscount, calculateItemTotals, calculateInvoiceTotals, form, trigger]);
|
}, [
|
||||||
|
deferredItems,
|
||||||
|
deferredDiscount,
|
||||||
|
calculateItemTotals,
|
||||||
|
calculateInvoiceTotals,
|
||||||
|
form,
|
||||||
|
trigger,
|
||||||
|
debug,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ayudante para rellenar los importes de una línea
|
// Ayudante para rellenar los importes de una línea
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user