diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 0f4cf2dd50..aedb331883 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -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]); } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 6864013e58..0188612250 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -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->amount = $amount; - $limit->repeat_freq = $repeatFreq; - $limit->repeats = 0; + $limit->start_date = $start; + $limit->end_date = $end; + $limit->amount = $amount; $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; } } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 7fb856f0e2..eed24b1da4 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -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; }