2023-08-09 14:13:32 +02:00
|
|
|
/*
|
|
|
|
* budgets.js
|
|
|
|
* Copyright (c) 2023 james@firefly-iii.org
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
import {getVariable} from "../../store/get-variable.js";
|
|
|
|
import Get from "../../api/v2/model/subscription/get.js";
|
|
|
|
import Chart from 'chart.js/auto';
|
|
|
|
import {getDefaultChartSettings} from "../../support/default-chart-settings.js";
|
|
|
|
import formatMoney from "../../util/format-money.js";
|
|
|
|
import {format} from "date-fns";
|
|
|
|
|
|
|
|
let currencies = [];
|
|
|
|
let chart = null;
|
|
|
|
let chartData = null;
|
|
|
|
|
|
|
|
export default () => ({
|
|
|
|
loading: false,
|
|
|
|
autoConversion: false,
|
|
|
|
loadChart() {
|
|
|
|
if (true === this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
if (null !== chartData) {
|
|
|
|
this.drawChart(this.generateOptions(chartData));
|
|
|
|
this.loading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getFreshData();
|
|
|
|
},
|
|
|
|
drawChart(options) {
|
|
|
|
if (null !== chart) {
|
|
|
|
chart.data.datasets = options.data.datasets;
|
|
|
|
chart.update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
chart = new Chart(document.querySelector("#subscriptions-chart"), options);
|
|
|
|
},
|
|
|
|
getFreshData() {
|
|
|
|
const getter = new Get();
|
|
|
|
let params = {
|
|
|
|
start: format(new Date(window.store.get('start')), 'y-MM-dd'),
|
|
|
|
end: format(new Date(window.store.get('end')), 'y-MM-dd')
|
|
|
|
};
|
|
|
|
|
|
|
|
getter.paid(params).then((response) => {
|
|
|
|
let paidData = response.data;
|
|
|
|
getter.unpaid(params).then((response) => {
|
|
|
|
let unpaidData = response.data;
|
|
|
|
let chartData = {paid: paidData, unpaid: unpaidData};
|
|
|
|
this.drawChart(this.generateOptions(chartData));
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
generateOptions(data) {
|
2023-08-11 06:03:31 +02:00
|
|
|
let options = getDefaultChartSettings('pie');
|
|
|
|
// console.log(data);
|
2023-08-09 14:13:32 +02:00
|
|
|
options.data.labels = ['TODO paid', 'TODO unpaid'];
|
|
|
|
options.data.datasets = [];
|
|
|
|
let collection = {};
|
|
|
|
for (let i in data.paid) {
|
|
|
|
if (data.paid.hasOwnProperty(i)) {
|
|
|
|
let current = data.paid[i];
|
|
|
|
let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
|
|
|
|
let amount = this.autoConversion ? current.native_sum : current.sum;
|
|
|
|
if (!collection.hasOwnProperty(currencyCode)) {
|
|
|
|
collection[currencyCode] = {
|
|
|
|
paid: 0,
|
|
|
|
unpaid: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// in case of paid, add to "paid":
|
|
|
|
collection[currencyCode].paid += (parseFloat(amount) * -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unpaid
|
|
|
|
for (let i in data.unpaid) {
|
|
|
|
if (data.unpaid.hasOwnProperty(i)) {
|
|
|
|
let current = data.unpaid[i];
|
|
|
|
let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
|
|
|
|
let amount = this.autoConversion ? current.native_sum : current.sum;
|
|
|
|
if (!collection.hasOwnProperty(currencyCode)) {
|
|
|
|
collection[currencyCode] = {
|
|
|
|
paid: 0,
|
|
|
|
unpaid: 0,
|
|
|
|
};
|
|
|
|
}
|
2023-08-11 06:03:31 +02:00
|
|
|
// console.log(current);
|
2023-08-09 14:13:32 +02:00
|
|
|
// in case of paid, add to "paid":
|
|
|
|
collection[currencyCode].unpaid += parseFloat(amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let currencyCode in collection) {
|
|
|
|
if (collection.hasOwnProperty(currencyCode)) {
|
|
|
|
let current = collection[currencyCode];
|
|
|
|
options.data.datasets.push(
|
|
|
|
{
|
|
|
|
label: currencyCode,
|
|
|
|
data: [current.paid, current.unpaid],
|
|
|
|
backgroundColor: [
|
|
|
|
'rgb(54, 162, 235)', // green (paid)
|
|
|
|
'rgb(255, 99, 132)', // red (unpaid_
|
|
|
|
],
|
|
|
|
//hoverOffset: 4
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
init() {
|
|
|
|
Promise.all([getVariable('autoConversion', false),]).then((values) => {
|
|
|
|
this.autoConversion = values[0];
|
|
|
|
if (false === this.loading) {
|
|
|
|
this.loadChart();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
window.store.observe('end', () => {
|
|
|
|
if (false === this.loading) {
|
|
|
|
this.chartData = null;
|
|
|
|
this.loadChart();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
window.store.observe('autoConversion', (newValue) => {
|
|
|
|
this.autoConversion = newValue;
|
|
|
|
if (false === this.loading) {
|
|
|
|
this.loadChart();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|