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 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-19 17:20:09 +02:00
|
|
|
// compute an account's score: from 1 (good) to 10 (bad), 0 is neutral/undecided
|
2014-08-19 17:28:41 +02:00
|
|
|
$scope.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;
|
2014-08-19 17:08:58 +02:00
|
|
|
} else if ( account.match( /^Expenses:(courses|Hang)$/ ) ) {
|
2014-08-19 17:20:09 +02:00
|
|
|
return 1;
|
2014-08-19 17:08:58 +02:00
|
|
|
} else if ( account.match( /^Expenses:Home/ ) ) {
|
2014-08-19 17:20:09 +02:00
|
|
|
return 1;
|
2014-08-19 17:08:58 +02:00
|
|
|
} else if ( account.match( /^Expenses:Health/ ) ) {
|
2014-08-19 17:20:09 +02:00
|
|
|
return 1;
|
2014-08-19 17:08:58 +02:00
|
|
|
} else if ( account.match( /^Expenses:Car/ ) ) {
|
2014-08-19 17:20:09 +02:00
|
|
|
return 4;
|
2014-08-19 17:08:58 +02:00
|
|
|
} 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;
|
2014-08-19 17:08:58 +02:00
|
|
|
} else {
|
2014-08-19 17:20:09 +02:00
|
|
|
return 0;
|
2014-08-19 17:08:58 +02:00
|
|
|
}
|
|
|
|
};
|
2014-08-19 17:20:09 +02:00
|
|
|
|
2014-08-19 17:28:41 +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-08-09 18:16:10 +02:00
|
|
|
$scope.color = function() {
|
2014-07-30 23:35:50 +02:00
|
|
|
return function( d, i ) {
|
2014-08-19 17:28:41 +02:00
|
|
|
return $scope.coloring_score( $scope.score_account( d.data.account ) );
|
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
|
2014-08-19 17:09:17 +02:00
|
|
|
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-08-09 18:42:54 +02:00
|
|
|
|
2014-07-30 23:35:50 +02:00
|
|
|
var retrieve_data = function() {
|
2014-09-30 12:15:37 +02:00
|
|
|
// $scope.from_date = moment().subtract( $scope.period_offset, 'months' ).startOf( 'month' ).toDate();
|
|
|
|
// $scope.to_date = moment().subtract( $scope.period_offset, 'months' ).endOf( 'month' ).toDate();
|
|
|
|
$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 ] ) : null;
|
2014-09-25 16:53:31 +02:00
|
|
|
var from = moment( $scope.from_date );
|
2014-09-30 12:15:37 +02:00
|
|
|
var period = 'from ' + from.year() + '-' + ( from.month() + 1 ) + '-' + from.date();
|
|
|
|
if ( !_($scope.to_date).isNull() ) {
|
|
|
|
var to = moment( $scope.to_date );
|
|
|
|
period = period + ' to ' + to.year() + '-' + ( to.month() + 1 ) + '-' + to.date();
|
|
|
|
}
|
2014-10-01 13:58:08 +02:00
|
|
|
|
2014-08-01 22:26:20 +02:00
|
|
|
$scope.balance = { expenses: [],
|
|
|
|
income: [],
|
|
|
|
details: {} };
|
|
|
|
|
2014-09-25 16:53:31 +02:00
|
|
|
$http.get( '/api/ledger/balance',
|
|
|
|
{ params: { period: period,
|
|
|
|
categories: 'Expenses' } } )
|
2014-07-30 23:35:50 +02:00
|
|
|
.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 ) {
|
2014-09-25 16:53:31 +02:00
|
|
|
$http.get( '/api/ledger/register',
|
|
|
|
{ params: { period: period,
|
|
|
|
category: account.account } } )
|
2014-08-01 22:26:20 +02:00
|
|
|
.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 );
|
|
|
|
} );
|
2014-09-25 16:53:31 +02:00
|
|
|
$http.get( '/api/ledger/balance',
|
|
|
|
{ params: { period: period,
|
|
|
|
categories: 'Income' } } )
|
|
|
|
|
2014-07-30 23:35:50 +02:00
|
|
|
.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 ) {
|
2014-09-25 16:53:31 +02:00
|
|
|
$http.get( '/api/ledger/register',
|
|
|
|
{ params: { period: period,
|
|
|
|
category: account.account } } )
|
2014-08-01 22:26:20 +02:00
|
|
|
.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
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2014-09-30 12:15:37 +02:00
|
|
|
$scope.dates_salaries = [];
|
|
|
|
$scope.period_offset = 0;
|
|
|
|
$scope.after = function() { if ( $scope.period_offset < $scope.dates_salaries.length - 1 ) { $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; };
|
|
|
|
|
|
|
|
$http.get( '/api/ledger/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(); } );
|
|
|
|
|
|
|
|
} );
|
|
|
|
} ] );
|