diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 303c6ac578..0f4cf2dd50 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -21,6 +21,7 @@ use FireflyIII\Http\Requests\BudgetFormRequest; use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Models\AccountType; use FireflyIII\Models\Budget; +use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\LimitRepetition; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; @@ -407,24 +408,22 @@ class BudgetController extends Controller 'budgeted' => '0', 'currentRep' => false, ]; - $allRepetitions = $this->repository->getAllBudgetLimitRepetitions($start, $end); - $otherRepetitions = new Collection; + $budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end); + $otherLimits = new Collection; - // get all the limit repetitions relevant between start and end and examine them: - /** @var LimitRepetition $repetition */ - foreach ($allRepetitions as $repetition) { - if ($repetition->budget_id == $budget->id) { - if ($repetition->startdate->isSameDay($start) && $repetition->enddate->isSameDay($end) - ) { - $return[$budgetId]['currentRep'] = $repetition; - $return[$budgetId]['budgeted'] = $repetition->amount; - continue; - } - // otherwise it's just one of the many relevant repetitions: - $otherRepetitions->push($repetition); + // get all the budget limits relevant between start and end and examine them: + /** @var BudgetLimit $limit */ + foreach ($budgetLimits as $limit) { + if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) + ) { + $return[$budgetId]['currentLimit'] = $limit; + $return[$budgetId]['budgeted'] = $limit->amount; + continue; } + // otherwise it's just one of the many relevant repetitions: + $otherLimits->push($limit); } - $return[$budgetId]['otherRepetitions'] = $otherRepetitions; + $return[$budgetId]['otherLimits'] = $otherLimits; } return $return; diff --git a/app/Models/BudgetLimit.php b/app/Models/BudgetLimit.php index 622ad9bbc7..b606529277 100644 --- a/app/Models/BudgetLimit.php +++ b/app/Models/BudgetLimit.php @@ -32,12 +32,12 @@ class BudgetLimit extends Model = [ 'created_at' => 'date', 'updated_at' => 'date', - 'startdate' => 'date', + 'start_date' => 'date', + 'end_date' => 'date', 'repeats' => 'boolean', ]; /** @var array */ - protected $dates = ['created_at', 'updated_at']; - protected $hidden = ['amount_encrypted']; + protected $dates = ['created_at', 'updated_at','start_date','end_date']; /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 92645343aa..6864013e58 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -14,8 +14,6 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; -use FireflyIII\Events\StoredBudgetLimit; -use FireflyIII\Events\UpdatedBudgetLimit; use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\Budget; @@ -248,6 +246,36 @@ class BudgetRepository implements BudgetRepositoryInterface return $amount; } + /** + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getBudgetLimits(Budget $budget, Carbon $start, Carbon $end): Collection + { + $set = $budget->budgetLimits() + ->where( + function (Builder $q1) use ($start, $end) { + $q1->where( + function (Builder $q2) use ($start, $end) { + $q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00')); + $q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00')); + } + ) + ->orWhere( + function (Builder $q3) use ($start, $end) { + $q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00')); + $q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00')); + } + ); + } + )->get(); + + return $set; + } + /** * This method is being used to generate the budget overview in the year/multi-year report. Its used * in both the year/multi-year budget overview AND in the accompanying chart. @@ -603,9 +631,6 @@ class BudgetRepository implements BudgetRepositoryInterface $limit->amount = $amount; $limit->save(); - // fire event to create or update LimitRepetition. - event(new UpdatedBudgetLimit($limit, $end)); - return $limit; } @@ -617,7 +642,6 @@ class BudgetRepository implements BudgetRepositoryInterface $limit->repeat_freq = $repeatFreq; $limit->repeats = 0; $limit->save(); - event(new StoredBudgetLimit($limit, $end)); // likewise, there should be a limit repetition to match the end date diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 8d04c7bff6..7fb856f0e2 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -84,6 +84,8 @@ interface BudgetRepositoryInterface public function getActiveBudgets(): Collection; /** + * @deprecated + * * @param Carbon $start * @param Carbon $end * @@ -100,6 +102,15 @@ interface BudgetRepositoryInterface */ public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string; + /** + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * + * @return Collection + */ + public function getBudgetLimits(Budget $budget, Carbon $start, Carbon $end): Collection; + /** * * @param Collection $budgets diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twig index f837687845..f1aa8a889a 100644 --- a/resources/views/budgets/index.twig +++ b/resources/views/budgets/index.twig @@ -90,8 +90,8 @@