diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index d8f8a070d3..af9fd47e50 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -35,7 +35,6 @@ use FireflyIII\Models\RuleTrigger; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\User; -use http\Exception\RuntimeException; use Illuminate\Database\QueryException; use Illuminate\Support\Collection; use Log; @@ -80,6 +79,8 @@ class BudgetRepository implements BudgetRepositoryInterface $budget->order = $index + 1; $budget->save(); } + // other budgets, set to 0. + $this->user->budgets()->where('active', 0)->update(['order' => 0]); return true; } @@ -278,11 +279,13 @@ class BudgetRepository implements BudgetRepositoryInterface */ public function store(array $data): Budget { + $order = $this->getMaxOrder(); try { $newBudget = Budget::create( [ 'user_id' => $this->user->id, 'name' => $data['name'], + 'order' => $order + 1, ] ); } catch (QueryException $e) { @@ -488,4 +491,9 @@ class BudgetRepository implements BudgetRepositoryInterface { return $budget->attachments()->get(); } + + public function getMaxOrder(): int + { + return (int)$this->user->budgets()->max('order'); + } } diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index a170900cef..9da1aa922b 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -58,6 +58,11 @@ interface BudgetRepositoryInterface */ public function destroyAutoBudget(Budget $budget): void; + /** + * @return int + */ + public function getMaxOrder(): int; + /** * @return bool