mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
support plus account balance which account currency does not equal to default currency
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import utils from './utils.js'
|
||||
|
||||
const exchangeRatesLocalStorageKey = 'lab_exchange_rates';
|
||||
|
||||
function getExchangeRates() {
|
||||
@@ -14,8 +16,39 @@ function clearExchangeRates() {
|
||||
localStorage.removeItem(exchangeRatesLocalStorageKey);
|
||||
}
|
||||
|
||||
function getExchangeRate(fromCurrency, toCurrency) {
|
||||
const exchangeRates = getExchangeRates().exchangeRates;
|
||||
const exchangeRateMap = {};
|
||||
|
||||
for (let i = 0; i < exchangeRates.length; i++) {
|
||||
const exchangeRate = exchangeRates[i];
|
||||
exchangeRateMap[exchangeRate.currency] = exchangeRate;
|
||||
}
|
||||
|
||||
const fromCurrencyExchangeRate = exchangeRateMap[fromCurrency];
|
||||
const toCurrencyExchangeRate = exchangeRateMap[toCurrency];
|
||||
|
||||
if (!fromCurrencyExchangeRate || !toCurrencyExchangeRate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parseFloat(toCurrencyExchangeRate.rate) / parseFloat(fromCurrencyExchangeRate.rate);
|
||||
}
|
||||
|
||||
function getOtherCurrencyAmount(amount, fromCurrency, toCurrency) {
|
||||
const exchangeRate = getExchangeRate(fromCurrency, toCurrency);
|
||||
|
||||
if (!utils.isNumber(exchangeRate)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return amount * exchangeRate;
|
||||
}
|
||||
|
||||
export default {
|
||||
getExchangeRates,
|
||||
setExchangeRates,
|
||||
clearExchangeRates,
|
||||
getExchangeRate,
|
||||
getOtherCurrencyAmount,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user