2014-07-30 20:48:32 +02:00
|
|
|
app.controller( 'BalanceCtrl',
|
2014-08-01 22:26:20 +02:00
|
|
|
[ '$scope', '$http', '$filter',
|
|
|
|
function( $scope, $http, $filter ) {
|
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
|
|
|
|
2014-08-09 18:16:10 +02:00
|
|
|
$scope.color = function() {
|
2014-07-30 23:35:50 +02:00
|
|
|
return function( d, i ) {
|
2014-08-09 18:16:10 +02:00
|
|
|
if ( d.data.account.match( /^Income:salaire$/ ) ) {
|
|
|
|
return '#0f0';
|
|
|
|
} else if ( d.data.account.match( /^Income:Gift$/ ) ) {
|
|
|
|
return '#ef0';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:Home/ ) ) {
|
|
|
|
return '#00f';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:Health/ ) ) {
|
|
|
|
return '#0cf';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:(courses|Hang)$/ ) ) {
|
|
|
|
return '#0b6';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:Car/ ) ) {
|
|
|
|
return '#111';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:(Food|Transport)/ ) ) {
|
|
|
|
return '#b60';
|
|
|
|
} else if ( d.data.account.match( /^Expenses:(Shopping|Gadgets|Entertainement)/ ) ) {
|
|
|
|
return '#f00';
|
|
|
|
} else {
|
|
|
|
return '#ddd';
|
|
|
|
}
|
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
|
|
|
}]);
|