Skip to content

Firefly III: a personal finances manager

Extended budgets and limits [skip-ci]

James Cole988046189d89
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 @@
+<div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <table class="table table-bordered table-striped">
+ <tr>
+ <th>Budget</th>
+ <th>Current envelope(s)</th>
+ <th>&nbsp;</th>
+ </tr>
+ @foreach($budgets as $budget)
+ <tr>
+ <td>
+ <a href="{{route('budgets.show',$budget->id)}}">{{{$budget->name}}}</a>
+
+ </td>
+ <td>
+ <div class="row">
+ <div class="col-sm-2">
+ <small>Envelope</small>
+ </div>
+ <div class="col-sm-2">
+ <small>Left</small>
+ </div>
+ </div>
+ @foreach($budget->limits as $limit)
+ @foreach($limit->limitrepetitions as $index => $rep)
+ <div class="row">
+ <div class="col-sm-2">
+ <span class="label label-primary">
+ <span class="glyphicon glyphicon-envelope"></span>
+ {{mf($rep->amount,false)}}</span>
+ </div>
+ <div class="col-sm-2">
+ @if($rep->left() < 0)
+ <span class="label label-danger">
+ <span class="glyphicon glyphicon-envelope"></span>
+ {{mf($rep->left(),false)}}</span>
+ @else
+ <span class="label label-success">
+ <span class="glyphicon glyphicon-envelope"></span>
+ {{mf($rep->left(),false)}}</span>
+ @endif
+ </div>
+ <div class="col-sm-3">
+ <small>
+ @if($limit->repeat_freq == 'monthly')
+ {{$rep->startdate->format('F Y')}}
+ @else
+ NO FORMAT
+ @endif
+ </small>
+ </div>
+ @if($limit->repeats == 1)
+ <div class="col-sm-2">
+ <span class="label label-warning">auto repeats</span>
+ </div>
+ @endif
+ <div class="col-sm-2 @if($limit->repeats == 0) col-sm-offset-2 @endif">
+ <a href="#" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span></a>
+ @if($limit->repeats == 0 || ($limit->repeats == 1 && $index == 0))
+ <a href="#" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
+ @endif
+ </div>
+ </div>
+ @endforeach
+ @endforeach
+ <p style="margin-top:5px;">
+ <a href="{{route('budgets.limits.create',$budget->id)}}" class="btn btn-default btn-xs"><span
+ class="glyphicon-plus-sign glyphicon"></span> Add another limit</a>
+ </p>
+ </td>
+ <td>
+ <div class="btn-group btn-group-sm">
+ <a href="#" class="btn btn-default"><span class="glyphicon glyphicon-pencil"></span></a>
+ <a href="#" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
+
+ </div>
+ </td>
+ </tr>
+ @endforeach
+
+ </table>
+
+ </div>
+</div>
\ 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)
+<div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <h3>{{$data['date']}}</h3>
+ <table class="table table-bordered table-striped">
+ <tr>
+ <th style="width:45%;">Budget</th>
+ <th style="width:15%;">Envelope</th>
+ <th style="width:15%;">Left</th>
+ <th>&nbsp;</th>
+ </tr>
+ @foreach($data['limitrepetitions'] as $index => $rep)
+ <tr>
+ <td>
+ <a href="{{route('budgets.show',$rep->limit->budget->id)}}">{{{$rep->limit->budget->name}}}</a>
+ </td>
+ <td>
+ <span class="label label-primary">
+ <span class="glyphicon glyphicon-envelope"></span> {{mf($rep->amount,false)}}</span>
+ </td>
+ <td>
+ @if($rep->left() < 0)
+ <span class="label label-danger">
+ <span class="glyphicon glyphicon-envelope"></span>
+ {{mf($rep->left(),false)}}</span>
+ @else
+ <span class="label label-success">
+ <span class="glyphicon glyphicon-envelope"></span>
+ {{mf($rep->left(),false)}}</span>
+ @endif
+ </td>
+ <td>
+ <a href="{{route('budgets.limits.delete',$rep->limit->id)}}" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
+ @if($rep->limit->repeats == 1)
+ <span class="label label-warning">auto repeats</span>
+ @endif
+ </td>
+ </tr>
+@endforeach
+</table>
+</div>
+</div>
+@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.
-
+ </p>
+ <p>
+ @if($group == 'budget')
+ <a class="btn btn-default" href ="{{route('budgets.index','date')}}"><span class="glyphicon glyphicon-th-list"></span> Group by date</a>
+ @else
+ <a class="btn btn-default" href ="{{route('budgets.index','budget')}}"><span class="glyphicon glyphicon-th-list"></span> Group by budget</a>
+ @endif
</p>
</div>
</div>
-<div class="row">
- <div class="col-lg-12 col-md-12 col-sm-12">
- <table class="table table-bordered table-striped">
- <tr>
- <th>Budget</th>
- <th>Current envelope(s)</th>
- <th>&nbsp;</th>
- </tr>
- @foreach($budgets as $budget)
- <tr>
- <td>
- <a href="{{route('budgets.show',$budget->id)}}">{{{$budget->name}}}</a>
-
- </td>
- <td>
- <div class="row">
- <div class="col-sm-2">
- <small>Envelope</small>
- </div>
- <div class="col-sm-2">
- <small>Left</small>
- </div>
- </div>
- @foreach($budget->limits as $limit)
- @foreach($limit->limitrepetitions as $index => $rep)
- <div class="row">
- <div class="col-sm-2">
- <span class="label label-primary">
- <span class="glyphicon glyphicon-envelope"></span>
- {{mf($rep->amount,false)}}</span>
- </div>
- <div class="col-sm-2">
- @if($rep->left() < 0)
- <span class="label label-danger">
- <span class="glyphicon glyphicon-envelope"></span>
- {{mf($rep->left(),false)}}</span>
- @else
- <span class="label label-success">
- <span class="glyphicon glyphicon-envelope"></span>
- {{mf($rep->left(),false)}}</span>
- @endif
- </div>
- <div class="col-sm-3">
- <small>
- @if($limit->repeat_freq == 'monthly')
- {{$rep->startdate->format('F Y')}}
- @else
- NO FORMAT
- @endif
- </small>
- </div>
- @if($limit->repeats == 1)
- <div class="col-sm-2">
- <span class="label label-warning">auto repeats</span>
- </div>
- @endif
- <div class="col-sm-2 @if($limit->repeats == 0) col-sm-offset-2 @endif">
- <a href="#" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span></a>
- @if($limit->repeats == 0 || ($limit->repeats == 1 && $index == 0))
- <a href="#" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
- @endif
- </div>
- </div>
- @endforeach
- @endforeach
- <p style="margin-top:5px;">
- <a href="{{route('budgets.limits.create',$budget->id)}}" class="btn btn-default btn-xs"><span
- class="glyphicon-plus-sign glyphicon"></span> Add another limit</a>
- </p>
- </td>
- <td>
- <div class="btn-group btn-group-sm">
- <a href="#" class="btn btn-default"><span class="glyphicon glyphicon-pencil"></span></a>
- <a href="#" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></a>
-
- </div>
- </td>
- </tr>
- @endforeach
-
- </table>
-
- </div>
-</div>
+@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 @@
</div>
</div>
-{{Form::open(['class' => 'form-horizontal','url' => route('limits.store')])}}
+{{Form::open(['class' => 'form-horizontal','url' => route('budgets.limits.store')])}}
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-6">