refactor: implementing new formatted amount hooks

This commit is contained in:
Ahmed Bouhuolia
2024-12-02 15:32:39 +02:00
parent 03b0d2519b
commit 05cf94940e
14 changed files with 187 additions and 253 deletions

View File

@@ -12,6 +12,7 @@ import {
repeatValue,
defaultFastFieldShouldUpdate,
formattedAmount,
toSafeNumber,
} from '@/utils';
import { ERROR } from '@/constants/errors';
import { AppToaster } from '@/components';
@@ -305,6 +306,17 @@ export const useInvoiceSubtotal = () => {
return React.useMemo(() => getEntriesTotal(entries), [entries]);
};
/**
* Retrieves the invoice subtotal formatted.
* @returns {string}
*/
export const useInvoiceSubtotalFormatted = () => {
const subtotal = useInvoiceSubtotal();
const { values } = useFormikContext();
return formattedAmount(subtotal, values.currency_code);
};
/**
* Retrieves the invoice discount amount.
* @returns {number}
@@ -438,13 +450,26 @@ export const useInvoiceTotal = () => {
const totalTaxAmount = useInvoiceTotalTaxAmount();
const isExclusiveTax = useIsInvoiceTaxExclusive();
const discountAmount = useInvoiceDiscountAmount();
const adjustmentAmount = useInvoiceAdjustmentAmount();
return R.compose(
R.when(R.always(isExclusiveTax), R.add(totalTaxAmount)),
R.subtract(R.__, discountAmount),
R.subtract(R.__, adjustmentAmount),
)(subtotal);
};
/**
* Retrieves the invoice total formatted.
* @returns {string}
*/
export const useInvoiceTotalFormatted = () => {
const total = useInvoiceTotal();
const { values } = useFormikContext();
return formattedAmount(total, values.currency_code);
};
/**
* Retrieves the paid amount of the invoice.
* @returns {number}
@@ -452,7 +477,18 @@ export const useInvoiceTotal = () => {
export const useInvoicePaidAmount = () => {
const { invoice } = useInvoiceFormContext();
return invoice?.payment_amount || 0;
return toSafeNumber(invoice?.payment_amount);
};
/**
* Retrieves the paid amount of the invoice formatted.
* @returns {string}
*/
export const useInvoicePaidAmountFormatted = () => {
const paidAmount = useInvoicePaidAmount();
const { values } = useFormikContext();
return formattedAmount(paidAmount, values.currency_code);
};
/**
@@ -466,6 +502,17 @@ export const useInvoiceDueAmount = () => {
return Math.max(total - paidAmount, 0);
};
/**
* Retrieves the invoice due amount formatted.
* @returns {string}
*/
export const useInvoiceDueAmountFormatted = () => {
const dueAmount = useInvoiceDueAmount();
const { values } = useFormikContext();
return formattedAmount(dueAmount, values.currency_code);
};
/**
* Detrmines whether the tax is inclusive.
* @returns {boolean}