diff --git a/public/app/js/app.js b/public/app/js/app.js index 4570ef42..1771ee3b 100644 --- a/public/app/js/app.js +++ b/public/app/js/app.js @@ -18,8 +18,8 @@ app.config(['$stateProvider', '$urlRouterProvider', } ]); app.component('dashboard', { - controller: ['$filter', 'API', - function ($filter, API) { + controller: ['$filter', '$q', 'API', + function ($filter, $q, API) { var ctrl = this; ctrl.xFunction = function () { return function (d) { @@ -160,13 +160,14 @@ app.component('dashboard', { }; ctrl.depth = 99; var retrieve_period_detailed_data = function () { + console.log(ctrl.period); ctrl.balance = { buckets: [new Bucket('Expenses Liabilities Equity Income', ctrl.period), new Bucket('Assets', null)], details: {} }; - _(ctrl.balance.buckets).each(function (bucket) { - API.balance({ + return $q.all(_(ctrl.balance.buckets).map(function (bucket) { + return API.balance({ period: bucket.period, categories: bucket.categories, depth: ctrl.depth @@ -192,18 +193,19 @@ app.component('dashboard', { bucket.accounts_selected = bucket.raw_data; ctrl.filter_data(); }); - }); + })); }; var retrieve_accounts = function () { - API.accounts() + return $q.when(API.accounts() .then(function (response) { ctrl.accounts = response.data.map(function (account_ary) { return account_ary.join(':'); }); - }); + })); }; var retrieve_graph_values = function (params) { - API.graph_values(params).then(function (response) { + return $q.when(API.graph_values(params) + .then(function (response) { ctrl.periods = []; var largest_cat = _(response.data).reduce(function (memo, cat) { return cat.length > memo.length ? cat : memo; @@ -278,13 +280,17 @@ app.component('dashboard', { }; ctrl.periods = _.chain(ctrl.periods).uniq().sort().reverse().value(); ctrl.period = _(ctrl.periods).first(); - }); + })); }; ctrl.graphed_accounts = ['Expenses', 'Income']; - retrieve_accounts(); - retrieve_period_detailed_data(); - retrieve_graph_values({ period: '', - categories: ctrl.graphed_accounts.join(' ') }); + retrieve_accounts().then(function (response) { + retrieve_graph_values({ + period: '', + categories: ctrl.graphed_accounts.join(' ') + }).then(function (response) { + retrieve_period_detailed_data(); + }); + }); } ], template: "\n\n \n \n \n \n \n \n \n \n

{{$ctrl.period | amDateFormat:'MMMM YYYY'}}

\n \n \n {{account.account}} = {{account.amount | number:2}} \u20AC\n \n \n \n \n \n \n \n \n \n \n \n \n
\n" diff --git a/public/app/js/app.min.js b/public/app/js/app.min.js index 3ca662a2..19dbff00 100644 --- a/public/app/js/app.min.js +++ b/public/app/js/app.min.js @@ -1,17 +1,17 @@ -var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,b){a!=Array.prototype&&a!=Object.prototype&&(a[c]=b.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_"; +var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,d){a!=Array.prototype&&a!=Object.prototype&&(a[c]=d.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_"; $jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.Symbol=function(){var a=0;return function(c){return $jscomp.SYMBOL_PREFIX+(c||"")+a++}}(); $jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var a=$jscomp.global.Symbol.iterator;a||(a=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&$jscomp.defineProperty(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(a){var c=0;return $jscomp.iteratorPrototype(function(){return c=a?(b=-1*a-100,c=["#f0f"]):-10>=a&&(b=-1*a-10,c=["#360"]);return c[b]};b.color=function(){return function(a,c){return b.coloring_score(e(a.data.account))}};b.filter_data=function(){_(b.balance.buckets).each(function(b){b.data=[];_(b.accounts_selected).isEmpty()&&0===b.score_threshold?b.data=b.raw_data:_(b.accounts_selected).each(function(c){b.data=b.data.concat(a("filter")(b.raw_data, -c,!0))});b.total_detailed=_.chain(b.data).groupBy(function(a){return a.account.split(":")[0]}).each(function(a){a.total=_(a).reduce(function(a,b){return a+b.amount},0)}).value();b.total_detailed=_.chain(b.total_detailed).keys().map(function(a){return{account:a,amount:b.total_detailed[a].total}}).value()})};var d=function(a,b){var c=this;this.categories=a;this.period=b;this.score_threshold=0;this.orderBy="amount";this.orderDesc=!1;this.order_by=function(a){c.orderBy==a?c.orderDesc=!c.orderDesc:c.orderBy= -a};this.pie_graph_options={chart:{type:"pieChart",donut:!0,donutRatio:.25,height:300,x:function(a){return a.account},y:function(a){return a.amount},showLabels:!1,showLegend:!0,legendPosition:"right",showTooltipPercent:!0,duration:500,labelThreshold:.01,labelSunbeamLayout:!0,labelsOutside:!0}}};b.depth=99;var g=function(){b.balance={buckets:[new d("Expenses Liabilities Equity Income",b.period),new d("Assets",null)],details:{}};_(b.balance.buckets).each(function(a){c.balance({period:a.period,categories:a.categories, -depth:b.depth}).then(function(c){a.raw_data=_.chain(c.data).map(function(a){a.amount=0>a.amount?-1*a.amount:a.amount;a.score=e(a.account);return a}).sortBy(function(a){return 1/a.amount}).sortBy(function(a){return a.account.split(":")[0]}).value().reverse();a.raw_total=_(c.data).reduce(function(a,b){return a+b.amount},0);a.accounts_selected=a.raw_data;b.filter_data()})})};b.graphed_accounts=["Expenses","Income"];(function(){c.accounts().then(function(a){b.accounts=a.data.map(function(a){return a.join(":")})})})(); -g();(function(a){c.graph_values(a).then(function(a){b.periods=[];var c=_(a.data).reduce(function(a,b){return b.length>a.length?b:a},[]);_.chain(c).pluck("date").each(function(b){_(a.data).each(function(a){var c=_(a).find({date:b});_(c).isUndefined()&&a.push({date:b,amount:0,currency:_(a).first().currency})})});_(a.data).each(function(a){a=_(a).sortBy(function(a){return a.date})});b.graphiques={monthly_values:{options:{chart:{type:"multiBarChart",height:300,showControls:!1,showLegend:!0,showLabels:!0, -stacked:!1,duration:500,reduceXTicks:!1,rotateLabels:67,labelSunbeamLayout:!0,useInteractiveGuideline:!1,multibar:{dispatch:{elementClick:function(a){b.period=a.data.x;g()}}}}},data:_.chain(a.data).keys().reverse().map(function(c){var e="Income"==c?-1:1;return{key:c,values:_.chain(a.data[c]).map(function(a){var d=new Date(a.date);d=d.getFullYear()+"-"+(9>d.getMonth()?"0":"")+(d.getMonth()+1);b.periods.push(d);return{key:c,x:d,y:parseInt(a.amount)*e}}).sortBy(function(a){return a.x}).value()}}).value()}}; -b.periods=_.chain(b.periods).uniq().sort().reverse().value();b.period=_(b.periods).first()})})({period:"",categories:b.graphed_accounts.join(" ")})}],template:'\n\x3cmd-content flex\x3d"100" layout\x3d"column"\x3e\n \x3cmd-card flex\x3d"100" layout\x3d"row"\x3e\n \x3cmd-card flex\x3d"20"\x3e\n \x3cselect style\x3d"height: 100%;" multiple ng:model\x3d"$ctrl.graphed_accounts"\x3e\n \x3coption ng:repeat\x3d"account in $ctrl.accounts"\x3e{{account}}\x3c/option\x3e\n \x3c/select\x3e\n \x3c/md-card\x3e\n \x3cmd-card flex\x3d"81"\x3e\n \x3cnvd3 data\x3d"$ctrl.graphiques.monthly_values.data"\n options\x3d"$ctrl.graphiques.monthly_values.options"\x3e\x3c/nvd3\x3e\n \x3c/md-card\x3e\n \x3c/md-card\x3e\n \x3ch1 style\x3d"text-align: center;"\x3e{{$ctrl.period | amDateFormat:\'MMMM YYYY\'}}\x3c/h1\x3e\n \x3cmd-card flex\x3d"100" layout\x3d"column"\n ng:repeat\x3d"bucket in $ctrl.balance.buckets"\x3e\n \x3cmd-toolbar\x3e\n \x3cspan ng:repeat\x3d"account in bucket.total_detailed"\x3e{{account.account}} \x3d {{account.amount | number:2}} \u20ac\x3c/span\x3e\n \x3c/md-toolbar\x3e\n \x3cmd-content layout\x3d"row"\x3e\n \x3cmd-card flex\x3d"20"\x3e\n \x3cselect style\x3d"height: 100%;" multiple\n ng:model\x3d"bucket.accounts_selected"\n ng:options\x3d"account.account for account in bucket.raw_data | orderBy:\'account\'"\n ng:change\x3d"filter_data()"\x3e\n \x3coption value\x3d\'\'\x3e...\x3c/option\x3e\n \x3c/select\x3e\n \x3c/md-card\x3e\n \x3cmd-card flex\x3d"78"\x3e\n \x3cnvd3 data\x3d"bucket.data"\n options\x3d"bucket.pie_graph_options" \x3e\n \x3c/nvd3\x3e\n \x3c/md-card\x3e\n \x3c!-- \x3cmd-card flex\x3d"56"\x3e\n \x3ctable class\x3d"table"\x3e\n \x3cthead\x3e\n \x3ctr\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'account\' )"\x3eaccount\x3c/md-buton\x3e\x3c/th\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'amount\' )"\x3eamount\x3c/md-buton\x3e\x3c/th\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'score\' )"\x3escore\x3c/md-buton\x3e\x3c/th\x3e\n \x3c/tr\x3e\n \x3c/thead\x3e\n \x3ctbody\x3e\n \x3ctr ng:repeat\x3d"account in bucket.data | orderBy:bucket.orderBy:bucket.orderDesc"\n ng:class\x3d"{\'even\': $even, \'odd\': $odd}"\n style\x3d"border-left:10px solid {{coloring_score( account.score )}};border-right:10px solid {{coloring_score( account.score )}}"\x3e\n \x3ctd style\x3d"border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.account}}\n \x3c/td\x3e\n \x3ctd style\x3d"text-align:right;border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.amount | number:2}} \u20ac\n \x3c/td\x3e\n \x3ctd style\x3d"text-align:right;border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.score}}\n \x3c/td\x3e\n \x3c/tr\x3e\n \x3c/tbody\x3e\n \x3c/table\x3e\n \x3c/md-card\x3e --\x3e\n \x3c/md-content\x3e\n \x3c/md-card\x3e\n\x3c/md-content\x3e\n'}); +0:a.match(/^Assets/)?-100:0};b.coloring_score=function(a){var b=a,f="#99f #0f0 #3f0 #6f0 #9f0 #cf0 #fc0 #f90 #f60 #f30 #f00".split(" ");-100>=a?(b=-1*a-100,f=["#f0f"]):-10>=a&&(b=-1*a-10,f=["#360"]);return f[b]};b.color=function(){return function(a,c){return b.coloring_score(e(a.data.account))}};b.filter_data=function(){_(b.balance.buckets).each(function(b){b.data=[];_(b.accounts_selected).isEmpty()&&0===b.score_threshold?b.data=b.raw_data:_(b.accounts_selected).each(function(c){b.data=b.data.concat(a("filter")(b.raw_data, +c,!0))});b.total_detailed=_.chain(b.data).groupBy(function(a){return a.account.split(":")[0]}).each(function(a){a.total=_(a).reduce(function(a,b){return a+b.amount},0)}).value();b.total_detailed=_.chain(b.total_detailed).keys().map(function(a){return{account:a,amount:b.total_detailed[a].total}}).value()})};var g=function(a,b){var c=this;this.categories=a;this.period=b;this.score_threshold=0;this.orderBy="amount";this.orderDesc=!1;this.order_by=function(a){c.orderBy==a?c.orderDesc=!c.orderDesc:c.orderBy= +a};this.pie_graph_options={chart:{type:"pieChart",donut:!0,donutRatio:.25,height:300,x:function(a){return a.account},y:function(a){return a.amount},showLabels:!1,showLegend:!0,legendPosition:"right",showTooltipPercent:!0,duration:500,labelThreshold:.01,labelSunbeamLayout:!0,labelsOutside:!0}}};b.depth=99;var h=function(){console.log(b.period);b.balance={buckets:[new g("Expenses Liabilities Equity Income",b.period),new g("Assets",null)],details:{}};return c.all(_(b.balance.buckets).map(function(a){return d.balance({period:a.period, +categories:a.categories,depth:b.depth}).then(function(c){a.raw_data=_.chain(c.data).map(function(a){a.amount=0>a.amount?-1*a.amount:a.amount;a.score=e(a.account);return a}).sortBy(function(a){return 1/a.amount}).sortBy(function(a){return a.account.split(":")[0]}).value().reverse();a.raw_total=_(c.data).reduce(function(a,b){return a+b.amount},0);a.accounts_selected=a.raw_data;b.filter_data()})}))},k=function(a){return c.when(d.graph_values(a).then(function(a){b.periods=[];var c=_(a.data).reduce(function(a, +b){return b.length>a.length?b:a},[]);_.chain(c).pluck("date").each(function(b){_(a.data).each(function(a){var c=_(a).find({date:b});_(c).isUndefined()&&a.push({date:b,amount:0,currency:_(a).first().currency})})});_(a.data).each(function(a){a=_(a).sortBy(function(a){return a.date})});b.graphiques={monthly_values:{options:{chart:{type:"multiBarChart",height:300,showControls:!1,showLegend:!0,showLabels:!0,stacked:!1,duration:500,reduceXTicks:!1,rotateLabels:67,labelSunbeamLayout:!0,useInteractiveGuideline:!1, +multibar:{dispatch:{elementClick:function(a){b.period=a.data.x;h()}}}}},data:_.chain(a.data).keys().reverse().map(function(c){var d="Income"==c?-1:1;return{key:c,values:_.chain(a.data[c]).map(function(a){var e=new Date(a.date);e=e.getFullYear()+"-"+(9>e.getMonth()?"0":"")+(e.getMonth()+1);b.periods.push(e);return{key:c,x:e,y:parseInt(a.amount)*d}}).sortBy(function(a){return a.x}).value()}}).value()}};b.periods=_.chain(b.periods).uniq().sort().reverse().value();b.period=_(b.periods).first()}))};b.graphed_accounts= +["Expenses","Income"];(function(){return c.when(d.accounts().then(function(a){b.accounts=a.data.map(function(a){return a.join(":")})}))})().then(function(a){k({period:"",categories:b.graphed_accounts.join(" ")}).then(function(a){h()})})}],template:'\n\x3cmd-content flex\x3d"100" layout\x3d"column"\x3e\n \x3cmd-card flex\x3d"100" layout\x3d"row"\x3e\n \x3cmd-card flex\x3d"20"\x3e\n \x3cselect style\x3d"height: 100%;" multiple ng:model\x3d"$ctrl.graphed_accounts"\x3e\n \x3coption ng:repeat\x3d"account in $ctrl.accounts"\x3e{{account}}\x3c/option\x3e\n \x3c/select\x3e\n \x3c/md-card\x3e\n \x3cmd-card flex\x3d"81"\x3e\n \x3cnvd3 data\x3d"$ctrl.graphiques.monthly_values.data"\n options\x3d"$ctrl.graphiques.monthly_values.options"\x3e\x3c/nvd3\x3e\n \x3c/md-card\x3e\n \x3c/md-card\x3e\n \x3ch1 style\x3d"text-align: center;"\x3e{{$ctrl.period | amDateFormat:\'MMMM YYYY\'}}\x3c/h1\x3e\n \x3cmd-card flex\x3d"100" layout\x3d"column"\n ng:repeat\x3d"bucket in $ctrl.balance.buckets"\x3e\n \x3cmd-toolbar\x3e\n \x3cspan ng:repeat\x3d"account in bucket.total_detailed"\x3e{{account.account}} \x3d {{account.amount | number:2}} \u20ac\x3c/span\x3e\n \x3c/md-toolbar\x3e\n \x3cmd-content layout\x3d"row"\x3e\n \x3cmd-card flex\x3d"20"\x3e\n \x3cselect style\x3d"height: 100%;" multiple\n ng:model\x3d"bucket.accounts_selected"\n ng:options\x3d"account.account for account in bucket.raw_data | orderBy:\'account\'"\n ng:change\x3d"filter_data()"\x3e\n \x3coption value\x3d\'\'\x3e...\x3c/option\x3e\n \x3c/select\x3e\n \x3c/md-card\x3e\n \x3cmd-card flex\x3d"78"\x3e\n \x3cnvd3 data\x3d"bucket.data"\n options\x3d"bucket.pie_graph_options" \x3e\n \x3c/nvd3\x3e\n \x3c/md-card\x3e\n \x3c!-- \x3cmd-card flex\x3d"56"\x3e\n \x3ctable class\x3d"table"\x3e\n \x3cthead\x3e\n \x3ctr\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'account\' )"\x3eaccount\x3c/md-buton\x3e\x3c/th\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'amount\' )"\x3eamount\x3c/md-buton\x3e\x3c/th\x3e\n \x3cth\x3e\x3cmd-buton ng:click\x3d"bucket.order_by( \'score\' )"\x3escore\x3c/md-buton\x3e\x3c/th\x3e\n \x3c/tr\x3e\n \x3c/thead\x3e\n \x3ctbody\x3e\n \x3ctr ng:repeat\x3d"account in bucket.data | orderBy:bucket.orderBy:bucket.orderDesc"\n ng:class\x3d"{\'even\': $even, \'odd\': $odd}"\n style\x3d"border-left:10px solid {{coloring_score( account.score )}};border-right:10px solid {{coloring_score( account.score )}}"\x3e\n \x3ctd style\x3d"border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.account}}\n \x3c/td\x3e\n \x3ctd style\x3d"text-align:right;border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.amount | number:2}} \u20ac\n \x3c/td\x3e\n \x3ctd style\x3d"text-align:right;border-bottom:1px solid {{coloring_score( account.score )}}"\x3e\n {{account.score}}\n \x3c/td\x3e\n \x3c/tr\x3e\n \x3c/tbody\x3e\n \x3c/table\x3e\n \x3c/md-card\x3e --\x3e\n \x3c/md-content\x3e\n \x3c/md-card\x3e\n\x3c/md-content\x3e\n'}); app.service("API",["$http",function(a){this.balance=function(c){return a.get("/api/ledger/balance",{params:{period:c.period,categories:c.categories,depth:c.depth}})};this.register=function(c){return a.get("/api/ledger/register",{params:{period:c.period,categories:c.categories}})};this.graph_values=function(c){return a.get("/api/ledger/graph_values",{params:{period:c.period,categories:c.categories}})};this.budget=function(c){return a.get("/api/ledger/budget",{params:{period:c.period,categories:c.categories}})}; this.dates_salaries=function(){return a.get("/api/ledger/dates_salaries")};this.accounts=function(){return a.get("/api/ledger/accounts")};this.cleared=function(){return a.get("/api/ledger/cleared")}}]); diff --git a/public/app/ts/components/dashboard.ts b/public/app/ts/components/dashboard.ts index bb3eea0a..f6b60adf 100644 --- a/public/app/ts/components/dashboard.ts +++ b/public/app/ts/components/dashboard.ts @@ -1,289 +1,296 @@ app.component('dashboard', - { - controller: ['$filter', 'API', - function($filter, API) { - let ctrl = this; + { + controller: ['$filter', '$q', 'API', + function($filter, $q, API) { + let ctrl = this; - ctrl.xFunction = function() { - return function(d) { - return d.account; - }; - }; - ctrl.yFunction = function() { - return function(d) { - return d.amount; - }; - }; - ctrl.toolTipContentFunction = function() { - return function(key, x, y, e, graph) { - let details = ctrl.balance.details[key]; - return '

' + key + '

' + '' + _(details).map(function(transaction) { - return ''; - }).join('') + '' + '
' + transaction.date + '' + transaction.payee + '' + $filter('number')(transaction.amount, 2) + ' ' + transaction.currency + '
Total :' + x + ' €
'; - }; - }; + ctrl.xFunction = function() { + return function(d) { + return d.account; + }; + }; + ctrl.yFunction = function() { + return function(d) { + return d.amount; + }; + }; + ctrl.toolTipContentFunction = function() { + return function(key, x, y, e, graph) { + let details = ctrl.balance.details[key]; + return '

' + key + '

' + '' + _(details).map(function(transaction) { + return ''; + }).join('') + '' + '
' + transaction.date + '' + transaction.payee + '' + $filter('number')(transaction.amount, 2) + ' ' + transaction.currency + '
Total :' + x + ' €
'; + }; + }; - // compute an account's score: from 1 (good) to 10 (bad), 0 is neutral/undecided - let score_account = function(account) { - if (account.match(/^Income/)) { - return -10; - } else if (account.match(/^Expenses:(courses|Hang)$/)) { - return 1; - } else if (account.match(/^Expenses:Home/)) { - return 1; - } else if (account.match(/^Expenses:Health/)) { - return 1; - } else if (account.match(/^Expenses:Car/)) { - return 4; - } else if (account.match(/^Expenses:(Food|Transport)/)) { - return 5; - } else if (account.match(/^Expenses:(Shopping|Leisure)/)) { - return 9; - } else if (account.match(/^Expenses:Gadgets/)) { - return 10; - } else if (account.match(/^Liabilities/)) { - return 0; - } else if (account.match(/^Assets/)) { - return -100; - } else { - return 0; - } - }; + // compute an account's score: from 1 (good) to 10 (bad), 0 is neutral/undecided + let score_account = function(account) { + if (account.match(/^Income/)) { + return -10; + } else if (account.match(/^Expenses:(courses|Hang)$/)) { + return 1; + } else if (account.match(/^Expenses:Home/)) { + return 1; + } else if (account.match(/^Expenses:Health/)) { + return 1; + } else if (account.match(/^Expenses:Car/)) { + return 4; + } else if (account.match(/^Expenses:(Food|Transport)/)) { + return 5; + } else if (account.match(/^Expenses:(Shopping|Leisure)/)) { + return 9; + } else if (account.match(/^Expenses:Gadgets/)) { + return 10; + } else if (account.match(/^Liabilities/)) { + return 0; + } else if (account.match(/^Assets/)) { + return -100; + } else { + return 0; + } + }; - ctrl.coloring_score = function(score) { - let adjusted_score = score; - let color_scale = ['#99f', '#0f0', '#3f0', '#6f0', '#9f0', '#cf0', '#fc0', '#f90', '#f60', '#f30', '#f00']; + ctrl.coloring_score = function(score) { + let adjusted_score = score; + let color_scale = ['#99f', '#0f0', '#3f0', '#6f0', '#9f0', '#cf0', '#fc0', '#f90', '#f60', '#f30', '#f00']; - if (score <= -100) { - // Assets - adjusted_score = (score * -1) - 100; - color_scale = ['#f0f']; - } else if (score <= -10) { - // Income - adjusted_score = (score * -1) - 10; - color_scale = ['#360']; - } + if (score <= -100) { + // Assets + adjusted_score = (score * -1) - 100; + color_scale = ['#f0f']; + } else if (score <= -10) { + // Income + adjusted_score = (score * -1) - 10; + color_scale = ['#360']; + } - return color_scale[adjusted_score]; - }; + return color_scale[adjusted_score]; + }; - ctrl.color = function() { - return function(d, i) { - return ctrl.coloring_score(score_account(d.data.account)); - }; - }; + ctrl.color = function() { + return function(d, i) { + return ctrl.coloring_score(score_account(d.data.account)); + }; + }; - ctrl.filter_data = function() { - _(ctrl.balance.buckets).each(function(bucket) { - bucket.data = []; + ctrl.filter_data = function() { + _(ctrl.balance.buckets).each(function(bucket) { + bucket.data = []; - if (_(bucket.accounts_selected).isEmpty() && bucket.score_threshold === 0) { - bucket.data = bucket.raw_data; - } else { - _(bucket.accounts_selected).each(function(account_selected) { - bucket.data = bucket.data.concat($filter('filter')(bucket.raw_data, account_selected, true)); - }); - } + if (_(bucket.accounts_selected).isEmpty() && bucket.score_threshold === 0) { + bucket.data = bucket.raw_data; + } else { + _(bucket.accounts_selected).each(function(account_selected) { + bucket.data = bucket.data.concat($filter('filter')(bucket.raw_data, account_selected, true)); + }); + } - bucket.total_detailed = _.chain(bucket.data) - .groupBy(function(account) { - return account.account.split(':')[0]; - }) - .each(function(category) { - category.total = _(category).reduce(function(memo, account) { - return memo + account.amount; - }, 0); - }) - .value(); - bucket.total_detailed = _.chain(bucket.total_detailed) - .keys() - .map(function(key) { - return { - account: key, - amount: bucket.total_detailed[key].total - }; - }) - .value(); + bucket.total_detailed = _.chain(bucket.data) + .groupBy(function(account) { + return account.account.split(':')[0]; + }) + .each(function(category) { + category.total = _(category).reduce(function(memo, account) { + return memo + account.amount; + }, 0); + }) + .value(); + bucket.total_detailed = _.chain(bucket.total_detailed) + .keys() + .map(function(key) { + return { + account: key, + amount: bucket.total_detailed[key].total + }; + }) + .value(); - }); - }; + }); + }; - let Bucket = function(categories, period) { - let _this = this; - this.categories = categories; - this.period = period; - this.score_threshold = 0; - this.orderBy = 'amount'; - this.orderDesc = false; - this.order_by = function(field) { - if (_this.orderBy == field) { - _this.orderDesc = !_this.orderDesc; - } else { - _this.orderBy = field; - } - }; + let Bucket = function(categories, period) { + let _this = this; + this.categories = categories; + this.period = period; + this.score_threshold = 0; + this.orderBy = 'amount'; + this.orderDesc = false; + this.order_by = function(field) { + if (_this.orderBy == field) { + _this.orderDesc = !_this.orderDesc; + } else { + _this.orderBy = field; + } + }; - this.pie_graph_options = { - chart: { - type: 'pieChart', - donut: true, - donutRatio: 0.25, - height: 300, - x: function(d) { return d.account; }, - y: function(d) { return d.amount; }, - showLabels: false, - showLegend: true, - legendPosition: 'right', - showTooltipPercent: true, - duration: 500, - labelThreshold: 0.01, - labelSunbeamLayout: true, - labelsOutside: true - } - }; - }; + this.pie_graph_options = { + chart: { + type: 'pieChart', + donut: true, + donutRatio: 0.25, + height: 300, + x: function(d) { return d.account; }, + y: function(d) { return d.amount; }, + showLabels: false, + showLegend: true, + legendPosition: 'right', + showTooltipPercent: true, + duration: 500, + labelThreshold: 0.01, + labelSunbeamLayout: true, + labelsOutside: true + } + }; + }; - ctrl.depth = 99; + ctrl.depth = 99; - let retrieve_period_detailed_data = function() { - ctrl.balance = { - buckets: [new Bucket('Expenses Liabilities Equity Income', ctrl.period), - new Bucket('Assets', null)], - details: {} - }; + let retrieve_period_detailed_data = function() { + console.log(ctrl.period) + ctrl.balance = { + buckets: [new Bucket('Expenses Liabilities Equity Income', ctrl.period), + new Bucket('Assets', null)], + details: {} + }; - _(ctrl.balance.buckets).each(function(bucket) { - API.balance({ - period: bucket.period, - categories: bucket.categories, - depth: ctrl.depth - }) - .then(function(response) { - bucket.raw_data = _.chain(response.data) - .map(function(account) { - account.amount = (account.amount < 0) ? account.amount * -1 : account.amount; - account.score = score_account(account.account); - return account; - }) - .sortBy(function(account) { - return 1 / account.amount; - }) - .sortBy(function(account) { - return account.account.split(":")[0]; - }) - .value() - .reverse(); - bucket.raw_total = _(response.data).reduce(function(memo, account) { - return memo + account.amount; - }, 0); - bucket.accounts_selected = bucket.raw_data; + return $q.all(_(ctrl.balance.buckets).map(function(bucket) { + return API.balance({ + period: bucket.period, + categories: bucket.categories, + depth: ctrl.depth + }) + .then(function(response) { + bucket.raw_data = _.chain(response.data) + .map(function(account) { + account.amount = (account.amount < 0) ? account.amount * -1 : account.amount; + account.score = score_account(account.account); + return account; + }) + .sortBy(function(account) { + return 1 / account.amount; + }) + .sortBy(function(account) { + return account.account.split(":")[0]; + }) + .value() + .reverse(); + bucket.raw_total = _(response.data).reduce(function(memo, account) { + return memo + account.amount; + }, 0); + bucket.accounts_selected = bucket.raw_data; - ctrl.filter_data(); - }); - }); - }; + ctrl.filter_data(); + }); + })); + }; - let retrieve_accounts = function() { - API.accounts() - .then(function(response) { - ctrl.accounts = response.data.map(function(account_ary) { - return account_ary.join(':'); - }); - }); - }; + let retrieve_accounts = function() { + return $q.when(API.accounts() + .then(function(response) { + ctrl.accounts = response.data.map(function(account_ary) { + return account_ary.join(':'); + }); + })); + }; - let retrieve_graph_values = function(params) { - API.graph_values(params).then(function(response) { - ctrl.periods = []; + let retrieve_graph_values = function(params) { + return $q.when(API.graph_values(params) + .then(function(response) { + ctrl.periods = []; - let largest_cat = _(response.data).reduce(function(memo, cat) { - return cat.length > memo.length ? cat : memo; - }, []); - _.chain(largest_cat) - .pluck('date') - .each(function(date) { - _(response.data).each(function(cat) { - let value = _(cat).find({ date: date }); - if (_(value).isUndefined()) { - cat.push({ - date: date, - amount: 0, - currency: _(cat).first().currency - }); - } - }); - }); - _(response.data).each(function(cat) { - cat = _(cat).sortBy(function(month) { - return month.date; - }); - }); + let largest_cat = _(response.data).reduce(function(memo, cat) { + return cat.length > memo.length ? cat : memo; + }, []); + _.chain(largest_cat) + .pluck('date') + .each(function(date) { + _(response.data).each(function(cat) { + let value = _(cat).find({ date: date }); + if (_(value).isUndefined()) { + cat.push({ + date: date, + amount: 0, + currency: _(cat).first().currency + }); + } + }); + }); + _(response.data).each(function(cat) { + cat = _(cat).sortBy(function(month) { + return month.date; + }); + }); - ctrl.graphiques = { - monthly_values: { - options: { - chart: { - type: 'multiBarChart', - height: 300, - showControls: false, - showLegend: true, - showLabels: true, - stacked: false, - duration: 500, - reduceXTicks: false, - rotateLabels: 67, - labelSunbeamLayout: true, - useInteractiveGuideline: false, - multibar: { - dispatch: { - elementClick: function(event) { - ctrl.period = event.data.x; - retrieve_period_detailed_data(); - } - } - } - } - }, - data: _.chain(response.data) - .keys() - .reverse() - .map(function(key) { - let multiplicator = (key == "Income") ? -1 : 1; - return { - key: key, - values: _.chain(response.data[key]).map(function(value) { - let date = new Date(value.date); - let period = date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1); - ctrl.periods.push(period); + ctrl.graphiques = { + monthly_values: { + options: { + chart: { + type: 'multiBarChart', + height: 300, + showControls: false, + showLegend: true, + showLabels: true, + stacked: false, + duration: 500, + reduceXTicks: false, + rotateLabels: 67, + labelSunbeamLayout: true, + useInteractiveGuideline: false, + multibar: { + dispatch: { + elementClick: function(event) { + ctrl.period = event.data.x; + retrieve_period_detailed_data(); + } + } + } + } + }, + data: _.chain(response.data) + .keys() + .reverse() + .map(function(key) { + let multiplicator = (key == "Income") ? -1 : 1; + return { + key: key, + values: _.chain(response.data[key]).map(function(value) { + let date = new Date(value.date); + let period = date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1); + ctrl.periods.push(period); - return { - key: key, - x: period, - y: parseInt(value.amount) * multiplicator - }; - }) - .sortBy(function(item) { return item.x; }) - .value() - }; - }) - .value() - } - }; + return { + key: key, + x: period, + y: parseInt(value.amount) * multiplicator + }; + }) + .sortBy(function(item) { return item.x; }) + .value() + }; + }) + .value() + } + }; - ctrl.periods = _.chain(ctrl.periods).uniq().sort().reverse().value(); - ctrl.period = _(ctrl.periods).first(); - }); - }; + ctrl.periods = _.chain(ctrl.periods).uniq().sort().reverse().value(); + ctrl.period = _(ctrl.periods).first(); + })); + }; - ctrl.graphed_accounts = ['Expenses', 'Income']; + ctrl.graphed_accounts = ['Expenses', 'Income']; - retrieve_accounts(); - retrieve_period_detailed_data(); - retrieve_graph_values({ period: '', - categories: ctrl.graphed_accounts.join(' ') }); - } - ], - template: ` + retrieve_accounts().then(function(response) { + retrieve_graph_values({ + period: '', + categories: ctrl.graphed_accounts.join(' ') + }).then(function(response) { + retrieve_period_detailed_data(); + + }); + }); + } + ], + template: ` @@ -346,4 +353,4 @@ app.component('dashboard', ` - }); + });