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

174 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-07-30 20:48:32 +02:00
app.controller( 'BalanceCtrl',
2014-08-09 18:42:54 +02:00
[ '$scope', '$http', '$filter', 'ngTableParams',
function( $scope, $http, $filter, ngTableParams ) {
2014-07-30 23:35:50 +02:00
$scope.now = moment();
$scope.previous_period = function() {
$scope.now.subtract( 'months', 1 );
retrieve_data();
};
$scope.next_period = function() {
$scope.now.add( 'months', 1 );
retrieve_data();
};
2014-07-30 20:48:32 +02:00
$scope.xFunction = function() {
return function( d ) {
return d.account;
};
};
$scope.yFunction = function() {
return function( d ) {
return d.amount;
};
};
2014-07-30 23:35:50 +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 ];
return '<h3>' + details.key + '</h3>'
2014-08-01 22:57:13 +02:00
+ '<table>'
2014-08-01 22:40:57 +02:00
+ _(details.values).map( function( transaction ) {
return '<tr><td>'
+ transaction.date + '</td><td>'
+ transaction.payee + '</td><td style="text-align: right">'
2014-08-01 22:57:13 +02:00
+ $filter( 'number' )( transaction.amount, 2 ) + ' '
2014-08-01 22:40:57 +02:00
+ transaction.currency + '</td></tr>';
}).join( '' )
2014-08-01 22:57:13 +02:00
+ '<tr><th></th><th>Total :</th><th>' + x + ' €</th></tr>'
2014-08-01 22:40:57 +02:00
+ '</table>';
2014-07-30 23:35:50 +02:00
};
};
2014-08-01 22:26:20 +02:00
// compute an account's score: from 0 (good) to 9 (bad)
var score_account = function( account ) {
if ( account.match( /^Income:salaire$/ ) ) {
return 0;
} else if ( account.match( /^Income:Gift$/ ) ) {
return 5;
} else if ( account.match( /^Expenses:(courses|Hang)$/ ) ) {
return 0;
} else if ( account.match( /^Expenses:Home/ ) ) {
return 0;
} else if ( account.match( /^Expenses:Health/ ) ) {
return 0;
} else if ( account.match( /^Expenses:Car/ ) ) {
return 3;
} else if ( account.match( /^Expenses:(Food|Transport)/ ) ) {
return 6;
} else if ( account.match( /^Expenses:(Shopping|Gadgets|Entertainement)/ ) ) {
return 8;
} else {
return 5;
}
};
2014-08-09 18:16:10 +02:00
$scope.color = function() {
2014-07-30 23:35:50 +02:00
return function( d, i ) {
var score = score_account( d.data.account );
switch( score ) {
case 0:
2014-08-09 18:16:10 +02:00
return '#0f0';
break;
case 1:
return '#3f0';
break;
case 2:
return '#6f0';
break;
case 3:
return '#9f0';
break;
case 4:
return '#cf0';
break;
case 5:
return '#fc0';
break;
case 6:
return '#f90';
break;
case 7:
return '#f60';
break;
case 8:
return '#f30';
break;
case 9:
2014-08-09 18:16:10 +02:00
return '#f00';
break;
default:
return '#fff';
2014-08-09 18:16:10 +02:00
}
2014-07-30 23:35:50 +02:00
};
};
2014-08-09 18:42:54 +02:00
$scope.tableParams = new ngTableParams( { page: 1, // show first page
count: 999 // count per page
},
{ counts: [], // hide page counts control
total: 1, // value less than count hide pagination
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
} } );
2014-07-30 23:35:50 +02:00
var retrieve_data = function() {
2014-08-01 22:26:20 +02:00
$scope.balance = { expenses: [],
income: [],
details: {} };
2014-07-30 23:35:50 +02:00
$http.get( '/api/ledger/balance?period='
+ $scope.now.year()
+ '-'
+ ( $scope.now.month() + 1 )
+ '&categories=Expenses' )
.then( function( response ) {
2014-07-31 18:08:40 +02:00
$scope.balance.expenses = _(response.data).sortBy( function( account ) {
2014-07-31 18:11:15 +02:00
return 1 / account.amount;
2014-07-31 18:08:40 +02:00
} );
2014-08-01 22:26:20 +02:00
_($scope.balance.expenses).each(
function( account ) {
$http.get( '/api/ledger/register?period='
+ $scope.now.year()
+ '-'
+ ( $scope.now.month() + 1 )
+ '&category='
+ account.account )
.then( function( response ) {
$scope.balance.details[ account.account ] = response.data;
} );
} );
2014-07-30 23:35:50 +02:00
$scope.balance.expenses_total = _(response.data).reduce( function( memo, account ){ return memo + account.amount; }, 0 );
} );
$http.get( '/api/ledger/balance?period='
+ $scope.now.year()
+ '-'
+ ( $scope.now.month() + 1 )
+ '&categories=Income' )
.then( function( response ) {
2014-08-01 22:26:20 +02:00
$scope.balance.income = _(response.data)
.map( function( account ) {
account.amount = account.amount * -1;
return account;
} );
$scope.balance.income = _($scope.balance.income)
.sortBy( function( account ) {
return account.amount;
} );
_($scope.balance.income)
.each( function( account ) {
$http.get( '/api/ledger/register?period='
+ $scope.now.year()
+ '-'
+ ( $scope.now.month() + 1 )
+ '&category='
+ account.account )
.then( function( response ) {
$scope.balance.details[ account.account ] = response.data;
} );
} );
$scope.balance.income_total = _(response.data)
.reduce( function( memo, account ){ return memo + account.amount; }, 0 );
2014-07-30 23:35:50 +02:00
} );
};
retrieve_data();
2014-07-30 20:48:32 +02:00
}]);