code refactor
This commit is contained in:
@@ -629,7 +629,6 @@ const allCurrencies = {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const allCurrencyDisplayModes = {
|
||||
None: 0,
|
||||
Symbol: 1,
|
||||
@@ -637,12 +636,14 @@ const allCurrencyDisplayModes = {
|
||||
Name: 3
|
||||
};
|
||||
|
||||
const defaultCurrency = allCurrencies.USD.code;
|
||||
const defaultCurrencyDisplayMode = allCurrencyDisplayModes.Symbol;
|
||||
|
||||
export default {
|
||||
parentAccountCurrencyPlaceholder: parentAccountCurrencyPlaceholder,
|
||||
defaultCurrencySymbol: defaultCurrencySymbol,
|
||||
all: allCurrencies,
|
||||
defaultCurrency: defaultCurrency,
|
||||
allCurrencyDisplayModes: allCurrencyDisplayModes,
|
||||
defaultCurrencyDisplayMode: defaultCurrencyDisplayMode
|
||||
};
|
||||
|
||||
@@ -80,7 +80,10 @@ const allDateRanges = {
|
||||
}
|
||||
};
|
||||
|
||||
const defaultFirstDayOfWeek = allWeekDays.Sunday.type;
|
||||
|
||||
export default {
|
||||
allWeekDays: allWeekDays,
|
||||
allDateRanges: allDateRanges,
|
||||
defaultFirstDayOfWeek: defaultFirstDayOfWeek
|
||||
};
|
||||
|
||||
@@ -209,6 +209,17 @@ Vue.prototype.$locale = {
|
||||
moment.locale(locale);
|
||||
services.setLocale(locale);
|
||||
document.querySelector('html').setAttribute('lang', locale);
|
||||
|
||||
const defaultCurrency = i18n.t('default.currency');
|
||||
const defaultFirstDayOfWeekName = i18n.t('default.firstDayOfWeek');
|
||||
let defaultFirstDayOfWeek = datetime.defaultFirstDayOfWeek;
|
||||
|
||||
if (datetime.allWeekDays[defaultFirstDayOfWeekName]) {
|
||||
defaultFirstDayOfWeek = datetime.allWeekDays[defaultFirstDayOfWeekName].type;
|
||||
}
|
||||
|
||||
store.dispatch('updateLocalizedDefaultSettings', { defaultCurrency, defaultFirstDayOfWeek });
|
||||
|
||||
return locale;
|
||||
},
|
||||
getTimezone: function () {
|
||||
|
||||
+21
-1
@@ -1,4 +1,5 @@
|
||||
import datetimeConstants from '../consts/datetime.js';
|
||||
import currencyConstants from '../consts/currency.js';
|
||||
import statisticsConstants from '../consts/statistics.js';
|
||||
import userState from '../lib/userstate.js';
|
||||
import settings from '../lib/settings.js';
|
||||
@@ -7,6 +8,8 @@ import utils from '../lib/utils.js';
|
||||
import {
|
||||
RESET_STATE,
|
||||
|
||||
UPDATE_DEFAULT_SETTING,
|
||||
|
||||
STORE_USER_INFO,
|
||||
CLEAR_USER_INFO,
|
||||
|
||||
@@ -53,6 +56,10 @@ import {
|
||||
UPDATE_TRANSACTION_STATISTICS_INVALID_STATE,
|
||||
} from './mutations.js';
|
||||
|
||||
import {
|
||||
updateLocalizedDefaultSettings
|
||||
} from './setting.js';
|
||||
|
||||
import {
|
||||
authorize,
|
||||
authorize2FA,
|
||||
@@ -100,7 +107,8 @@ import {
|
||||
loadTransactionStatistics,
|
||||
initTransactionStatisticsFilter,
|
||||
updateTransactionStatisticsFilter,
|
||||
statisticsItemsByTransactionStatisticsData
|
||||
statisticsItemsByTransactionStatisticsData,
|
||||
statisticsItemsByAccountsData,
|
||||
} from './statistics.js';
|
||||
|
||||
import {
|
||||
@@ -154,6 +162,10 @@ import {
|
||||
const stores = {
|
||||
strict: !settings.isProduction(),
|
||||
state: {
|
||||
defaultSetting: {
|
||||
currency: currencyConstants.defaultCurrency,
|
||||
firstDayOfWeek: datetimeConstants.defaultFirstDayOfWeek
|
||||
},
|
||||
currentUserInfo: userState.getUserInfo(),
|
||||
latestExchangeRates: getExchangeRatesFromLocalStorage(),
|
||||
allAccounts: [],
|
||||
@@ -204,6 +216,7 @@ const stores = {
|
||||
|
||||
// statistics
|
||||
statisticsItemsByTransactionStatisticsData,
|
||||
statisticsItemsByAccountsData,
|
||||
|
||||
// account
|
||||
allPlainAccounts,
|
||||
@@ -258,6 +271,10 @@ const stores = {
|
||||
|
||||
clearExchangeRatesFromLocalStorage();
|
||||
},
|
||||
[UPDATE_DEFAULT_SETTING] (state, { defaultCurrency, defaultFirstDayOfWeek }) {
|
||||
state.defaultSetting.currency = defaultCurrency;
|
||||
state.defaultSetting.firstDayOfWeek = defaultFirstDayOfWeek;
|
||||
},
|
||||
[STORE_USER_INFO] (state, userInfo) {
|
||||
state.currentUserInfo = userInfo;
|
||||
userState.updateUserInfo(userInfo);
|
||||
@@ -900,6 +917,9 @@ const stores = {
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
// setting
|
||||
updateLocalizedDefaultSettings,
|
||||
|
||||
// user
|
||||
authorize,
|
||||
authorize2FA,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
export const RESET_STATE = 'RESET_STATE';
|
||||
|
||||
export const UPDATE_DEFAULT_SETTING = 'UPDATE_DEFAULT_SETTING';
|
||||
|
||||
export const STORE_USER_INFO = 'STORE_USER_INFO';
|
||||
export const CLEAR_USER_INFO = 'CLEAR_USER_INFO';
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import {
|
||||
UPDATE_DEFAULT_SETTING
|
||||
} from './mutations.js';
|
||||
|
||||
export function updateLocalizedDefaultSettings(context, { defaultCurrency, defaultFirstDayOfWeek }) {
|
||||
context.commit(UPDATE_DEFAULT_SETTING, {
|
||||
defaultCurrency,
|
||||
defaultFirstDayOfWeek,
|
||||
});
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -343,10 +343,10 @@ export function currentUserNickname(state) {
|
||||
|
||||
export function currentUserDefaultCurrency(state) {
|
||||
const userInfo = state.currentUserInfo || {};
|
||||
return userInfo.defaultCurrency || null;
|
||||
return userInfo.defaultCurrency || state.defaultSetting.currency;
|
||||
}
|
||||
|
||||
export function currentUserFirstDayOfWeek(state) {
|
||||
const userInfo = state.currentUserInfo || {};
|
||||
return utils.isNumber(userInfo.firstDayOfWeek) ? userInfo.firstDayOfWeek : null;
|
||||
return utils.isNumber(userInfo.firstDayOfWeek) ? userInfo.firstDayOfWeek : state.defaultSetting.firstDayOfWeek;
|
||||
}
|
||||
|
||||
@@ -263,14 +263,6 @@ export default {
|
||||
},
|
||||
computed: {
|
||||
defaultCurrency() {
|
||||
if (this.query.accountId && this.query.accountId !== '0') {
|
||||
const account = this.allAccounts[this.query.accountId];
|
||||
|
||||
if (account && account.currency && account.currency !== this.$constants.currency.parentAccountCurrencyPlaceholder) {
|
||||
return account.currency;
|
||||
}
|
||||
}
|
||||
|
||||
return this.$store.getters.currentUserDefaultCurrency || this.$t('default.currency');
|
||||
},
|
||||
firstDayOfWeek() {
|
||||
@@ -309,7 +301,7 @@ export default {
|
||||
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);
|
||||
combinedData = this.$store.getters.statisticsItemsByAccountsData;
|
||||
}
|
||||
|
||||
const allStatisticsItems = [];
|
||||
@@ -598,74 +590,6 @@ export default {
|
||||
settings() {
|
||||
this.$f7router.navigate('/statistic/settings');
|
||||
},
|
||||
getDataItemsByAccounts(accounts) {
|
||||
const allDataItems = {};
|
||||
let totalAmount = 0;
|
||||
let totalNonNegativeAmount = 0;
|
||||
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
const account = accounts[i];
|
||||
|
||||
if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.AccountTotalAssets.type) {
|
||||
if (!account.isAsset) {
|
||||
continue;
|
||||
}
|
||||
} else if (this.query.chartDataType === this.$constants.statistics.allChartDataTypes.AccountTotalLiabilities.type) {
|
||||
if (!account.isLiability) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.query.filterAccountIds && this.query.filterAccountIds[account.id]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let primaryAccount = this.$store.state.allAccountsMap[account.parentId];
|
||||
|
||||
if (!primaryAccount) {
|
||||
primaryAccount = account;
|
||||
}
|
||||
|
||||
let amount = account.balance;
|
||||
|
||||
if (account.currency !== this.defaultCurrency) {
|
||||
amount = Math.floor(this.$store.getters.getExchangedAmount(amount, account.currency, this.defaultCurrency));
|
||||
|
||||
if (!this.$utilities.isNumber(amount)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (account.isLiability) {
|
||||
amount = -amount;
|
||||
}
|
||||
|
||||
const data = {
|
||||
name: account.name,
|
||||
type: 'account',
|
||||
id: account.id,
|
||||
icon: account.icon || self.$constants.icons.defaultAccountIcon.icon,
|
||||
color: account.color || self.$constants.colors.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
|
||||
}
|
||||
},
|
||||
scrollPopoverToSelectedItem(event) {
|
||||
if (!event || !event.$el || !event.$el.length) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user