From 2680cd8b7a341b17ba1d102b7f87a33f642db510 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 27 Jul 2014 22:48:13 +0200 Subject: [PATCH] More work on the budget controller [skip ci] --- app/controllers/BudgetController.php | 103 ++++++++++-------- .../Budget/EloquentBudgetRepository.php | 11 +- app/views/accounts/create.blade.php | 7 +- app/views/budgets/create.blade.php | 68 +++++++++--- app/views/budgets/indexByBudget.blade.php | 7 +- app/views/budgets/indexByDate.blade.php | 11 +- app/views/partials/menu/accounts.blade.php | 2 +- app/views/partials/menu/budgets.blade.php | 2 +- 8 files changed, 130 insertions(+), 81 deletions(-) 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 @@ _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 .= '

' . $month . '

'; - /** @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 .= '
'; - - } - $str .= 'sum: ' . $sum . '

'; - } - - 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 .= '

' . $month . '

'; +// /** @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 .= '
'; +// +// } +// $str .= 'sum: ' . $sum . '

'; +// } +// +// 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 @@

Firefly Add a new personal account

+

Accounts are the record holders for transactions and transfers. Money moves from one account to another. @@ -35,8 +36,7 @@ @if($errors->has('name'))

{{$errors->first('name')}}

@else - Use something descriptive such as "checking account" or "My Bank Main Account". + Use something descriptive such as "checking account" or "My Bank Main Account". @endif @@ -51,8 +51,7 @@
- {{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' => - 'form-control'])}} + {{Form::input('number','openingbalance', Input::old('openingbalance'), ['step' => 'any', 'class' => 'form-control'])}}
@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 @@

Firefly Create a budget

+

Use budgets to organize and limit your expenses.

Firefly uses the envelope system. 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')))}} +

Mandatory fields

- -
+ +
+ @if($errors->has('name')) +

{{$errors->first('name')}}

+ @else For example: groceries, bills + @endif
@@ -36,35 +43,51 @@

Optional fields

- -
- + +
+
+ + +
+ + @if($errors->has('amount')) +

{{$errors->first('amount')}}

+ @else What's the most you're willing to spend in this budget? This amount is "put" in the virtual envelope. + @endif
- -
- {{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')) +

{{$errors->first('repeat_freq')}}

+ @else How long will the envelope last? A week, a month, or even longer? + @endif
- -
+ +
+ @if($errors->has('repeats')) +

{{$errors->first('repeats')}}

+ @else 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 fall back to old habits. Instead, you should recreate the envelope yourself each [period]. + @endif
@@ -72,8 +95,25 @@
- -



+ + +
+ +
+
+ +
+
+
+ +
+
+ +
+
@@ -81,7 +121,3 @@ @stop -@section('scripts') - - -@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 @@

@@ -98,7 +99,7 @@ @endforeach

Add another limit + class="glyphicon-plus-sign glyphicon"> Add another envelope

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

* Every month, week, year, etc.

-

- Group by budget - Create an envelope -

+
@@ -34,7 +35,7 @@

{{$entry['date']}} - Create an envelope for {{$entry['date']}} + Create a new envelope for {{$entry['date']}}

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(); @include('partials.menu.shared') 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();
  • Home
  • Budgets
  • Create budget
  • -
  • Set limit
  • +
  • Create envelope
  • @include('partials.menu.shared')