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

186 lines
6.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',
2014-10-01 15:25:20 +02:00
function ( $scope, $http, $filter, ngTableParams ) {
$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-01 15:25:20 +02:00
return '<material-content><h3>' + details.key + '</h3>' + '<table>' + _( details.values ).map( function ( transaction ) {
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-01 15:25:20 +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;
} 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-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-10-01 15:25:20 +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-08-09 18:42:54 +02:00
2014-10-01 15:25:20 +02:00
var retrieve_data = function () {
2014-09-30 12:15:37 +02:00
$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();
2014-10-01 15:25:20 +02:00
if ( !_( $scope.to_date ).isNull() ) {
2014-09-30 12:15:37 +02:00
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-10-01 15:25:20 +02:00
$scope.balance = {
buckets: [ {
name: 'Expenses',
data: [],
total: 0
}, {
name: 'Income',
data: [],
total: 0
} ],
details: {}
};
2014-08-01 22:26:20 +02:00
2014-10-01 15:25:20 +02:00
$http.get( '/api/ledger/balance', {
params: {
period: period,
categories: 'Expenses'
}
} )
.then( function ( response ) {
$scope.balance.buckets[ 0 ].data = _( 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-10-01 15:25:20 +02:00
_( $scope.balance.buckets[ 0 ].data ).each(
function ( account ) {
$http.get( '/api/ledger/register', {
params: {
period: period,
category: account.account
}
} )
.then( function ( response ) {
2014-08-01 22:26:20 +02:00
$scope.balance.details[ account.account ] = response.data;
} );
} );
2014-10-01 15:25:20 +02:00
$scope.balance.buckets[ 0 ].total = _( response.data ).reduce( function ( memo, account ) {
return memo + account.amount;
}, 0 );
2014-07-30 23:35:50 +02:00
} );
2014-10-01 15:25:20 +02:00
$http.get( '/api/ledger/balance', {
params: {
period: period,
categories: 'Income'
}
} )
2014-09-25 16:53:31 +02:00
2014-10-01 15:25:20 +02:00
.then( function ( response ) {
$scope.balance.buckets[ 1 ].data = _( response.data )
.map( function ( account ) {
2014-08-01 22:26:20 +02:00
account.amount = account.amount * -1;
return account;
} );
2014-10-01 15:25:20 +02:00
$scope.balance.buckets[ 1 ].data = _( $scope.balance.buckets[ 1 ].data )
.sortBy( function ( account ) {
2014-08-01 22:26:20 +02:00
return account.amount;
} );
2014-10-01 15:25:20 +02:00
_( $scope.balance.buckets[ 1 ].data )
.each( function ( account ) {
$http.get( '/api/ledger/register', {
params: {
period: period,
category: account.account
}
} )
.then( function ( response ) {
2014-08-01 22:26:20 +02:00
$scope.balance.details[ account.account ] = response.data;
} );
} );
2014-10-01 15:25:20 +02:00
$scope.balance.buckets[ 1 ].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;
2014-10-01 15:25:20 +02:00
$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;
};
2014-09-30 12:15:37 +02:00
$http.get( '/api/ledger/dates_salaries' )
2014-10-01 15:25:20 +02:00
.then( function ( response ) {
2014-09-30 12:15:37 +02:00
$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
2014-10-01 15:25:20 +02:00
$scope.$watch( 'period_offset', function () {
retrieve_data();
} );
2014-09-30 12:15:37 +02:00
} );
2014-10-01 15:25:20 +02:00
}
] );