Skip to content

Open source personal finance application

Update stat getter, add getTrns

ilkome9def1b487d15
diff --git a/src/components/stat/StatItemChildCats.vue b/src/components/stat/StatItemChildCats.vue
index ab31d4b2..c42a183e 100644
--- a/src/components/stat/StatItemChildCats.vue
+++ b/src/components/stat/StatItemChildCats.vue
@@ -68,7 +68,7 @@ export default {
.items
template(v-for="category in statCategories")
StatItemChildCatsItem(
- :biggest="$store.getters.stat[typeName].biggest"
+ :biggest="$store.getters.statCurrentPeriod[typeName].biggest"
:category="$store.state.categories.items[category.categoryId]"
:categoryId="category.categoryId"
:key="category.categoryId"
diff --git a/src/components/stat/StatMobile.vue b/src/components/stat/StatMobile.vue
index 0e1aa60f..c108d09c 100644
--- a/src/components/stat/StatMobile.vue
+++ b/src/components/stat/StatMobile.vue
@@ -26,6 +26,11 @@ export default {
activeTabStat () {
return this.$store.state.ui.stat.activeTab
},
+ isEmptyData () {
+ return this.statCurrentPeriod.incomes.categoriesIds.length === 0 &&
+ this.statCurrentPeriod.expenses.categoriesIds.length === 0 &&
+ this.$store.getters.selectedTrnsIdsWithDate.length === 0
+ },
filterCategory () {
return this.$store.state.categories.items[this.$store.state.filter.categoryId]
},
@@ -35,8 +40,8 @@ export default {
filterWallet () {
return this.$store.state.wallets.items[this.$store.state.filter.walletId]
},
- stat () {
- return this.$store.getters.stat
+ statCurrentPeriod () {
+ return this.$store.getters.statCurrentPeriod
}
},
@@ -66,6 +71,7 @@ export default {
:name="filterWallet.name"
icon="mdi mdi-credit-card-multiple"
v-on:onClick="clearWalletFilter")
+
template(v-if="$store.state.filter.categoryId")
template(v-if="filterCategory.parentId !== 0")
FilterItem(
@@ -82,8 +88,8 @@ export default {
//- empty
//------------------------------------------------
EmptyData(
- v-if="stat.incomes.categoriesIds.length === 0 && stat.expenses.categoriesIds.length === 0 && $store.getters.selectedTrnsIdsWithDate.length === 0"
- :text="$lang.stat.empty")
+ v-if="isEmptyData"
+ :text="$lang.statCurrentPeriod.empty")
//- history
//------------------------------------------------
@@ -95,7 +101,7 @@ export default {
div(v-show="activeTabStat === 'stat'")
.stat__content
//- incomes
- .statGroup(v-if="stat.incomes.categoriesIds.length")
+ .statGroup(v-if="statCurrentPeriod.incomes.categoriesIds.length")
.statItem-header
.statItem-total
.statItem-total__title._incomes {{ $lang.money.incomes }}
@@ -103,7 +109,7 @@ export default {
Amount(
:currency="$store.state.currencies.base"
:type="1"
- :value="stat.incomes.total.incomes")
+ :value="statCurrentPeriod.incomes.total")
.statItem-average(v-if="$store.state.stat.showedPeriods > 1")
.statItem-average__title
.statItem-average__title-icon: .mdi.mdi-chart-timeline
@@ -120,17 +126,17 @@ export default {
//- incomes: cats stat item
.stat__cats(v-show="$store.state.ui.statItems === 'visible'")
StatItem(
- v-for="categoryId in stat.incomes.categoriesIds"
- :biggest="stat.incomes.biggest"
+ v-for="categoryId in statCurrentPeriod.incomes.categoriesIds"
+ :biggest="statCurrentPeriod.incomes.biggest"
:category="$store.state.categories.items[categoryId]"
:categoryId="categoryId"
:currency="$store.state.currencies.base"
:key="categoryId"
:type="1"
- :total="stat.categories[categoryId].incomes")
+ :total="statCurrentPeriod.categories[categoryId].incomes")
//- expenses
- .statGroup(v-if="stat.expenses.categoriesIds.length")
+ .statGroup(v-if="statCurrentPeriod.expenses.categoriesIds.length")
.statItem-header
.statItem-total
.statItem-total__title._expenses {{ $lang.money.expenses }}
@@ -138,7 +144,7 @@ export default {
Amount(
:currency="$store.state.currencies.base"
:type="0"
- :value="stat.expenses.total.expenses")
+ :value="statCurrentPeriod.expenses.total")
.statItem-average(v-if="$store.state.stat.showedPeriods > 1")
.statItem-average__title
.statItem-average__title-icon: .mdi.mdi-chart-timeline
@@ -156,14 +162,14 @@ export default {
//- expenses: cats stat item
.stat__cats(v-show="$store.state.ui.statItems === 'visible'")
StatItem(
- v-for="categoryId in stat.expenses.categoriesIds"
- :biggest="stat.expenses.biggest"
+ v-for="categoryId in statCurrentPeriod.expenses.categoriesIds"
+ :biggest="statCurrentPeriod.expenses.biggest"
:category="$store.state.categories.items[categoryId]"
:categoryId="categoryId"
:currency="$store.state.currencies.base"
:key="categoryId"
:type="0"
- :total="stat.categories[categoryId].expenses")
+ :total="statCurrentPeriod.categories[categoryId].expenses")
.stat__trns(v-if="$store.state.ui.statLastTrnsVisibility === 'visible' && $store.getters.selectedTrnsIdsWithDate.length > 0")
.stat__name Last transactions
@@ -182,6 +188,7 @@ export default {
.customize
display none
+
@media $media-phone-sm
display block
padding 0 $m7
diff --git a/src/components/stat/StatPc.vue b/src/components/stat/StatPc.vue
index 9ed7ed84..c32ec479 100644
--- a/src/components/stat/StatPc.vue
+++ b/src/components/stat/StatPc.vue
@@ -1,18 +1,16 @@
<script>
-import Amount from '@/components/amount/Amount'
import StatItem from '@/components/stat/StatItem'
import PeriodCatsChart from '@/components/stat/cats/PeriodCatsChart'
export default {
components: {
- Amount,
PeriodCatsChart,
StatItem
},
computed: {
- stat () {
- return this.$store.getters.stat
+ statCurrentPeriod () {
+ return this.$store.getters.statCurrentPeriod
}
}
}
@@ -21,42 +19,44 @@ export default {
<template lang="pug">
.stat
.stat__content
+ //------------------------------------------------
//- expenses
//------------------------------------------------
.stat__item
- template(v-if="stat.expenses.categoriesIds.length")
+ template(v-if="statCurrentPeriod.expenses.categoriesIds.length")
.stat__chart(v-if="$store.state.ui.catsChart === 'visible'")
PeriodCatsChart(type="expenses")
.stat__cats(v-if="$store.state.ui.statItems === 'visible'")
StatItem(
- v-for="categoryId in stat.expenses.categoriesIds"
- :biggest="stat.expenses.biggest"
+ v-for="categoryId in statCurrentPeriod.expenses.categoriesIds"
+ :biggest="statCurrentPeriod.expenses.biggest"
:category="$store.state.categories.items[categoryId]"
:categoryId="categoryId"
:currency="$store.state.currencies.base"
:type="0"
:key="categoryId"
- :total="stat.categories[categoryId].expenses")
+ :total="statCurrentPeriod.categories[categoryId].expenses")
.stat__empty(v-else) No expenses
+ //------------------------------------------------
//- incomes
//------------------------------------------------
.stat__item
- template(v-if="stat.incomes.categoriesIds.length")
+ template(v-if="statCurrentPeriod.incomes.categoriesIds.length")
.stat__chart(v-if="$store.state.ui.catsChart === 'visible'")
PeriodCatsChart(type="incomes")
.stat__cats(v-if="$store.state.ui.statItems === 'visible'")
StatItem(
- v-for="categoryId in stat.incomes.categoriesIds"
- :biggest="stat.incomes.biggest"
+ v-for="categoryId in statCurrentPeriod.incomes.categoriesIds"
+ :biggest="statCurrentPeriod.incomes.biggest"
:category="$store.state.categories.items[categoryId]"
:categoryId="categoryId"
:currency="$store.state.currencies.base"
:type="1"
:key="categoryId"
- :total="stat.categories[categoryId].incomes")
+ :total="statCurrentPeriod.categories[categoryId].incomes")
.stat__empty(v-else) No incomes
</template>
diff --git a/src/components/stat/StatSummaryMobile.vue b/src/components/stat/StatSummaryMobile.vue
index a54f38ed..63cfafe0 100644
--- a/src/components/stat/StatSummaryMobile.vue
+++ b/src/components/stat/StatSummaryMobile.vue
@@ -8,7 +8,7 @@ export default {
computed: {
stat () {
- return this.$store.getters.stat
+ return this.$store.getters.statCurrentPeriod
},
statAverage () {
return this.$store.getters.statAverage
@@ -30,18 +30,18 @@ export default {
:center="true"
:currency="$store.state.currencies.base"
:type="1"
- :value="stat.incomes.total.incomes")
+ :value="stat.incomes.total")
template(v-else) .
.summary__item._total(@click="$store.dispatch('toogleVisibilityStatItems')")
template(v-if="statAverage.total !== 0")
- template(v-if="stat.incomes.total.incomes > 0 || stat.expenses.total.expenses > 0")
+ template(v-if="stat.incomes.total > 0 || stat.expenses.total > 0")
.summary__item__title {{ $lang.money.total }}
.summary__item__amount
Amount(
:center="true"
:currency="$store.state.currencies.base"
- :value="stat.incomes.total.incomes - stat.expenses.total.expenses")
+ :value="stat.incomes.total - stat.expenses.total")
template(v-else) .
.summary__item._expenses(@click="$store.dispatch('toogleShowStatGraphs')")
@@ -51,7 +51,7 @@ export default {
Amount(
:center="true"
:currency="$store.state.currencies.base"
- :value="stat.expenses.total.expenses"
+ :value="stat.expenses.total"
:type="0")
template(v-else) .
</template>
diff --git a/src/components/stat/StatSummaryPc.vue b/src/components/stat/StatSummaryPc.vue
index e444b530..161b43f1 100644
--- a/src/components/stat/StatSummaryPc.vue
+++ b/src/components/stat/StatSummaryPc.vue
@@ -7,8 +7,8 @@ export default {
},
computed: {
- stat () {
- return this.$store.getters.stat
+ statCurrentPeriod () {
+ return this.$store.getters.statCurrentPeriod
},
statAverage () {
return this.$store.getters.statAverage
@@ -33,7 +33,7 @@ export default {
Amount(
:big="true"
:currency="$store.state.currencies.base"
- :value="stat.expenses.total.expenses"
+ :value="statCurrentPeriod.expenses.total"
:type="0")
.summary__row(v-if="period !== 'all' && statAverage.expenses > 0")
@@ -55,7 +55,7 @@ export default {
Amount(
:big="true"
:currency="$store.state.currencies.base"
- :value="stat.incomes.total.incomes - stat.expenses.total.expenses")
+ :value="statCurrentPeriod.incomes.total - statCurrentPeriod.expenses.total")
template(v-if="period !== 'all' && statAverage.total !== 0")
.summary__row
@@ -77,7 +77,7 @@ export default {
Amount(
:big="true"
:currency="$store.state.currencies.base"
- :value="stat.incomes.total.incomes"
+ :value="statCurrentPeriod.incomes.total"
:type="1")
template(v-if="period !== 'all' && statAverage.incomes !== 0")
diff --git a/src/components/stat/cats/PeriodCatsChart.vue b/src/components/stat/cats/PeriodCatsChart.vue
index 747f4128..6b13ce94 100644
--- a/src/components/stat/cats/PeriodCatsChart.vue
+++ b/src/components/stat/cats/PeriodCatsChart.vue
@@ -11,7 +11,10 @@ export default {
props: {
type: {
type: String,
- required: true
+ required: true,
+ validator: function (value) {
+ return value === 'incomes' || value === 'expenses'
+ }
}
},
@@ -32,13 +35,13 @@ export default {
}
},
biggestAmount () {
- return this.$store.getters.stat[this.type].biggest
+ return this.$store.getters.statCurrentPeriod[this.type].biggest
},
categories () {
return this.$store.state.categories.items
},
- stat () {
- return this.$store.getters.stat
+ statCurrentPeriod () {
+ return this.$store.getters.statCurrentPeriod
}
},
@@ -56,14 +59,14 @@ export default {
<template lang="pug">
.cats-chart(:class="className")
.cats-chart__items(
- v-if="stat[type].categoriesIds.length > 0")
+ v-if="statCurrentPeriod[type].categoriesIds.length > 0")
PeriodCatsChartItem(
- v-for="categoryId in stat[type].categoriesIds"
+ v-for="categoryId in statCurrentPeriod[type].categoriesIds"
:biggest="biggestAmount"
:category="categories[categoryId]"
:categoryId="categoryId"
:key="`charts-${categoryId}`"
- :total="stat.categories[categoryId][type]"
+ :total="statCurrentPeriod.categories[categoryId][type]"
v-on:onActiveCategoryChange="handleActiveCategoryChange")
.cats-chart__popup(
diff --git a/src/components/stat/cats/PeriodCatsChartPopup.vue b/src/components/stat/cats/PeriodCatsChartPopup.vue
index ecf24981..ea2d6ee2 100644
--- a/src/components/stat/cats/PeriodCatsChartPopup.vue
+++ b/src/components/stat/cats/PeriodCatsChartPopup.vue
@@ -23,8 +23,8 @@ export default {
computed: {
amount () {
- if (this.$store.getters.stat.categories[this.categoryId]) {
- return this.$store.getters.stat.categories[this.categoryId][this.type]
+ if (this.$store.getters.statCurrentPeriod.categories[this.categoryId]) {
+ return this.$store.getters.statCurrentPeriod.categories[this.categoryId][this.type]
}
},
categoryName () {
@@ -39,9 +39,10 @@ export default {
<template lang="pug">
.chart-popup(
v-if="categoryId && amount && categoryName"
- :style="{ left: `${offset}px` }"
-)
+ :style="{ left: `${offset}px` }")
+
.chart-popup__name {{ categoryName }}
+
.chart-popup__item
Amount(
:value="amount"
diff --git a/src/components/stat/store/getters.js b/src/components/stat/store/getters.js
index 9ad65687..05a32442 100644
--- a/src/components/stat/store/getters.js
+++ b/src/components/stat/store/getters.js
@@ -1,15 +1,4 @@
import moment from 'moment'
-import orderBy from 'lodash/orderBy'
-
-function sortAndFilterCategories ({ categories, type }) {
- return Object.keys(categories)
- .filter(categoryId => categories[categoryId][type] > 0)
- .sort((a, b) => {
- if (categories[a][type] > categories[b][type]) return -1
- if (categories[a][type] < categories[b][type]) return 1
- return 0
- })
-}
export default {
/**
@@ -76,125 +65,174 @@ export default {
return stat
},
- /**
- * Biggest period value.
- *
- * @returns {Number}
- */
- statByPeriodsBiggestPeriod (state, getters, rootState, rootGetters) {
- const stat = rootGetters.statByPeriods
- if (stat) {
- const statSortedByBiggesValues = orderBy(stat, e => e.incomes > e.expenses ? e.incomes : e.expenses, 'desc')
-
- let biggestValues
- if (statSortedByBiggesValues[0].incomes > statSortedByBiggesValues[0].expenses) {
- biggestValues = statSortedByBiggesValues[0].incomes
- } else {
- biggestValues = statSortedByBiggesValues[0].expenses
+ isFirstPeriodSelected (state, getters, rootState, rootGetters) {
+ if (rootGetters.hasTrns) {
+ // -----------------------------------------------
+ // For future case when period from custom user calendar
+ // rootState.filter.date
+ // -----------------------------------------------
+ // const lastCreatedTrnId = rootGetters.lastCreatedTrnId
+ // const lastCreatedTrnDate = rootState.trns.items[lastCreatedTrnId].date
+ // if (moment(rootState.filter.date).isSame(lastCreatedTrnDate, rootState.filter.period)) {
+ // return true
+ // }
+ if (moment(rootState.filter.date).isSame(moment(), rootState.filter.period)) {
+ return true
}
+ } else {
+ return true
+ }
+ },
- return parseInt(biggestValues)
+ isLastPeriodSelected (state, getters, rootState, rootGetters) {
+ if (rootGetters.hasTrns) {
+ const firstCreatedTrnId = rootGetters.firstCreatedTrnId
+ const firstCreatedTrnDate = rootState.trns.items[firstCreatedTrnId].date
+ if (moment(rootState.filter.date).isSame(firstCreatedTrnDate, rootState.filter.period)) {
+ return true
+ }
+ } else {
+ return true
}
},
- stat (state, getters, rootState, rootGetters) {
- const selectedCategoryId = rootState.filter.categoryId
+ /**
+ * Return root category ID from trnId
+ *
+ * @param {String} trnId
+ * @return {String} root categoryId
+ */
+ getRootCategoryIdFromTrnId: (state, getters, rootState) => (trnId) => {
const trns = rootState.trns.items
const categories = rootState.categories.items
+ const trnCategoryId = trns[trnId].categoryId
+ const trnCategoryParentId = categories[trnCategoryId].parentId
+ return trnCategoryParentId || trnCategoryId
+ },
+
+ /**
+ * Create categories with trnsIds from list of trnsIds
+ *
+ * @param {Array} trnsIds
+ * @return {Object} categoryId - categoryId with trnsIDs
+ * @return {Array} categoryId[] - trnsIds
+ * @return {String} categoryId[] - trnId
+ * { categiryId: [...trnId, trnId] }
+ */
+ getCategoriesIdsWithTrnsIds: (state, getters, rootState, rootGetters) => ({ trnsIds }) => {
+ const filterCategoryId = rootState.filter.categoryId
+ const trns = rootState.trns.items
const transferCategoryId = rootGetters.transferCategoryId
- const categoriesWithTrnsIds = {}
-
- // Selected trns
- if (rootGetters.selectedTrnsIdsWithDate.length) {
- for (const trnId of rootGetters.selectedTrnsIdsWithDate) {
- if (trns[trnId]) {
- const trnCategoryId = trns[trnId].categoryId
- if (!trnCategoryId) break
- if (!categories[trnCategoryId]) break
-
- // Get root category
- let trnCategoryParentId = categories[trnCategoryId].parentId
- let categoryId = trnCategoryParentId || trnCategoryId
- if (selectedCategoryId) categoryId = trnCategoryId
-
- // Push trns ids to categories object. Exclude transfer category
- if (categoryId !== transferCategoryId) {
- if (!categoriesWithTrnsIds[categoryId]) {
- categoriesWithTrnsIds[categoryId] = [trnId]
- } else {
- categoriesWithTrnsIds[categoryId].push(trnId)
- }
+ let categoriesWithTrnsIds = {}
+
+ for (const trnId of trnsIds) {
+ if (trns[trnId]) {
+ let categoryId
+ filterCategoryId
+ ? categoryId = trns[trnId].categoryId
+ : categoryId = rootGetters.getRootCategoryIdFromTrnId(trnId)
+
+ // Push trnId to categoriey. Exclude transfer category
+ if (categoryId !== transferCategoryId) {
+ if (!categoriesWithTrnsIds[categoryId]) {
+ categoriesWithTrnsIds[categoryId] = [trnId]
+ } else {
+ categoriesWithTrnsIds[categoryId].push(trnId)
}
}
}
}
+ return categoriesWithTrnsIds
+ },
+
+ /**
+ * Stat by periods with period name and total.
+ *
+ * @return {Object}
+ * @return {Object} categories
+ * @return {Number} expensesBiggest
+ * @return {Array} expensesCategoriesIds - categories ids
+ * @return {Number} incomesBiggest
+ * @return {Array} incomesCategoriesIds - categories ids
+ * @return {Object} total
+ * @return {Number} total.expenses
+ * @return {Number} total.incomes
+ * @return {Number} total.total
+ *
+ */
+ getStat: (state, getters, rootState, rootGetters) => ({ date }) => {
+ const selectedTrns = rootGetters.getTrns({ date })
+ const categoriesWithTrnsIds = rootGetters.getCategoriesIdsWithTrnsIds({ trnsIds: selectedTrns })
+ const totalAllTrns = rootGetters.getTotalOfTrnsIds(selectedTrns)
+
+ // count total in categories
const categoriesTotal = {}
for (const categoryId in categoriesWithTrnsIds) {
- const trnsIds = categoriesWithTrnsIds[categoryId]
- categoriesTotal[categoryId] = rootGetters.getTotalOfTrnsIds(trnsIds)
+ categoriesTotal[categoryId] = rootGetters.getTotalOfTrnsIds(categoriesWithTrnsIds[categoryId])
}
- const sortedExpensesCategoriesIdsBySum = sortAndFilterCategories({
- categories: categoriesTotal, type: 'expenses'
- })
- const sortedIncomesCategoriesIdsBySum = sortAndFilterCategories({
- categories: categoriesTotal, type: 'incomes'
- })
+ // separate catgories by incomes and expenses
+ const statIncomes = {}
+ const statExpenses = {}
+ for (const categoryId in categoriesWithTrnsIds) {
+ const total = categoriesTotal[categoryId]
+ if (total.incomes > 0) statIncomes[categoryId] = total
+ if (total.expenses > 0) statExpenses[categoryId] = total
+ }
- const sortedIncomesCategories = {}
- for (const categoryId of sortedExpensesCategoriesIdsBySum) {
- sortedIncomesCategories[categoryId] = categoriesTotal[categoryId]
+ // sort categories amount
+ function sortCategoriesByTotal (categories, typeName) {
+ const categoriesIds = Object.keys(categories)
+ let sortedCategoriesIds = []
+
+ if (categoriesIds.length) {
+ sortedCategoriesIds = categoriesIds.sort((a, b) => {
+ if (categories[a][typeName] > categories[b][typeName]) return -1
+ if (categories[a][typeName] < categories[b][typeName]) return 1
+ return 0
+ })
+ }
+
+ return sortedCategoriesIds
}
- const sortedExpensesCategories = {}
- for (const categoryId of sortedIncomesCategoriesIdsBySum) {
- sortedExpensesCategories[categoryId] = categoriesTotal[categoryId]
+ // sorted
+ const incomesCategoriesIds = sortCategoriesByTotal(statIncomes, 'incomes')
+ const expensesCategoriesIds = sortCategoriesByTotal(statExpenses, 'expenses')
+
+ // get first item in sorted catgories
+ function getBiggestAmount (categoriesTotal, categoriesIds, typeName) {
+ const biggestAmount = categoriesIds[0]
+ return categoriesTotal[biggestAmount] && Math.abs(categoriesTotal[biggestAmount][typeName])
}
- return {
+ // biggest
+ const expensesBiggest = getBiggestAmount(categoriesTotal, expensesCategoriesIds, 'expenses')
+ const incomesBiggest = getBiggestAmount(categoriesTotal, incomesCategoriesIds, 'incomes')
+
+ const stat = {
categories: categoriesTotal,
+ total: totalAllTrns.total,
expenses: {
- biggest: categoriesTotal[sortedExpensesCategoriesIdsBySum[0]] && Math.abs(categoriesTotal[sortedExpensesCategoriesIdsBySum[0]].expenses),
- categoriesIds: sortedExpensesCategoriesIdsBySum,
- total: rootGetters.getTotalOfTrnsIds(rootGetters.selectedTrnsIdsWithDate.filter(trnId => rootState.trns.items[trnId].type === 0))
+ biggest: expensesBiggest,
+ categoriesIds: expensesCategoriesIds,
+ total: totalAllTrns.expenses
},
incomes: {
- biggest: categoriesTotal[sortedIncomesCategoriesIdsBySum[0]] && Math.abs(categoriesTotal[sortedIncomesCategoriesIdsBySum[0]].incomes),
- categoriesIds: sortedIncomesCategoriesIdsBySum,
- total: rootGetters.getTotalOfTrnsIds(rootGetters.selectedTrnsIdsWithDate.filter(trnId => rootState.trns.items[trnId].type === 1))
+ biggest: incomesBiggest,
+ categoriesIds: incomesCategoriesIds,
+ total: totalAllTrns.incomes
}
}
- },
- isFirstPeriodSelected (state, getters, rootState, rootGetters) {
- if (rootGetters.hasTrns) {
- // -----------------------------------------------
- // For future case when period from custom user calendar
- // rootState.filter.date
- // -----------------------------------------------
- // const lastCreatedTrnId = rootGetters.lastCreatedTrnId
- // const lastCreatedTrnDate = rootState.trns.items[lastCreatedTrnId].date
- // if (moment(rootState.filter.date).isSame(lastCreatedTrnDate, rootState.filter.period)) {
- // return true
- // }
- if (moment(rootState.filter.date).isSame(moment(), rootState.filter.period)) {
- return true
- }
- } else {
- return true
- }
+ return stat
},
- isLastPeriodSelected (state, getters, rootState, rootGetters) {
- if (rootGetters.hasTrns) {
- const firstCreatedTrnId = rootGetters.firstCreatedTrnId
- const firstCreatedTrnDate = rootState.trns.items[firstCreatedTrnId].date
- if (moment(rootState.filter.date).isSame(firstCreatedTrnDate, rootState.filter.period)) {
- return true
- }
- } else {
- return true
- }
+
+ statCurrentPeriod (state, getters, rootState, rootGetters) {
+ const date = rootState.filter.date
+ const stat = rootGetters.getStat({ date })
+ return stat
}
}
diff --git a/src/components/trns/store/getters.js b/src/components/trns/store/getters.js
index 0e4f6cf9..9dc923da 100644
--- a/src/components/trns/store/getters.js
+++ b/src/components/trns/store/getters.js
@@ -17,20 +17,28 @@ export default {
return false
},
- // getTotalOfTrnsIds
- getTotalOfTrnsIds: (state, getters, rootState, rootGetters) => (trnsIds) => {
+ /**
+ * Return total amounts of trnsIds
+ *
+ * @param {Array} trnsIds
+ * @return {Object} return
+ * @return {String} return.expenses
+ * @return {String} return.incomes
+ * @return {String} return.total
+ */
+ getTotalOfTrnsIds: (state, getters, rootState, rootGetters) => (trnsIds, inculdeTrnasfers = false) => {
const trns = rootState.trns.items
const currencies = rootState.currencies.items
const wallets = rootState.wallets.items
const baseCurrency = rootState.currencies.base
const transferCategoryId = rootGetters.transferCategoryId
- let incomes = 0
let expenses = 0
+ let incomes = 0
for (const key of trnsIds) {
const trn = trns[key]
- if (trn && (trn.categoryId !== transferCategoryId)) {
+ if (trn && (inculdeTrnasfers || trn.categoryId !== transferCategoryId)) {
const wallet = wallets[trn.accountId]
if (wallet && currencies) {
let amount = 0
@@ -45,8 +53,8 @@ export default {
}
}
return {
- incomes: Math.abs(incomes),
expenses: Math.abs(expenses),
+ incomes: Math.abs(incomes),
total: parseInt(incomes - expenses)
}
},
@@ -190,6 +198,57 @@ export default {
if (categoryId) trnsIds = trnsIds.filter(id => trns[id].categoryId === categoryId)
if (type) trnsIds = trnsIds.filter(id => trns[id].type === type)
+ return trnsIds
+ },
+
+ getTrns: (state, getters, rootState, rootGetters) => ({ date, periodName }) => {
+ if (!rootGetters.hasTrns) return []
+
+ const categories = rootState.categories.items
+ const categoriesIds = Object.keys(categories)
+ const filterCategoryId = rootState.filter.categoryId
+ const filterWalletId = rootState.filter.walletId
+ const trns = rootState.trns.items
+ let trnsIds = Object.keys(trns)
+
+ const filterDate = moment(date)
+ const filterPeriod = periodName || rootState.filter.period
+ const startDateValue = filterDate.startOf(filterPeriod).valueOf()
+ const endDateValue = filterDate.endOf(filterPeriod).valueOf()
+
+ // filter date
+ if (filterPeriod !== 'all') {
+ trnsIds = trnsIds
+ .filter(trnId => (trns[trnId].date >= startDateValue) && (trns[trnId].date <= endDateValue))
+ }
+
+ // filter wallet
+ if (filterWalletId) {
+ trnsIds = trnsIds.filter(trnId => trns[trnId].accountId === filterWalletId)
+ }
+
+ // filter category
+ if (filterCategoryId) {
+ const childCategoriesIds = categoriesIds.filter(id => categories[id].parentId === filterCategoryId)
+ if (childCategoriesIds.length) {
+ trnsIds = trnsIds.filter(trnId => {
+ const trnCategoryId = trns[trnId].categoryId
+ for (const categoryId of childCategoriesIds) {
+ if (trnCategoryId === categoryId) return true
+ }
+ })
+ } else {
+ trnsIds = trnsIds.filter(trnId => trns[trnId].categoryId === filterCategoryId)
+ }
+ }
+
+ trnsIds = trnsIds
+ .sort((a, b) => {
+ if (trns[a].date > trns[b].date) return -1
+ if (trns[a].date < trns[b].date) return 1
+ return 0
+ })
+
return trnsIds
}
}