Skip to content

Firefly III: a personal finances manager

More work on the budget controller [skip ci]

James Cole2680cd8b7a34
diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php
index 2c81e339a0..518618ba49 100644
--- a/app/controllers/BudgetController.php
+++ b/app/controllers/BudgetController.php
@@ -1,8 +1,9 @@
<?php
+use Carbon\Carbon;
use Firefly\Helper\Controllers\BudgetInterface as BI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
-use Carbon\Carbon;
+
/**
* Class BudgetController
*/
@@ -66,44 +67,44 @@ class BudgetController extends BaseController
*
* @return string
*/
- public function show($budgetId)
+ public function show(Budget $budget)
{
- /** @var \Budget $budget */
- $budget = $this->_budgets->find($budgetId);
-
- $list = $budget->transactionjournals()->get();
- $return = [];
- /** @var \TransactionJournal $entry */
- foreach ($list as $entry) {
- $month = $entry->date->format('F Y');
- $return[$month] = isset($return[$month]) ? $return[$month] : [];
- $return[$month][] = $entry;
-
- }
- $str = '';
-
- foreach ($return as $month => $set) {
- $str .= '<h1>' . $month . '</h1>';
- /** @var \TransactionJournal $tj */
- $sum = 0;
- foreach ($set as $tj) {
- $str .= '#' . $tj->id . ' ' . $tj->description . ': ';
-
- foreach ($tj->transactions as $index => $t) {
- $str .= $t->amount . ', ';
- if ($index == 0) {
- $sum += $t->amount;
-
- }
- }
- $str .= '<br>';
-
- }
- $str .= 'sum: ' . $sum . '<br><br>';
- }
-
- return $str;
-
+ return $budget->id;
+// /** @var \Budget $budget */
+// $budget = $this->_budgets->find($budgetId);
+//
+// $list = $budget->transactionjournals()->get();
+// $return = [];
+// /** @var \TransactionJournal $entry */
+// foreach ($list as $entry) {
+// $month = $entry->date->format('F Y');
+// $return[$month] = isset($return[$month]) ? $return[$month] : [];
+// $return[$month][] = $entry;
+//
+// }
+// $str = '';
+//
+// foreach ($return as $month => $set) {
+// $str .= '<h1>' . $month . '</h1>';
+// /** @var \TransactionJournal $tj */
+// $sum = 0;
+// foreach ($set as $tj) {
+// $str .= '#' . $tj->id . ' ' . $tj->description . ': ';
+//
+// foreach ($tj->transactions as $index => $t) {
+// $str .= $t->amount . ', ';
+// if ($index == 0) {
+// $sum += $t->amount;
+//
+// }
+// }
+// $str .= '<br>';
+//
+// }
+// $str .= 'sum: ' . $sum . '<br><br>';
+// }
+//
+// return $str;
}
@@ -113,17 +114,25 @@ class BudgetController extends BaseController
public function store()
{
- $data = [
- 'name' => Input::get('name'),
- 'amount' => floatval(Input::get('amount')),
- 'repeat_freq' => Input::get('period'),
- 'repeats' => intval(Input::get('repeats'))
- ];
+ $budget = $this->_repository->store(Input::all());
+ if ($budget->id) {
+ Session::flash('success', 'Budget created!');
- $this->_budgets->store($data);
- Session::flash('success', 'Budget created!');
+ if (Input::get('create') == '1') {
+ return Redirect::route('budgets.create', ['from' => Input::get('from')]);
+ }
+
+ if (Input::get('from') == 'date') {
+ return Redirect::route('budgets.index');
+ } else {
+ return Redirect::route('budgets.index.budget');
+ }
+ } else {
+ Session::flash('error', 'Could not save the new budget');
+
+ return Redirect::route('budgets.create')->withInput();
+ }
- return Redirect::route('budgets.index');
}
diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
index 113e2adc5b..26ea2236d3 100644
--- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
+++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
@@ -121,7 +121,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->save();
// if limit, create limit (repetition itself will be picked up elsewhere).
- if ($data['amount'] > 0) {
+ if (floatval($data['amount']) > 0) {
$limit = new \Limit;
$limit->budget()->associate($budget);
$startDate = new Carbon;
@@ -152,10 +152,13 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$limit->amount = $data['amount'];
$limit->repeats = $data['repeats'];
$limit->repeat_freq = $data['repeat_freq'];
- $limit->save();
+ if ($limit->validate()) {
+ $limit->save();
+ }
+ }
+ if($budget->validate()) {
+ $budget->save();
}
-
-
return $budget;
}
diff --git a/app/views/accounts/create.blade.php b/app/views/accounts/create.blade.php
index 25b83aff34..7a2f3a6ebc 100644
--- a/app/views/accounts/create.blade.php
+++ b/app/views/accounts/create.blade.php
@@ -5,6 +5,7 @@
<h1>Firefly
<small>Add a new personal account</small>
</h1>
+
<p class="lead">
Accounts are the record holders for transactions and transfers. Money moves
from one account to another.
@@ -35,8 +36,7 @@
@if($errors->has('name'))
<p class="text-danger">{{$errors->first('name')}}</p>
@else
- <span
- class="help-block">Use something descriptive such as "checking account" or "My Bank Main Account".</span>
+ <span class="help-block">Use something descriptive such as "checking account" or "My Bank Main Account".</span>
@endif
</div>
@@ -51,8 +51,7 @@
<div class="col-sm-8">
<div class="input-group">
<span class="input-group-addon">&euro;</span>
- {{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' =>
- 'form-control'])}}
+ {{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' => 'form-control'])}}
</div>
@if($errors->has('openingbalance'))
diff --git a/app/views/budgets/create.blade.php b/app/views/budgets/create.blade.php
index e25252c560..c40103f946 100644
--- a/app/views/budgets/create.blade.php
+++ b/app/views/budgets/create.blade.php
@@ -5,6 +5,7 @@
<h1>Firefly
<small>Create a budget</small>
</h1>
+ <p class="lead">Use budgets to organize and limit your expenses.</p>
<p class="text-info">
Firefly uses the <a href="http://en.wikipedia.org/wiki/Envelope_System" class="text-success">envelope system</a>. Every budget
is an envelope in which you put money every [period]. Expenses allocated to each budget are paid from this
@@ -19,15 +20,21 @@
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.store')])}}
+{{Form::hidden('from',e(Input::get('from')))}}
+
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-6">
<h4>Mandatory fields</h4>
<div class="form-group">
- <label for="name" class="col-sm-3 control-label">Name</label>
- <div class="col-sm-9">
+ <label for="name" class="col-sm-4 control-label">Name</label>
+ <div class="col-sm-8">
<input type="text" name="name" class="form-control" id="name" value="{{Input::old('name')}}" placeholder="Name">
+ @if($errors->has('name'))
+ <p class="text-danger">{{$errors->first('name')}}</p>
+ @else
<span class="help-block">For example: groceries, bills</span>
+ @endif
</div>
</div>
@@ -36,35 +43,51 @@
<h4>Optional fields</h4>
<div class="form-group">
- <label for="amount" class="col-sm-3 control-label">Max. amount</label>
- <div class="col-sm-9">
- <input type="number" min="0.01" step="any" name="amount" class="form-control" id="amount" value="{{Input::old('amount')}}">
+ <label for="amount" class="col-sm-4 control-label">Max. amount</label>
+ <div class="col-sm-8">
+ <div class="input-group">
+ <span class="input-group-addon">&euro;</span>
+ <input type="number" min="0.01" step="any" name="amount" class="form-control" id="amount" value="{{Input::old('amount')}}">
+ </div>
+
+ @if($errors->has('amount'))
+ <p class="text-danger">{{$errors->first('amount')}}</p>
+ @else
<span class="help-block">What's the most you're willing to spend in this budget? This amount is "put" in the virtual
envelope.</span>
+ @endif
</div>
</div>
<div class="form-group">
- <label for="period" class="col-sm-3 control-label">Spending period</label>
- <div class="col-sm-9">
- {{Form::select('period',$periods,Input::old('period') ?: 'monthly',['class' => 'form-control'])}}
+ <label for="period" class="col-sm-4 control-label">Spending period</label>
+ <div class="col-sm-8">
+ {{Form::select('repeat_freq',$periods,Input::old('repeat_freq') ?: 'monthly',['class' => 'form-control'])}}
+ @if($errors->has('repeat_freq'))
+ <p class="text-danger">{{$errors->first('repeat_freq')}}</p>
+ @else
<span class="help-block">How long will the envelope last? A week, a month, or even longer?</span>
+ @endif
</div>
</div>
<div class="form-group">
- <label for="period" class="col-sm-3 control-label">Repeat</label>
- <div class="col-sm-9">
+ <label for="period" class="col-sm-4 control-label">Repeat</label>
+ <div class="col-sm-8">
<div class="checkbox">
<label>
<input type="checkbox" value="1" name="repeats">
Repeat
</label>
</div>
+ @if($errors->has('repeats'))
+ <p class="text-danger">{{$errors->first('repeats')}}</p>
+ @else
<span class="help-block">If you want, Firefly can automatically recreate the "envelope" and fill it again
when the timespan above has expired. Be careful with this option though. It makes it easier
to <a href="http://en.wikipedia.org/wiki/Personal_budget#Concepts">fall back to old habits</a>.
Instead, you should recreate the envelope yourself each [period].</span>
+ @endif
</div>
</div>
</div>
@@ -72,8 +95,25 @@
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-6">
- <input type="submit" name="submit" class="btn btn-info" value="Create new budget" />
- <br /><br /><br /><br />
+
+ <!-- add another after this one? -->
+ <div class="form-group">
+ <label for="create" class="col-sm-4 control-label">&nbsp;</label>
+ <div class="col-sm-8">
+ <div class="checkbox">
+ <label>
+ {{Form::checkbox('create',1,Input::old('create') == '1')}}
+ Create another (return to this form)
+ </label>
+ </div>
+ </div>
+ </div>
+
+ <div class="form-group">
+ <div class="col-sm-offset-4 col-sm-8">
+ <button type="submit" class="btn btn-default btn-success">Create the budget</button>
+ </div>
+ </div>
</div>
</div>
@@ -81,7 +121,3 @@
@stop
-@section('scripts')
- <script type="text/javascript" src="assets/javascript/moment.min.js"></script>
- <script type="text/javascript" src="assets/javascript/limits.js"></script>
-@stop
\ No newline at end of file
diff --git a/app/views/budgets/indexByBudget.blade.php b/app/views/budgets/indexByBudget.blade.php
index 541b00a49c..d0eb199283 100644
--- a/app/views/budgets/indexByBudget.blade.php
+++ b/app/views/budgets/indexByBudget.blade.php
@@ -23,8 +23,9 @@
</p>
<div class="btn-group">
- <a class="btn btn-default" href ="{{route('budgets.index')}}"><span class="glyphicon glyphicon-th"></span> Group budgets by date</a>
- <a class="btn btn-default" href ="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope</a>
+ <a class="btn btn-default" href ="{{route('budgets.index')}}"><span class="glyphicon glyphicon-indent-left"></span> Group by date</a>
+ <a class="btn btn-default" href ="{{route('budgets.create')}}?from=budget"><span class="glyphicon glyphicon-plus-sign"></span> Create a new budget</a>
+ <a class="btn btn-default" href ="{{route('budgets.limits.create')}}?from=budget"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope</a>
</div>
</p>
</div>
@@ -98,7 +99,7 @@
@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>
+ class="glyphicon-plus-sign glyphicon"></span> Add another envelope</a>
</p>
</td>
<td>
diff --git a/app/views/budgets/indexByDate.blade.php b/app/views/budgets/indexByDate.blade.php
index 46c9940d2f..ac43e3c56b 100644
--- a/app/views/budgets/indexByDate.blade.php
+++ b/app/views/budgets/indexByDate.blade.php
@@ -21,10 +21,11 @@
<p class="text-info">
* <small>Every month, week, year, etc.</small>
</p>
- <p>
- <a class="btn btn-default" href ="{{route('budgets.index.budget')}}"><span class="glyphicon glyphicon-indent-left"></span> Group by budget</a>
- <a class="btn btn-default" href ="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create an envelope</a>
- </p>
+ <div class="btn-group">
+ <a class="btn btn-default" href ="{{route('budgets.index.budget')}}"><span class="glyphicon glyphicon-tasks"></span> Group by budget</a>
+ <a class="btn btn-default" href ="{{route('budgets.create')}}?from=date"><span class="glyphicon glyphicon-plus-sign"></span> Create a new budget</a>
+ <a class="btn btn-default" href ="{{route('budgets.limits.create')}}?from=date"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope</a>
+ </div>
</div>
</div>
@@ -34,7 +35,7 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h3><a href="#transactions-in-this-period">{{$entry['date']}}</a>
- <a class="btn btn-default btn-xs" href ="{{route('budgets.limits.create')}}#date-and-budget-selected"><span class="glyphicon glyphicon-plus-sign"></span> Create an envelope for {{$entry['date']}}</a>
+ <a class="btn btn-default btn-xs" href ="{{route('budgets.limits.create')}}#date-and-budget-selected"><span class="glyphicon glyphicon-plus-sign"></span> Create a new envelope for {{$entry['date']}}</a>
</h3>
<table class="table table-bordered table-striped">
<tr>
diff --git a/app/views/partials/menu/accounts.blade.php b/app/views/partials/menu/accounts.blade.php
index 6d96bd3672..b54b29cebd 100644
--- a/app/views/partials/menu/accounts.blade.php
+++ b/app/views/partials/menu/accounts.blade.php
@@ -19,7 +19,7 @@ $r = Route::current()->getName();
<ul class="nav navbar-nav">
<li @if($r=='index')class="active"@endif><a href="{{route('index')}}">Home</a></li>
<li @if($r=='accounts.index')class="active"@endif><a href="{{route('accounts.index')}}">Accounts</a></li>
- <li @if($r=='accounts.create')class="active"@endif><a href="{{route('accounts.create')}}"><span class="glyphicon glyphicon-plus"></span> Create</a></li>
+ <li @if($r=='accounts.create')class="active"@endif><a href="{{route('accounts.create')}}"><span class="glyphicon glyphicon-plus"></span> Create account</a></li>
</ul>
@include('partials.menu.shared')
</div><!-- /.navbar-collapse -->
diff --git a/app/views/partials/menu/budgets.blade.php b/app/views/partials/menu/budgets.blade.php
index b6e16f1c7f..be66580b97 100644
--- a/app/views/partials/menu/budgets.blade.php
+++ b/app/views/partials/menu/budgets.blade.php
@@ -20,7 +20,7 @@ $r = Route::current()->getName();
<li @if($r=='index')class="active"@endif><a href="{{route('index')}}">Home</a></li>
<li @if($r=='budgets.index')class="active"@endif><a href="{{route('budgets.index')}}">Budgets</a></li>
<li @if($r=='budgets.create')class="active"@endif><a href="{{route('budgets.create')}}"><span class="glyphicon glyphicon-plus"></span> Create budget</a></li>
- <li @if($r=='budgets.limits.create')class="active"@endif><a href="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus"></span> Set limit</a></li>
+ <li @if($r=='budgets.limits.create')class="active"@endif><a href="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus"></span> Create envelope</a></li>
</ul>
@include('partials.menu.shared')
</div><!-- /.navbar-collapse -->