diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index e46a75bcf2..3d2f95d75a 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -1,21 +1,24 @@ _repository = $repository; View::share('title', 'Budgets'); View::share('mainTitleIcon', 'fa-tasks'); } @@ -30,34 +33,8 @@ class BudgetController extends BaseController { $amount = intval(Input::get('amount')); $date = Session::get('start'); - /** @var \Limit $limit */ - $limit = $budget->limits()->where('startdate', $date->format('Y-m-d'))->first(); - if (!$limit) { - // create one! - $limit = new Limit; - $limit->budget()->associate($budget); - $limit->startdate = $date; - $limit->amount = $amount; - $limit->repeat_freq = 'monthly'; - $limit->repeats = 0; - $limit->save(); - /* - * A newly stored limit also created a limit repetition. - */ - Event::fire('limits.store', [$limit]); + $limit = $this->_repository->updateLimitAmount($budget, $date, $amount); - } else { - if ($amount > 0) { - $limit->amount = $amount; - $limit->save(); - /* - * An updated limit also updates the associated limit repetitions. - */ - Event::fire('limits.update', [$limit]); - } else { - $limit->delete(); - } - } // try to find the limit repetition for this limit: $repetition = $limit->limitrepetitions()->first(); if ($repetition) { @@ -83,7 +60,9 @@ class BudgetController extends BaseController */ public function delete(Budget $budget) { - return View::make('budgets.delete')->with('budget', $budget)->with('subTitle', 'Delete budget "' . $budget->name . '"'); + $subTitle = 'Delete budget "' . e($budget->name) . '"'; + + return View::make('budgets.delete', compact('budget', 'subTitle')); } /** @@ -93,10 +72,7 @@ class BudgetController extends BaseController */ public function destroy(Budget $budget) { - /** @var \FireflyIII\Database\Budget $repos */ - $repos = App::make('FireflyIII\Database\Budget'); - // remove budget - $repos->destroy($budget); + $this->_repository->destroy($budget); Session::flash('success', 'The budget was deleted.'); return Redirect::route('budgets.index'); @@ -110,9 +86,9 @@ class BudgetController extends BaseController */ public function edit(Budget $budget) { - Session::flash('preFilled', ['name' => $budget->name]); + $subTitle = 'Edit budget "' . $budget->name . '"'; - return View::make('budgets.edit')->with('budget', $budget)->with('subTitle', 'Edit budget "' . $budget->name . '"'); + return View::make('budgets.edit', compact('budget', 'subTitle')); } diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php index f9563d8066..369dd15512 100644 --- a/app/lib/FireflyIII/Database/Budget.php +++ b/app/lib/FireflyIII/Database/Budget.php @@ -61,7 +61,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface /** * @param \Eloquent $model - * @param array $data + * @param array $data * * @return bool */ @@ -263,4 +263,58 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface return $sum; } + + /** + * @param \Budget $budget + * @param Carbon $date + * @param $amount + * + * @return \Limit + * @throws \Exception + */ + public function updateLimitAmount(\Budget $budget, Carbon $date, $amount) + { + /** @var \Limit $limit */ + $limit = $this->limitOnStartingOnDate($budget, $date); + if (!$limit) { + // create one! + $limit = new \Limit; + $limit->budget()->associate($budget); + $limit->startdate = $date; + $limit->amount = $amount; + $limit->repeat_freq = 'monthly'; + $limit->repeats = 0; + $limit->save(); + /* + * A newly stored limit also created a limit repetition. + */ + \Event::fire('limits.store', [$limit]); + } else { + if ($amount > 0) { + $limit->amount = $amount; + $limit->save(); + /* + * An updated limit also updates the associated limit repetitions. + */ + \Event::fire('limits.update', [$limit]); + } else { + $limit->delete(); + } + } + + return $limit; + + + } + + /** + * @param \Budget $budget + * @param Carbon $date + * + * @return \Limit + */ + public function limitOnStartingOnDate(\Budget $budget, Carbon $date) + { + return $budget->limits()->where('startdate', $date->format('Y-m-d'))->first(); + } } \ No newline at end of file diff --git a/app/lib/FireflyIII/Event/Budget.php b/app/lib/FireflyIII/Event/Budget.php index 087d23c968..adbd8abb95 100644 --- a/app/lib/FireflyIII/Event/Budget.php +++ b/app/lib/FireflyIII/Event/Budget.php @@ -15,7 +15,7 @@ class Budget { - $end = DateKit::addPeriod(clone $limit->startdate, $limit->repeat_freq, 0); + $end = \DateKit::addPeriod(clone $limit->startdate, $limit->repeat_freq, 0); $end->subDay(); $set = $limit->limitrepetitions()->where('startdate', $limit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->get(); diff --git a/app/views/budgets/edit.blade.php b/app/views/budgets/edit.blade.php index 8fefee9bfd..998cd90a1b 100644 --- a/app/views/budgets/edit.blade.php +++ b/app/views/budgets/edit.blade.php @@ -7,7 +7,7 @@ -{{Form::open(['class' => 'form-horizontal','url' => route('budgets.update',$budget->id)])}} +{{Form::model($budget, ['class' => 'form-horizontal','url' => route('budgets.update',$budget->id)])}}