code refactor

This commit is contained in:
MaysWind
2021-04-05 22:22:48 +08:00
parent 8fa13d8b2c
commit d43e7fa542
9 changed files with 127 additions and 81 deletions
+75
View File
@@ -6,6 +6,8 @@ import services from '../lib/services.js';
import logger from '../lib/logger.js';
import utils from '../lib/utils.js';
import { getExchangedAmount } from './exchangeRates.js';
import {
LOAD_TRANSACTION_STATISTICS,
INIT_TRANSACTION_STATISTICS_FILTER,
@@ -191,3 +193,76 @@ export function statisticsItemsByTransactionStatisticsData(state) {
items: allDataItems
}
}
export function statisticsItemsByAccountsData(state, getters) {
if (!getters.allPlainAccounts) {
return null;
}
const allDataItems = {};
let totalAmount = 0;
let totalNonNegativeAmount = 0;
for (let i = 0; i < getters.allPlainAccounts.length; i++) {
const account = getters.allPlainAccounts[i];
if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalAssets.type) {
if (!account.isAsset) {
continue;
}
} else if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.AccountTotalLiabilities.type) {
if (!account.isLiability) {
continue;
}
}
if (state.transactionStatisticsFilter.filterAccountIds && state.transactionStatisticsFilter.filterAccountIds[account.id]) {
continue;
}
let primaryAccount = state.allAccountsMap[account.parentId];
if (!primaryAccount) {
primaryAccount = account;
}
let amount = account.balance;
if (account.currency !== getters.currentUserDefaultCurrency) {
amount = Math.floor(getExchangedAmount(state)(amount, account.currency, getters.currentUserDefaultCurrency));
if (!utils.isNumber(amount)) {
continue;
}
}
if (account.isLiability) {
amount = -amount;
}
const data = {
name: account.name,
type: 'account',
id: account.id,
icon: account.icon || iconConstants.defaultAccountIcon.icon,
color: account.color || colorConstants.defaultAccountColor,
hidden: primaryAccount.hidden || account.hidden,
displayOrders: [primaryAccount.category, primaryAccount.displayOrder, account.displayOrder],
totalAmount: amount
};
totalAmount += amount;
if (amount > 0) {
totalNonNegativeAmount += amount;
}
allDataItems[account.id] = data;
}
return {
totalAmount: totalAmount,
totalNonNegativeAmount: totalNonNegativeAmount,
items: allDataItems
}
}