diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index 5c587842e3..74ed304099 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -18,6 +18,9 @@ class BudgetController extends BaseController View::share('mainTitleIcon', 'fa-tasks'); } + /** + * @return \Illuminate\Http\RedirectResponse + */ public function postUpdateIncome() { /** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $preferences */ @@ -141,40 +144,35 @@ class BudgetController extends BaseController return View::make('budgets.show'); } + /** + * @return $this + */ + public function create() + { + return View::make('budgets.create')->with('subTitle', 'Create a new budget'); + } + /** + * @param Budget $budget + * + * @return $this + */ + public function delete(Budget $budget) + { + return View::make('budgets.delete')->with('budget', $budget)->with('subTitle', 'Delete budget "' . $budget->name . '"'); + } + public function destroy(Budget $budget) + { + /** @var \FireflyIII\Database\Budget $repos */ + $repos = App::make('FireflyIII\Database\Budget'); + // remove budget + $repos->destroy($budget); + Session::flash('success', 'The budget was deleted.'); + return Redirect::route('budgets.index'); + + } -// -// public function create() -// { -// throw new NotImplementedException; -//// $periods = \Config::get('firefly.periods_to_text'); -//// -//// return View::make('budgets.create')->with('periods', $periods)->with('subTitle', 'Create a new budget'); -// } -// -// public function delete(Budget $budget) -// { -// throw new NotImplementedException; -//// return View::make('budgets.delete')->with('budget', $budget) -//// ->with('subTitle', 'Delete budget "' . $budget->name . '"'); -// } -// -// public function destroy(Budget $budget) -// { -// throw new NotImplementedException; -//// // remove budget -//// Event::fire('budgets.destroy', [$budget]); // just before deletion. -//// $this->_repository->destroy($budget); -//// Session::flash('success', 'The budget was deleted.'); -//// -//// // redirect: -//// if (Input::get('from') == 'date') { -//// return Redirect::route('budgets.index'); -//// } -//// return Redirect::route('budgets.index.budget'); -// -// } /** * @param Budget $budget * @@ -266,33 +264,48 @@ class BudgetController extends BaseController // ->with('subTitle', 'Overview for ' . $title); // } // -// /** -// * @return \Illuminate\Http\RedirectResponse -// */ -// public function store() -// { -// -// $budget = $this->_repository->store(Input::all()); -// if ($budget->validate()) { -// Event::fire('budgets.store', [$budget]); -// Session::flash('success', 'Budget created!'); -// -// if (Input::get('create') == '1') { -// return Redirect::route('budgets.create', ['from' => Input::get('from')]); -// } -// -// if (Input::get('from') == 'date') { -// return Redirect::route('budgets.index'); -// } else { -// return Redirect::route('budgets.index.budget'); -// } -// } else { -// Session::flash('error', 'Could not save the new budget'); -// -// return Redirect::route('budgets.create')->withInput()->withErrors($budget->errors()); -// } -// -// } + /** + * @return \Illuminate\Http\RedirectResponse + */ + public function store() + { + /** @var \FireflyIII\Database\Budget $repos */ + $repos = App::make('FireflyIII\Database\Budget'); + $data = Input::except('_token'); + + switch ($data['post_submit_action']) { + default: + throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"'); + break; + case 'create_another': + case 'store': + $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 budget: ' . $messages['errors']->first()); + return Redirect::route('budgets.create')->withInput()->withErrors($messages['errors']); + } + // store! + $repos->store($data); + Session::flash('success', 'New budget stored!'); + + if ($data['post_submit_action'] == 'create_another') { + return Redirect::route('budgets.create'); + } else { + return Redirect::route('budgets.index'); + } + break; + 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('budgets.create')->withInput(); + break; + } + } // /** * @param Budget $budget @@ -317,7 +330,7 @@ class BudgetController extends BaseController if ($messages['errors']->count() > 0) { Session::flash('warnings', $messages['warnings']); Session::flash('successes', $messages['successes']); - Session::flash('error', 'Could not save account: ' . $messages['errors']->first()); + Session::flash('error', 'Could not save budget: ' . $messages['errors']->first()); return Redirect::route('budgets.edit', $budget->id)->withInput()->withErrors($messages['errors']); } // store! diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php index d707f33adf..7d82420abb 100644 --- a/app/lib/FireflyIII/Database/Budget.php +++ b/app/lib/FireflyIII/Database/Budget.php @@ -49,7 +49,8 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function destroy(Ardent $model) { - // TODO: Implement destroy() method. + $model->delete(); + return true; } /** @@ -75,7 +76,34 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function validate(array $model) { - throw new NotImplementedException; + $warnings = new MessageBag; + $successes = new MessageBag; + $errors = new MessageBag; + + if(isset($model['name'])) { + if(strlen($model['name']) < 1) { + $errors->add('name', 'Name is too short'); + } + if(strlen($model['name']) > 200) { + $errors->add('name', 'Name is too long'); + + } + } else { + $errors->add('name', 'Name is mandatory'); + } + $validator = \Validator::make([$model], \Budget::$rules); + if ($validator->invalid()) { + $errors->merge($errors); + } + if(!$errors->has('name')) { + $successes->add('name','OK'); + } + + return [ + 'errors' => $errors, + 'warnings' => $warnings, + 'successes' => $successes + ]; } /** @@ -85,7 +113,17 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function store(array $data) { - // TODO: Implement store() method. + $data['user_id'] = $this->getUser()->id; + + $budget = new \Budget($data); + $budget->class = 'Budget'; + + if (!$budget->validate()) { + var_dump($budget->errors()->all()); + exit; + } + $budget->save(); + return $budget; } /** @@ -179,6 +217,15 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function update(Ardent $model, array $data) { - // TODO: Implement update() method. + $model->name = $data['name']; + if (!$model->validate()) { + var_dump($model->errors()->all()); + exit; + } + + + $model->save(); + + return true; } } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 0d4c5bd6fa..0444f2c99a 100644 --- a/app/routes.php +++ b/app/routes.php @@ -277,9 +277,8 @@ Route::group( // budget controller: Route::post('/budgets/income', ['uses' => 'BudgetController@postUpdateIncome', 'as' => 'budgets.postIncome']); Route::post('/budgets/update/{budget}', ['uses' => 'BudgetController@update', 'as' => 'budgets.update']); - #Route::post('/budgets/store', ['uses' => 'BudgetController@store', 'as' => 'budgets.store']); - - #Route::post('/budgets/destroy/{budget}', ['uses' => 'BudgetController@destroy', 'as' => 'budgets.destroy']); + Route::post('/budgets/store', ['uses' => 'BudgetController@store', 'as' => 'budgets.store']); + Route::post('/budgets/destroy/{budget}', ['uses' => 'BudgetController@destroy', 'as' => 'budgets.destroy']); // category controller Route::post('/categories/store', ['uses' => 'CategoryController@store', 'as' => 'categories.store']); diff --git a/app/views/budgets/create.blade.php b/app/views/budgets/create.blade.php index b15dc00408..0b20da4663 100644 --- a/app/views/budgets/create.blade.php +++ b/app/views/budgets/create.blade.php @@ -1,118 +1,39 @@ @extends('layouts.default') @section('content') -
Use budgets to organize and limit your expenses.
-- Firefly uses the envelope system. Every budget - is an envelope in which you put money every [period]. Expenses allocated to each budget are paid from this - (virtual) envelope. -
-- When the envelope is empty, you must stop spending on the budget. If the envelope still has some money left at the - end of the [period], congratulations! You have saved money! -
-{{$errors->first('name')}}
- @else - For example: groceries, bills - @endif ++ +
{{$errors->first('amount')}}
- @else - What's the most you're willing to spend in this budget? This amount is "put" in the virtual - envelope. - @endif -{{$errors->first('repeat_freq')}}
- @else - How long will the envelope last? A week, a month, or even longer? - @endif -{{$errors->first('repeats')}}
- @else - If you want, Firefly can automatically recreate the "envelope" and fill it again - when the timespan above has expired. Be careful with this option though. It makes it easier - to fall back to old habits. - Instead, you should recreate the envelope yourself each [period]. - @endif- Remember that deleting something is permanent. -
++ Are you sure? +
+ ++ + Cancel +
+- - Account "{{{$budget->name}}}" still has {{$budget->transactionjournals()->count()}} transaction(s) associated to it. - These will NOT be deleted but will lose their connection to the budget. -
- @endif - -- Press "Delete permanently" If you are sure you want to delete "{{{$budget->name}}}". -
-