refactor: update estimate and receipt forms to use new subtotal and total formatting utilities

This commit is contained in:
Ahmed Bouhuolia
2024-12-01 18:19:09 +02:00
parent 000c3e40e1
commit 03b0d2519b
4 changed files with 47 additions and 16 deletions

View File

@@ -6,13 +6,15 @@ import { T, TotalLines, TotalLine, TotalLineTextStyle } from '@/components';
import { import {
useEstimateAdjustmentFormatted, useEstimateAdjustmentFormatted,
useEstimateDiscountFormatted, useEstimateDiscountFormatted,
useEstimateTotals, useEstimateSubtotalFormatted,
useEstimateTotalFormatted,
} from './utils'; } from './utils';
import { AdjustmentTotalLine } from '../../Invoices/InvoiceForm/AdjustmentTotalLine'; import { AdjustmentTotalLine } from '../../Invoices/InvoiceForm/AdjustmentTotalLine';
import { DiscountTotalLine } from '../../Invoices/InvoiceForm/DiscountTotalLine'; import { DiscountTotalLine } from '../../Invoices/InvoiceForm/DiscountTotalLine';
export function EstimateFormFooterRight() { export function EstimateFormFooterRight() {
const { formattedSubtotal, formattedTotal } = useEstimateTotals(); const subtotalFormatted = useEstimateSubtotalFormatted();
const totalFormatted = useEstimateTotalFormatted();
const { const {
values: { currency_code }, values: { currency_code },
} = useFormikContext(); } = useFormikContext();
@@ -23,7 +25,7 @@ export function EstimateFormFooterRight() {
<EstimateTotalLines labelColWidth={'180px'} amountColWidth={'180px'}> <EstimateTotalLines labelColWidth={'180px'} amountColWidth={'180px'}>
<TotalLine <TotalLine
title={<T id={'estimate_form.label.subtotal'} />} title={<T id={'estimate_form.label.subtotal'} />}
value={formattedSubtotal} value={subtotalFormatted}
/> />
<DiscountTotalLine <DiscountTotalLine
currencyCode={currency_code} currencyCode={currency_code}
@@ -32,7 +34,7 @@ export function EstimateFormFooterRight() {
<AdjustmentTotalLine adjustmentAmount={adjustmentAmount} /> <AdjustmentTotalLine adjustmentAmount={adjustmentAmount} />
<TotalLine <TotalLine
title={<T id={'estimate_form.label.total'} />} title={<T id={'estimate_form.label.total'} />}
value={formattedTotal} value={totalFormatted}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />
</EstimateTotalLines> </EstimateTotalLines>

View File

@@ -10,6 +10,7 @@ import {
repeatValue, repeatValue,
transformToForm, transformToForm,
formattedAmount, formattedAmount,
toSafeNumber,
} from '@/utils'; } from '@/utils';
import { useEstimateFormContext } from './EstimateFormProvider'; import { useEstimateFormContext } from './EstimateFormProvider';
import { import {
@@ -272,9 +273,11 @@ export const useEstimateSubtotalFormatted = () => {
*/ */
export const useEstimateDiscount = () => { export const useEstimateDiscount = () => {
const { values } = useFormikContext(); const { values } = useFormikContext();
const discountAmount = parseFloat(values.discount); const discount = toSafeNumber(values.discount);
return discountAmount; return values?.discount_type === 'percentage'
? (subtotal * discount) / 100
: discount;
}; };
/** /**
@@ -296,7 +299,7 @@ export const useEstimateDiscountFormatted = () => {
*/ */
export const useEstimateAdjustment = () => { export const useEstimateAdjustment = () => {
const { values } = useFormikContext(); const { values } = useFormikContext();
const adjustmentAmount = parseFloat(values.adjustment); const adjustmentAmount = toSafeNumber(values.adjustment);
return adjustmentAmount; return adjustmentAmount;
}; };

View File

@@ -12,23 +12,23 @@ import {
import { import {
useReceiptAdjustmentFormatted, useReceiptAdjustmentFormatted,
useReceiptDiscountAmountFormatted, useReceiptDiscountAmountFormatted,
useReceiptSubtotalFormatted,
useReceiptTotalFormatted,
useReceiptTotals, 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 { const { formattedDueTotal, formattedPaymentTotal } = useReceiptTotals();
formattedSubtotal,
formattedTotal,
formattedDueTotal,
formattedPaymentTotal,
} = useReceiptTotals();
const { const {
values: { currency_code }, values: { currency_code },
} = useFormikContext(); } = useFormikContext();
const subtotalFormatted = useReceiptSubtotalFormatted();
const totalFormatted = useReceiptTotalFormatted();
const discountAmount = useReceiptDiscountAmountFormatted(); const discountAmount = useReceiptDiscountAmountFormatted();
const adjustmentAmount = useReceiptAdjustmentFormatted(); const adjustmentAmount = useReceiptAdjustmentFormatted();
@@ -36,7 +36,7 @@ export function ReceiptFormFooterRight() {
<ReceiptTotalLines labelColWidth={'180px'} amountColWidth={'180px'}> <ReceiptTotalLines labelColWidth={'180px'} amountColWidth={'180px'}>
<TotalLine <TotalLine
title={<T id={'receipt_form.label.subtotal'} />} title={<T id={'receipt_form.label.subtotal'} />}
value={formattedSubtotal} value={subtotalFormatted}
/> />
<DiscountTotalLine <DiscountTotalLine
currencyCode={currency_code} currencyCode={currency_code}
@@ -45,7 +45,7 @@ export function ReceiptFormFooterRight() {
<AdjustmentTotalLine adjustmentAmount={adjustmentAmount} /> <AdjustmentTotalLine adjustmentAmount={adjustmentAmount} />
<TotalLine <TotalLine
title={<T id={'receipt_form.label.total'} />} title={<T id={'receipt_form.label.total'} />}
value={formattedTotal} value={totalFormatted}
borderStyle={TotalLineBorderStyle.SingleDark} borderStyle={TotalLineBorderStyle.SingleDark}
textStyle={TotalLineTextStyle.Bold} textStyle={TotalLineTextStyle.Bold}
/> />

View File

@@ -268,14 +268,40 @@ export const useReceiptSubtotal = () => {
return subtotal; return subtotal;
}; };
/**
* Retrieves the formatted subtotal.
* @returns {string}
*/
export const useReceiptSubtotalFormatted = () => {
const subtotal = useReceiptSubtotal();
const { values } = useFormikContext();
return formattedAmount(subtotal, values.currency_code, { money: true });
};
/**
* Retrieves the receipt discount amount.
* @returns {number}
*/
export const useReceiptDiscountAmount = () => {
const { values } = useFormikContext();
const subtotal = useReceiptSubtotal();
const discount = toSafeNumber(values.discount);
return values?.discount_type === 'percentage'
? (subtotal * discount) / 100
: discount;
};
/** /**
* Retrieves the formatted discount amount. * Retrieves the formatted discount amount.
* @returns {string} * @returns {string}
*/ */
export const useReceiptDiscountAmountFormatted = () => { export const useReceiptDiscountAmountFormatted = () => {
const { values } = useFormikContext(); const { values } = useFormikContext();
const discount = useReceiptDiscountAmount();
return formattedAmount(values.discount, values.currency_code); return formattedAmount(discount, values.currency_code);
}; };
/** /**