2023-08-10 19:51:36 +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/piggy-bank/get.js";
|
2023-08-12 17:41:56 +02:00
|
|
|
import {I18n} from "i18n-js";
|
|
|
|
import {loadTranslations} from "../../support/load-translations.js";
|
2023-08-10 19:51:36 +02:00
|
|
|
|
|
|
|
let apiData = {};
|
2023-08-12 07:53:11 +02:00
|
|
|
let afterPromises = false;
|
2023-08-12 17:41:56 +02:00
|
|
|
let i18n;
|
2023-08-10 19:51:36 +02:00
|
|
|
|
|
|
|
export default () => ({
|
|
|
|
loading: false,
|
|
|
|
autoConversion: false,
|
|
|
|
sankeyGrouping: 'account',
|
|
|
|
piggies: [],
|
|
|
|
getFreshData() {
|
|
|
|
let params = {
|
|
|
|
start: window.store.get('start').slice(0, 10),
|
|
|
|
end: window.store.get('end').slice(0, 10),
|
|
|
|
page: 1
|
|
|
|
};
|
|
|
|
this.downloadPiggyBanks(params);
|
|
|
|
},
|
|
|
|
downloadPiggyBanks(params) {
|
2023-08-11 06:03:31 +02:00
|
|
|
// console.log('Downloading page ' + params.page + '...');
|
2023-08-10 19:51:36 +02:00
|
|
|
const getter = new Get();
|
|
|
|
getter.get(params).then((response) => {
|
|
|
|
apiData = [...apiData, ...response.data.data];
|
|
|
|
if (parseInt(response.data.meta.pagination.total_pages) > params.page) {
|
|
|
|
params.page++;
|
|
|
|
this.downloadPiggyBanks(params);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.parsePiggies();
|
|
|
|
this.loading = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
parsePiggies() {
|
|
|
|
let dataSet = [];
|
|
|
|
for (let i in apiData) {
|
|
|
|
if (apiData.hasOwnProperty(i)) {
|
|
|
|
let current = apiData[i];
|
|
|
|
if (current.attributes.percentage >= 100) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (0 === current.attributes.percentage) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-12 17:41:56 +02:00
|
|
|
let groupName = current.object_group_title ?? i18n.t('firefly.default_group_title_name_plain');
|
2023-08-10 19:51:36 +02:00
|
|
|
if (!dataSet.hasOwnProperty(groupName)) {
|
|
|
|
dataSet[groupName] = {
|
|
|
|
id: current.object_group_id ?? 0,
|
|
|
|
title: groupName,
|
|
|
|
order: current.object_group_order ?? 0,
|
|
|
|
piggies: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let piggy = {
|
|
|
|
id: current.id,
|
|
|
|
name: current.attributes.name,
|
|
|
|
percentage: parseInt(current.attributes.percentage),
|
|
|
|
amount: this.autoConversion ? current.attributes.native_current_amount : current.attributes.current_amount,
|
|
|
|
// left to save
|
|
|
|
left_to_save: this.autoConversion ? current.attributes.native_left_to_save : current.attributes.left_to_save,
|
|
|
|
// target amount
|
|
|
|
target_amount: this.autoConversion ? current.attributes.native_target_amount : current.attributes.target_amount,
|
|
|
|
// save per month
|
|
|
|
save_per_month: this.autoConversion ? current.attributes.native_save_per_month : current.attributes.save_per_month,
|
|
|
|
currency_code: this.autoConversion ? current.attributes.native_code : current.attributes.currency_code,
|
|
|
|
|
|
|
|
};
|
|
|
|
dataSet[groupName].piggies.push(piggy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.piggies = Object.values(dataSet);
|
2023-08-11 06:03:31 +02:00
|
|
|
// console.log(this.piggies);
|
2023-08-10 19:51:36 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
loadPiggyBanks() {
|
|
|
|
if (true === this.loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
if (0 !== this.piggies.length) {
|
|
|
|
this.parsePiggies();
|
|
|
|
this.loading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getFreshData();
|
|
|
|
},
|
|
|
|
init() {
|
2023-08-12 07:53:11 +02:00
|
|
|
// console.log('piggies init');
|
2023-08-10 19:51:36 +02:00
|
|
|
apiData = [];
|
2023-08-12 17:41:56 +02:00
|
|
|
Promise.all([getVariable('autoConversion', false), getVariable('language', 'en-US')]).then((values) => {
|
|
|
|
|
|
|
|
i18n = new I18n();
|
|
|
|
i18n.locale = values[1];
|
|
|
|
loadTranslations(i18n, values[1]);
|
|
|
|
|
2023-08-12 07:53:11 +02:00
|
|
|
// console.log('piggies after promises');
|
|
|
|
afterPromises = true;
|
2023-08-10 19:51:36 +02:00
|
|
|
this.autoConversion = values[0];
|
|
|
|
this.loadPiggyBanks();
|
|
|
|
});
|
|
|
|
window.store.observe('end', () => {
|
2023-08-12 07:53:11 +02:00
|
|
|
if (!afterPromises) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// console.log('piggies observe end');
|
2023-08-10 19:51:36 +02:00
|
|
|
apiData = [];
|
|
|
|
this.loadPiggyBanks();
|
|
|
|
});
|
|
|
|
window.store.observe('autoConversion', (newValue) => {
|
2023-08-12 07:53:11 +02:00
|
|
|
if (!afterPromises) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// console.log('piggies observe autoConversion');
|
2023-08-10 19:51:36 +02:00
|
|
|
this.autoConversion = newValue;
|
|
|
|
this.loadPiggyBanks();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|