diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php index fad626f33d..b105dfd4a0 100644 --- a/app/controllers/PiggybankController.php +++ b/app/controllers/PiggybankController.php @@ -25,13 +25,21 @@ class PiggybankController extends BaseController /** * @return $this */ - public function create() + public function createPiggybank() + { + $accounts = $this->_accounts->getActiveDefaultAsSelectList(); + + return View::make('piggybanks.create-piggybank')->with('accounts', $accounts); + } + + public function createRepeated() { $accounts = $this->_accounts->getActiveDefaultAsSelectList(); return View::make('piggybanks.create')->with('accounts', $accounts); } + /** * @param Piggybank $piggyBank * @@ -72,20 +80,12 @@ class PiggybankController extends BaseController */ public function index() { - $count = $this->_repository->count(); + $countRepeating = $this->_repository->countRepeating(); + $countNonRepeating = $this->_repository->countNonrepeating(); $piggybanks = $this->_repository->get(); - $accounts = []; - // get accounts: - foreach ($piggybanks as $piggyBank) { - $account = $piggyBank->account; - $id = $account->id; - $accounts[$id] = $account; - } - - - return View::make('piggybanks.index')->with('count', $count)->with('accounts', $accounts)->with( - 'piggybanks', $piggybanks - ); + return View::make('piggybanks.index')->with('piggybanks', $piggybanks) + ->with('countRepeating',$countRepeating) + ->with('countNonRepeating',$countNonRepeating); } /** @@ -96,6 +96,34 @@ class PiggybankController extends BaseController return View::make('piggybanks.show')->with('piggyBank', $piggyBank); } + /** + * @return $this|\Illuminate\Http\RedirectResponse + */ + public function storePiggybank() + { + $data = Input::all(); + unset($data['_token']); + + // extend the data array with the settings needed to create a piggy bank: + $data['repeats'] = 0; + $data['rep_times'] = 0; + $data['order'] = 0; + + $piggyBank = $this->_repository->store($data); + if ($piggyBank->validate()) { + Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!'); + + return Redirect::route('piggybanks.index'); + + + } else { + Session::flash('error', 'Could not save piggy bank: ' . $piggyBank->errors()->first()); + + return Redirect::route('piggybanks.create.piggybank')->withInput()->withErrors($piggyBank->errors()); + } + + } + /** * @return $this|\Illuminate\Http\RedirectResponse */ diff --git a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php index 2861812c95..6e3b58ad93 100644 --- a/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php +++ b/app/lib/Firefly/Storage/Piggybank/EloquentPiggybankRepository.php @@ -34,6 +34,21 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface )->where('piggybanks.id', $piggyBankId)->first(['piggybanks.*']); } + public function countRepeating() + { + return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where( + 'accounts.user_id', \Auth::user()->id + )->where('repeats', 1)->count(); + } + + public function countNonrepeating() + { + return \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where( + 'accounts.user_id', \Auth::user()->id + )->where('repeats', 0)->count(); + + } + /** * @return mixed */ @@ -57,14 +72,8 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface $account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null; - $piggyBank = new \Piggybank; + $piggyBank = new \Piggybank($data); $piggyBank->account()->associate($account); - $piggyBank->targetdate - = isset($data['targetdate']) && strlen($data['targetdate']) > 0 ? $data['targetdate'] : null; - $piggyBank->name = isset($data['name']) ? $data['name'] : null; - $piggyBank->amount = 0; - $piggyBank->target = floatval($data['target']); - $piggyBank->order = 1; if ($piggyBank->validate()) { $piggyBank->save(); } diff --git a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php index 329c15aab2..26d241bdf4 100644 --- a/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Piggybank/PiggybankRepositoryInterface.php @@ -23,6 +23,16 @@ interface PiggybankRepositoryInterface */ public function count(); + /** + * @return mixed + */ + public function countRepeating(); + + /** + * @return mixed + */ + public function countNonrepeating(); + /** * @param $data * diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php index 370d5eb1e8..30c4488a52 100644 --- a/app/models/Piggybank.php +++ b/app/models/Piggybank.php @@ -39,6 +39,15 @@ use LaravelBook\Ardent\Ardent as Ardent; */ class Piggybank extends Ardent { + public $fillable = [ + 'name', + 'account_id', + 'targetamount', + 'repeats', + 'rep_times', + 'order' + ]; + public static $rules = [ 'account_id' => 'required|exists:accounts,id', diff --git a/app/routes.php b/app/routes.php index 7fab71dca2..2da63ef17a 100644 --- a/app/routes.php +++ b/app/routes.php @@ -135,7 +135,8 @@ Route::group(['before' => 'auth'], function () { // piggy bank controller Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']); - Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']); + Route::get('/piggybanks/create/piggybank', ['uses' => 'PiggybankController@createPiggybank','as' => 'piggybanks.create.piggybank']); + Route::get('/piggybanks/create/repeated', ['uses' => 'PiggybankController@createRepeated','as' => 'piggybanks.create.repeated']); Route::get('/piggybanks/show/{piggybank}', ['uses' => 'PiggybankController@show','as' => 'piggybanks.show']); Route::get('/piggybanks/edit/{piggybank}', ['uses' => 'PiggybankController@edit','as' => 'piggybanks.edit']); Route::get('/piggybanks/delete/{piggybank}', ['uses' => 'PiggybankController@delete','as' => 'piggybanks.delete']); @@ -199,7 +200,8 @@ Route::group(['before' => 'csrf|auth'], function () { // piggy bank controller - Route::post('/piggybanks/store',['uses' => 'PiggybankController@store','as' => 'piggybanks.store']); + Route::post('/piggybanks/store/piggybank',['uses' => 'PiggybankController@storePiggybank','as' => 'piggybanks.store.piggybank']); + Route::post('/piggybanks/store/repeated',['uses' => 'PiggybankController@storeRepeated','as' => 'piggybanks.store.repeated']); Route::post('/piggybanks/update', ['uses' => 'PiggybankController@update','as' => 'piggybanks.update']); Route::post('/piggybanks/destroy/{piggybank}', ['uses' => 'PiggybankController@destroy','as' => 'piggybanks.destroy']); diff --git a/app/views/piggybanks/create-piggybank.blade.php b/app/views/piggybanks/create-piggybank.blade.php new file mode 100644 index 0000000000..68147e1686 --- /dev/null +++ b/app/views/piggybanks/create-piggybank.blade.php @@ -0,0 +1,101 @@ +@extends('layouts.default') +@section('content') +
+
+

Firefly + Create a new piggy bank +

+

Use piggy banks to save for a one-time goal.

+
+
+ +{{Form::open(['class' => 'form-horizontal','url' => route('piggybanks.store.piggybank')])}} + +
+
+

Mandatory fields

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

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

+ @else + For example: new bike, new camera + @endif +
+
+ +
+ +
+ {{Form::select('account_id',$accounts,Input::old('account_id') ?: Input::get('account'),['class' => 'form-control'])}} + @if($errors->has('account_id')) +

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

+ @else + Indicate on which account you've got your savings. + @endif +
+
+ +
+ {{ Form::label('targetamount', 'Target amount', ['class' => 'col-sm-4 control-label'])}} +
+
+ + {{Form::input('number','targetamount', Input::old('targetamount'), ['step' => 'any', 'min' => '1', 'class' => 'form-control'])}} +
+ + @if($errors->has('targetamount')) +

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

+ @else + How much money do you need to save? + @endif +
+
+
+
+

Optional fields

+ + +
+ {{ Form::label('startdate', 'Start date', ['class' => 'col-sm-4 control-label'])}} +
+ + This date indicates when you start(ed) saving money for this piggy bank. This field defaults to today and you should keep it on today. +
+
+ +
+ {{ Form::label('targetdate', 'Target date', ['class' => 'col-sm-4 control-label'])}} +
+ + This field indicates when you want to have saved the indicated amount. +
+
+ + +
+
+ +
+
+ +
+
+ +
+
+
+
+ +{{Form::close()}} +@stop +@section('scripts') + +@stop diff --git a/app/views/piggybanks/create.blade.php b/app/views/piggybanks/create-repeated.blade.php similarity index 100% rename from app/views/piggybanks/create.blade.php rename to app/views/piggybanks/create-repeated.blade.php diff --git a/app/views/piggybanks/index.blade.php b/app/views/piggybanks/index.blade.php index 8bbc65b0d7..72fbc0d5b7 100644 --- a/app/views/piggybanks/index.blade.php +++ b/app/views/piggybanks/index.blade.php @@ -16,152 +16,37 @@ to the piggy bank.

- Create new piggy bank + Create new piggy bank

-

Repeated expenses

+

Save money for repeated expenses

Taxes are due every year. Or maybe you want to save up for your yearly fireworks-binge. Buy a new smart phone every three years. Firefly can help you organize these repeated expenses.

- Create new repeated expense + Create new repeated expense

-{{-- -

- Saving money is hard. Firefly's piggy banks can help you to save money. You can do two things using - these piggy banks: -

-
    -
  1. Save money towards a singular goal such as a new bike or a new car.
  2. -
  3. Save money repeatedly, for yearly expenses or long-term recurring investments. One example may be buying - a new phone every three year.
  4. -
- @if($count == 0) -

- Create new piggy bank -

+
+
+

Current piggy banks

+ @if($countNonRepeating == 0) +

No piggy banks found.

@endif -
-
- -@if($count > 0) -
-
-

Accounts used for piggy banks

-

- @if(count($accounts) != 1) - These (savings) accounts have - @else - This (savings) account has - @endif - @if(count($piggybanks) == 1) - a piggy bank - @else - piggy banks - @endif - associated to - @if(count($piggybanks) != 1) - them. - @else - it. - @endif - If you transfer money to or from - @if(count($accounts) != 1) - these accounts, - @else - this account, - @endif - you may associate it with your @if(count($piggybanks) != 1) - piggy banks. - @else - piggy bank. - @endif -

-
-
- - - - - - - - @foreach($accounts as $account) - - - - - - - @endforeach -
AccountCurrent balanceLeft for (other) piggy banksTotal target
{{{$account->name}}}{{mf($account->balance)}}{{mf($account->left)}}{{mf($account->total)}}
+

Current repeated expenses

+ @if($countRepeating == 0) +

No repeated expenses found.

+ @endif
- -
-
-

Piggy banks

- - @foreach($piggybanks as $piggybank) -

- {{{$piggybank->name}}} -

- @endforeach - - - @foreach($piggybanks as $piggybank) - - @if(!is_null($piggybank->targetdate)) -

- Target date: {{$piggybank->targetdate->format('jS F Y')}} -

- @endif - - - - - - - -
-
- - -
-
{{$piggybank->pct}} -
- - -
-
- @endforeach -

- Create new piggy bank -

- -
-
-@endif ---}} - @stop @section('scripts') - - @stop \ No newline at end of file