Some cleanup.

This commit is contained in:
James Cole 2014-12-07 20:45:52 +01:00
parent 6aecd77b77
commit 360f286ed3
4 changed files with 70 additions and 40 deletions

View File

@ -1,21 +1,24 @@
<?php
use FireflyIII\Database\Budget as BudgetRepository;
use FireflyIII\Exception\FireflyException;
use Illuminate\Support\MessageBag;
/**
* Class BudgetController
*/
class BudgetController extends BaseController
{
/** @var BudgetRepository */
protected $_repository;
/**
*
* @param BudgetRepository $repository
*/
public function __construct()
public function __construct(BudgetRepository $repository)
{
$this->_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'));
}

View File

@ -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();
}
}

View File

@ -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();

View File

@ -7,7 +7,7 @@
</div>
</div>
{{Form::open(['class' => 'form-horizontal','url' => route('budgets.update',$budget->id)])}}
{{Form::model($budget, ['class' => 'form-horizontal','url' => route('budgets.update',$budget->id)])}}
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-6">
<div class="panel panel-primary">