Parse improvements for budget overview.

This commit is contained in:
James Cole
2021-03-28 14:34:02 +02:00
parent 10390953fe
commit 3586c76b95
5 changed files with 77 additions and 64 deletions

View File

@@ -86,7 +86,7 @@ export default {
yearly: [], yearly: [],
other: [], other: [],
}, },
budgets: {}, budgets: {}, // used to collect some meta data.
rawBudgets: [], rawBudgets: [],
locale: 'en-US', locale: 'en-US',
ready: false, ready: false,
@@ -116,17 +116,14 @@ export default {
}, },
}, },
computed: { computed: {
...mapGetters([ ...mapGetters(['start', 'end']),
'start',
'end'
]),
'datesReady': function () { 'datesReady': function () {
return null !== this.start && null !== this.end && this.ready; return null !== this.start && null !== this.end && this.ready;
} }
}, },
methods: methods:
{ {
getBudgets() { getBudgets: function () {
this.budgets = {}; this.budgets = {};
this.rawBudgets = []; this.rawBudgets = [];
this.budgetLimits = { this.budgetLimits = {
@@ -148,12 +145,16 @@ export default {
); );
}, },
parseBudgets(data) { parseBudgets(data) {
for (let key in data.data) { for (let i in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { if (data.data.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.data[key]; let current = data.data[i];
for (let subKey in current.attributes.spent) { if (false === current.attributes.active) {
if (current.attributes.spent.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) { // skip inactive budgets
let spentData = current.attributes.spent[subKey]; 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( this.rawBudgets.push(
{ {
id: parseInt(current.id), id: parseInt(current.id),
@@ -163,9 +164,9 @@ export default {
spent: spentData.sum spent: spentData.sum
} }
); );
console.log('Added budget ' + current.attributes.name + ' (' + spentData.currency_code + ')');
} }
} }
} }
} }
this.getBudgetLimits(); this.getBudgetLimits();
@@ -181,61 +182,75 @@ export default {
); );
}, },
parseBudgetLimits(data) { parseBudgetLimits(data) {
for (let key in data.included) { // collect budget meta data.
if (data.included.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { for (let i in data.included) {
this.budgets[data.included[key].id] = 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, id: currentId,
name: data.included[key].attributes.name, name: current.attributes.name,
}; };
console.log('Collected meta data: budget #' + currentId + ' is named ' + current.attributes.name);
} }
} }
for (let key in data.data) { for (let i in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { 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 pctGreen = 0;
let pctOrange = 0; let pctOrange = 0;
let pctRed = 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: // 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: // 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)) { if (0.0 !== spentFloat && spentFloat * -1 < amount) {
pctGreen = (parseFloat(data.data[key].attributes.spent) * -1 / parseFloat(data.data[key].attributes.amount) * 100); pctGreen = (spentFloat * -1) / (amount * 100);
} }
// spent over budget // 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)) { if (0.0 !== spentFloat && (spentFloat * -1) > amount) {
pctOrange = (parseFloat(data.data[key].attributes.amount) / parseFloat(data.data[key].attributes.spent) * -1) * 100; pctOrange = (amount / (spentFloat * -1)) * 100;
pctRed = 100 - pctOrange; pctRed = 100 - pctOrange;
} }
let obj = { let obj = {
id: data.data[key].id, id: currentId,
amount: data.data[key].attributes.amount, amount: current.attributes.amount,
budget_id: data.data[key].attributes.budget_id, budget_id: budgetId,
budget_name: this.budgets[data.data[key].attributes.budget_id].name, budget_name: this.budgets[current.attributes.budget_id].name,
currency_id: data.data[key].attributes.currency_id, currency_id: currencyId,
currency_code: data.data[key].attributes.currency_code, currency_code: current.attributes.currency_code,
period: data.data[key].attributes.period, period: current.attributes.period,
start: new Date(data.data[key].attributes.start), start: new Date(current.attributes.start),
end: new Date(data.data[key].attributes.end), end: new Date(current.attributes.end),
spent: data.data[key].attributes.spent, spent: current.attributes.spent,
pctGreen: pctGreen, pctGreen: pctGreen,
pctOrange: pctOrange, pctOrange: pctOrange,
pctRed: pctRed, pctRed: pctRed,
}; };
let period = data.data[key].attributes.period ?? 'other';
this.budgetLimits[period].push(obj); this.budgetLimits[period].push(obj);
} }
} }
}, },
filterBudgets(budgetId, currencyId) { filterBudgets(budgetId, currencyId) {
for (let key in this.rawBudgets) { for (let i in this.rawBudgets) {
if (this.rawBudgets.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { if (this.rawBudgets.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
if (this.rawBudgets[key].currency_id === currencyId && this.rawBudgets[key].id === budgetId) { if (this.rawBudgets[i].currency_id === currencyId && this.rawBudgets[i].id === budgetId) {
this.rawBudgets.splice(key, 1); 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: { computed: {
...mapGetters([ ...mapGetters(['start', 'end']),
'start',
'end'
]),
'datesReady': function () { 'datesReady': function () {
return null !== this.start && null !== this.end && this.ready; return null !== this.start && null !== this.end && this.ready;
} }
@@ -150,17 +147,17 @@ export default {
}); });
}, },
parseCategories(data) { parseCategories(data) {
for (let key in data.data) { for (let i in data.data) {
if (data.data.hasOwnProperty(key) && /^0$|^[1-9]\d*$/.test(key) && key <= 4294967294) { if (data.data.hasOwnProperty(i) && /^0$|^[1-9]\d*$/.test(i) && i <= 4294967294) {
let current = data.data[key]; let current = data.data[i];
let entryKey = null; let entryKey = null;
let categoryId = parseInt(current.id); let categoryId = parseInt(current.id);
// loop spent info: // loop spent info:
for (let subKey in current.attributes.spent) { for (let ii in current.attributes.spent) {
if (current.attributes.spent.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) { if (current.attributes.spent.hasOwnProperty(ii) && /^0$|^[1-9]\d*$/.test(ii) && ii <= 4294967294) {
let spentData = current.attributes.spent[subKey]; let spentData = current.attributes.spent[ii];
entryKey = spentData.currency_id.toString() + '-' + current.id.toString(); entryKey = spentData.currency_id + '-' + current.id;
// does the categories list thing have this combo? if not, create it. // does the categories list thing have this combo? if not, create it.
this.categories[entryKey] = this.categories[entryKey] ?? this.categories[entryKey] = this.categories[entryKey] ??
@@ -180,10 +177,10 @@ export default {
} }
// loop earned info // loop earned info
for (let subKey in current.attributes.earned) { for (let ii in current.attributes.earned) {
if (current.attributes.earned.hasOwnProperty(subKey) && /^0$|^[1-9]\d*$/.test(subKey) && subKey <= 4294967294) { if (current.attributes.earned.hasOwnProperty(ii) && /^0$|^[1-9]\d*$/.test(ii) && ii <= 4294967294) {
let earnedData = current.attributes.earned[subKey]; let earnedData = current.attributes.earned[ii];
entryKey = earnedData.currency_id.toString() + '-' + current.id.toString(); entryKey = earnedData.currency_id + '-' + current.id;
// does the categories list thing have this combo? if not, create it. // does the categories list thing have this combo? if not, create it.
this.categories[entryKey] = this.categories[entryKey] ?? this.categories[entryKey] = this.categories[entryKey] ??
@@ -208,17 +205,17 @@ export default {
sortCategories() { sortCategories() {
// no longer care about keys: // no longer care about keys:
let array = []; let array = [];
for (let cat in this.categories) { for (let i in this.categories) {
if (this.categories.hasOwnProperty(cat)) { if (this.categories.hasOwnProperty(i)) {
array.push(this.categories[cat]); array.push(this.categories[i]);
} }
} }
array.sort(function (one, two) { array.sort(function (one, two) {
return (one.spent + one.earned) - (two.spent + two.earned); return (one.spent + one.earned) - (two.spent + two.earned);
}); });
for (let cat in array) { for (let i in array) {
if (array.hasOwnProperty(cat)) { if (array.hasOwnProperty(i)) {
let current = array[cat]; let current = array[i];
current.spentPct = (current.spent / this.spent) * 100; current.spentPct = (current.spent / this.spent) * 100;
current.earnedPct = (current.earned / this.earned) * 100; current.earnedPct = (current.earned / this.earned) * 100;
this.sortedList.push(current); this.sortedList.push(current);

View File

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