Update budget limit #508

This commit is contained in:
James Cole 2016-12-29 20:52:02 +01:00
parent a58cd83ea7
commit 497400587d
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 13 additions and 22 deletions

View File

@ -78,14 +78,13 @@ class BudgetController extends Controller
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data;
$limitRepetition = $repository->updateLimitAmount($budget, $start, $end, $viewRange, $amount);
$budgetLimit = $repository->updateLimitAmount($budget, $start, $end, $amount);
if ($amount == 0) {
$limitRepetition = null;
$budgetLimit = null;
}
Preferences::mark();
return Response::json(['name' => $budget->name, 'repetition' => $limitRepetition ? $limitRepetition->id : 0, 'amount' => $amount]);
return Response::json(['name' => $budget->name, 'limit' => $budgetLimit ? $budgetLimit->id : 0, 'amount' => $amount]);
}

View File

@ -606,19 +606,18 @@ class BudgetRepository implements BudgetRepositoryInterface
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $range
* @param int $amount
*
* @return BudgetLimit
*/
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, int $amount): BudgetLimit
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, int $amount): BudgetLimit
{
// there might be a budget limit for this startdate:
$repeatFreq = config('firefly.range_to_repeat_freq.' . $range);
// there might be a budget limit for these dates:
/** @var BudgetLimit $limit */
$limit = $budget->budgetlimits()
->where('budget_limits.startdate', $start)
->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']);
->where('budget_limits.start_date', $start->format('Y-m-d'))
->where('budget_limits.end_date', $end->format('Y-m-d'))
->first(['budget_limits.*']);
// delete if amount is zero.
if (!is_null($limit) && $amount <= 0.0) {
@ -634,20 +633,14 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit;
}
// create one and return it.
// or create one and return it.
$limit = new BudgetLimit;
$limit->budget()->associate($budget);
$limit->startdate = $start;
$limit->start_date = $start;
$limit->end_date = $end;
$limit->amount = $amount;
$limit->repeat_freq = $repeatFreq;
$limit->repeats = 0;
$limit->save();
// likewise, there should be a limit repetition to match the end date
// (which is always the end of the month) but that is caught by an event.
// so handled automatically.
return $limit;
}
}

View File

@ -189,11 +189,10 @@ interface BudgetRepositoryInterface
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param string $range
* @param int $amount
*
* @return BudgetLimit
*/
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, int $amount): BudgetLimit;
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, int $amount): BudgetLimit;
}