From 988046189d89b5e6a15e42d0cf0b3eeb60c5cb71 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 20 Jul 2014 20:46:10 +0200 Subject: [PATCH] Extended budgets and limits [skip-ci] --- app/controllers/BudgetController.php | 49 +++++++++- app/controllers/LimitController.php | 2 +- .../Storage/Limit/EloquentLimitRepository.php | 10 +- .../Limit/LimitRepositoryInterface.php | 2 + app/routes.php | 5 +- app/views/budgets/index-budget.blade.php | 84 ++++++++++++++++ app/views/budgets/index-date.blade.php | 43 ++++++++ app/views/budgets/index.blade.php | 97 +++---------------- app/views/limits/create.blade.php | 2 +- 9 files changed, 199 insertions(+), 95 deletions(-) create mode 100644 app/views/budgets/index-budget.blade.php create mode 100644 app/views/budgets/index-date.blade.php diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index 1b3c466bd2..8dedc33674 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -13,13 +13,54 @@ class BudgetController extends BaseController View::share('menu', 'budgets'); } - public function index() + public function index($group = null) { - $budgets = $this->_budgets->get(); - $today = new \Carbon\Carbon; + + $opts = ['date', 'budget']; + $group = in_array($group, $opts) ? $group : 'date'; + + switch ($group) { + case 'date': + // get a list of dates by getting all repetitions: + $budgets = $this->_budgets->get(); + $reps = []; + foreach ($budgets as $budget) { + foreach ($budget->limits as $limit) { + foreach ($limit->limitrepetitions as $rep) { + + $monthOrder = $rep->startdate->format('Y-m'); + $month = $rep->startdate->format('F Y'); + $reps[$monthOrder] = isset($reps[$monthOrder]) ? $reps[$monthOrder] : ['date' => $month]; + + } + } + } + // put all the budgets under their respective date: + foreach ($budgets as $budget) { + foreach ($budget->limits as $limit) { + foreach ($limit->limitrepetitions as $rep) { + $month = $rep->startdate->format('Y-m'); + $reps[$month]['limitrepetitions'][] = $rep; + } + } + } + krsort($reps); + + return View::make('budgets.index')->with('group', $group)->with('reps',$reps); + + + break; + case 'budget': + $budgets = $this->_budgets->get(); + $today = new \Carbon\Carbon; + return View::make('budgets.index')->with('budgets', $budgets)->with('today', $today)->with( + 'group', $group + ); + + break; + } - return View::make('budgets.index')->with('budgets', $budgets)->with('today', $today); } public function create() diff --git a/app/controllers/LimitController.php b/app/controllers/LimitController.php index de01c8d4a4..07d6f739c3 100644 --- a/app/controllers/LimitController.php +++ b/app/controllers/LimitController.php @@ -39,7 +39,7 @@ class LimitController extends BaseController { // find a limit with these properties, as we might already have one: $limit = $this->_limits->store(Input::all()); - if($limit->id) { + if ($limit->id) { return Redirect::route('budgets.index'); } else { return Redirect::route('budgets.limits.create')->withInput(); diff --git a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php index 6212c410ab..30eedc9402 100644 --- a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php +++ b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php @@ -11,6 +11,13 @@ namespace Firefly\Storage\Limit; class EloquentLimitRepository implements LimitRepositoryInterface { + + public function find($limitId) + { + return \Limit::with('limitrepetitions')->where('limits.id', $limitId)->leftJoin('components', 'components.id', '=', 'limits.component_id') + ->where('components.user_id', \Auth::user()->id)->first(); + } + public function store($data) { $budget = \Budget::find($data['budget_id']); @@ -74,8 +81,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface { $type = \TransactionType::where('type', 'Withdrawal')->first(); - $result = $budget->transactionjournals()->after($start)-> - before($end)->get(); + $result = $budget->transactionjournals()->after($start)->before($end)->get(); return $result; diff --git a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php index 5d2b651348..dda40f2d39 100644 --- a/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Limit/LimitRepositoryInterface.php @@ -9,4 +9,6 @@ interface LimitRepositoryInterface public function store($data); public function getTJByBudgetAndDateRange(\Budget $budget, \Carbon\Carbon $start, \Carbon\Carbon $end); + + public function find($limitId); } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 83df9a0cc5..45e888e7e1 100644 --- a/app/routes.php +++ b/app/routes.php @@ -27,12 +27,13 @@ Route::group(['before' => 'auth'], function () { Route::get('/accounts/{account}', ['uses' => 'AccountController@show', 'as' => 'accounts.show']); // budget controller: - Route::get('/budgets',['uses' => 'BudgetController@index','as' => 'budgets.index']); + Route::get('/budgets/{group?}',['uses' => 'BudgetController@index','as' => 'budgets.index']); Route::get('/budget/create',['uses' => 'BudgetController@create', 'as' => 'budgets.create']); Route::get('/budget/show/{id}',['uses' => 'BudgetController@show', 'as' => 'budgets.show']); // limit controller: Route::get('/budgets/limits/create/{id?}',['uses' => 'LimitController@create','as' => 'budgets.limits.create']); + Route::get('/budgets/limits/delete/{id?}',['uses' => 'LimitController@delete','as' => 'budgets.limits.delete']); // JSON controller: Route::get('/json/beneficiaries', ['uses' => 'JsonController@beneficiaries', 'as' => 'json.beneficiaries']); @@ -71,7 +72,7 @@ Route::group(['before' => 'csrf|auth'], function () { Route::get('/accounts/store', ['uses' => 'AccountController@store', 'as' => 'accounts.store']); // limit controller: - Route::post('/limits/store', ['uses' => 'LimitController@store', 'as' => 'limits.store']); + Route::post('/limits/store', ['uses' => 'LimitController@store', 'as' => 'budgets.limits.store']); // transaction controller: Route::post('/transactions/store/{what}', ['uses' => 'TransactionController@store', 'as' => 'transactions.store']) diff --git a/app/views/budgets/index-budget.blade.php b/app/views/budgets/index-budget.blade.php new file mode 100644 index 0000000000..7a9116bd68 --- /dev/null +++ b/app/views/budgets/index-budget.blade.php @@ -0,0 +1,84 @@ +
+
+ + + + + + + @foreach($budgets as $budget) + + + + + + @endforeach + +
BudgetCurrent envelope(s) 
+ {{{$budget->name}}} + + +
+
+ Envelope +
+
+ Left +
+
+ @foreach($budget->limits as $limit) + @foreach($limit->limitrepetitions as $index => $rep) +
+
+ + + {{mf($rep->amount,false)}} +
+
+ @if($rep->left() < 0) + + + {{mf($rep->left(),false)}} + @else + + + {{mf($rep->left(),false)}} + @endif +
+
+ + @if($limit->repeat_freq == 'monthly') + {{$rep->startdate->format('F Y')}} + @else + NO FORMAT + @endif + +
+ @if($limit->repeats == 1) +
+ auto repeats +
+ @endif +
+ + @if($limit->repeats == 0 || ($limit->repeats == 1 && $index == 0)) + + @endif +
+
+ @endforeach + @endforeach +

+ Add another limit +

+
+
+ + + +
+
+ +
+
\ No newline at end of file diff --git a/app/views/budgets/index-date.blade.php b/app/views/budgets/index-date.blade.php new file mode 100644 index 0000000000..5a29d2842c --- /dev/null +++ b/app/views/budgets/index-date.blade.php @@ -0,0 +1,43 @@ +@foreach($reps as $date => $data) +
+
+

{{$data['date']}}

+ + + + + + + + @foreach($data['limitrepetitions'] as $index => $rep) + + + + + + +@endforeach +
BudgetEnvelopeLeft 
+ {{{$rep->limit->budget->name}}} + + + {{mf($rep->amount,false)}} + + @if($rep->left() < 0) + + + {{mf($rep->left(),false)}} + @else + + + {{mf($rep->left(),false)}} + @endif + + + @if($rep->limit->repeats == 1) + auto repeats + @endif +
+
+
+@endforeach \ No newline at end of file diff --git a/app/views/budgets/index.blade.php b/app/views/budgets/index.blade.php index c185da7bd8..bcb631a388 100644 --- a/app/views/budgets/index.blade.php +++ b/app/views/budgets/index.blade.php @@ -11,92 +11,19 @@ budgets, which means that for each period of time (for example a month) a virtual "envelope" can be created containing a certain amount of money. Money spent within a budget is removed from the envelope. - +

+

+ @if($group == 'budget') + Group by date + @else + Group by budget + @endif

-
-
- - - - - - - @foreach($budgets as $budget) - - - - - - @endforeach - -
BudgetCurrent envelope(s) 
- {{{$budget->name}}} - - -
-
- Envelope -
-
- Left -
-
- @foreach($budget->limits as $limit) - @foreach($limit->limitrepetitions as $index => $rep) -
-
- - - {{mf($rep->amount,false)}} -
-
- @if($rep->left() < 0) - - - {{mf($rep->left(),false)}} - @else - - - {{mf($rep->left(),false)}} - @endif -
-
- - @if($limit->repeat_freq == 'monthly') - {{$rep->startdate->format('F Y')}} - @else - NO FORMAT - @endif - -
- @if($limit->repeats == 1) -
- auto repeats -
- @endif -
- - @if($limit->repeats == 0 || ($limit->repeats == 1 && $index == 0)) - - @endif -
-
- @endforeach - @endforeach -

- Add another limit -

-
-
- - - -
-
- -
-
+@if($group == 'budget') + @include('budgets.index-budget') +@else + @include('budgets.index-date') +@endif @stop \ No newline at end of file diff --git a/app/views/limits/create.blade.php b/app/views/limits/create.blade.php index 11ac4ca58b..b98e20a15f 100644 --- a/app/views/limits/create.blade.php +++ b/app/views/limits/create.blade.php @@ -19,7 +19,7 @@ -{{Form::open(['class' => 'form-horizontal','url' => route('limits.store')])}} +{{Form::open(['class' => 'form-horizontal','url' => route('budgets.limits.store')])}}