From 346c16dbf636ca426bb8ef8850273d9801859bfd Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Thu, 25 Sep 2014 16:53:31 +0200 Subject: [PATCH] refactoring of period --- public/app/js/main/controllers/BalanceCtrl.js | 63 +++++++++---------- public/app/js/main/templates/balance.tpl.html | 18 +++--- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/public/app/js/main/controllers/BalanceCtrl.js b/public/app/js/main/controllers/BalanceCtrl.js index 7e9f1d4c..bb3a028d 100644 --- a/public/app/js/main/controllers/BalanceCtrl.js +++ b/public/app/js/main/controllers/BalanceCtrl.js @@ -1,16 +1,6 @@ app.controller( 'BalanceCtrl', [ '$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() { return function( d ) { 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() { + $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: [], income: [], details: {} }; - $http.get( '/api/ledger/balance?period=' - + $scope.now.year() - + '-' - + ( $scope.now.month() + 1 ) - + '&categories=Expenses' ) + $http.get( '/api/ledger/balance', + { params: { period: period, + categories: 'Expenses' } } ) .then( function( response ) { $scope.balance.expenses = _(response.data).sortBy( function( account ) { return 1 / account.amount; } ); _($scope.balance.expenses).each( function( account ) { - $http.get( '/api/ledger/register?period=' - + $scope.now.year() - + '-' - + ( $scope.now.month() + 1 ) - + '&category=' - + account.account ) + $http.get( '/api/ledger/register', + { params: { period: period, + category: account.account } } ) .then( function( response ) { $scope.balance.details[ account.account ] = response.data; } ); } ); $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' ) + $http.get( '/api/ledger/balance', + { params: { period: period, + categories: 'Income' } } ) + .then( function( response ) { $scope.balance.income = _(response.data) .map( function( account ) { @@ -139,12 +139,9 @@ app.controller( 'BalanceCtrl', } ); _($scope.balance.income) .each( function( account ) { - $http.get( '/api/ledger/register?period=' - + $scope.now.year() - + '-' - + ( $scope.now.month() + 1 ) - + '&category=' - + account.account ) + $http.get( '/api/ledger/register', + { params: { period: period, + category: account.account } } ) .then( function( response ) { $scope.balance.details[ account.account ] = response.data; } ); @@ -154,5 +151,5 @@ app.controller( 'BalanceCtrl', } ); }; - retrieve_data(); + //retrieve_data(); }]); diff --git a/public/app/js/main/templates/balance.tpl.html b/public/app/js/main/templates/balance.tpl.html index 894aa898..dd7d089b 100644 --- a/public/app/js/main/templates/balance.tpl.html +++ b/public/app/js/main/templates/balance.tpl.html @@ -2,15 +2,15 @@

- - {{now | amDateFormat:'MMMM YYYY'}} - + From {{from_date | date:'longDate'}} to {{to_date | date:'longDate'}} + +
+ +
+ + +
+

Balance: {{( balance.income_total - balance.expenses_total ) | number:2}} €