fix: wrong invoice due amount

This commit is contained in:
Ahmed Bouhuolia
2024-08-07 18:52:36 +02:00
parent 8cab012324
commit 3d200f4d7d
3 changed files with 88 additions and 71 deletions

View File

@@ -269,59 +269,6 @@ export const useInvoiceSubtotal = () => {
return React.useMemo(() => getEntriesTotal(entries), [entries]);
};
/**
* Retreives the invoice totals.
*/
export const useInvoiceTotals = () => {
const {
values: { entries, currency_code: currencyCode },
} = useFormikContext();
// Retrieves the invoice entries total.
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
const total_ = useInvoiceTotal();
// Retrieves the formatted total money.
const formattedTotal = React.useMemo(
() => formattedAmount(total_, currencyCode),
[total_, currencyCode],
);
// Retrieves the formatted subtotal.
const formattedSubtotal = React.useMemo(
() => formattedAmount(total, currencyCode, { money: false }),
[total, currencyCode],
);
// Retrieves the payment total.
const paymentTotal = React.useMemo(() => 0, []);
// Retireves the formatted payment total.
const formattedPaymentTotal = React.useMemo(
() => formattedAmount(paymentTotal, currencyCode),
[paymentTotal, currencyCode],
);
// Retrieves the formatted due total.
const dueTotal = React.useMemo(
() => total_ - paymentTotal,
[total_, paymentTotal],
);
// Retrieves the formatted due total.
const formattedDueTotal = React.useMemo(
() => formattedAmount(dueTotal, currencyCode),
[dueTotal, currencyCode],
);
return {
total,
paymentTotal,
dueTotal,
formattedTotal,
formattedSubtotal,
formattedPaymentTotal,
formattedDueTotal,
};
};
/**
* Detarmines whether the invoice has foreign customer.
* @returns {boolean}
@@ -409,14 +356,25 @@ export const useInvoiceTotal = () => {
);
};
/**
* Retrieves the paid amount of the invoice.
* @returns {number}
*/
export const useInvoicePaidAmount = () => {
const { invoice } = useInvoiceFormContext();
return invoice?.payment_amount || 0;
};
/**
* Retreives the invoice due amount.
* @returns {number}
*/
export const useInvoiceDueAmount = () => {
const total = useInvoiceTotal();
const paidAmount = useInvoicePaidAmount();
return total;
return Math.max(total - paidAmount, 0);
};
/**
@@ -438,3 +396,13 @@ export const useIsInvoiceTaxExclusive = () => {
return values.inclusive_exclusive_tax === TaxType.Exclusive;
};
/**
* Retrieves the invoice currency code.
* @returns {string}
*/
export const useInvoiceCurrencyCode = () => {
const { values } = useFormikContext();
return values.currency_code;
};