mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 07:27: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,
|
||||
};
|
||||
|
||||
@@ -188,17 +188,28 @@ export default {
|
||||
|
||||
const accountsBalance = this.$utilities.getAllFilteredAccountsBalance(this.accounts, () => true);
|
||||
let netAssets = 0;
|
||||
let hasUnCalculatedAmount = false;
|
||||
|
||||
for (let i = 0; i < accountsBalance.length; i++) {
|
||||
if (accountsBalance[i].currency === this.defaultCurrency) {
|
||||
netAssets += accountsBalance[i].balance;
|
||||
} else {
|
||||
// TODO: calculate exchange rate
|
||||
netAssets += accountsBalance[i].balance;
|
||||
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
|
||||
|
||||
if (!this.$utilities.isNumber(balance)) {
|
||||
hasUnCalculatedAmount = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
netAssets += Math.floor(balance);
|
||||
}
|
||||
}
|
||||
|
||||
return netAssets;
|
||||
if (hasUnCalculatedAmount) {
|
||||
return netAssets + '+';
|
||||
} else {
|
||||
return netAssets;
|
||||
}
|
||||
},
|
||||
totalAssets() {
|
||||
if (!this.showAccountBalance) {
|
||||
@@ -207,17 +218,28 @@ export default {
|
||||
|
||||
const accountsBalance = this.$utilities.getAllFilteredAccountsBalance(this.accounts, category => category.isAsset);
|
||||
let totalAssets = 0;
|
||||
let hasUnCalculatedAmount = false;
|
||||
|
||||
for (let i = 0; i < accountsBalance.length; i++) {
|
||||
if (accountsBalance[i].currency === this.defaultCurrency) {
|
||||
totalAssets += accountsBalance[i].balance;
|
||||
} else {
|
||||
// TODO: calculate exchange rate
|
||||
totalAssets += accountsBalance[i].balance;
|
||||
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
|
||||
|
||||
if (!this.$utilities.isNumber(balance)) {
|
||||
hasUnCalculatedAmount = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
totalAssets += Math.floor(balance);
|
||||
}
|
||||
}
|
||||
|
||||
return totalAssets;
|
||||
if (hasUnCalculatedAmount) {
|
||||
return totalAssets + '+';
|
||||
} else {
|
||||
return totalAssets;
|
||||
}
|
||||
},
|
||||
totalLiabilities() {
|
||||
if (!this.showAccountBalance) {
|
||||
@@ -226,17 +248,28 @@ export default {
|
||||
|
||||
const accountsBalance = this.$utilities.getAllFilteredAccountsBalance(this.accounts, category => category.isLiability);
|
||||
let totalLiabilities = 0;
|
||||
let hasUnCalculatedAmount = false;
|
||||
|
||||
for (let i = 0; i < accountsBalance.length; i++) {
|
||||
if (accountsBalance[i].currency === this.defaultCurrency) {
|
||||
totalLiabilities += accountsBalance[i].balance;
|
||||
} else {
|
||||
// TODO: calculate exchange rate
|
||||
totalLiabilities += accountsBalance[i].balance;
|
||||
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
|
||||
|
||||
if (!this.$utilities.isNumber(balance)) {
|
||||
hasUnCalculatedAmount = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
totalLiabilities += Math.floor(balance);
|
||||
}
|
||||
}
|
||||
|
||||
return totalLiabilities;
|
||||
if (hasUnCalculatedAmount) {
|
||||
return totalLiabilities + '+';
|
||||
} else {
|
||||
return totalLiabilities;
|
||||
}
|
||||
}
|
||||
},
|
||||
created() {
|
||||
@@ -333,17 +366,28 @@ export default {
|
||||
|
||||
const accountsBalance = this.$utilities.getAllFilteredAccountsBalance(this.accounts, category => category.id === accountCategory.id);
|
||||
let totalBalance = 0;
|
||||
let hasUnCalculatedAmount = false;
|
||||
|
||||
for (let i = 0; i < accountsBalance.length; i++) {
|
||||
if (accountsBalance[i].currency === this.defaultCurrency) {
|
||||
totalBalance += accountsBalance[i].balance;
|
||||
} else {
|
||||
// TODO: calculate exchange rate
|
||||
totalBalance += accountsBalance[i].balance;
|
||||
const balance = this.$exchangeRates.getOtherCurrencyAmount(accountsBalance[i].balance, accountsBalance[i].currency, this.defaultCurrency);
|
||||
|
||||
if (!this.$utilities.isNumber(balance)) {
|
||||
hasUnCalculatedAmount = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
totalBalance += Math.floor(balance);
|
||||
}
|
||||
}
|
||||
|
||||
return totalBalance;
|
||||
if (hasUnCalculatedAmount) {
|
||||
return totalBalance + '+';
|
||||
} else {
|
||||
return totalBalance;
|
||||
}
|
||||
},
|
||||
setSortable() {
|
||||
this.showHidden = true;
|
||||
|
||||
Reference in New Issue
Block a user