mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: Auto re-calculate the items rate once changing the invoice exchange rate.
This commit is contained in:
@@ -5,7 +5,7 @@ import intl from 'react-intl-universal';
|
||||
import moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
import { omit, first, sumBy } from 'lodash';
|
||||
import { omit, first, sumBy, round } from 'lodash';
|
||||
import {
|
||||
compose,
|
||||
transformToForm,
|
||||
@@ -57,7 +57,7 @@ export const defaultInvoice = {
|
||||
reference_no: '',
|
||||
invoice_message: '',
|
||||
terms_conditions: '',
|
||||
exchange_rate: 1,
|
||||
exchange_rate: '1',
|
||||
currency_code: '',
|
||||
branch_id: '',
|
||||
warehouse_id: '',
|
||||
@@ -398,3 +398,85 @@ export const useIsInvoiceTaxExclusive = () => {
|
||||
|
||||
return values.inclusive_exclusive_tax === TaxType.Exclusive;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert the given rate to the local currency.
|
||||
* @param {number} rate
|
||||
* @param {number} exchangeRate
|
||||
* @returns {number}
|
||||
*/
|
||||
export const convertToForeignCurrency = (
|
||||
rate: number,
|
||||
exchangeRate: number,
|
||||
) => {
|
||||
return rate * exchangeRate;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts the given rate to the base currency.
|
||||
* @param {number} rate
|
||||
* @param {number} exchangeRate
|
||||
* @returns {number}
|
||||
*/
|
||||
export const covertToBaseCurrency = (rate: number, exchangeRate: number) => {
|
||||
return rate / exchangeRate;
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverts the given rate from the old exchange rate and covert it to the new
|
||||
* currency based on the given new exchange rate.
|
||||
* @param {number} rate -
|
||||
* @param {number} oldExchangeRate - Old exchange rate.
|
||||
* @param {number} newExchangeRate - New exchange rate.
|
||||
* @returns {number}
|
||||
*/
|
||||
const revertAndConvertExchangeRate = (
|
||||
rate: number,
|
||||
oldExchangeRate: number,
|
||||
newExchangeRate: number,
|
||||
) => {
|
||||
const oldValue = convertToForeignCurrency(rate, oldExchangeRate);
|
||||
const newValue = covertToBaseCurrency(oldValue, newExchangeRate);
|
||||
|
||||
return round(newValue, 3);
|
||||
};
|
||||
|
||||
/**
|
||||
* Assign the new item entry rate after converting to the new exchange rate.
|
||||
* @params {number} oldExchangeRate -
|
||||
* @params {number} newExchangeRate -
|
||||
* @params {IItemEntry} entries -
|
||||
*/
|
||||
const assignRateRevertAndCovertExchangeRate = R.curry(
|
||||
(oldExchangeRate: number, newExchangeRate: number, entries: IItemEntry[]) => {
|
||||
return entries.map((entry) => ({
|
||||
...entry,
|
||||
rate: revertAndConvertExchangeRate(
|
||||
entry.rate,
|
||||
oldExchangeRate,
|
||||
newExchangeRate,
|
||||
),
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Compose invoice entries on exchange rate change.
|
||||
* @returns {(oldExchangeRate: number, newExchangeRate: number) => IItemEntry[]}
|
||||
*/
|
||||
export const useInvoiceEntriesOnExchangeRateChange = () => {
|
||||
const {
|
||||
values: { entries },
|
||||
} = useFormikContext();
|
||||
|
||||
return React.useMemo(() => {
|
||||
return R.curry((oldExchangeRate: number, newExchangeRate: number) => {
|
||||
return R.compose(
|
||||
// Updates entries total.
|
||||
updateItemsEntriesTotal,
|
||||
// Assign a new rate of the given new exchange rate from the old exchange rate.
|
||||
assignRateRevertAndCovertExchangeRate(oldExchangeRate, newExchangeRate),
|
||||
)(entries);
|
||||
});
|
||||
}, [entries]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user