ledgerrb/public/app/js/controllers/BalanceCtrl.js

192 lines
6.6 KiB
JavaScript
Raw Normal View History

2014-07-30 20:48:32 +02:00
app.controller( 'BalanceCtrl',
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 ];
2014-10-12 10:42:53 +02:00
return '<material-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>';
} ).join( '' ) + '<tr><th></th><th>Total :</th><th>' + x + ' €</th></tr>' + '</table></material-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 ) {
2014-08-19 17:38:58 +02:00
if ( account.match( /^Income:(salaire|Sécu|Mutuelle)$/ ) ) {
2014-08-19 17:20:09 +02:00
return 1;
2014-08-19 17:38:58 +02:00
} else if ( account.match( /^Income:(Gift|Remboursement)$/ ) ) {
2014-08-19 17:20:09 +02:00
return 6;
} 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)/ ) ) {
return 6;
2014-08-19 17:20:09 +02:00
} else if ( account.match( /^Expenses:(Shopping|Entertainement)/ ) ) {
return 9;
} else if ( account.match( /^Expenses:Gadgets/ ) ) {
return 10;
} 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 ) {
2014-09-25 16:53:54 +02:00
var color_scale = [ '#99f', '#0f0', '#3f0', '#6f0', '#9f0', '#cf0', '#fc0', '#f90', '#f60', '#f30', '#f00' ];
2014-08-19 17:20:09 +02:00
return color_scale[ score ];
};
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 ) );
} );
}
bucket.total = _( bucket.data ).reduce( function ( memo, account ) {
return memo + account.amount;
}, 0 );
2014-10-12 10:42:53 +02:00
} );
};
$scope.select = { score_higher_than: function( bucket, score ) {
bucket.accounts_selected = _(bucket.raw_data).filter( function( account ) {
return account.score >= score;
} );
}};
2014-10-27 15:20:20 +01:00
var Bucket = function( categories ) {
var _this = this;
this.categories = categories;
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;
}
};
};
$scope.balance = {
2014-10-27 15:20:20 +01:00
buckets: [ new Bucket( 'Expenses' ),
new Bucket( 'Income' ) ],
details: {}
};
2014-10-12 10:42:53 +02:00
2014-10-01 15:25:20 +02:00
var retrieve_data = function () {
2014-10-05 09:49:42 +02:00
var from, to, period;
2014-10-05 09:49:42 +02:00
if ( $scope.period_offset === $scope.dates_salaries.length ) {
$scope.from_date = moment( _($scope.dates_salaries).last() ).add( 1, 'month' ).toDate();
2014-10-05 09:49:42 +02:00
from = moment( $scope.from_date );
period = 'from ' + from.year() + '-' + ( from.month() + 1 ) + '-' + from.date();
} else {
$scope.from_date = new Date( $scope.dates_salaries[ $scope.period_offset ] );
$scope.to_date = ( $scope.period_offset < $scope.dates_salaries.length - 1 ) ? new Date( $scope.dates_salaries[ $scope.period_offset + 1 ] ) : moment( $scope.from_date ).add( 1, 'month' ).toDate();
from = moment( $scope.from_date );
to = moment( $scope.to_date );
period = 'from ' + from.year() + '-' + ( from.month() + 1 ) + '-' + from.date();
period += ' to ' + to.year() + '-' + ( to.month() + 1 ) + '-' + to.date();
}
2014-10-01 13:58:08 +02:00
2014-10-12 08:36:08 +02:00
API.register( { period: period,
categories: '' } )
.then( function ( response ) {
2014-10-12 11:13:02 +02:00
$scope.balance.details = _($scope.balance.details).extend( _(response.data.values).groupBy( 'account' ) );
2014-10-12 08:36:08 +02:00
} );
2014-10-05 09:50:00 +02:00
_($scope.balance.buckets).each( function( bucket ) {
2014-10-12 08:23:36 +02:00
API.balance( { period: period,
categories: bucket.categories } )
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
} )
2014-10-05 09:50:00 +02:00
.value();
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
$scope.select.score_higher_than( bucket, bucket.score_threshold );
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
};
2014-09-30 12:15:37 +02:00
$scope.dates_salaries = [];
$scope.period_offset = 0;
2014-10-01 15:25:20 +02:00
$scope.after = function () {
2014-10-05 09:50:00 +02:00
if ( $scope.period_offset < $scope.dates_salaries.length ) {
2014-10-01 15:25:20 +02:00
$scope.period_offset++;
}
};
$scope.before = function () {
if ( $scope.period_offset > 0 ) {
$scope.period_offset--;
}
};
$scope.reset_offset = function () {
$scope.period_offset = $scope.dates_salaries.length - 1;
};
2014-09-30 12:15:37 +02:00
2014-10-12 08:23:36 +02:00
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
API.dates_salaries()
.then( function ( response ) {
$scope.dates_salaries = response.data;
$scope.reset_offset();
// retrieve_data() when the value of week_offset changes
// n.b.: triggered when week_offset is initialized above
$scope.$watch( 'period_offset', function () {
retrieve_data();
} );
} );
2014-10-12 08:23:36 +02:00
} );
2014-10-01 15:25:20 +02:00
}
] );