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

@@ -9,6 +9,9 @@ import {
TotalLine,
TotalLineBorderStyle,
TotalLineTextStyle,
FFormGroup,
FInputGroup,
FSelect,
} from '@/components';
import { useInvoiceAggregatedTaxRates } from './utils';
import { TaxType } from '@/interfaces/TaxRates';
@@ -18,6 +21,7 @@ import {
InvoiceSubTotalFormatted,
InvoiceTotalFormatted,
} from './components';
import { Button } from '@blueprintjs/core';
export function InvoiceFormFooterRight() {
const {
@@ -38,6 +42,31 @@ export function InvoiceFormFooterRight() {
}
value={<InvoiceSubTotalFormatted />}
/>
<FFormGroup name={'discount'} label={'Discount'} inline>
<FInputGroup
name={'discount'}
rightElement={
<FSelect
name={'discount_type'}
items={[
{ text: 'USD', value: 'amount' },
{ text: '%', value: 'percentage' },
]}
input={({ text }) => (
<Button small minimal>
{text}
</Button>
)}
filterable={false}
/>
}
/>
</FFormGroup>
<FFormGroup name={'adjustment'} label={'Adjustment'} inline>
<FInputGroup name={'adjustment'} />
</FFormGroup>
{taxEntries.map((tax, index) => (
<TotalLine
key={index}

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);
};
/**