ledgerrb/public/app/js/controllers/DashboardCtrl.js

238 lines
7.8 KiB
JavaScript
Raw Normal View History

app.controller( 'DashboardCtrl',
2014-10-27 22:32:04 +01:00
[ '$scope', '$filter', 'API',
function ( $scope, $filter, API ) {
2014-10-01 15:25:20 +02:00
$scope.xFunction = function () {
return function ( d ) {
2014-07-30 20:48:32 +02:00
return d.account;
};
};
2014-10-01 15:25:20 +02:00
$scope.yFunction = function () {
return function ( d ) {
2014-07-30 20:48:32 +02:00
return d.amount;
};
};
2014-10-01 15:25:20 +02:00
$scope.toolTipContentFunction = function () {
return function ( key, x, y, e, graph ) {
2014-08-01 22:40:57 +02:00
var details = $scope.balance.details[ key ];
2015-02-04 16:38:34 +01:00
return '<md-content><h3>' + key + '</h3>' + '<table>' + _( details ).map( function ( transaction ) {
2014-10-01 15:25:20 +02:00
return '<tr><td>' + transaction.date + '</td><td>' + transaction.payee + '</td><td style="text-align: right">' + $filter( 'number' )( transaction.amount, 2 ) + ' ' + transaction.currency + '</td></tr>';
2015-02-04 16:38:34 +01:00
} ).join( '' ) + '<tr><th></th><th>Total :</th><th>' + x + ' €</th></tr>' + '</table></md-content>';
2014-07-30 23:35:50 +02:00
};
};
2014-08-01 22:26:20 +02:00
2014-08-19 17:20:09 +02:00
// compute an account's score: from 1 (good) to 10 (bad), 0 is neutral/undecided
2014-10-12 12:28:20 +02:00
var score_account = function ( account ) {
2015-10-02 15:02:02 +02:00
if ( account.match( /^Income/ ) ) {
return -10;
} else if ( account.match( /^Expenses:(courses|Hang)$/ ) ) {
2014-08-19 17:20:09 +02:00
return 1;
} else if ( account.match( /^Expenses:Home/ ) ) {
2014-08-19 17:20:09 +02:00
return 1;
} else if ( account.match( /^Expenses:Health/ ) ) {
2014-08-19 17:20:09 +02:00
return 1;
} else if ( account.match( /^Expenses:Car/ ) ) {
2014-08-19 17:20:09 +02:00
return 4;
} else if ( account.match( /^Expenses:(Food|Transport)/ ) ) {
2015-05-15 09:22:34 +02:00
return 5;
2014-11-06 16:46:42 +01:00
} else if ( account.match( /^Expenses:(Shopping|Leisure)/ ) ) {
2014-08-19 17:20:09 +02:00
return 9;
} else if ( account.match( /^Expenses:Gadgets/ ) ) {
return 10;
2015-05-15 10:45:03 +02:00
} else if ( account.match( /^Liabilities/ ) ) {
2015-10-02 15:02:02 +02:00
return 0;
2015-05-15 10:45:03 +02:00
} else if ( account.match( /^Assets/ ) ) {
2015-10-02 15:02:02 +02:00
return -100;
} else {
2014-08-19 17:20:09 +02:00
return 0;
}
};
2014-08-19 17:20:09 +02:00
2014-10-01 15:25:20 +02:00
$scope.coloring_score = function ( score ) {
2015-05-15 10:45:03 +02:00
var adjusted_score = score;
2014-09-25 16:53:54 +02:00
var color_scale = [ '#99f', '#0f0', '#3f0', '#6f0', '#9f0', '#cf0', '#fc0', '#f90', '#f60', '#f30', '#f00' ];
2015-05-15 10:45:03 +02:00
2015-10-02 15:02:02 +02:00
if ( score <= -100 ) {
2015-05-15 10:45:03 +02:00
// Assets
2015-10-02 15:02:02 +02:00
adjusted_score = ( score * -1 ) - 100;
2015-05-15 10:45:03 +02:00
color_scale = [ '#f0f' ];
2015-10-02 15:02:02 +02:00
} else if ( score <= -10 ) {
2015-05-15 10:45:03 +02:00
// Income
2015-10-02 15:02:02 +02:00
adjusted_score = ( score * -1 ) - 10;
color_scale = [ '#360' ];
2015-05-15 10:45:03 +02:00
}
return color_scale[ adjusted_score ];
2014-08-19 17:20:09 +02:00
};
2014-10-01 15:25:20 +02:00
$scope.color = function () {
return function ( d, i ) {
2014-10-12 12:28:20 +02:00
return $scope.coloring_score( score_account( d.data.account ) );
2014-07-30 23:35:50 +02:00
};
};
2014-10-12 11:13:02 +02:00
$scope.filter_data = function() {
2014-10-12 10:42:53 +02:00
_($scope.balance.buckets).each( function( bucket ) {
2014-10-12 11:13:02 +02:00
bucket.data = [];
if ( _(bucket.accounts_selected).isEmpty() && bucket.score_threshold === 0 ) {
2014-10-12 11:13:02 +02:00
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 ) );
} );
}
2015-05-24 11:46:05 +02:00
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 );
} )
2015-10-02 15:02:02 +02:00
.value();
2015-05-24 11:46:05 +02:00
bucket.total_detailed = _.chain(bucket.total_detailed)
.keys()
.map( function( key ) {
return { account: key,
amount: bucket.total_detailed[key].total };
} )
.value();
2014-10-12 10:42:53 +02:00
} );
};
var Bucket = function( categories, period ) {
2014-10-27 15:20:20 +01:00
var _this = this;
this.categories = categories;
this.period = period;
2014-10-27 15:20:20 +01:00
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;
}
};
};
2015-05-24 13:10:29 +02:00
$scope.depth = 99;
2014-11-08 17:40:48 +01:00
2015-08-30 19:54:06 +02:00
var retrieve_period_detailed_data = function () {
$scope.balance = {
2015-08-30 19:54:06 +02:00
buckets: [ new Bucket( 'Expenses Liabilities Equity Income', $scope.period ),
new Bucket( 'Assets', null ) ],
details: {}
};
2014-10-05 09:50:00 +02:00
_($scope.balance.buckets).each( function( bucket ) {
API.balance( { period: bucket.period,
2015-05-24 13:10:29 +02:00
categories: bucket.categories,
depth: $scope.depth } )
2014-10-05 09:50:00 +02:00
.then( function ( response ) {
2014-10-12 11:13:02 +02:00
bucket.raw_data = _.chain( response.data )
2014-10-05 09:50:00 +02:00
.map( function( account ) {
account.amount = ( account.amount < 0 ) ? account.amount * -1 : account.amount;
2014-10-12 12:28:20 +02:00
account.score = score_account( account.account );
2014-10-05 09:50:00 +02:00
return account;
2014-10-01 15:25:20 +02:00
} )
2014-10-05 09:50:00 +02:00
.sortBy( function ( account ) {
return 1 / account.amount;
2014-10-01 15:25:20 +02:00
} )
2015-05-15 10:45:03 +02:00
.sortBy( function ( account ) {
return account.account.split(":")[0];
} )
.value()
.reverse();
2014-10-12 12:28:20 +02:00
bucket.raw_total = _( response.data ).reduce( function ( memo, account ) {
return memo + account.amount;
}, 0 );
2014-10-12 11:13:02 +02:00
bucket.accounts_selected = bucket.raw_data;
2014-10-12 10:42:53 +02:00
2014-10-12 11:13:02 +02:00
$scope.filter_data();
2014-10-05 09:50:00 +02:00
} );
} );
2014-07-30 23:35:50 +02:00
};
2015-08-30 19:54:06 +02:00
var retrieve_accounts = function() {
API.accounts()
.then( function ( response ) {
$scope.accounts = response.data.map( function( account_ary ) {
return account_ary.join( ':' );
2014-10-12 10:42:53 +02:00
} );
2015-08-30 19:54:06 +02:00
} );
};
var retrieve_graph_values = function( params ) {
API.graph_values( params ).then( function( response ) {
$scope.periods = [];
2016-08-09 15:30:06 +02:00
var 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 ) {
var 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;
} );
} );
console.log( response.data )
2015-08-30 19:54:06 +02:00
$scope.monthly_values = _.chain( response.data )
.keys()
.reverse()
.map( function( key ) {
var multiplicator = ( key == "Income" ) ? -1 : 1;
return { "key": key,
"values": _(response.data[ key ]).map( function( value ) {
var date = new Date( value.date );
var period = date.getFullYear() + '-' + ( date.getMonth() < 9 ? '0' : '' ) + ( date.getMonth() + 1 );
$scope.periods.push( period );
return [ period,
2015-08-30 19:54:06 +02:00
parseInt( value.amount ) * multiplicator ];
2015-10-09 10:09:22 +02:00
} )
};
2015-08-30 19:54:06 +02:00
} )
.value();
2015-10-09 10:09:22 +02:00
$scope.periods = _.chain($scope.periods).uniq().sort().reverse().value();
$scope.period = _($scope.periods).first();
2014-10-12 08:23:36 +02:00
} );
2015-08-30 19:54:06 +02:00
};
$scope.barGraphToolTipContentFunction = function () {
return function ( key, x, y, e, graph ) {
return '<md-content><h3><em>' + key + '</em> during ' + x + '</h3><h2>' + y + ' €</h2></md-content>';
};
};
2015-09-03 16:55:58 +02:00
$scope.graphed_accounts = [ 'Expenses', 'Income' ];
2015-08-30 19:54:06 +02:00
$scope.$watch( 'period', function () {
retrieve_period_detailed_data();
} );
2015-08-30 19:54:06 +02:00
retrieve_accounts();
2015-09-03 16:55:58 +02:00
$scope.$watch( 'graphed_accounts', function () {
retrieve_graph_values( { period: '',
categories: $scope.graphed_accounts.join(' ') } );
} );
$scope.$on( 'elementClick.directive', function( angularEvent, event ) {
$scope.period = event.point[ 0 ];
retrieve_period_detailed_data();
} );
2014-10-01 15:25:20 +02:00
}
] );