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

@@ -11,24 +11,22 @@ import {
useBillAdjustmentAmountFormatted, useBillAdjustmentAmountFormatted,
useBillAggregatedTaxRates, useBillAggregatedTaxRates,
useBillDiscountAmountFormatted, useBillDiscountAmountFormatted,
useBillDueAmountFormatted,
useBillPaidAmountFormatted,
useBillSubtotalFormatted, useBillSubtotalFormatted,
useBillTotalFormatted, useBillTotalFormatted,
useBillTotals,
} from './utils'; } from './utils';
import { TaxType } from '@/interfaces/TaxRates'; import { TaxType } from '@/interfaces/TaxRates';
import { AdjustmentTotalLine } from '@/containers/Sales/Invoices/InvoiceForm/AdjustmentTotalLine'; import { AdjustmentTotalLine } from '@/containers/Sales/Invoices/InvoiceForm/AdjustmentTotalLine';
import { DiscountTotalLine } from '@/containers/Sales/Invoices/InvoiceForm/DiscountTotalLine'; import { DiscountTotalLine } from '@/containers/Sales/Invoices/InvoiceForm/DiscountTotalLine';
export function BillFormFooterRight() { export function BillFormFooterRight() {
const {
formattedDueTotal,
formattedPaymentTotal,
} = useBillTotals();
const { const {
values: { inclusive_exclusive_tax, currency_code }, values: { inclusive_exclusive_tax, currency_code },
} = useFormikContext(); } = useFormikContext();
const dueAmountFormatted = useBillDueAmountFormatted();
const paidAmountFormatted = useBillPaidAmountFormatted();
const subtotalFormatted = useBillSubtotalFormatted(); const subtotalFormatted = useBillSubtotalFormatted();
const totalFormatted = useBillTotalFormatted(); const totalFormatted = useBillTotalFormatted();
const taxEntries = useBillAggregatedTaxRates(); const taxEntries = useBillAggregatedTaxRates();
@@ -68,12 +66,12 @@ export function BillFormFooterRight() {
/> />
<TotalLine <TotalLine
title={'Paid Amount'} title={'Paid Amount'}
value={formattedPaymentTotal} value={paidAmountFormatted}
borderStyle={TotalLineBorderStyle.None} borderStyle={TotalLineBorderStyle.None}
/> />
<TotalLine <TotalLine
title={'Due Amount'} title={'Due Amount'}
value={formattedDueTotal} value={dueAmountFormatted}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />
</BillTotalLines> </BillTotalLines>

View File

@@ -260,58 +260,6 @@ export const useSetPrimaryWarehouseToForm = () => {
}, [isWarehousesSuccess, setFieldValue, warehouses]); }, [isWarehousesSuccess, setFieldValue, warehouses]);
}; };
/**
* Retreives the bill totals.
*/
export const useBillTotals = () => {
const {
values: { currency_code: currencyCode },
} = useFormikContext();
// Retrieves the bill subtotal.
const subtotal = useBillSubtotal();
const total = useBillTotal();
// Retrieves the formatted total money.
const formattedTotal = React.useMemo(
() => formattedAmount(total, currencyCode),
[total, currencyCode],
);
// Retrieves the formatted subtotal.
const formattedSubtotal = React.useMemo(
() => formattedAmount(subtotal, currencyCode, { money: false }),
[subtotal, 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 bill has foreign customer. * Detarmines whether the bill has foreign customer.
* @returns {boolean} * @returns {boolean}
@@ -377,9 +325,9 @@ export const useBillSubtotal = () => {
*/ */
export const useBillSubtotalFormatted = () => { export const useBillSubtotalFormatted = () => {
const subtotal = useBillSubtotal(); const subtotal = useBillSubtotal();
const { currency_code: currencyCode } = useFormikContext(); const { values} = useFormikContext();
return formattedAmount(subtotal, currencyCode); return formattedAmount(subtotal, values.currency_code);
}; };
/** /**
@@ -398,9 +346,9 @@ export const useBillDiscountAmount = () => {
*/ */
export const useBillDiscountAmountFormatted = () => { export const useBillDiscountAmountFormatted = () => {
const discountAmount = useBillDiscountAmount(); const discountAmount = useBillDiscountAmount();
const { currency_code: currencyCode } = useFormikContext(); const { values } = useFormikContext();
return formattedAmount(discountAmount, currencyCode); return formattedAmount(discountAmount, values.currency_code);
}; };
/** /**
@@ -419,9 +367,9 @@ export const useBillAdjustmentAmount = () => {
*/ */
export const useBillAdjustmentAmountFormatted = () => { export const useBillAdjustmentAmountFormatted = () => {
const adjustmentAmount = useBillAdjustmentAmount(); const adjustmentAmount = useBillAdjustmentAmount();
const { currency_code: currencyCode } = useFormikContext(); const { values } = useFormikContext();
return formattedAmount(adjustmentAmount, currencyCode); return formattedAmount(adjustmentAmount, values.currency_code);
}; };
/** /**
@@ -436,6 +384,7 @@ export const useBillTotalTaxAmount = () => {
.filter((entry) => entry.tax_amount) .filter((entry) => entry.tax_amount)
.sumBy('tax_amount') .sumBy('tax_amount')
.value(); .value();
}, [values.entries]); }, [values.entries]);
}; };
@@ -473,7 +422,50 @@ export const useBillTotal = () => {
*/ */
export const useBillTotalFormatted = () => { export const useBillTotalFormatted = () => {
const total = useBillTotal(); const total = useBillTotal();
const { currency_code: currencyCode } = useFormikContext(); const { values } = useFormikContext();
return formattedAmount(total, currencyCode); return formattedAmount(total, values.currency_code);
};
/**
* Retrieves the bill paid amount.
* @returns {number}
*/
export const useBillPaidAmount = () => {
const { values } = useFormikContext();
return toSafeNumber(0);
};
/**
* Retrieves the bill paid amount formatted.
* @returns {string}
*/
export const useBillPaidAmountFormatted = () => {
const paidAmount = useBillPaidAmount();
const { values } = useFormikContext();
return formattedAmount(paidAmount, values.currency_code);
};
/**
* Retrieves the bill due amount.
* @returns {number}
*/
export const useBillDueAmount = () => {
const total = useBillTotal();
const paidAmount = useBillPaidAmount();
return total - paidAmount;
};
/**
* Retrieves the bill due amount formatted.
* @returns {string}
*/
export const useBillDueAmountFormatted = () => {
const dueAmount = useBillDueAmount();
const { values } = useFormikContext();
return formattedAmount(dueAmount, values.currency_code);
}; };

View File

@@ -5,7 +5,7 @@ import { useFormikContext } from 'formik';
import { T, TotalLines, TotalLine, TotalLineTextStyle } from '@/components'; import { T, TotalLines, TotalLine, TotalLineTextStyle } from '@/components';
import { import {
useVendorCreditAdjustmentAmountFormatted, useVendorCreditAdjustmentAmountFormatted,
useVendorCreditDiscountAmount, useVendorCreditDiscountAmountFormatted,
useVendorCreditSubtotalFormatted, useVendorCreditSubtotalFormatted,
useVendorCreditTotalFormatted, useVendorCreditTotalFormatted,
} from './utils'; } from './utils';
@@ -19,8 +19,8 @@ export function VendorCreditNoteFormFooterRight() {
const totalFormatted = useVendorCreditTotalFormatted(); const totalFormatted = useVendorCreditTotalFormatted();
const subtotalFormatted = useVendorCreditSubtotalFormatted(); const subtotalFormatted = useVendorCreditSubtotalFormatted();
const discountAmount = useVendorCreditDiscountAmount(); const discountAmountFormatted = useVendorCreditDiscountAmountFormatted();
const adjustmentAmount = useVendorCreditAdjustmentAmountFormatted(); const adjustmentAmountFormatted = useVendorCreditAdjustmentAmountFormatted();
return ( return (
<VendorCreditNoteTotalLines <VendorCreditNoteTotalLines
@@ -33,9 +33,9 @@ export function VendorCreditNoteFormFooterRight() {
/> />
<DiscountTotalLine <DiscountTotalLine
currencyCode={currency_code} currencyCode={currency_code}
discountAmount={discountAmount} discountAmount={discountAmountFormatted}
/> />
<AdjustmentTotalLine adjustmentAmount={adjustmentAmount} /> <AdjustmentTotalLine adjustmentAmount={adjustmentAmountFormatted} />
<TotalLine <TotalLine
title={<T id={'vendor_credit_form.label.total'} />} title={<T id={'vendor_credit_form.label.total'} />}
value={totalFormatted} value={totalFormatted}

View File

@@ -185,32 +185,6 @@ export const useSetPrimaryWarehouseToForm = () => {
}, [isWarehousesSuccess, setFieldValue, warehouses]); }, [isWarehousesSuccess, setFieldValue, warehouses]);
}; };
export const useVendorCrditNoteTotals = () => {
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 vendor credit subtotal. * Retrieves the vendor credit subtotal.
* @returns {number} * @returns {number}

View File

@@ -185,6 +185,7 @@ export const useCreditNoteSubtotal = () => {
const { const {
values: { entries }, values: { entries },
} = useFormikContext(); } = useFormikContext();
const total = React.useMemo(() => getEntriesTotal(entries), [entries]); const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
return total; return total;

View File

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

View File

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

View File

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

View File

@@ -13,14 +13,12 @@ import {
useInvoiceAdjustmentAmountFormatted, useInvoiceAdjustmentAmountFormatted,
useInvoiceAggregatedTaxRates, useInvoiceAggregatedTaxRates,
useInvoiceDiscountAmountFormatted, useInvoiceDiscountAmountFormatted,
useInvoiceDueAmountFormatted,
useInvoicePaidAmountFormatted,
useInvoiceSubtotalFormatted,
useInvoiceTotalFormatted,
} from './utils'; } from './utils';
import { TaxType } from '@/interfaces/TaxRates'; import { TaxType } from '@/interfaces/TaxRates';
import {
InvoiceDueAmountFormatted,
InvoicePaidAmountFormatted,
InvoiceSubTotalFormatted,
InvoiceTotalFormatted,
} from './components';
import { AdjustmentTotalLine } from './AdjustmentTotalLine'; import { AdjustmentTotalLine } from './AdjustmentTotalLine';
import { DiscountTotalLine } from './DiscountTotalLine'; import { DiscountTotalLine } from './DiscountTotalLine';
@@ -32,6 +30,10 @@ export function InvoiceFormFooterRight() {
const taxEntries = useInvoiceAggregatedTaxRates(); const taxEntries = useInvoiceAggregatedTaxRates();
const adjustmentAmount = useInvoiceAdjustmentAmountFormatted(); const adjustmentAmount = useInvoiceAdjustmentAmountFormatted();
const discountAmount = useInvoiceDiscountAmountFormatted(); const discountAmount = useInvoiceDiscountAmountFormatted();
const totalFormatted = useInvoiceTotalFormatted();
const subtotalFormatted = useInvoiceSubtotalFormatted();
const paidAmountFormatted = useInvoicePaidAmountFormatted();
const dueAmountFormatted = useInvoiceDueAmountFormatted();
return ( return (
<InvoiceTotalLines labelColWidth={'180px'} amountColWidth={'180px'}> <InvoiceTotalLines labelColWidth={'180px'} amountColWidth={'180px'}>
@@ -43,7 +45,7 @@ export function InvoiceFormFooterRight() {
: 'Subtotal'} : 'Subtotal'}
</> </>
} }
value={<InvoiceSubTotalFormatted />} value={subtotalFormatted}
/> />
<DiscountTotalLine <DiscountTotalLine
currencyCode={currency_code} currencyCode={currency_code}
@@ -61,18 +63,18 @@ export function InvoiceFormFooterRight() {
))} ))}
<TotalLine <TotalLine
title={`Total (${currency_code})`} title={`Total (${currency_code})`}
value={<InvoiceTotalFormatted />} value={totalFormatted}
borderStyle={TotalLineBorderStyle.SingleDark} borderStyle={TotalLineBorderStyle.SingleDark}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />
<TotalLine <TotalLine
title={<T id={'invoice_form.label.payment_amount'} />} title={<T id={'invoice_form.label.payment_amount'} />}
value={<InvoicePaidAmountFormatted />} value={paidAmountFormatted}
borderStyle={TotalLineBorderStyle.None} borderStyle={TotalLineBorderStyle.None}
/> />
<TotalLine <TotalLine
title={<T id={'invoice_form.label.due_amount'} />} title={<T id={'invoice_form.label.due_amount'} />}
value={<InvoiceDueAmountFormatted />} value={dueAmountFormatted}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />
</InvoiceTotalLines> </InvoiceTotalLines>

View File

@@ -116,47 +116,3 @@ export const InvoiceExchangeRateSync = R.compose(withDialogActions)(
return null; 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} />;
};

View File

@@ -12,6 +12,7 @@ import {
repeatValue, repeatValue,
defaultFastFieldShouldUpdate, defaultFastFieldShouldUpdate,
formattedAmount, formattedAmount,
toSafeNumber,
} from '@/utils'; } from '@/utils';
import { ERROR } from '@/constants/errors'; import { ERROR } from '@/constants/errors';
import { AppToaster } from '@/components'; import { AppToaster } from '@/components';
@@ -305,6 +306,17 @@ export const useInvoiceSubtotal = () => {
return React.useMemo(() => getEntriesTotal(entries), [entries]); 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. * Retrieves the invoice discount amount.
* @returns {number} * @returns {number}
@@ -438,13 +450,26 @@ export const useInvoiceTotal = () => {
const totalTaxAmount = useInvoiceTotalTaxAmount(); const totalTaxAmount = useInvoiceTotalTaxAmount();
const isExclusiveTax = useIsInvoiceTaxExclusive(); const isExclusiveTax = useIsInvoiceTaxExclusive();
const discountAmount = useInvoiceDiscountAmount(); const discountAmount = useInvoiceDiscountAmount();
const adjustmentAmount = useInvoiceAdjustmentAmount();
return R.compose( return R.compose(
R.when(R.always(isExclusiveTax), R.add(totalTaxAmount)), R.when(R.always(isExclusiveTax), R.add(totalTaxAmount)),
R.subtract(R.__, discountAmount), R.subtract(R.__, discountAmount),
R.subtract(R.__, adjustmentAmount),
)(subtotal); )(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. * Retrieves the paid amount of the invoice.
* @returns {number} * @returns {number}
@@ -452,7 +477,18 @@ export const useInvoiceTotal = () => {
export const useInvoicePaidAmount = () => { export const useInvoicePaidAmount = () => {
const { invoice } = useInvoiceFormContext(); 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); 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. * Detrmines whether the tax is inclusive.
* @returns {boolean} * @returns {boolean}

View File

@@ -12,20 +12,22 @@ import {
import { import {
useReceiptAdjustmentFormatted, useReceiptAdjustmentFormatted,
useReceiptDiscountAmountFormatted, useReceiptDiscountAmountFormatted,
useReceiptDueAmountFormatted,
useReceiptPaidAmountFormatted,
useReceiptSubtotalFormatted, useReceiptSubtotalFormatted,
useReceiptTotalFormatted, useReceiptTotalFormatted,
useReceiptTotals,
} from './utils'; } from './utils';
import { DiscountTotalLine } from '../../Invoices/InvoiceForm/DiscountTotalLine'; import { DiscountTotalLine } from '../../Invoices/InvoiceForm/DiscountTotalLine';
import { AdjustmentTotalLine } from '../../Invoices/InvoiceForm/AdjustmentTotalLine'; import { AdjustmentTotalLine } from '../../Invoices/InvoiceForm/AdjustmentTotalLine';
export function ReceiptFormFooterRight() { export function ReceiptFormFooterRight() {
const { formattedDueTotal, formattedPaymentTotal } = useReceiptTotals();
const { const {
values: { currency_code }, values: { currency_code },
} = useFormikContext(); } = useFormikContext();
const paidAmountFormatted = useReceiptPaidAmountFormatted();
const dueAmountFormatted = useReceiptDueAmountFormatted();
const subtotalFormatted = useReceiptSubtotalFormatted(); const subtotalFormatted = useReceiptSubtotalFormatted();
const totalFormatted = useReceiptTotalFormatted(); const totalFormatted = useReceiptTotalFormatted();
@@ -51,12 +53,12 @@ export function ReceiptFormFooterRight() {
/> />
<TotalLine <TotalLine
title={<T id={'receipt_form.label.payment_amount'} />} title={<T id={'receipt_form.label.payment_amount'} />}
value={formattedPaymentTotal} value={paidAmountFormatted}
borderStyle={TotalLineBorderStyle.None} borderStyle={TotalLineBorderStyle.None}
/> />
<TotalLine <TotalLine
title={<T id={'receipt_form.label.due_amount'} />} title={<T id={'receipt_form.label.due_amount'} />}
value={formattedDueTotal} value={dueAmountFormatted}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />
</ReceiptTotalLines> </ReceiptTotalLines>

View File

@@ -7,7 +7,7 @@ import * as R from 'ramda';
import { ExchangeRateInputGroup } from '@/components'; import { ExchangeRateInputGroup } from '@/components';
import { useCurrentOrganization } from '@/hooks/state'; import { useCurrentOrganization } from '@/hooks/state';
import { useReceiptIsForeignCustomer, useReceiptTotals } from './utils'; import { useReceiptIsForeignCustomer, useReceiptTotal } from './utils';
import { useUpdateEffect } from '@/hooks'; import { useUpdateEffect } from '@/hooks';
import { transactionNumber } from '@/utils'; import { transactionNumber } from '@/utils';
import withSettings from '@/containers/Settings/withSettings'; import withSettings from '@/containers/Settings/withSettings';
@@ -98,7 +98,7 @@ export const ReceiptSyncAutoExRateToForm = R.compose(withDialogActions)(
// #withDialogActions // #withDialogActions
openDialog, openDialog,
}) => { }) => {
const { total } = useReceiptTotals(); const total = useReceiptTotal();
const timeout = useRef(); const timeout = useRef();
useSyncExRateToForm({ useSyncExRateToForm({

View File

@@ -202,57 +202,6 @@ export const useSetPrimaryBranchToForm = () => {
}, [isBranchesSuccess, setFieldValue, branches]); }, [isBranchesSuccess, setFieldValue, branches]);
}; };
/**
* Retreives the Receipt totals.
*/
export const useReceiptTotals = () => {
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],
);
// 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,
};
};
/** /**
* Retrieves the receipt subtotal. * Retrieves the receipt subtotal.
* @returns {number} * @returns {number}
@@ -349,6 +298,47 @@ export const useReceiptTotalFormatted = () => {
return formattedAmount(total, values.currency_code); return formattedAmount(total, values.currency_code);
}; };
/**
* Retrieves the receipt paid amount.
* @returns {number}
*/
export const useReceiptPaidAmount = () => {
return toSafeNumber(0);
};
/**
* Retrieves the formatted receipt paid amount.
* @returns {string}
*/
export const useReceiptPaidAmountFormatted = () => {
const paidAmount = useReceiptPaidAmount();
const { values } = useFormikContext();
return formattedAmount(paidAmount, values.currency_code);
};
/**
* Retrieves the receipt due amount.
* @returns {number}
*/
export const useReceiptDueAmount = () => {
const total = useReceiptTotal();
const paidAmount = useReceiptPaidAmount();
return total - paidAmount;
};
/**
* Retrieves the formatted receipt due amount.
* @returns {string}
*/
export const useReceiptDueAmountFormatted = () => {
const dueAmount = useReceiptDueAmount();
const { values } = useFormikContext();
return formattedAmount(dueAmount, values.currency_code);
};
/** /**
* Detarmines whether the receipt has foreign customer. * Detarmines whether the receipt has foreign customer.
* @returns {boolean} * @returns {boolean}