feat: enhance discount handling in financial forms

- Implemented discount and adjustment fields in Bill, Credit Note, Estimate, Invoice, and Receipt forms.
- Created new components for displaying discount and adjustment totals, improving clarity in financial documents.
- Updated utility functions to format discount and adjustment amounts consistently across various forms.
- Enhanced user experience by integrating discount functionality into the form context, allowing for better data management and display.

This update improves the overall functionality and user experience related to discounts in financial transactions.
This commit is contained in:
Ahmed Bouhuolia
2024-12-01 17:59:01 +02:00
parent ffb06f5194
commit 000c3e40e1
17 changed files with 644 additions and 194 deletions

View File

@@ -64,7 +64,7 @@ export const defaultEstimate = {
attachments: [],
pdf_template_id: '',
discount: '',
discount_type: 'amount'
discount_type: 'amount',
};
const ERRORS = {
@@ -238,6 +238,107 @@ export const useEstimateTotals = () => {
};
};
/**
* Retrieves the estimate subtotal.
* @returns {number}
*/
export const useEstimateSubtotal = () => {
const {
values: { entries },
} = useFormikContext();
// Retrieves the invoice entries total.
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
return total;
};
/**
* Retrieves the estimate subtotal formatted.
* @returns {string}
*/
export const useEstimateSubtotalFormatted = () => {
const subtotal = useEstimateSubtotal();
const {
values: { currency_code: currencyCode },
} = useFormikContext();
return formattedAmount(subtotal, currencyCode);
};
/**
* Retrieves the estimate discount amount.
* @returns {number}
*/
export const useEstimateDiscount = () => {
const { values } = useFormikContext();
const discountAmount = parseFloat(values.discount);
return discountAmount;
};
/**
* Retrieves the estimate discount formatted.
* @returns {string}
*/
export const useEstimateDiscountFormatted = () => {
const discount = useEstimateDiscount();
const {
values: { currency_code: currencyCode },
} = useFormikContext();
return formattedAmount(discount, currencyCode);
};
/**
* Retrieves the estimate adjustment amount.
* @returns {number}
*/
export const useEstimateAdjustment = () => {
const { values } = useFormikContext();
const adjustmentAmount = parseFloat(values.adjustment);
return adjustmentAmount;
};
/**
* Retrieves the estimate adjustment formatted.
* @returns {string}
*/
export const useEstimateAdjustmentFormatted = () => {
const adjustment = useEstimateAdjustment();
const {
values: { currency_code: currencyCode },
} = useFormikContext();
return formattedAmount(adjustment, currencyCode);
};
/**
* Retrieves the estimate total.
* @returns {number}
*/
export const useEstimateTotal = () => {
const subtotal = useEstimateSubtotal();
const discount = useEstimateDiscount();
const adjustment = useEstimateAdjustment();
return subtotal - discount - adjustment;
};
/**
* Retrieves the estimate total formatted.
* @returns {string}
*/
export const useEstimateTotalFormatted = () => {
const total = useEstimateTotal();
const {
values: { currency_code: currencyCode },
} = useFormikContext();
return formattedAmount(total, currencyCode);
};
/**
* Detarmines whether the estimate has foreign customer.
* @returns {boolean}