diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormFooterRight.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormFooterRight.tsx index 5c152e26b..68bc36872 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormFooterRight.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormFooterRight.tsx @@ -10,18 +10,16 @@ import { TotalLineBorderStyle, TotalLineTextStyle, } from '@/components'; -import { useInvoiceAggregatedTaxRates, useInvoiceTotals } from './utils'; +import { useInvoiceAggregatedTaxRates } from './utils'; import { TaxType } from '@/interfaces/TaxRates'; +import { + InvoiceDueAmountFormatted, + InvoicePaidAmountFormatted, + InvoiceSubTotalFormatted, + InvoiceTotalFormatted, +} from './components'; export function InvoiceFormFooterRight() { - // Calculate the total due amount of invoice entries. - const { - formattedSubtotal, - formattedTotal, - formattedDueTotal, - formattedPaymentTotal, - } = useInvoiceTotals(); - const { values: { inclusive_exclusive_tax, currency_code }, } = useFormikContext(); @@ -38,7 +36,7 @@ export function InvoiceFormFooterRight() { : 'Subtotal'} } - value={formattedSubtotal} + value={} /> {taxEntries.map((tax, index) => ( } borderStyle={TotalLineBorderStyle.SingleDark} textStyle={TotalLineTextStyle.Bold} /> } - value={formattedPaymentTotal} + value={} borderStyle={TotalLineBorderStyle.None} /> } - value={formattedDueTotal} + value={} textStyle={TotalLineTextStyle.Bold} /> diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormHeader.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormHeader.tsx index 66d1ed48c..8ee7401bd 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormHeader.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/InvoiceFormHeader.tsx @@ -8,7 +8,7 @@ import InvoiceFormHeaderFields from './InvoiceFormHeaderFields'; import { CLASSES } from '@/constants/classes'; import { PageFormBigNumber } from '@/components'; -import { useInvoiceSubtotal } from './utils'; +import { useInvoiceDueAmount } from './utils'; /** * Invoice form header section. @@ -32,7 +32,7 @@ function InvoiceFormBigTotal() { } = useFormikContext(); // Calculate the total due amount of invoice entries. - const totalDueAmount = useInvoiceSubtotal(); + const totalDueAmount = useInvoiceDueAmount(); return ( { + const currencyCode = useInvoiceCurrencyCode(); + const total = useInvoiceTotal(); + + return ; +}; + +/** + * Renders the invoice formatted subtotal. + * @returns {JSX.Element} + */ +export const InvoiceSubTotalFormatted = () => { + const currencyCode = useInvoiceCurrencyCode(); + const subTotal = useInvoiceSubtotal(); + + return ; +}; + +/** + * Renders the invoice formatted due amount. + * @returns {JSX.Element} + */ +export const InvoiceDueAmountFormatted = () => { + const currencyCode = useInvoiceCurrencyCode(); + const dueAmount = useInvoiceDueAmount(); + + return ; +}; + +/** + * Renders the invoice formatted paid amount. + * @returns {JSX.Element} + */ +export const InvoicePaidAmountFormatted = () => { + const currencyCode = useInvoiceCurrencyCode(); + const paidAmount = useInvoicePaidAmount(); + + return ; +}; diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx index 6d693415e..c031ce74c 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx @@ -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; +};