code refactor

This commit is contained in:
MaysWind
2021-04-05 18:47:05 +08:00
parent d8b6091dcc
commit 8fa13d8b2c
3 changed files with 145 additions and 131 deletions
+5 -1
View File
@@ -99,7 +99,8 @@ import {
import {
loadTransactionStatistics,
initTransactionStatisticsFilter,
updateTransactionStatisticsFilter
updateTransactionStatisticsFilter,
statisticsItemsByTransactionStatisticsData
} from './statistics.js';
import {
@@ -201,6 +202,9 @@ const stores = {
exchangeRatesLastUpdateTime,
getExchangedAmount,
// statistics
statisticsItemsByTransactionStatisticsData,
// account
allPlainAccounts,
allVisiblePlainAccounts,
+139
View File
@@ -1,5 +1,10 @@
import statisticsConstants from '../consts/statistics.js';
import categoryConstants from '../consts/category.js';
import iconConstants from '../consts/icon.js';
import colorConstants from '../consts/color.js';
import services from '../lib/services.js';
import logger from '../lib/logger.js';
import utils from '../lib/utils.js';
import {
LOAD_TRANSACTION_STATISTICS,
@@ -52,3 +57,137 @@ export function initTransactionStatisticsFilter(context, filter) {
export function updateTransactionStatisticsFilter(context, filter) {
context.commit(UPDATE_TRANSACTION_STATISTICS_FILTER, filter);
}
export function statisticsItemsByTransactionStatisticsData(state) {
if (!state.transactionStatistics || !state.transactionStatistics.items) {
return null;
}
const allDataItems = {};
let totalAmount = 0;
let totalNonNegativeAmount = 0;
for (let i = 0; i < state.transactionStatistics.items.length; i++) {
const item = state.transactionStatistics.items[i];
if (!item.primaryAccount || !item.account || !item.primaryCategory || !item.category) {
continue;
}
if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByAccount.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByPrimaryCategory.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseBySecondaryCategory.type) {
if (item.category.type !== categoryConstants.allCategoryTypes.Expense) {
continue;
}
} else if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByAccount.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByPrimaryCategory.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeBySecondaryCategory.type) {
if (item.category.type !== categoryConstants.allCategoryTypes.Income) {
continue;
}
} else {
continue;
}
if (state.transactionStatisticsFilter.filterAccountIds && state.transactionStatisticsFilter.filterAccountIds[item.account.id]) {
continue;
}
if (state.transactionStatisticsFilter.filterCategoryIds && state.transactionStatisticsFilter.filterCategoryIds[item.category.id]) {
continue;
}
if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByAccount.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByAccount.type) {
if (utils.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.account.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.account.name,
type: 'account',
id: item.account.id,
icon: item.account.icon || iconConstants.defaultAccountIcon.icon,
color: item.account.color || colorConstants.defaultAccountColor,
hidden: item.primaryAccount.hidden || item.account.hidden,
displayOrders: [item.primaryAccount.category, item.primaryAccount.displayOrder, item.account.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.account.id] = data;
}
} else if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseByPrimaryCategory.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeByPrimaryCategory.type) {
if (utils.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.primaryCategory.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.primaryCategory.name,
type: 'category',
id: item.primaryCategory.id,
icon: item.primaryCategory.icon || iconConstants.defaultCategoryIcon.icon,
color: item.primaryCategory.color || colorConstants.defaultCategoryColor,
hidden: item.primaryCategory.hidden,
displayOrders: [item.primaryCategory.type, item.primaryCategory.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.primaryCategory.id] = data;
}
} else if (state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.ExpenseBySecondaryCategory.type ||
state.transactionStatisticsFilter.chartDataType === statisticsConstants.allChartDataTypes.IncomeBySecondaryCategory.type) {
if (utils.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.category.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.category.name,
type: 'category',
id: item.category.id,
icon: item.category.icon || iconConstants.defaultCategoryIcon.icon,
color: item.category.color || colorConstants.defaultCategoryColor,
hidden: item.primaryCategory.hidden || item.category.hidden,
displayOrders: [item.primaryCategory.type, item.primaryCategory.displayOrder, item.category.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.category.id] = data;
}
}
}
return {
totalAmount: totalAmount,
totalNonNegativeAmount: totalNonNegativeAmount,
items: allDataItems
}
}
+1 -130
View File
@@ -306,7 +306,7 @@ export default {
self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByAccount.type ||
self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type ||
self.query.chartDataType === self.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) {
combinedData = this.getDataItemsByTransactions(self.$store.state.transactionStatistics);
combinedData = this.$store.getters.statisticsItemsByTransactionStatisticsData;
} else if (self.query.chartDataType === self.$constants.statistics.allChartDataTypes.AccountTotalAssets.type ||
self.query.chartDataType === self.$constants.statistics.allChartDataTypes.AccountTotalLiabilities.type) {
combinedData = this.getDataItemsByAccounts(self.$store.getters.allPlainAccounts);
@@ -598,135 +598,6 @@ export default {
settings() {
this.$f7router.navigate('/statistic/settings');
},
getDataItemsByTransactions(transactionStatistics) {
const allDataItems = {};
let totalAmount = 0;
let totalNonNegativeAmount = 0;
for (let i = 0; i < transactionStatistics.items.length; i++) {
const item = transactionStatistics.items[i];
if (!item.primaryAccount || !item.account || !item.primaryCategory || !item.category) {
continue;
}
if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseByAccount.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory.type) {
if (item.category.type !== this.$constants.category.allCategoryTypes.Expense) {
continue;
}
} else if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeByAccount.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) {
if (item.category.type !== this.$constants.category.allCategoryTypes.Income) {
continue;
}
} else {
continue;
}
if (this.query.filterAccountIds && this.query.filterAccountIds[item.account.id]) {
continue;
}
if (this.query.filterCategoryIds && this.query.filterCategoryIds[item.category.id]) {
continue;
}
if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseByAccount.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeByAccount.type) {
if (this.$utilities.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.account.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.account.name,
type: 'account',
id: item.account.id,
icon: item.account.icon || this.$constants.icons.defaultAccountIcon.icon,
color: item.account.color || this.$constants.colors.defaultAccountColor,
hidden: item.primaryAccount.hidden || item.account.hidden,
displayOrders: [item.primaryAccount.category, item.primaryAccount.displayOrder, item.account.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.account.id] = data;
}
} else if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseByPrimaryCategory.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeByPrimaryCategory.type) {
if (this.$utilities.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.primaryCategory.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.primaryCategory.name,
type: 'category',
id: item.primaryCategory.id,
icon: item.primaryCategory.icon || this.$constants.icons.defaultCategoryIcon.icon,
color: item.primaryCategory.color || this.$constants.colors.defaultCategoryColor,
hidden: item.primaryCategory.hidden,
displayOrders: [item.primaryCategory.type, item.primaryCategory.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.primaryCategory.id] = data;
}
} else if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.ExpenseBySecondaryCategory.type ||
this.query.chartDataType === this.$constants.statistics.allChartDataTypes.IncomeBySecondaryCategory.type) {
if (this.$utilities.isNumber(item.amountInDefaultCurrency)) {
let data = allDataItems[item.category.id];
if (data) {
data.totalAmount += item.amountInDefaultCurrency;
} else {
data = {
name: item.category.name,
type: 'category',
id: item.category.id,
icon: item.category.icon || this.$constants.icons.defaultCategoryIcon.icon,
color: item.category.color || this.$constants.colors.defaultCategoryColor,
hidden: item.primaryCategory.hidden || item.category.hidden,
displayOrders: [item.primaryCategory.type, item.primaryCategory.displayOrder, item.category.displayOrder],
totalAmount: item.amountInDefaultCurrency
}
}
totalAmount += item.amountInDefaultCurrency;
if (item.amountInDefaultCurrency > 0) {
totalNonNegativeAmount += item.amountInDefaultCurrency;
}
allDataItems[item.category.id] = data;
}
}
}
return {
totalAmount: totalAmount,
totalNonNegativeAmount: totalNonNegativeAmount,
items: allDataItems
}
},
getDataItemsByAccounts(accounts) {
const allDataItems = {};
let totalAmount = 0;