82 lines
1.6 KiB
JavaScript
82 lines
1.6 KiB
JavaScript
import { isInRange } from '../lib/dateFilter'
|
|
|
|
const getDate = (value, precision) => {
|
|
const d = new Date(value)
|
|
|
|
let day = d.getUTCDate()
|
|
let month = d.getUTCMonth() + 1
|
|
const year = d.getUTCFullYear()
|
|
|
|
if (precision === '2weeks') {
|
|
day = day < 16 ? 1 : 16
|
|
}
|
|
|
|
if (day < 10) {
|
|
day = `0${day}`
|
|
}
|
|
|
|
if (month < 10) {
|
|
month = `0${month}`
|
|
}
|
|
|
|
if (precision === 'day') {
|
|
return `${day}/${month}/${year}`
|
|
}
|
|
|
|
if (precision === '2weeks') {
|
|
if (d.getUTCDate() < 16) {
|
|
return `${month}/${year} s1-2`
|
|
} else {
|
|
return `${month}/${year} s3-4`
|
|
}
|
|
}
|
|
|
|
return `${month}/${year}`
|
|
}
|
|
|
|
const compute = (transactions, precision, dateFrom, dateTo) => {
|
|
const indexes = {}
|
|
const values = {}
|
|
const labels = []
|
|
const bar = {
|
|
label: 'Capital',
|
|
borderColor: 'rgb(54, 162, 235)',
|
|
backgroundColor: 'rgb(216, 234, 255)',
|
|
fill: true,
|
|
cubicInterpolationMode: 'monotone',
|
|
data: [],
|
|
}
|
|
let lastDate = null
|
|
|
|
transactions.forEach((transaction) => {
|
|
let date = getDate(transaction.date, precision)
|
|
let begin = !Object.hasOwn(indexes, date)
|
|
|
|
if (begin) {
|
|
indexes[date] = labels.length
|
|
values[date] = lastDate !== null ? values[lastDate] : 0
|
|
|
|
if (isInRange(transaction.date, dateFrom, dateTo)) {
|
|
labels.push(date)
|
|
}
|
|
}
|
|
|
|
lastDate = date
|
|
values[date] += transaction.credit
|
|
values[date] -= transaction.debit
|
|
})
|
|
|
|
labels.forEach((label) => {
|
|
const value = values[label]
|
|
bar.data.push(value)
|
|
})
|
|
|
|
const config = {
|
|
labels: labels,
|
|
datasets: [bar],
|
|
}
|
|
|
|
return config
|
|
}
|
|
|
|
export { compute }
|