fix CategoriesStats: compute mensuel average

This commit is contained in:
Simon Vieille 2025-02-09 19:53:59 +01:00
commit a5092f9064
3 changed files with 38 additions and 4 deletions

View file

@ -1,5 +1,18 @@
import {isInRange} from '../lib/dateFilter'
const getDate = (value) => {
const d = new Date(value)
let month = d.getUTCMonth() + 1
const year = d.getUTCFullYear()
if (month < 10) {
month = `0${month}`
}
return `${month}/${year}`
}
const compute = (transactions, dateFrom, dateTo, order) => {
const emptyCategory = {
id: -1,
@ -27,6 +40,8 @@ const compute = (transactions, dateFrom, dateTo, order) => {
data[category.label] = {
category: category,
average: 0,
monthAverage: 0,
months: {},
count: 0,
sum: 0,
}
@ -34,10 +49,22 @@ const compute = (transactions, dateFrom, dateTo, order) => {
++data[category.label].count
data[category.label].sum += transaction.debit
const date = getDate(transaction.date)
if (!Object.prototype.hasOwnProperty.call(data[category.label].months, date)) {
data[category.label].months[date] = 0
}
data[category.label].months[date] += transaction.debit
})
for (let i in data) {
data[i].average = data[i].sum / data[i].count
const monthsValues = Object.values(data[i].months)
data[i].monthAverage = monthsValues.reduce((a, b) => a + b, 0) / monthsValues.length
}
data = Object.values(data).sort((a, b) => {

View file

@ -37,7 +37,7 @@ const compute = (transactions, dateFrom, dateTo) => {
return
}
let date = getDate(transaction.date)
const date = getDate(transaction.date)
if (!Object.prototype.hasOwnProperty.call(indexes, date)) {
indexes[date] = labels.length

View file

@ -21,6 +21,11 @@
class="text-end"
>Somme totale</BTh
>
<BTh
width="200px"
class="text-end"
>Montant moyen</BTh
>
<BTh
width="200px"
class="text-end"
@ -29,7 +34,7 @@
<BTh
width="170px"
class="text-end"
>Quantité</BTh
>Transactions</BTh
>
</BTr>
</BThead>
@ -44,6 +49,7 @@
></BTd>
<BTd class="text-end">{{ renderEuro(item.sum) }}</BTd>
<BTd class="text-end">{{ renderEuro(item.average) }}</BTd>
<BTd class="text-end">{{ renderEuro(item.monthAverage) }}</BTd>
<BTd class="text-end">{{ item.count }}</BTd>
</BTr>
</BTbody>
@ -75,8 +81,9 @@ import {renderCategory, renderEuro} from '../../lib/renderers'
const order = ref(getStorage('dashboard:debitAverage:order', 'sum'))
const orders = [
{value: 'sum', text: 'Somme total'},
{value: 'average', text: 'Moyenne mensuelle'},
{value: 'count', text: 'Quantité'},
{value: 'average', text: 'Montant moyen'},
{value: 'monthAverage', text: 'Moyenne mensuelle'},
{value: 'count', text: 'Transactions'},
]
watch(order, (v) => saveStorage('dashboard:debitAverage:order', v))