Skip to content

Firefly III: a personal finances manager

Allow piggy bank edit/update.

Sander Dorigofe05d218fcde
diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php
index aa008c0088..623072abe8 100644
--- a/app/controllers/PiggybankController.php
+++ b/app/controllers/PiggybankController.php
@@ -529,34 +529,49 @@ class PiggybankController extends BaseController
//
// }
-// /**
-// * @param Piggybank $piggyBank
-// *
-// * @return $this|\Illuminate\Http\RedirectResponse
-// */
+ /**
+ * @param Piggybank $piggyBank
+ *
+ * @return $this|\Illuminate\Http\RedirectResponse
+ */
public function update(Piggybank $piggyBank)
{
- throw new NotImplementedException;
-// $piggyBank = $this->_repository->update($piggyBank, Input::all());
-// if ($piggyBank->validate()) {
-// if ($piggyBank->repeats == 1) {
-// $route = 'piggybanks.index.repeated';
-// $message = 'Repeated expense';
-// } else {
-// $route = 'piggybanks.index.piggybanks';
-// $message = 'Piggy bank';
-// }
-//
-//
-// Session::flash('success', $message . ' "' . $piggyBank->name . '" updated.');
-// Event::fire('piggybanks.update', [$piggyBank]);
-//
-// return Redirect::route($route);
-// } else {
-// Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
-//
-// return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
-// }
+
+ /** @var \FireflyIII\Database\Piggybank $repos */
+ $repos = App::make('FireflyIII\Database\Piggybank');
+ $data = Input::except('_token');
+
+ switch (Input::get('post_submit_action')) {
+ default:
+ throw new FireflyException('Cannot handle post_submit_action "' . e(Input::get('post_submit_action')) . '"');
+ break;
+ case 'create_another':
+ case 'update':
+ $messages = $repos->validate($data);
+ /** @var MessageBag $messages ['errors'] */
+ if ($messages['errors']->count() > 0) {
+ Session::flash('warnings', $messages['warnings']);
+ Session::flash('successes', $messages['successes']);
+ Session::flash('error', 'Could not save piggy bank: ' . $messages['errors']->first());
+ return Redirect::route('piggybanks.edit', $piggyBank->id)->withInput()->withErrors($messages['errors']);
+ }
+ // store!
+ $repos->update($piggyBank, $data);
+ Session::flash('success', 'Piggy bank updated!');
+
+ if ($data['post_submit_action'] == 'create_another') {
+ return Redirect::route('piggybanks.edit', $piggyBank->id);
+ } else {
+ return Redirect::route('piggybanks.index');
+ }
+ case 'validate_only':
+ $messageBags = $repos->validate($data);
+ Session::flash('warnings', $messageBags['warnings']);
+ Session::flash('successes', $messageBags['successes']);
+ Session::flash('errors', $messageBags['errors']);
+ return Redirect::route('piggybanks.edit', $piggyBank->id)->withInput();
+ break;
+ }
}
diff --git a/app/lib/FireflyIII/Database/Piggybank.php b/app/lib/FireflyIII/Database/Piggybank.php
index 9f34185387..f99aa14040 100644
--- a/app/lib/FireflyIII/Database/Piggybank.php
+++ b/app/lib/FireflyIII/Database/Piggybank.php
@@ -231,6 +231,20 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function update(Ardent $model, array $data)
{
- // TODO: Implement update() method.
+ /** @var \Piggybank $model */
+ $model->name = $data['name'];
+ $model->account_id = intval($data['account_id']);
+ $model->targetamount = floatval($data['targetamount']);
+ $model->targetdate = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
+ $model->rep_every = isset($data['rep_every']) ? $data['rep_every'] : 0;
+ $model->reminder_skip = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
+ $model->order = isset($data['order']) ? $data['order'] : 0;
+ $model->remind_me = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
+ if(!$model->validate()) {
+ var_dump($model->errors());
+ exit();
+ }
+ $model->save();
+ return true;
}
}
\ No newline at end of file
diff --git a/app/views/piggybanks/edit.blade.php b/app/views/piggybanks/edit.blade.php
index f46f1fadce..e8ae4435ac 100644
--- a/app/views/piggybanks/edit.blade.php
+++ b/app/views/piggybanks/edit.blade.php
@@ -40,7 +40,7 @@
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
- {{Form::ffOptionsList('create','piggy bank')}}
+ {{Form::ffOptionsList('update','piggy bank')}}
</div>
</div>