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') +
Use piggy banks to save for a one-time goal.
+{{$errors->first('name')}}
+ @else + For example: new bike, new camera + @endif +{{$errors->first('account_id')}}
+ @else + Indicate on which account you've got your savings. + @endif +{{$errors->first('targetamount')}}
+ @else + How much money do you need to save? + @endif +- 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.
- Saving money is hard. Firefly's piggy banks can help you to save money. You can do two things using - these piggy banks: -
-No piggy banks found.
@endif -- @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 -
Account | -Current balance | -Left for (other) piggy banks | -Total target | -
---|---|---|---|
{{{$account->name}}} | -{{mf($account->balance)}} | -{{mf($account->left)}} | -{{mf($account->total)}} | -
No repeated expenses found.
+ @endif- Target date: {{$piggybank->targetdate->format('jS F Y')}} -
- @endif -
-
- €
-
-
- |
- - | {{$piggybank->pct}} | -- - | -