diff --git a/app.rb b/app.rb index 861485a5..4cb0d83d 100644 --- a/app.rb +++ b/app.rb @@ -29,6 +29,10 @@ class LedgerRbApp < Sinatra::Base Ledger.accounts( params[ :depth ] ).to_json end + get '/api/ledger/dates_salaries/?' do + Ledger.dates_salaries.to_json + end + get '/api/ledger/register/?' do param :period, String, default: nil param :category, String, required: true diff --git a/lib/ledger.rb b/lib/ledger.rb index ec0eac63..9e279b47 100644 --- a/lib/ledger.rb +++ b/lib/ledger.rb @@ -27,6 +27,15 @@ module Ledger end.uniq end + def dates_salaries( category = 'salaire' ) + CSV.parse( run( '', 'csv', category ) ) + .map do + |row| + Date.parse row[ 0 ] + end + .uniq + end + def register( period = nil, categories = '' ) period = period.nil? ? '' : "-p '#{period}'" diff --git a/public/app/js/main/controllers/BalanceCtrl.js b/public/app/js/main/controllers/BalanceCtrl.js index c971cfd7..975d44ea 100644 --- a/public/app/js/main/controllers/BalanceCtrl.js +++ b/public/app/js/main/controllers/BalanceCtrl.js @@ -74,23 +74,18 @@ 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(); + // $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; 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(); - + 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(); + } + console.log(period) $scope.balance = { expenses: [], income: [], details: {} }; @@ -141,5 +136,21 @@ app.controller( 'BalanceCtrl', } ); }; - //retrieve_data(); - }]); + $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(); } ); + + } ); + } ] ); diff --git a/public/app/js/main/templates/balance.tpl.html b/public/app/js/main/templates/balance.tpl.html index dd7d089b..9072cb6d 100644 --- a/public/app/js/main/templates/balance.tpl.html +++ b/public/app/js/main/templates/balance.tpl.html @@ -2,13 +2,17 @@

- From {{from_date | date:'longDate'}} to {{to_date | date:'longDate'}} + From {{from_date | date:'longDate'}} to {{to_date | date:'longDate'}}
- - + +