Update budget limit #508

This commit is contained in:
James Cole
2016-12-29 20:52:02 +01:00
parent a58cd83ea7
commit 497400587d
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()); $start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */ /** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth()); $end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data; $budgetLimit = $repository->updateLimitAmount($budget, $start, $end, $amount);
$limitRepetition = $repository->updateLimitAmount($budget, $start, $end, $viewRange, $amount);
if ($amount == 0) { if ($amount == 0) {
$limitRepetition = null; $budgetLimit = null;
} }
Preferences::mark(); 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 Budget $budget
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param string $range
* @param int $amount * @param int $amount
* *
* @return BudgetLimit * @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: // there might be a budget limit for these dates:
$repeatFreq = config('firefly.range_to_repeat_freq.' . $range);
/** @var BudgetLimit $limit */ /** @var BudgetLimit $limit */
$limit = $budget->budgetlimits() $limit = $budget->budgetlimits()
->where('budget_limits.startdate', $start) ->where('budget_limits.start_date', $start->format('Y-m-d'))
->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']); ->where('budget_limits.end_date', $end->format('Y-m-d'))
->first(['budget_limits.*']);
// delete if amount is zero. // delete if amount is zero.
if (!is_null($limit) && $amount <= 0.0) { if (!is_null($limit) && $amount <= 0.0) {
@@ -634,20 +633,14 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit; return $limit;
} }
// create one and return it. // or create one and return it.
$limit = new BudgetLimit; $limit = new BudgetLimit;
$limit->budget()->associate($budget); $limit->budget()->associate($budget);
$limit->startdate = $start; $limit->start_date = $start;
$limit->end_date = $end;
$limit->amount = $amount; $limit->amount = $amount;
$limit->repeat_freq = $repeatFreq;
$limit->repeats = 0;
$limit->save(); $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; return $limit;
} }
} }

View File

@@ -189,11 +189,10 @@ interface BudgetRepositoryInterface
* @param Budget $budget * @param Budget $budget
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* @param string $range
* @param int $amount * @param int $amount
* *
* @return BudgetLimit * @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;
} }