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

@@ -13,13 +13,13 @@ import { AdjustmentTotalLine } from '../../Invoices/InvoiceForm/AdjustmentTotalL
import { DiscountTotalLine } from '../../Invoices/InvoiceForm/DiscountTotalLine';
export function EstimateFormFooterRight() {
const subtotalFormatted = useEstimateSubtotalFormatted();
const totalFormatted = useEstimateTotalFormatted();
const {
values: { currency_code },
} = useFormikContext();
const discountAmount = useEstimateDiscountFormatted();
const adjustmentAmount = useEstimateAdjustmentFormatted();
const subtotalFormatted = useEstimateSubtotalFormatted();
const totalFormatted = useEstimateTotalFormatted();
const discountAmountFormatted = useEstimateDiscountFormatted();
const adjustmentAmountFormatted = useEstimateAdjustmentFormatted();
return (
<EstimateTotalLines labelColWidth={'180px'} amountColWidth={'180px'}>
@@ -29,9 +29,9 @@ export function EstimateFormFooterRight() {
/>
<DiscountTotalLine
currencyCode={currency_code}
discountAmount={discountAmount}
discountAmount={discountAmountFormatted}
/>
<AdjustmentTotalLine adjustmentAmount={adjustmentAmount} />
<AdjustmentTotalLine adjustmentAmount={adjustmentAmountFormatted} />
<TotalLine
title={<T id={'estimate_form.label.total'} />}
value={totalFormatted}

View File

@@ -6,7 +6,7 @@ import * as R from 'ramda';
import { useFormikContext } from 'formik';
import { ExchangeRateInputGroup } from '@/components';
import { useCurrentOrganization } from '@/hooks/state';
import { useEstimateIsForeignCustomer, useEstimateTotals } from './utils';
import { useEstimateIsForeignCustomer, useEstimateSubtotal } from './utils';
import { transactionNumber } from '@/utils';
import { useUpdateEffect } from '@/hooks';
import withSettings from '@/containers/Settings/withSettings';
@@ -102,13 +102,13 @@ export const EstimateSyncAutoExRateToForm = R.compose(withDialogActions)(
// #withDialogActions
openDialog,
}) => {
const { total } = useEstimateTotals();
const subtotal = useEstimateSubtotal();
const timeout = useRef();
useSyncExRateToForm({
onSynced: () => {
// If the total bigger then zero show alert to the user after adjusting entries.
if (total > 0) {
if (subtotal > 0) {
clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
openDialog(DialogsName.InvoiceExchangeRateChangeNotice);

View File

@@ -1,5 +1,5 @@
// @ts-nocheck
import React from 'react';
import React, { useMemo } from 'react';
import * as R from 'ramda';
import intl from 'react-intl-universal';
import moment from 'moment';
@@ -64,6 +64,7 @@ export const defaultEstimate = {
entries: [...repeatValue(defaultEstimateEntry, MIN_LINES_NUMBER)],
attachments: [],
pdf_template_id: '',
adjustment: '',
discount: '',
discount_type: 'amount',
};
@@ -210,35 +211,6 @@ export const useSetPrimaryBranchToForm = () => {
}, [isBranchesSuccess, setFieldValue, branches]);
};
/**
* Retreives the estimate totals.
*/
export const useEstimateTotals = () => {
const {
values: { entries, currency_code: currencyCode },
} = useFormikContext();
// Retrieves the invoice entries total.
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
// 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],
);
return {
total,
formattedTotal,
formattedSubtotal,
};
};
/**
* Retrieves the estimate subtotal.
* @returns {number}
@@ -249,9 +221,9 @@ export const useEstimateSubtotal = () => {
} = useFormikContext();
// Retrieves the invoice entries total.
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
const subtotal = useMemo(() => getEntriesTotal(entries), [entries]);
return total;
return subtotal;
};
/**