Parse improvements for budget overview.

This commit is contained in:
James Cole 2021-03-28 14:34:02 +02:00
parent 10390953fe
commit 3586c76b95
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
5 changed files with 77 additions and 64 deletions

View File

@ -86,7 +86,7 @@ export default {
yearly: [],
other: [],
},
budgets: {},
budgets: {}, // used to collect some meta data.
rawBudgets: [],
locale: 'en-US',
ready: false,
@ -116,17 +116,14 @@ export default {
},
},
computed: {
...mapGetters([
'start',
'end'
]),
...mapGetters(['start', 'end']),
'datesReady': function () {
return null !== this.start && null !== this.end && this.ready;
}
},
methods:
{
getBudgets() {
getBudgets: function () {
this.budgets = {};
this.rawBudgets = [];
this.budgetLimits = {
@ -148,12 +145,16 @@ export default {
);
},
parseBudgets(data) {
for (let key in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
let current = data.data[key];
for (let subKey in current.attributes.spent) {
if (current.attributes.spent.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) {
let spentData = current.attributes.spent[subKey];
for (let i in data.data) {
if (data.data.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.data[i];
if (false === current.attributes.active) {
// skip inactive budgets
continue;
}
for (let ii in current.attributes.spent) {
if (current.attributes.spent.hasOwnProperty(ii) && /^0$|^[1-9]\d*$/.test(ii) && ii <= 4294967294) {
let spentData = current.attributes.spent[ii];
this.rawBudgets.push(
{
id: parseInt(current.id),
@ -163,9 +164,9 @@ export default {
spent: spentData.sum
}
);
console.log('Added budget ' + current.attributes.name + ' (' + spentData.currency_code + ')');
}
}
}
}
this.getBudgetLimits();
@ -181,61 +182,75 @@ export default {
);
},
parseBudgetLimits(data) {
for (let key in data.included) {
if (data.included.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
this.budgets[data.included[key].id] =
// collect budget meta data.
for (let i in data.included) {
if (data.included.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.included[i];
let currentId = parseInt(current.id);
this.budgets[currentId] =
{
id: data.included[key].id,
name: data.included[key].attributes.name,
id: currentId,
name: current.attributes.name,
};
console.log('Collected meta data: budget #' + currentId + ' is named ' + current.attributes.name);
}
}
for (let key in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
for (let i in data.data) {
if (data.data.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.data[i];
let currentId = parseInt(current.id);
let budgetId = parseInt(current.attributes.budget_id);
let currencyId = parseInt(current.attributes.currency_id);
let spentFloat = parseFloat(current.attributes.spent);
let amount = parseFloat(current.attributes.amount);
let period = current.attributes.period ?? 'other';
let pctGreen = 0;
let pctOrange = 0;
let pctRed = 0;
console.log('Collected "' + period + '" budget limit #' + currentId + ' (part of budget #' + budgetId + ')');
console.log('Spent ' + spentFloat + ' of ' + amount);
// remove budget info from rawBudgets if it's there:
this.filterBudgets(data.data[key].attributes.budget_id, data.data[key].attributes.currency_id);
this.filterBudgets(budgetId, currencyId);
// spent within budget:
if (0.0 !== parseFloat(data.data[key].attributes.spent) && (parseFloat(data.data[key].attributes.spent) * -1) < parseFloat(data.data[key].attributes.amount)) {
pctGreen = (parseFloat(data.data[key].attributes.spent) * -1 / parseFloat(data.data[key].attributes.amount) * 100);
if (0.0 !== spentFloat && spentFloat * -1 < amount) {
pctGreen = (spentFloat * -1) / (amount * 100);
}
// spent over budget
if (0.0 !== parseFloat(data.data[key].attributes.spent) && (parseFloat(data.data[key].attributes.spent) * -1) > parseFloat(data.data[key].attributes.amount)) {
pctOrange = (parseFloat(data.data[key].attributes.amount) / parseFloat(data.data[key].attributes.spent) * -1) * 100;
if (0.0 !== spentFloat && (spentFloat * -1) > amount) {
pctOrange = (amount / (spentFloat * -1)) * 100;
pctRed = 100 - pctOrange;
}
let obj = {
id: data.data[key].id,
amount: data.data[key].attributes.amount,
budget_id: data.data[key].attributes.budget_id,
budget_name: this.budgets[data.data[key].attributes.budget_id].name,
currency_id: data.data[key].attributes.currency_id,
currency_code: data.data[key].attributes.currency_code,
period: data.data[key].attributes.period,
start: new Date(data.data[key].attributes.start),
end: new Date(data.data[key].attributes.end),
spent: data.data[key].attributes.spent,
id: currentId,
amount: current.attributes.amount,
budget_id: budgetId,
budget_name: this.budgets[current.attributes.budget_id].name,
currency_id: currencyId,
currency_code: current.attributes.currency_code,
period: current.attributes.period,
start: new Date(current.attributes.start),
end: new Date(current.attributes.end),
spent: current.attributes.spent,
pctGreen: pctGreen,
pctOrange: pctOrange,
pctRed: pctRed,
};
let period = data.data[key].attributes.period ?? 'other';
this.budgetLimits[period].push(obj);
}
}
},
filterBudgets(budgetId, currencyId) {
for (let key in this.rawBudgets) {
if (this.rawBudgets.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
if (this.rawBudgets[key].currency_id === currencyId && this.rawBudgets[key].id === budgetId) {
this.rawBudgets.splice(key, 1);
for (let i in this.rawBudgets) {
if (this.rawBudgets.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
if (this.rawBudgets[i].currency_id === currencyId && this.rawBudgets[i].id === budgetId) {
console.log('Budget ' + this.rawBudgets[i].name + ' with currency ' + this.rawBudgets[i].currency_code + ' will be removed in favor of a budget limit.');
this.rawBudgets.splice(parseInt(i), 1);
}
}
}

View File

@ -105,10 +105,7 @@ export default {
}
},
computed: {
...mapGetters([
'start',
'end'
]),
...mapGetters(['start', 'end']),
'datesReady': function () {
return null !== this.start && null !== this.end && this.ready;
}
@ -150,17 +147,17 @@ export default {
});
},
parseCategories(data) {
for (let key in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) {
let current = data.data[key];
for (let i in data.data) {
if (data.data.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.data[i];
let entryKey = null;
let categoryId = parseInt(current.id);
// loop spent info:
for (let subKey in current.attributes.spent) {
if (current.attributes.spent.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) {
let spentData = current.attributes.spent[subKey];
entryKey = spentData.currency_id.toString() + '-' + current.id.toString();
for (let ii in current.attributes.spent) {
if (current.attributes.spent.hasOwnProperty(ii) && /^0$|^[1-9]\d*$/.test(ii) && ii <= 4294967294) {
let spentData = current.attributes.spent[ii];
entryKey = spentData.currency_id + '-' + current.id;
// does the categories list thing have this combo? if not, create it.
this.categories[entryKey] = this.categories[entryKey] ??
@ -180,10 +177,10 @@ export default {
}
// loop earned info
for (let subKey in current.attributes.earned) {
if (current.attributes.earned.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) {
let earnedData = current.attributes.earned[subKey];
entryKey = earnedData.currency_id.toString() + '-' + current.id.toString();
for (let ii in current.attributes.earned) {
if (current.attributes.earned.hasOwnProperty(ii) && /^0$|^[1-9]\d*$/.test(ii) && ii <= 4294967294) {
let earnedData = current.attributes.earned[ii];
entryKey = earnedData.currency_id + '-' + current.id;
// does the categories list thing have this combo? if not, create it.
this.categories[entryKey] = this.categories[entryKey] ??
@ -208,17 +205,17 @@ export default {
sortCategories() {
// no longer care about keys:
let array = [];
for (let cat in this.categories) {
if (this.categories.hasOwnProperty(cat)) {
array.push(this.categories[cat]);
for (let i in this.categories) {
if (this.categories.hasOwnProperty(i)) {
array.push(this.categories[i]);
}
}
array.sort(function (one, two) {
return (one.spent + one.earned) - (two.spent + two.earned);
});
for (let cat in array) {
if (array.hasOwnProperty(cat)) {
let current = array[cat];
for (let i in array) {
if (array.hasOwnProperty(i)) {
let current = array[i];
current.spentPct = (current.spent / this.spent) * 100;
current.earnedPct = (current.earned / this.earned) * 100;
this.sortedList.push(current);

View File

@ -75,9 +75,10 @@ new Vue({
return createElement(Dashboard, {props: props});
},
beforeCreate() {
// TODO migrate to "root" store.
this.$store.commit('initialiseStore');
this.$store.dispatch('updateCurrencyPreference');
this.$store.dispatch('updateListPageSizePreference');
this.$store.dispatch('root/initialiseStore');
this.$store.dispatch('dashboard/index/initialiseStore');
},
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long