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
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');
}

View File

@ -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;
}

View File

@ -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'))

View File

@ -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

View File

@ -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>

View File

@ -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>

View File

@ -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 -->

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=='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 -->