More work on the budget controller [skip ci]

This commit is contained in:
James Cole 2014-07-27 22:48:13 +02:00
parent 3c97a1018a
commit 2680cd8b7a
8 changed files with 130 additions and 81 deletions

View File

@ -1,8 +1,9 @@
<?php <?php
use Carbon\Carbon;
use Firefly\Helper\Controllers\BudgetInterface as BI; use Firefly\Helper\Controllers\BudgetInterface as BI;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI; use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
use Carbon\Carbon;
/** /**
* Class BudgetController * Class BudgetController
*/ */
@ -66,44 +67,44 @@ class BudgetController extends BaseController
* *
* @return string * @return string
*/ */
public function show($budgetId) public function show(Budget $budget)
{ {
/** @var \Budget $budget */ return $budget->id;
$budget = $this->_budgets->find($budgetId); // /** @var \Budget $budget */
// $budget = $this->_budgets->find($budgetId);
$list = $budget->transactionjournals()->get(); //
$return = []; // $list = $budget->transactionjournals()->get();
/** @var \TransactionJournal $entry */ // $return = [];
foreach ($list as $entry) { // /** @var \TransactionJournal $entry */
$month = $entry->date->format('F Y'); // foreach ($list as $entry) {
$return[$month] = isset($return[$month]) ? $return[$month] : []; // $month = $entry->date->format('F Y');
$return[$month][] = $entry; // $return[$month] = isset($return[$month]) ? $return[$month] : [];
// $return[$month][] = $entry;
} //
$str = ''; // }
// $str = '';
foreach ($return as $month => $set) { //
$str .= '<h1>' . $month . '</h1>'; // foreach ($return as $month => $set) {
/** @var \TransactionJournal $tj */ // $str .= '<h1>' . $month . '</h1>';
$sum = 0; // /** @var \TransactionJournal $tj */
foreach ($set as $tj) { // $sum = 0;
$str .= '#' . $tj->id . ' ' . $tj->description . ': '; // foreach ($set as $tj) {
// $str .= '#' . $tj->id . ' ' . $tj->description . ': ';
foreach ($tj->transactions as $index => $t) { //
$str .= $t->amount . ', '; // foreach ($tj->transactions as $index => $t) {
if ($index == 0) { // $str .= $t->amount . ', ';
$sum += $t->amount; // if ($index == 0) {
// $sum += $t->amount;
} //
} // }
$str .= '<br>'; // }
// $str .= '<br>';
} //
$str .= 'sum: ' . $sum . '<br><br>'; // }
} // $str .= 'sum: ' . $sum . '<br><br>';
// }
return $str; //
// return $str;
} }
@ -113,17 +114,25 @@ class BudgetController extends BaseController
public function store() public function store()
{ {
$data = [ $budget = $this->_repository->store(Input::all());
'name' => Input::get('name'), if ($budget->id) {
'amount' => floatval(Input::get('amount')), Session::flash('success', 'Budget created!');
'repeat_freq' => Input::get('period'),
'repeats' => intval(Input::get('repeats'))
];
$this->_budgets->store($data); if (Input::get('create') == '1') {
Session::flash('success', 'Budget created!'); 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');
} }

View File

@ -121,7 +121,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->save(); $budget->save();
// if limit, create limit (repetition itself will be picked up elsewhere). // if limit, create limit (repetition itself will be picked up elsewhere).
if ($data['amount'] > 0) { if (floatval($data['amount']) > 0) {
$limit = new \Limit; $limit = new \Limit;
$limit->budget()->associate($budget); $limit->budget()->associate($budget);
$startDate = new Carbon; $startDate = new Carbon;
@ -152,10 +152,13 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$limit->amount = $data['amount']; $limit->amount = $data['amount'];
$limit->repeats = $data['repeats']; $limit->repeats = $data['repeats'];
$limit->repeat_freq = $data['repeat_freq']; $limit->repeat_freq = $data['repeat_freq'];
$limit->save(); if ($limit->validate()) {
$limit->save();
}
}
if($budget->validate()) {
$budget->save();
} }
return $budget; return $budget;
} }

View File

@ -5,6 +5,7 @@
<h1>Firefly <h1>Firefly
<small>Add a new personal account</small> <small>Add a new personal account</small>
</h1> </h1>
<p class="lead"> <p class="lead">
Accounts are the record holders for transactions and transfers. Money moves Accounts are the record holders for transactions and transfers. Money moves
from one account to another. from one account to another.
@ -35,8 +36,7 @@
@if($errors->has('name')) @if($errors->has('name'))
<p class="text-danger">{{$errors->first('name')}}</p> <p class="text-danger">{{$errors->first('name')}}</p>
@else @else
<span <span class="help-block">Use something descriptive such as "checking account" or "My Bank Main Account".</span>
class="help-block">Use something descriptive such as "checking account" or "My Bank Main Account".</span>
@endif @endif
</div> </div>
@ -51,8 +51,7 @@
<div class="col-sm-8"> <div class="col-sm-8">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon">&euro;</span> <span class="input-group-addon">&euro;</span>
{{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' => {{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' => 'form-control'])}}
'form-control'])}}
</div> </div>
@if($errors->has('openingbalance')) @if($errors->has('openingbalance'))

View File

@ -5,6 +5,7 @@
<h1>Firefly <h1>Firefly
<small>Create a budget</small> <small>Create a budget</small>
</h1> </h1>
<p class="lead">Use budgets to organize and limit your expenses.</p>
<p class="text-info"> <p class="text-info">
Firefly uses the <a href="http://en.wikipedia.org/wiki/Envelope_System" class="text-success">envelope system</a>. Every budget 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 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::open(['class' => 'form-horizontal','url' => route('budgets.store')])}}
{{Form::hidden('from',e(Input::get('from')))}}
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-12 col-sm-6"> <div class="col-lg-6 col-md-12 col-sm-6">
<h4>Mandatory fields</h4> <h4>Mandatory fields</h4>
<div class="form-group"> <div class="form-group">
<label for="name" class="col-sm-3 control-label">Name</label> <label for="name" class="col-sm-4 control-label">Name</label>
<div class="col-sm-9"> <div class="col-sm-8">
<input type="text" name="name" class="form-control" id="name" value="{{Input::old('name')}}" placeholder="Name"> <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> <span class="help-block">For example: groceries, bills</span>
@endif
</div> </div>
</div> </div>
@ -36,35 +43,51 @@
<h4>Optional fields</h4> <h4>Optional fields</h4>
<div class="form-group"> <div class="form-group">
<label for="amount" class="col-sm-3 control-label">Max. amount</label> <label for="amount" class="col-sm-4 control-label">Max. amount</label>
<div class="col-sm-9"> <div class="col-sm-8">
<input type="number" min="0.01" step="any" name="amount" class="form-control" id="amount" value="{{Input::old('amount')}}"> <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 <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> envelope.</span>
@endif
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="period" class="col-sm-3 control-label">Spending period</label> <label for="period" class="col-sm-4 control-label">Spending period</label>
<div class="col-sm-9"> <div class="col-sm-8">
{{Form::select('period',$periods,Input::old('period') ?: 'monthly',['class' => 'form-control'])}} {{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> <span class="help-block">How long will the envelope last? A week, a month, or even longer?</span>
@endif
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="period" class="col-sm-3 control-label">Repeat</label> <label for="period" class="col-sm-4 control-label">Repeat</label>
<div class="col-sm-9"> <div class="col-sm-8">
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" value="1" name="repeats"> <input type="checkbox" value="1" name="repeats">
Repeat Repeat
</label> </label>
</div> </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 <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 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>. 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> Instead, you should recreate the envelope yourself each [period].</span>
@endif
</div> </div>
</div> </div>
</div> </div>
@ -72,8 +95,25 @@
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-12 col-sm-6"> <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>
</div> </div>
@ -81,7 +121,3 @@
@stop @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

View File

@ -23,8 +23,9 @@
</p> </p>
<div class="btn-group"> <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.index')}}"><span class="glyphicon glyphicon-indent-left"></span> Group 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.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> </div>
</p> </p>
</div> </div>
@ -98,7 +99,7 @@
@endforeach @endforeach
<p style="margin-top:5px;"> <p style="margin-top:5px;">
<a href="{{route('budgets.limits.create',$budget->id)}}" class="btn btn-default btn-xs"><span <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> </p>
</td> </td>
<td> <td>

View File

@ -21,10 +21,11 @@
<p class="text-info"> <p class="text-info">
* <small>Every month, week, year, etc.</small> * <small>Every month, week, year, etc.</small>
</p> </p>
<p> <div class="btn-group">
<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.index.budget')}}"><span class="glyphicon glyphicon-tasks"></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> <a class="btn btn-default" href ="{{route('budgets.create')}}?from=date"><span class="glyphicon glyphicon-plus-sign"></span> Create a new budget</a>
</p> <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>
</div> </div>
@ -34,7 +35,7 @@
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<h3><a href="#transactions-in-this-period">{{$entry['date']}}</a> <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> </h3>
<table class="table table-bordered table-striped"> <table class="table table-bordered table-striped">
<tr> <tr>

View File

@ -19,7 +19,7 @@ $r = Route::current()->getName();
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li @if($r=='index')class="active"@endif><a href="{{route('index')}}">Home</a></li> <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.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> </ul>
@include('partials.menu.shared') @include('partials.menu.shared')
</div><!-- /.navbar-collapse --> </div><!-- /.navbar-collapse -->

View File

@ -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=='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.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.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> </ul>
@include('partials.menu.shared') @include('partials.menu.shared')
</div><!-- /.navbar-collapse --> </div><!-- /.navbar-collapse -->