mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: Auto re-calculate the items rate once changing the invoice exchange rate.
This commit is contained in:
@@ -1,102 +1,29 @@
|
||||
// @ts-nocheck
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
import { defaultTo } from 'lodash';
|
||||
import { useQueryTenant } from '../useQueryRequest';
|
||||
import { transformPagination } from '@/utils';
|
||||
import useApiRequest from '../useRequest';
|
||||
import { useQuery } from 'react-query';
|
||||
import QUERY_TYPES from './types';
|
||||
|
||||
const defaultPagination = {
|
||||
pageSize: 20,
|
||||
page: 0,
|
||||
pagesCount: 0,
|
||||
};
|
||||
/**
|
||||
* Creates a new exchange rate.
|
||||
*/
|
||||
export function useCreateExchangeRate(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation((values) => apiRequest.post('exchange_rates', values), {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('EXCHANGES_RATES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
function getRandomItemFromArray(arr) {
|
||||
const randomIndex = Math.floor(Math.random() * arr.length);
|
||||
return arr[randomIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the exchange rate.
|
||||
* Retrieves tax rates.
|
||||
* @param {number} customerId - Customer id.
|
||||
*/
|
||||
export function useEdiExchangeRate(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation(
|
||||
([id, values]) => apiRequest.post(`exchange_rates/${id}`, values),
|
||||
{
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('EXCHANGES_RATES');
|
||||
},
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the exchange rate.
|
||||
*/
|
||||
export function useDeleteExchangeRate(props) {
|
||||
const queryClient = useQueryClient();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation((id) => apiRequest.delete(`exchange_rates/${id}`), {
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries('EXCHANGES_RATES');
|
||||
},
|
||||
...props,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the exchange rate list.
|
||||
*/
|
||||
export function useExchangeRates(query, props) {
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
const states = useQueryTenant(
|
||||
['EXCHANGES_RATES', query],
|
||||
() => apiRequest.get('exchange_rates', { params: query }),
|
||||
{
|
||||
select: (res) => ({
|
||||
exchangesRates: res.data.exchange_rates.results,
|
||||
pagination: transformPagination(res.data.exchange_rates.pagination),
|
||||
filterMeta: res.data.filter_meta,
|
||||
export function useExchangeRate(
|
||||
fromCurrency: string,
|
||||
toCurrency: string,
|
||||
props,
|
||||
) {
|
||||
return useQuery(
|
||||
[QUERY_TYPES.EXCHANGE_RATE, fromCurrency, toCurrency],
|
||||
() =>
|
||||
Promise.resolve({
|
||||
from_currency: fromCurrency,
|
||||
to_currency: toCurrency,
|
||||
exchange_rate: getRandomItemFromArray([4.231, 2.231]),
|
||||
}),
|
||||
...props,
|
||||
},
|
||||
props,
|
||||
);
|
||||
|
||||
return {
|
||||
...states,
|
||||
data: defaultTo(states.data, {
|
||||
exchangesRates: [],
|
||||
pagination: {
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
total: 0,
|
||||
},
|
||||
filterMeta: {},
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
export function useRefreshExchangeRate() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return {
|
||||
refresh: () => {
|
||||
queryClient.invalidateQueries('EXCHANGES_RATES');
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ const FINANCIAL_REPORTS = {
|
||||
REALIZED_GAIN_OR_LOSS: 'REALIZED_GAIN_OR_LOSS',
|
||||
UNREALIZED_GAIN_OR_LOSS: 'UNREALIZED_GAIN_OR_LOSS',
|
||||
PROJECT_PROFITABILITY_SUMMARY: 'PROJECT_PROFITABILITY_SUMMARY',
|
||||
SALES_TAX_LIABILITY_SUMMARY: 'SALES_TAX_LIABILITY_SUMMARY'
|
||||
SALES_TAX_LIABILITY_SUMMARY: 'SALES_TAX_LIABILITY_SUMMARY',
|
||||
};
|
||||
|
||||
const BILLS = {
|
||||
@@ -222,12 +222,17 @@ const DASHBOARD = {
|
||||
};
|
||||
|
||||
const ORGANIZATION = {
|
||||
ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES: 'ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES',
|
||||
ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES:
|
||||
'ORGANIZATION_MUTATE_BASE_CURRENCY_ABILITIES',
|
||||
};
|
||||
|
||||
export const TAX_RATES = {
|
||||
TAX_RATES: 'TAX_RATES',
|
||||
}
|
||||
};
|
||||
|
||||
export const EXCHANGE_RATE = {
|
||||
EXCHANGE_RATE: 'EXCHANGE_RATE',
|
||||
};
|
||||
|
||||
export default {
|
||||
...Authentication,
|
||||
@@ -262,5 +267,6 @@ export default {
|
||||
...BRANCHES,
|
||||
...DASHBOARD,
|
||||
...ORGANIZATION,
|
||||
...TAX_RATES
|
||||
...TAX_RATES,
|
||||
...EXCHANGE_RATE,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user