diff --git a/app/controllers/RecurringController.php b/app/controllers/RecurringController.php index 599c7563a6..3ff0fea28b 100644 --- a/app/controllers/RecurringController.php +++ b/app/controllers/RecurringController.php @@ -2,6 +2,7 @@ use Firefly\Exception\FireflyException; use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface as RTR; +use Firefly\Helper\Controllers\RecurringInterface as RI; /** * Class RecurringController @@ -11,13 +12,16 @@ use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface class RecurringController extends BaseController { protected $_repository; + protected $_helper; /** * @param RTR $repository + * @param RI $helper */ - public function __construct(RTR $repository) + public function __construct(RTR $repository, RI $helper) { $this->_repository = $repository; + $this->_helper = $helper; View::share('title', 'Recurring transactions'); View::share('mainTitleIcon', 'fa-rotate-right'); @@ -100,7 +104,7 @@ class RecurringController extends BaseController public function store() { - + $data = Input::except(['_token', 'post_submit_action']); switch (Input::get('post_submit_action')) { default: throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.'); @@ -110,7 +114,7 @@ class RecurringController extends BaseController /* * Try to store: */ - $messageBag = $this->_repository->store(Input::all()); + $messageBag = $this->_repository->store($data); /* * Failure! @@ -134,49 +138,76 @@ class RecurringController extends BaseController return Redirect::route('recurring.index'); } break; + case 'validate_only': + $messageBags = $this->_helper->validate($data); + + Session::flash('warnings', $messageBags['warnings']); + Session::flash('successes', $messageBags['successes']); + Session::flash('errors', $messageBags['errors']); + return Redirect::route('recurring.create')->withInput(); + break; } -// -// -// 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()); -// } } - /** - * @param RecurringTransaction $recurringTransaction - * - * @return $this|\Illuminate\Http\RedirectResponse - */ public function update(RecurringTransaction $recurringTransaction) { - /** @var \RecurringTransaction $recurringTransaction */ - $recurringTransaction = $this->_repository->update($recurringTransaction, Input::all()); - if ($recurringTransaction->errors()->count() == 0) { - Session::flash('success', 'The recurring transaction has been updated.'); - //Event::fire('recurring.update', [$recurringTransaction]); + $data = Input::except(['_token', 'post_submit_action']); + switch (Input::get('post_submit_action')) { + case 'update': + case 'return_to_edit': + $messageBag = $this->_repository->update($recurringTransaction, $data); + if ($messageBag->count() == 0) { + // has been saved, return to index: + Session::flash('success', 'Recurring transaction updated!'); - return Redirect::route('recurring.index'); - } else { - Session::flash( - 'error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first() - ); + if (Input::get('post_submit_action') == 'return_to_edit') { + return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput(); + } else { + return Redirect::route('recurring.index'); + } + } else { + Session::flash('error', 'Could not update recurring transaction: ' . $messageBag->first()); - return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors( - $recurringTransaction->errors() - ); + return Redirect::route('transactions.edit', $recurringTransaction->id)->withInput() + ->withErrors($messageBag); + } + + + break; + case 'validate_only': + $data = Input::all(); + $data['id'] = $recurringTransaction->id; + $messageBags = $this->_helper->validate($data); + + Session::flash('warnings', $messageBags['warnings']); + Session::flash('successes', $messageBags['successes']); + Session::flash('errors', $messageBags['errors']); + return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput(); + + break; + // update + default: + throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.'); + break; } + + +// /** @var \RecurringTransaction $recurringTransaction */ +// $recurringTransaction = $this->_repository->update($recurringTransaction, Input::all()); +// if ($recurringTransaction->errors()->count() == 0) { +// Session::flash('success', 'The recurring transaction has been updated.'); +// //Event::fire('recurring.update', [$recurringTransaction]); +// +// return Redirect::route('recurring.index'); +// } else { +// Session::flash( +// 'error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first() +// ); +// +// return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors( +// $recurringTransaction->errors() +// ); +// } } } \ No newline at end of file diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index cbe56bd115..bd73089784 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -323,7 +323,7 @@ class TransactionController extends BaseController public function update(TransactionJournal $journal) { switch (Input::get('post_submit_action')) { - case 'store': + case 'update': case 'return_to_edit': $what = strtolower($journal->transactionType->type); $messageBag = $this->_helper->update($journal, Input::all()); @@ -333,7 +333,7 @@ class TransactionController extends BaseController Event::fire('journals.update', [$journal]); if (Input::get('post_submit_action') == 'return_to_edit') { - return Redirect::route('transactions.edit', $journal->id); + return Redirect::route('transactions.edit', $journal->id)->withInput(); } else { return Redirect::route('transactions.index.' . $what); } diff --git a/app/lib/Firefly/Form/Form.php b/app/lib/Firefly/Form/Form.php index 5def4eaaf1..fbb2f2b2fb 100644 --- a/app/lib/Firefly/Form/Form.php +++ b/app/lib/Firefly/Form/Form.php @@ -100,10 +100,73 @@ class Form 'account_id' => 'Asset account' ]; - return isset($labels[$name]) ? $labels[$name] : str_replace('_',' ',ucfirst($name)); + return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name)); } + /** + * Return buttons for update/validate/return. + * + * @param $type + * @param $name + */ + public static function ffOptionsList($type, $name) + { + $previousValue = \Input::old('post_submit_action'); + $previousValue = is_null($previousValue) ? 'store' : $previousValue; + /* + * Store. + */ + $store = ''; + switch ($type) { + case 'create': + $store = '
'; + $store .= '
'; + break; + case 'update': + $store = '
'; + $store .= '
'; + break; + default: + throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type); + break; + } + + /* + * validate is always the same: + */ + $validate = '
'; + + /* + * Store & return: + */ + switch ($type) { + case 'create': + $return = '
'; + break; + case 'update': + $return = '
'; + break; + default: + throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type); + break; + } + return $store.$validate.$return; + } + /** * @param $type * @param $name diff --git a/app/lib/Firefly/Helper/Controllers/Recurring.php b/app/lib/Firefly/Helper/Controllers/Recurring.php new file mode 100644 index 0000000000..8335638413 --- /dev/null +++ b/app/lib/Firefly/Helper/Controllers/Recurring.php @@ -0,0 +1,120 @@ +add('name', 'The name should not be this short.'); + } + if (strlen($data['name']) > 250) { + $errors->add('name', 'The name should not be this long.'); + } + if (! isset($data['id'])) { + $count = \Auth::user()->recurringtransactions()->whereName($data['name'])->count(); + } else { + $count = \Auth::user()->recurringtransactions()->whereName($data['name'])->where('id', '!=', $data['id'])->count(); + } + if ($count > 0) { + $errors->add('name', 'A recurring transaction with this name already exists.'); + } + if (count($errors->get('name')) == 0) { + $successes->add('name', 'OK!'); + } + + /* + * Match + */ + if (count(explode(',', $data['match'])) > 10) { + $warnings->add('match', 'This many matches is pretty pointless'); + } + if (strlen($data['match']) == 0) { + $errors->add('match', 'Cannot match on nothing.'); + } + if (count($errors->get('match')) == 0) { + $successes->add('match', 'OK!'); + } + + /* + * Amount + */ + if (floatval($data['amount_max']) == 0 && floatval($data['amount_min']) == 0) { + $errors->add('amount_min', 'Amount max and min cannot both be zero.'); + $errors->add('amount_max', 'Amount max and min cannot both be zero.'); + } + + if (floatval($data['amount_max']) < floatval($data['amount_min'])) { + $errors->add('amount_max', 'Amount max must be more than amount min.'); + } + + if (floatval($data['amount_min']) > floatval($data['amount_max'])) { + $errors->add('amount_max', 'Amount min must be less than amount max.'); + } + if (count($errors->get('amount_min')) == 0) { + $successes->add('amount_min', 'OK!'); + } + if (count($errors->get('amount_max')) == 0) { + $successes->add('amount_max', 'OK!'); + } + + + /* + * Date + */ + try { + $date = new Carbon($data['date']); + } catch (Exception $e) { + $errors->add('date', 'The date entered was invalid'); + } + if (strlen($data['date']) == 0) { + $errors->add('date', 'The date entered was invalid'); + } + if (!$errors->has('date')) { + $successes->add('date', 'OK!'); + } + + $successes->add('active', 'OK!'); + $successes->add('automatch', 'OK!'); + + if (intval($data['skip']) < 0) { + $errors->add('skip', 'Cannot be below zero.'); + } else if (intval($data['skip']) > 31) { + $errors->add('skip', 'Cannot be above 31.'); + } + if (count($errors->get('skip')) == 0) { + $successes->add('skip', 'OK!'); + } + + $set = \Config::get('firefly.budget_periods'); + if (!in_array($data['repeat_freq'], $set)) { + $errors->add('repeat_freq', 'Invalid value.'); + } + if (count($errors->get('repeat_freq')) == 0) { + $successes->add('repeat_freq', 'OK!'); + } + + return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes]; + + } +} \ No newline at end of file diff --git a/app/lib/Firefly/Helper/Controllers/RecurringInterface.php b/app/lib/Firefly/Helper/Controllers/RecurringInterface.php new file mode 100644 index 0000000000..b14183201c --- /dev/null +++ b/app/lib/Firefly/Helper/Controllers/RecurringInterface.php @@ -0,0 +1,15 @@ +app->bind( + 'Firefly\Helper\Controllers\RecurringInterface', + 'Firefly\Helper\Controllers\Recurring' + ); + $this->app->bind( 'Firefly\Helper\Controllers\SearchInterface', 'Firefly\Helper\Controllers\Search' diff --git a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php index 7d337b696f..ac96284c30 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/EloquentRecurringTransactionRepository.php @@ -154,7 +154,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo // unique name? $count = $this->_user->recurringtransactions()->whereName($data['name'])->count(); - if($count > 0) { + if ($count > 0) { $messageBag->add('name', 'A recurring transaction with this name already exists.'); return $messageBag; } @@ -183,6 +183,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo if ($recurringTransaction->validate()) { $recurringTransaction->save(); + } else { + $messageBag = $recurringTransaction->errors(); } return $messageBag; @@ -192,10 +194,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo * @param \RecurringTransaction $recurringTransaction * @param $data * - * @return mixed|void + * @return MessageBag */ public function update(\RecurringTransaction $recurringTransaction, $data) { + $messageBag = new MessageBag; $recurringTransaction->name = $data['name']; $recurringTransaction->match = join(' ', explode(',', $data['match'])); $recurringTransaction->amount_max = floatval($data['amount_max']); @@ -203,9 +206,9 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo // 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.'); + $messageBag->add('amount_max', 'Amount max and min cannot both be zero.'); - return $recurringTransaction; + return $messageBag; } $recurringTransaction->date = new Carbon($data['date']); $recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0; @@ -215,9 +218,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo if ($recurringTransaction->validate()) { $recurringTransaction->save(); + } else { + $messageBag = $recurringTransaction->errors(); } - return $recurringTransaction; + return $messageBag; } diff --git a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php index 1f43e6d8f1..0e753f3e3d 100644 --- a/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php +++ b/app/lib/Firefly/Storage/RecurringTransaction/RecurringTransactionRepositoryInterface.php @@ -50,7 +50,7 @@ interface RecurringTransactionRepositoryInterface * @param \RecurringTransaction $recurringTransaction * @param $data * - * @return mixed + * @return MessageBag */ public function update(\RecurringTransaction $recurringTransaction, $data); diff --git a/app/start/global.php b/app/start/global.php index 23048b3399..c5098db09d 100644 --- a/app/start/global.php +++ b/app/start/global.php @@ -93,6 +93,10 @@ App::down( \Form::macro('ffCheckbox',function ($name, $value = 1, $checked = null, $options = []) { return \Firefly\Form\Form::ffCheckbox($name, $value, $checked, $options); }); +\Form::macro('ffOptionsList',function ($type, $name) { + return \Firefly\Form\Form::ffOptionsList($type, $name); +}); + /* |-------------------------------------------------------------------------- diff --git a/app/views/recurring/create.blade.php b/app/views/recurring/create.blade.php index 20cc6389e4..bb59c64eb5 100644 --- a/app/views/recurring/create.blade.php +++ b/app/views/recurring/create.blade.php @@ -43,45 +43,7 @@ Options
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
+ {{Form::ffOptionsList('create','recurring transaction')}}
diff --git a/app/views/recurring/edit.blade.php b/app/views/recurring/edit.blade.php index 3a33641f84..f4be05b370 100644 --- a/app/views/recurring/edit.blade.php +++ b/app/views/recurring/edit.blade.php @@ -10,82 +10,13 @@ Mandatory fields
- -
- -
- - @if($errors->has('name')) -

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

- @endif -
+ {{Form::ffText('name',$recurringTransaction->name)}} + {{Form::ffTags('match',join(',',explode(' ',$recurringTransaction->match)))}} + {{Form::ffAmount('amount_min',$recurringTransaction->amount_min)}} + {{Form::ffAmount('amount_max',$recurringTransaction->amount_max)}} + {{Form::ffDate('date',$recurringTransaction->date->format('Y-m-d'))}} + {{Form::ffSelect('repeat_freq',$periods,$recurringTransaction->repeat_freq)}}
-
- -
- - @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') ?: $recurringTransaction->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') ?: $recurringTransaction->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') ?: $recurringTransaction->date->format('Y-m-d'), - ['class' => 'form-control']) }} - @if($errors->has('date')) -

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

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

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

- @endif -
-
-
-

@@ -101,94 +32,21 @@ Optional fields

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

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

- @endif
- -
- -
-
- -
- Firefly will automatically match transactions. -
-
- -
- -
-
- -
- This recurring transaction is actually active. -
-
-
- -
Options
-
- -
-
- -
-
+ {{Form::ffOptionsList('update','recurring transaction')}}
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
+
{{Form::close()}} diff --git a/app/views/transactions/create.blade.php b/app/views/transactions/create.blade.php index 7acc907bcc..5a4b0600ce 100644 --- a/app/views/transactions/create.blade.php +++ b/app/views/transactions/create.blade.php @@ -79,49 +79,11 @@ Options
-
- -
-
- -
-
+ {{Form::ffOptionsList('create','transaction')}}
-
- -
-
- -
-
-
-
- -
-
- -
-
-
- {{Form::close()}} diff --git a/app/views/transactions/edit.blade.php b/app/views/transactions/edit.blade.php index 4a96bc8778..e802d31f68 100644 --- a/app/views/transactions/edit.blade.php +++ b/app/views/transactions/edit.blade.php @@ -78,43 +78,7 @@ Options
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
+ {{Form::ffOptionsList('update','transaction')}}