Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: due Amount on edit page is calculated wrong with "Ex tax" #562

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -38,7 +36,7 @@ export function InvoiceFormFooterRight() {
: 'Subtotal'}
</>
}
value={formattedSubtotal}
value={<InvoiceSubTotalFormatted />}
/>
{taxEntries.map((tax, index) => (
<TotalLine
Expand All @@ -50,18 +48,18 @@ export function InvoiceFormFooterRight() {
))}
<TotalLine
title={`Total (${currency_code})`}
value={formattedTotal}
value={<InvoiceTotalFormatted />}
borderStyle={TotalLineBorderStyle.SingleDark}
textStyle={TotalLineTextStyle.Bold}
/>
<TotalLine
title={<T id={'invoice_form.label.payment_amount'} />}
value={formattedPaymentTotal}
value={<InvoicePaidAmountFormatted />}
borderStyle={TotalLineBorderStyle.None}
/>
<TotalLine
title={<T id={'invoice_form.label.due_amount'} />}
value={formattedDueTotal}
value={<InvoiceDueAmountFormatted />}
textStyle={TotalLineTextStyle.Bold}
/>
</InvoiceTotalLines>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,7 +32,7 @@ function InvoiceFormBigTotal() {
} = useFormikContext();

// Calculate the total due amount of invoice entries.
const totalDueAmount = useInvoiceSubtotal();
const totalDueAmount = useInvoiceDueAmount();

return (
<PageFormBigNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import intl from 'react-intl-universal';
import * as R from 'ramda';
import { Button } from '@blueprintjs/core';
import { useFormikContext } from 'formik';
import { ExchangeRateInputGroup } from '@/components';
import { ExchangeRateInputGroup, FormatNumber } from '@/components';
import { useCurrentOrganization } from '@/hooks/state';
import { useInvoiceIsForeignCustomer, useInvoiceTotal } from './utils';
import withSettings from '@/containers/Settings/withSettings';
import {
useInvoiceCurrencyCode,
useInvoiceDueAmount,
useInvoiceIsForeignCustomer,
useInvoicePaidAmount,
useInvoiceSubtotal,
useInvoiceTotal,
} from './utils';
import { useUpdateEffect } from '@/hooks';
import { transactionNumber } from '@/utils';
import withDialogActions from '@/containers/Dialog/withDialogActions';
import { DialogsName } from '@/constants/dialogs';
import withSettings from '@/containers/Settings/withSettings';
import withDialogActions from '@/containers/Dialog/withDialogActions';
import {
useSyncExRateToForm,
withExchangeRateFetchingLoading,
Expand Down Expand Up @@ -109,3 +116,47 @@ export const InvoiceExchangeRateSync = R.compose(withDialogActions)(
return null;
},
);

/**
*Renders the invoice formatted total.
* @returns {JSX.Element}
*/
export const InvoiceTotalFormatted = () => {
const currencyCode = useInvoiceCurrencyCode();
const total = useInvoiceTotal();

return <FormatNumber value={total} currency={currencyCode} />;
};

/**
* Renders the invoice formatted subtotal.
* @returns {JSX.Element}
*/
export const InvoiceSubTotalFormatted = () => {
const currencyCode = useInvoiceCurrencyCode();
const subTotal = useInvoiceSubtotal();

return <FormatNumber value={subTotal} currency={currencyCode} />;
};

/**
* Renders the invoice formatted due amount.
* @returns {JSX.Element}
*/
export const InvoiceDueAmountFormatted = () => {
const currencyCode = useInvoiceCurrencyCode();
const dueAmount = useInvoiceDueAmount();

return <FormatNumber value={dueAmount} currency={currencyCode} />;
};

/**
* Renders the invoice formatted paid amount.
* @returns {JSX.Element}
*/
export const InvoicePaidAmountFormatted = () => {
const currencyCode = useInvoiceCurrencyCode();
const paidAmount = useInvoicePaidAmount();

return <FormatNumber value={paidAmount} currency={currencyCode} />;
};
76 changes: 22 additions & 54 deletions packages/webapp/src/containers/Sales/Invoices/InvoiceForm/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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);
};

/**
Expand All @@ -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;
};
Loading