From 28aaea1aa3e28bdfc51fe6c7e27817ae052d8a6e Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sun, 5 Oct 2014 19:29:25 +0200 Subject: [PATCH] Extended forms and started on recurring transactions. --- app/config/app.php | 4 +- app/controllers/RecurringController.php | 78 ++++++++---- app/controllers/TransactionController.php | 12 +- app/lib/Firefly/Form/Form.php | 82 ++++++++++--- ...EloquentRecurringTransactionRepository.php | 84 +++++++------ ...ecurringTransactionRepositoryInterface.php | 3 +- app/start/global.php | 13 +- app/views/recurring/create.blade.php | 114 ++---------------- app/views/transactions/create.blade.php | 2 +- app/views/transactions/edit.blade.php | 2 +- 10 files changed, 193 insertions(+), 201 deletions(-) diff --git a/app/config/app.php b/app/config/app.php index 85182e8c8e..87816af984 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -37,8 +37,8 @@ return [ 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', - 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', - 'Barryvdh\Debugbar\ServiceProvider', + # 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', + # 'Barryvdh\Debugbar\ServiceProvider', 'Firefly\Storage\StorageServiceProvider', 'Firefly\Helper\HelperServiceProvider', 'Firefly\Validation\ValidationServiceProvider', diff --git a/app/controllers/RecurringController.php b/app/controllers/RecurringController.php index 146a52d77c..599c7563a6 100644 --- a/app/controllers/RecurringController.php +++ b/app/controllers/RecurringController.php @@ -19,8 +19,8 @@ class RecurringController extends BaseController { $this->_repository = $repository; - View::share('title','Recurring transactions'); - View::share('mainTitleIcon','fa-rotate-right'); + View::share('title', 'Recurring transactions'); + View::share('mainTitleIcon', 'fa-rotate-right'); } /** @@ -28,7 +28,7 @@ class RecurringController extends BaseController */ public function create() { - View::share('subTitle','Create new'); + View::share('subTitle', 'Create new'); $periods = \Config::get('firefly.periods_to_text'); return View::make('recurring.create')->with('periods', $periods); @@ -41,7 +41,7 @@ class RecurringController extends BaseController */ public function delete(RecurringTransaction $recurringTransaction) { - View::share('subTitle','Delete "' .$recurringTransaction->name.'"'); + View::share('subTitle', 'Delete "' . $recurringTransaction->name . '"'); return View::make('recurring.delete')->with('recurringTransaction', $recurringTransaction); } @@ -73,7 +73,7 @@ class RecurringController extends BaseController { $periods = \Config::get('firefly.periods_to_text'); - View::share('subTitle','Edit "' .$recurringTransaction->name.'"'); + View::share('subTitle', 'Edit "' . $recurringTransaction->name . '"'); return View::make('recurring.edit')->with('periods', $periods)->with( 'recurringTransaction', $recurringTransaction @@ -93,14 +93,11 @@ class RecurringController extends BaseController */ public function show(RecurringTransaction $recurringTransaction) { - View::share('subTitle',$recurringTransaction->name); + View::share('subTitle', $recurringTransaction->name); return View::make('recurring.show')->with('recurring', $recurringTransaction); } - /** - * @return $this|\Illuminate\Http\RedirectResponse - */ public function store() { @@ -108,25 +105,54 @@ class RecurringController extends BaseController default: throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.'); break; + case 'store': + case 'create_another': + /* + * Try to store: + */ + $messageBag = $this->_repository->store(Input::all()); + + /* + * Failure! + */ + if ($messageBag->count() > 0) { + Session::flash('error', 'Could not save recurring transaction: ' . $messageBag->first()); + return Redirect::route('recurring.create')->withInput()->withErrors($messageBag); + } + + /* + * Success! + */ + Session::flash('success', 'Recurring transaction "' . e(Input::get('name')) . '" saved!'); + + /* + * Redirect to original location or back to the form. + */ + if (Input::get('post_submit_action') == 'create_another') { + return Redirect::route('recurring.create')->withInput(); + } else { + return Redirect::route('recurring.index'); + } + break; } - $recurringTransaction = $this->_repository->store(Input::all()); - - if ($recurringTransaction->errors()->count() == 0) { - Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!'); - //Event::fire('recurring.store', [$recurringTransaction]); - if (Input::get('create') == '1') { - return Redirect::route('recurring.create')->withInput(); - } else { - return Redirect::route('recurring.index'); - } - } else { - Session::flash( - 'error', 'Could not save the recurring transaction: ' . $recurringTransaction->errors()->first() - ); - - return Redirect::route('recurring.create')->withInput()->withErrors($recurringTransaction->errors()); - } +// +// +// if ($recurringTransaction->errors()->count() == 0) { +// Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!'); +// //Event::fire('recurring.store', [$recurringTransaction]); +// if (Input::get('create') == '1') { +// return Redirect::route('recurring.create')->withInput(); +// } else { +// return Redirect::route('recurring.index'); +// } +// } else { +// Session::flash( +// 'error', 'Could not save the recurring transaction: ' . $recurringTransaction->errors()->first() +// ); +// +// return Redirect::route('recurring.create')->withInput()->withErrors($recurringTransaction->errors()); +// } } /** diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 53d7546c85..cbe56bd115 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -67,19 +67,9 @@ class TransactionController extends BaseController $piggies = $toolkit->makeSelectList($piggyRepository->get()); $piggies[0] = '(no piggy bank)'; - /* - * Catch messages from validation round: - */ - if (Session::has('messages')) { - $messages = Session::get('messages'); - Session::forget('messages'); - } else { - $messages = new MessageBag; - } - return View::make('transactions.create')->with('accounts', $assetAccounts)->with('budgets', $budgets)->with( 'what', $what - )->with('piggies', $piggies)->with('subTitle', 'Add a new ' . $what)->with('messages', $messages); + )->with('piggies', $piggies)->with('subTitle', 'Add a new ' . $what); } /** diff --git a/app/lib/Firefly/Form/Form.php b/app/lib/Firefly/Form/Form.php index bbe8f3f4b4..5def4eaaf1 100644 --- a/app/lib/Firefly/Form/Form.php +++ b/app/lib/Firefly/Form/Form.php @@ -16,19 +16,52 @@ class Form * @return string * @throws FireflyException */ - public static function ffNumber($name, $value = null, array $options = []) + public static function ffInteger($name, $value = null, array $options = []) { - $options['step'] = 'any'; - $options['min'] = '0.01'; + $options['step'] = '1'; return self::ffInput('number', $name, $value, $options); } + public static function ffCheckbox($name, $value = 1, $checked = null, $options = []) + { + $options['checked'] = $checked ? true : null; + return self::ffInput('checkbox', $name, $value, $options); + } + + public static function ffAmount($name, $value = null, array $options = []) + { + $options['step'] = 'any'; + $options['min'] = '0.01'; + return self::ffInput('amount', $name, $value, $options); + + } + + /** + * @param $name + * @param null $value + * @param array $options + * @return string + * @throws FireflyException + */ public static function ffDate($name, $value = null, array $options = []) { return self::ffInput('date', $name, $value, $options); } + /** + * @param $name + * @param null $value + * @param array $options + * @return string + * @throws FireflyException + */ + public static function ffTags($name, $value = null, array $options = []) + { + $options['data-role'] = 'tagsinput'; + return self::ffInput('text', $name, $value, $options); + } + /** * @param $name * @param array $list @@ -55,6 +88,22 @@ class Form } + public static function label($name) + { + $labels = [ + 'amount_min' => 'Amount (min)', + 'amount_max' => 'Amount (max)', + 'match' => 'Matches on', + 'repeat_freq' => 'Repetition', + 'account_from_id' => 'Account from', + 'account_to_id' => 'Account to', + 'account_id' => 'Asset account' + ]; + + return isset($labels[$name]) ? $labels[$name] : str_replace('_',' ',ucfirst($name)); + + } + /** * @param $type * @param $name @@ -72,18 +121,11 @@ class Form $options['class'] = 'form-control'; $options['id'] = 'ffInput_' . $name; $options['autocomplete'] = 'off'; - $options['type'] = 'text'; - + $label = self::label($name); /* * Make label and placeholder look nice. */ - $options['placeholder'] = isset($options['label']) ? $options['label'] : ucfirst($name); - $options['label'] = isset($options['label']) ? $options['label'] : ucfirst($name); - if ($name == 'account_id') { - $options['label'] = 'Asset account'; - } - $options['label'] = str_replace(['_'], [' '], $options['label']); - $options['placeholder'] = str_replace(['_'], [' '], $options['placeholder']); + $options['placeholder'] = ucfirst($name); /* * Get the value. @@ -129,7 +171,6 @@ class Form /* * Add some HTML. */ - $label = isset($options['label']) ? $options['label'] : ucfirst($name); $html = '
'; $html .= ''; $html .= '
'; @@ -138,14 +179,27 @@ class Form /* * Switch input type: */ + unset($options['label']); switch ($type) { case 'text': $html .= \Form::input('text', $name, $value, $options); break; - case 'number': + case 'amount': $html .= '
'; $html .= \Form::input('number', $name, $value, $options); $html .= '
'; + break; + case 'number': + $html .= \Form::input('number', $name, $value, $options); + break; + case 'checkbox': + $checked = $options['checked']; + unset($options['checked'], $options['placeholder'], $options['autocomplete'], $options['class']); + $html .= '
'; + + break; case 'date': $html .= \Form::input('date', $name, $value, $options); diff --git a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php index 78d1261682..7d337b696f 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php @@ -5,6 +5,7 @@ namespace Firefly\Storage\RecurringTransaction; use Carbon\Carbon; use Illuminate\Queue\Jobs\Job; +use Illuminate\Support\MessageBag; /** * Class EloquentRecurringTransactionRepository @@ -45,7 +46,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo } /** - * @param Job $job + * @param Job $job * @param array $payload * * @return mixed @@ -57,13 +58,13 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo /** @var \Importmap $importMap */ $importMap = $repository->findImportmap($payload['mapID']); - $user = $importMap->user; + $user = $importMap->user; $this->overruleUser($user); /* * maybe the recurring transaction is already imported: */ - $oldId = intval($payload['data']['id']); + $oldId = intval($payload['data']['id']); $description = $payload['data']['description']; $importEntry = $repository->findImportEntry($importMap, 'RecurringTransaction', $oldId); @@ -84,17 +85,17 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo $recurringTransaction = $this->findByName($payload['data']['description']); if (is_null($recurringTransaction)) { $amount = floatval($payload['data']['amount']); - $pct = intval($payload['data']['pct']); + $pct = intval($payload['data']['pct']); $set = [ - 'name' => $description, - 'match' => join(',', explode(' ', $description)), - 'amount_min' => $amount * ($pct / 100) * -1, - 'amount_max' => $amount * (1 + ($pct / 100)) * -1, - 'date' => date('Y-m-') . $payload['data']['dom'], + 'name' => $description, + 'match' => join(',', explode(' ', $description)), + 'amount_min' => $amount * ($pct / 100) * -1, + 'amount_max' => $amount * (1 + ($pct / 100)) * -1, + 'date' => date('Y-m-') . $payload['data']['dom'], 'repeat_freq' => 'monthly', - 'active' => intval($payload['data']['inactive']) == 1 ? 0 : 1, - 'automatch' => 1, + 'active' => intval($payload['data']['inactive']) == 1 ? 0 : 1, + 'automatch' => 1, ]; $recurringTransaction = $this->store($set); @@ -131,45 +132,52 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo /** * @param $data * - * @return mixed|\RecurringTransaction + * @return MessageBag */ public function store($data) { + $messageBag = new MessageBag; $recurringTransaction = new \RecurringTransaction( [ - 'user_id' => $this->_user->id, - 'name' => $data['name'], - 'match' => join(' ', explode(',', $data['match'])), - 'amount_max' => floatval($data['amount_max']), - 'amount_min' => floatval($data['amount_min']), - 'date' => new Carbon($data['date']), - 'active' => isset($data['active']) ? intval($data['active']) : 0, - 'automatch' => isset($data['automatch']) ? intval($data['automatch']) : 0, - 'skip' => isset($data['skip']) ? intval($data['skip']) : 0, + 'user_id' => $this->_user->id, + 'name' => $data['name'], + 'match' => join(' ', explode(',', $data['match'])), + 'amount_max' => floatval($data['amount_max']), + 'amount_min' => floatval($data['amount_min']), + 'date' => new Carbon($data['date']), + 'active' => isset($data['active']) ? intval($data['active']) : 0, + 'automatch' => isset($data['automatch']) ? intval($data['automatch']) : 0, + 'skip' => isset($data['skip']) ? intval($data['skip']) : 0, 'repeat_freq' => $data['repeat_freq'], ] ); + // unique name? + $count = $this->_user->recurringtransactions()->whereName($data['name'])->count(); + if($count > 0) { + $messageBag->add('name', 'A recurring transaction with this name already exists.'); + return $messageBag; + } + // both amounts zero?: if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) { - $recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.'); - - return $recurringTransaction; + $messageBag->add('amount_max', 'Amount max and min cannot both be zero.'); + return $messageBag; } if ($recurringTransaction->amount_max < $recurringTransaction->amount_min) { - $recurringTransaction->errors()->add('amount_max', 'Amount max must be more than amount min.'); - return $recurringTransaction; + $messageBag->add('amount_max', 'Amount max must be more than amount min.'); + return $messageBag; } if ($recurringTransaction->amount_min > $recurringTransaction->amount_max) { - $recurringTransaction->errors()->add('amount_max', 'Amount min must be less than amount max.'); - return $recurringTransaction; + $messageBag->add('amount_max', 'Amount min must be less than amount max.'); + return $messageBag; } - if($recurringTransaction->date < Carbon::now()) { - $recurringTransaction->errors()->add('date', 'Must be in the future.'); - return $recurringTransaction; + if ($recurringTransaction->date < Carbon::now()) { + $messageBag->add('date', 'Must be in the future.'); + return $messageBag; } @@ -177,7 +185,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo $recurringTransaction->save(); } - return $recurringTransaction; + return $messageBag; } /** @@ -188,8 +196,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo */ public function update(\RecurringTransaction $recurringTransaction, $data) { - $recurringTransaction->name = $data['name']; - $recurringTransaction->match = join(' ', explode(',', $data['match'])); + $recurringTransaction->name = $data['name']; + $recurringTransaction->match = join(' ', explode(',', $data['match'])); $recurringTransaction->amount_max = floatval($data['amount_max']); $recurringTransaction->amount_min = floatval($data['amount_min']); @@ -199,10 +207,10 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo return $recurringTransaction; } - $recurringTransaction->date = new Carbon($data['date']); - $recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0; - $recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0; - $recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0; + $recurringTransaction->date = new Carbon($data['date']); + $recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0; + $recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0; + $recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0; $recurringTransaction->repeat_freq = $data['repeat_freq']; if ($recurringTransaction->validate()) { diff --git a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php index 30330bad1b..1f43e6d8f1 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php @@ -3,6 +3,7 @@ namespace Firefly\Storage\RecurringTransaction; use Illuminate\Queue\Jobs\Job; +use Illuminate\Support\MessageBag; /** * Interface RecurringTransactionRepositoryInterface @@ -34,7 +35,7 @@ interface RecurringTransactionRepositoryInterface /** * @param $data * - * @return mixed + * @return MessageBag */ public function store($data); diff --git a/app/start/global.php b/app/start/global.php index 981afc49ec..23048b3399 100644 --- a/app/start/global.php +++ b/app/start/global.php @@ -78,12 +78,21 @@ App::down( \Form::macro('ffSelect', function ($name, array $list = [], $selected = null, array $options = []) { return \Firefly\Form\Form::ffSelect($name, $list, $selected, $options); }); -\Form::macro('ffNumber', function ($name, $value = null, array $options = []) { - return \Firefly\Form\Form::ffNumber($name, $value, $options); +\Form::macro('ffInteger', function ($name, $value = null, array $options = []) { + return \Firefly\Form\Form::ffInteger($name, $value, $options); +}); +\Form::macro('ffAmount', function ($name, $value = null, array $options = []) { + return \Firefly\Form\Form::ffAmount($name, $value, $options); }); \Form::macro('ffDate', function ($name, $value = null, array $options = []) { return \Firefly\Form\Form::ffDate($name, $value, $options); }); +\Form::macro('ffTags', function ($name, $value = null, array $options = []) { + return \Firefly\Form\Form::ffTags($name, $value, $options); +}); +\Form::macro('ffCheckbox',function ($name, $value = 1, $checked = null, $options = []) { + return \Firefly\Form\Form::ffCheckbox($name, $value, $checked, $options); +}); /* |-------------------------------------------------------------------------- diff --git a/app/views/recurring/create.blade.php b/app/views/recurring/create.blade.php index 7b13c2e20c..20cc6389e4 100644 --- a/app/views/recurring/create.blade.php +++ b/app/views/recurring/create.blade.php @@ -10,74 +10,12 @@ Mandatory fields
- -
- -
- - @if($errors->has('name')) -

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

- @endif -
-
-
- -
- - @if($errors->has('match')) -

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

- @endif -
-
- -
- {{ Form::label('amount_min', 'Minimum amount', ['class' => 'col-sm-4 control-label'])}} -
-
- - {{Form::input('number','amount_min', Input::old('amount_min'), ['step' => 'any', 'class' => 'form-control'])}} -
- - @if($errors->has('amount_min')) -

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

- @endif -
-
- -
- {{ Form::label('amount_max', 'Maximum amount', ['class' => 'col-sm-4 control-label'])}} -
-
- - {{Form::input('number','amount_max', Input::old('amount_max'), ['step' => 'any', 'class' => 'form-control'])}} -
- - @if($errors->has('amount_max')) -

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

- @endif -
-
- -
- {{ Form::label('date', 'Date', ['class' => 'col-sm-4 control-label'])}} -
- {{ Form::input('date','date', Input::old('date') ?: Carbon\Carbon::now()->addDay()->format('Y-m-d'), ['class' - => 'form-control']) }} - @if($errors->has('date')) -

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

- @endif -
-
- -
- -
- {{Form::select('repeat_freq',$periods,Input::old('repeat_freq') ?: 'monthly',['class' => 'form-control'])}} - @if($errors->has('repeat_freq')) -

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

- @endif -
-
+ {{Form::ffText('name')}} + {{Form::ffTags('match')}} + {{Form::ffAmount('amount_min')}} + {{Form::ffAmount('amount_max')}} + {{Form::ffDate('date',Carbon\Carbon::now()->addDay()->format('Y-m-d'))}} + {{Form::ffSelect('repeat_freq',$periods,'monthly')}}

@@ -93,43 +31,9 @@ Optional fields

-
- {{ Form::label('skip', 'Skip', ['class' => 'col-sm-4 control-label'])}} -
- {{Form::input('number','skip', Input::old('skip') ?: 0, ['class' => 'form-control'])}} - - @if($errors->has('skip')) -

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

- @else - Make Firefly skip every n times. Fill in 2, and Firefly - will match, skip, skip and match a transaction. - @endif -
-
-
- -
-
- -
- Firefly will automatically match transactions. -
-
-
- -
-
- -
- This recurring transaction is actually active. -
-
+ {{Form::ffInteger('skip',0)}} + {{Form::ffCheckbox('automatch',1,true)}} + {{Form::ffCheckbox('active',1,true)}}
diff --git a/app/views/transactions/create.blade.php b/app/views/transactions/create.blade.php index 91fa93176f..7acc907bcc 100644 --- a/app/views/transactions/create.blade.php +++ b/app/views/transactions/create.blade.php @@ -37,7 +37,7 @@ - {{Form::ffNumber('amount')}} + {{Form::ffAmount('amount')}} {{Form::ffDate('date', date('Y-m-d'))}} diff --git a/app/views/transactions/edit.blade.php b/app/views/transactions/edit.blade.php index 1856d334b7..4a96bc8778 100644 --- a/app/views/transactions/edit.blade.php +++ b/app/views/transactions/edit.blade.php @@ -35,7 +35,7 @@ @endif - {{Form::ffNumber('amount',$data['amount'])}} + {{Form::ffAmount('amount',$data['amount'])}} {{Form::ffDate('date',$data['date'])}}