feat: add discount fields in sale and purchase forms

This commit is contained in:
Ahmed Bouhuolia
2024-11-30 18:02:50 +02:00
parent 73ab92e693
commit ffb06f5194
11 changed files with 352 additions and 36 deletions

View File

@@ -70,6 +70,9 @@ export const defaultInvoice = {
entries: [...repeatValue(defaultInvoiceEntry, MIN_LINES_NUMBER)],
attachments: [],
payment_methods: {},
discount: '',
discount_type: 'amount',
adjustment: '',
};
// Invoice entry request schema.
@@ -301,6 +304,20 @@ export const useInvoiceSubtotal = () => {
return React.useMemo(() => getEntriesTotal(entries), [entries]);
};
/**
* Retrieves the invoice discount amount.
* @returns {number}
*/
export const useInvoiceDiscountAmount = () => {
const { values } = useFormikContext();
const subtotal = useInvoiceSubtotal();
const discount = parseFloat(values.discount);
return values?.discount_type === 'percentage'
? (subtotal * discount) / 100
: discount;
};
/**
* Detarmines whether the invoice has foreign customer.
* @returns {boolean}
@@ -382,10 +399,12 @@ export const useInvoiceTotal = () => {
const subtotal = useInvoiceSubtotal();
const totalTaxAmount = useInvoiceTotalTaxAmount();
const isExclusiveTax = useIsInvoiceTaxExclusive();
const discountAmount = useInvoiceDiscountAmount();
return R.compose(R.when(R.always(isExclusiveTax), R.add(totalTaxAmount)))(
subtotal,
);
return R.compose(
R.when(R.always(isExclusiveTax), R.add(totalTaxAmount)),
R.subtract(R.__, discountAmount),
)(subtotal);
};
/**