diff --git a/app/Crud/Account/AccountCrud.php b/app/Crud/Account/AccountCrud.php index 483722fd6e..e5feeba19e 100644 --- a/app/Crud/Account/AccountCrud.php +++ b/app/Crud/Account/AccountCrud.php @@ -221,6 +221,33 @@ class AccountCrud implements AccountCrudInterface return $result; } + /** + * @param array $types + * + * @return Collection + */ + public function getActiveAccountsByType(array $types): Collection + { + /** @var Collection $result */ + $query = $this->user->accounts()->with( + ['accountmeta' => function (HasMany $query) { + $query->where('name', 'accountRole'); + }] + ); + if (count($types) > 0) { + $query->accountTypeIn($types); + } + $query->where('active', 1); + $result = $query->get(['accounts.*']); + $result = $result->sortBy( + function (Account $account) { + return strtolower($account->name); + } + ); + + return $result; + } + /** * @param array $data * diff --git a/app/Crud/Account/AccountCrudInterface.php b/app/Crud/Account/AccountCrudInterface.php index 460f6628e6..3f4ebf45e2 100644 --- a/app/Crud/Account/AccountCrudInterface.php +++ b/app/Crud/Account/AccountCrudInterface.php @@ -75,6 +75,13 @@ interface AccountCrudInterface */ public function getAccountsByType(array $types): Collection; + /** + * @param array $types + * + * @return Collection + */ + public function getActiveAccountsByType(array $types): Collection; + /** * @param array $data * diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 376079afbc..466e375119 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -13,6 +13,7 @@ namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use ExpandedForm; +use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Events\TransactionJournalStored; use FireflyIII\Events\TransactionJournalUpdated; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; @@ -59,12 +60,12 @@ class TransactionController extends Controller */ public function create(string $what = TransactionType::DEPOSIT) { - $crud = app('FireflyIII\Crud\Account\AccountCrudInterface'); + $crud = app(AccountCrudInterface::class); $budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); $piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); $what = strtolower($what); $uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size'))); - $assetAccounts = ExpandedForm::makeSelectList($crud->getAccountsByType(['Default account', 'Asset account'])); + $assetAccounts = ExpandedForm::makeSelectList($crud->getActiveAccountsByType(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets()); $piggyBanks = $piggyRepository->getPiggyBanksWithAmount(); $piggies = ExpandedForm::makeSelectListWithEmpty($piggyBanks);