Files
firefly-iii/resources/assets/v2/pages/dashboard/subscriptions.js

188 lines
6.4 KiB
JavaScript
Raw Normal View History

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 {getDefaultChartSettings} from "../../support/default-chart-settings.js";
import {format} from "date-fns";
2023-08-12 07:53:11 +02:00
import {Chart} from 'chart.js';
2023-08-12 17:41:56 +02:00
import {I18n} from "i18n-js";
import {loadTranslations} from "../../support/load-translations.js";
2023-08-09 14:13:32 +02:00
2023-08-27 07:45:09 +02:00
const CACHE_KEY = 'dashboard-subscriptions-data';
2023-08-12 07:53:11 +02:00
let chart = null;
let chartData = null;
let afterPromises = false;
2023-08-12 17:41:56 +02:00
let i18n; // for translating items in the chart.
2023-08-09 14:13:32 +02:00
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() {
2023-08-27 07:45:09 +02:00
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(CACHE_KEY);
if (cacheValid && typeof cachedData !== 'undefined') {
this.drawChart(this.generateOptions(cachedData));
this.loading = false;
return;
}
2023-08-09 14:13:32 +02:00
const getter = new Get();
2023-08-12 07:53:11 +02:00
let params = {
2023-08-09 14:13:32 +02:00
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;
2023-08-12 07:53:11 +02:00
let chartData = {paid: paidData, unpaid: unpaidData};
2023-08-27 07:45:09 +02:00
window.store.set(CACHE_KEY, chartData);
2023-08-09 14:13:32 +02:00
this.drawChart(this.generateOptions(chartData));
this.loading = false;
});
});
},
generateOptions(data) {
2023-08-12 07:53:11 +02:00
let options = getDefaultChartSettings('pie');
2023-08-11 06:03:31 +02:00
// console.log(data);
2023-08-12 17:41:56 +02:00
options.data.labels = [i18n.t('firefly.paid'), i18n.t('firefly.unpaid')];
2023-08-09 14:13:32 +02:00
options.data.datasets = [];
2023-08-12 07:53:11 +02:00
let collection = {};
2023-08-09 14:13:32 +02:00
for (let i in data.paid) {
if (data.paid.hasOwnProperty(i)) {
2023-08-12 07:53:11 +02:00
let current = data.paid[i];
2023-08-09 14:13:32 +02:00
let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
2023-08-12 07:53:11 +02:00
let amount = this.autoConversion ? current.native_sum : current.sum;
2023-08-09 14:13:32 +02:00
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)) {
2023-08-12 07:53:11 +02:00
let current = data.unpaid[i];
2023-08-09 14:13:32 +02:00
let currencyCode = this.autoConversion ? current.native_code : current.currency_code;
2023-08-12 07:53:11 +02:00
let amount = this.autoConversion ? current.native_sum : current.sum;
2023-08-09 14:13:32 +02:00
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() {
2023-08-12 07:53:11 +02:00
// console.log('subscriptions init');
2023-08-12 17:41:56 +02:00
Promise.all([getVariable('autoConversion', false), getVariable('language', 'en-US')]).then((values) => {
2023-08-12 07:53:11 +02:00
// console.log('subscriptions after promises');
2023-08-09 14:13:32 +02:00
this.autoConversion = values[0];
2023-08-12 07:53:11 +02:00
afterPromises = true;
2023-08-12 17:41:56 +02:00
i18n = new I18n();
i18n.locale = values[1];
2023-08-27 07:45:09 +02:00
loadTranslations(i18n, values[1]).then(() => {
if (false === this.loading) {
this.loadChart();
}
});
2023-08-12 17:41:56 +02:00
2023-08-09 14:13:32 +02:00
});
window.store.observe('end', () => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
// console.log('subscriptions observe end');
2023-08-09 14:13:32 +02:00
if (false === this.loading) {
this.chartData = null;
this.loadChart();
}
});
window.store.observe('autoConversion', (newValue) => {
2023-08-12 07:53:11 +02:00
if (!afterPromises) {
return;
}
// console.log('subscriptions observe autoConversion');
2023-08-09 14:13:32 +02:00
this.autoConversion = newValue;
if (false === this.loading) {
this.loadChart();
}
});
},
});