refactoring of period

This commit is contained in:
Gwenhael Le Moine 2014-09-25 16:53:31 +02:00
parent 00faf7d479
commit 346c16dbf6
2 changed files with 39 additions and 42 deletions

View file

@ -1,16 +1,6 @@
app.controller( 'BalanceCtrl', app.controller( 'BalanceCtrl',
[ '$scope', '$http', '$filter', 'ngTableParams', [ '$scope', '$http', '$filter', 'ngTableParams',
function( $scope, $http, $filter, ngTableParams ) { function( $scope, $http, $filter, ngTableParams ) {
$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();
};
$scope.xFunction = function() { $scope.xFunction = function() {
return function( d ) { return function( d ) {
return d.account; return d.account;
@ -94,39 +84,49 @@ app.controller( 'BalanceCtrl',
// } // }
} ); } );
$scope.month_offset = 0;
// retrieve_data() when the value of week_offset changes
// n.b.: triggered when week_offset is initialized above
$scope.$watch( 'month_offset', function() { retrieve_data(); } );
$scope.incr_offset = function() { $scope.month_offset++; };
$scope.decr_offset = function() { $scope.month_offset--; };
$scope.reset_offset = function() { $scope.month_offset = 0; };
var retrieve_data = function() { var retrieve_data = function() {
$scope.from_date = moment().subtract( $scope.month_offset, 'months' ).startOf( 'month' ).toDate();
$scope.to_date = moment().subtract( $scope.month_offset, 'months' ).endOf( 'month' ).toDate();
var from = moment( $scope.from_date );
var to = moment( $scope.to_date );
var period = 'from ' + from.year() + '-' + ( from.month() + 1 ) + '-' + from.date() + ' to ' + to.year() + '-' + ( to.month() + 1 ) + '-' + to.date();
$scope.balance = { expenses: [], $scope.balance = { expenses: [],
income: [], income: [],
details: {} }; details: {} };
$http.get( '/api/ledger/balance?period=' $http.get( '/api/ledger/balance',
+ $scope.now.year() { params: { period: period,
+ '-' categories: 'Expenses' } } )
+ ( $scope.now.month() + 1 )
+ '&categories=Expenses' )
.then( function( response ) { .then( function( response ) {
$scope.balance.expenses = _(response.data).sortBy( function( account ) { $scope.balance.expenses = _(response.data).sortBy( function( account ) {
return 1 / account.amount; return 1 / account.amount;
} ); } );
_($scope.balance.expenses).each( _($scope.balance.expenses).each(
function( account ) { function( account ) {
$http.get( '/api/ledger/register?period=' $http.get( '/api/ledger/register',
+ $scope.now.year() { params: { period: period,
+ '-' category: account.account } } )
+ ( $scope.now.month() + 1 )
+ '&category='
+ account.account )
.then( function( response ) { .then( function( response ) {
$scope.balance.details[ account.account ] = response.data; $scope.balance.details[ account.account ] = response.data;
} ); } );
} ); } );
$scope.balance.expenses_total = _(response.data).reduce( function( memo, account ){ return memo + account.amount; }, 0 ); $scope.balance.expenses_total = _(response.data).reduce( function( memo, account ){ return memo + account.amount; }, 0 );
} ); } );
$http.get( '/api/ledger/balance?period=' $http.get( '/api/ledger/balance',
+ $scope.now.year() { params: { period: period,
+ '-' categories: 'Income' } } )
+ ( $scope.now.month() + 1 )
+ '&categories=Income' )
.then( function( response ) { .then( function( response ) {
$scope.balance.income = _(response.data) $scope.balance.income = _(response.data)
.map( function( account ) { .map( function( account ) {
@ -139,12 +139,9 @@ app.controller( 'BalanceCtrl',
} ); } );
_($scope.balance.income) _($scope.balance.income)
.each( function( account ) { .each( function( account ) {
$http.get( '/api/ledger/register?period=' $http.get( '/api/ledger/register',
+ $scope.now.year() { params: { period: period,
+ '-' category: account.account } } )
+ ( $scope.now.month() + 1 )
+ '&category='
+ account.account )
.then( function( response ) { .then( function( response ) {
$scope.balance.details[ account.account ] = response.data; $scope.balance.details[ account.account ] = response.data;
} ); } );
@ -154,5 +151,5 @@ app.controller( 'BalanceCtrl',
} ); } );
}; };
retrieve_data(); //retrieve_data();
}]); }]);

View file

@ -2,15 +2,15 @@
<div class="col-md-12 date"> <div class="col-md-12 date">
<h2> <h2>
<button type="button" class="btn btn-primary" From {{from_date | date:'longDate'}} to {{to_date | date:'longDate'}}
data-ng-click="previous_period()">
<span class="glyphicon glyphicon-chevron-left"></span> <div class="pull-right">
</button> <button class="btn btn-default" data-ng-click="reset_offset()">Aujourd'hui</button>
<span>{{now | amDateFormat:'MMMM YYYY'}}</span> <div class="btn-group">
<button type="button" class="btn btn-primary" <button class="btn btn-default" data-ng-click="incr_offset()"><span class="glyphicon glyphicon-chevron-left"></span></button>
data-ng-click="next_period()"> <button class="btn btn-default" data-ng-click="decr_offset()"><span class="glyphicon glyphicon-chevron-right"></span></button>
<span class="glyphicon glyphicon-chevron-right"></span> </div>
</button> </div>
</h2> </h2>
<h2 class="balance" data-ng-class="{'negative': balance.income_total - balance.expenses_total < 0, 'positive': balance.income_total - balance.expenses_total > 0}"> <h2 class="balance" data-ng-class="{'negative': balance.income_total - balance.expenses_total < 0, 'positive': balance.income_total - balance.expenses_total > 0}">
Balance: {{( balance.income_total - balance.expenses_total ) | number:2}} € Balance: {{( balance.income_total - balance.expenses_total ) | number:2}} €