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

@@ -5,10 +5,14 @@ import {
TotalLine,
TotalLineBorderStyle,
TotalLineTextStyle,
FFormGroup,
FInputGroup,
FSelect,
} from '@/components';
import { useBillAggregatedTaxRates, useBillTotals } from './utils';
import { useFormikContext } from 'formik';
import { TaxType } from '@/interfaces/TaxRates';
import { Button } from '@blueprintjs/core';
export function BillFormFooterRight() {
const {
@@ -37,6 +41,34 @@ export function BillFormFooterRight() {
value={formattedSubtotal}
borderStyle={TotalLineBorderStyle.None}
/>
{/* ----------- Discount ----------- */}
<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>
{/* ----------- Adjustment ----------- */}
<FFormGroup name={'adjustment'} label={'Adjustment'} inline>
<FInputGroup name={'adjustment'} />
</FFormGroup>
{taxEntries.map((tax, index) => (
<TotalLine
key={index}

View File

@@ -13,6 +13,7 @@ import {
repeatValue,
orderingLinesIndexes,
formattedAmount,
toSafeNumber,
} from '@/utils';
import {
updateItemsEntriesTotal,
@@ -364,7 +365,27 @@ export const useBillSubtotal = () => {
};
/**
* Retreives the bill total tax amount.
* Retrieves the bill discount amount.
* @returns {number}
*/
export const useBillDiscountAmount = () => {
const { values } = useFormikContext();
return toSafeNumber(values.discount);
};
/**
* Retrieves the bill adjustment amount.
* @returns {number}
*/
export const useBillAdjustmentAmount = () => {
const { values } = useFormikContext();
return toSafeNumber(values.adjustment);
};
/**
* Retrieves the bill total tax amount.
* @returns {number}
*/
export const useBillTotalTaxAmount = () => {
@@ -389,15 +410,19 @@ export const useIsBillTaxExclusive = () => {
};
/**
* Retreives the bill total.
* Retrieves the bill total.
* @returns {number}
*/
export const useBillTotal = () => {
const subtotal = useBillSubtotal();
const totalTaxAmount = useBillTotalTaxAmount();
const isExclusiveTax = useIsBillTaxExclusive();
const discountAmount = useBillDiscountAmount();
const adjustmentAmount = useBillAdjustmentAmount();
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),
R.subtract(R.__, adjustmentAmount),
)(subtotal);
};

View File

@@ -53,6 +53,9 @@ export const defaultVendorsCreditNote = {
currency_code: '',
entries: [...repeatValue(defaultCreditNoteEntry, MIN_LINES_NUMBER)],
attachments: [],
discount: '',
discount_type: 'amount',
adjustment: '',
};
/**
@@ -207,6 +210,75 @@ export const useVendorCrditNoteTotals = () => {
};
};
/**
* Retrieves the vendor credit subtotal.
* @returns {number}
*/
export const useVendorCreditSubtotal = () => {
const {
values: { entries },
} = useFormikContext();
// Retrieves the invoice entries total.
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
return total;
};
/**
* Retrieves the vendor credit discount amount.
* @returns {number}
*/
export const useVendorCreditDiscountAmount = () => {
const { values } = useFormikContext();
return toSafeNumber(values.discount);
};
/**
* Retrieves the vendor credit adjustment amount.
* @returns {number}
*/
export const useVendorCreditAdjustment = () => {
const { values } = useFormikContext();
return toSafeNumber(values.adjustment);
};
/**
* Retrieves the vendor credit total.
* @returns {number}
*/
export const useVendorCreditTotal = () => {
const subtotal = useVendorCreditSubtotal();
const discountAmount = useVendorCreditDiscountAmount();
const adjustment = useVendorCreditAdjustment();
return subtotal - discountAmount - adjustment;
};
/**
* Retrieves the vendor credit formatted total.
* @returns {string}
*/
export const useVendorCreditFormattedTotal = () => {
const total = useVendorCreditTotal();
const currencyCode = useCurrentOrganizationCurrencyCode();
return formattedAmount(total, currencyCode);
};
/**
* Retrieves the vendor credit formatted subtotal.
* @returns {string}
*/
export const useVendorCreditFormattedSubtotal = () => {
const subtotal = useVendorCreditSubtotal();
const currencyCode = useCurrentOrganizationCurrencyCode();
return formattedAmount(subtotal, currencyCode);
};
/**
* Detarmines whether the vendor note has foreign customer.
* @returns {boolean}