Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix #4697
James Cole388d19b99cf2
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.phpindex 06ed6e8dd6..6177f200e8 100644--- a/app/Repositories/Account/AccountRepository.php+++ b/app/Repositories/Account/AccountRepository.php@@ -123,24 +123,14 @@ class AccountRepository implements AccountRepositoryInterface*/public function findByIbanNull(string $iban, array $types): ?Account{- $query = $this->user->accounts()->where('iban', '!=', '')->whereNotNull('iban');+ $query = $this->user->accounts()->where('accounts.iban', $iban);- if (0!==count($types)) {+ if (0 !== count($types)) {$query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');$query->whereIn('account_types.type', $types);}- // TODO a loop like this is no longer necessary-- $accounts = $query->get(['accounts.*']);- /** @var Account $account */- foreach ($accounts as $account) {- if ($account->iban === $iban) {- return $account;- }- }-- return null;+ return $query->first(['accounts.*']);}/**@@ -678,7 +668,7 @@ class AccountRepository implements AccountRepositoryInterfacepublic function searchAccountNr(string $query, array $types, int $limit): Collection{$dbQuery = $this->user->accounts()->distinct()- ->leftJoin('account_meta', 'accounts.id', 'account_meta.account_id')+ ->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')->where('accounts.active', 1)->orderBy('accounts.order', 'ASC')->orderBy('accounts.account_type_id', 'ASC')@@ -747,4 +737,29 @@ class AccountRepository implements AccountRepositoryInterfacereturn $service->update($account, $data);}++ /**+ * @inheritDoc+ */+ public function findByAccountNumber(string $number, array $types): ?Account+ {+ $dbQuery = $this->user+ ->accounts()+ ->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')+ ->where('accounts.active', true)+ ->where(+ function (EloquentBuilder $q1) use ($number) {+ $json = json_encode($number);+ $q1->where('account_meta.name', '=', 'account_number');+ $q1->where('account_meta.data', '=', $json);+ }+ );++ if (0 !== count($types)) {+ $dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');+ $dbQuery->whereIn('account_types.type', $types);+ }++ return $dbQuery->first(['accounts.*']);+ }}diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.phpindex 99385c33e4..56f7c7a8ad 100644--- a/app/Repositories/Account/AccountRepositoryInterface.php+++ b/app/Repositories/Account/AccountRepositoryInterface.php@@ -46,6 +46,7 @@ interface AccountRepositoryInterface*/public function count(array $types): int;+/*** Moved here from account CRUD.*@@ -65,6 +66,14 @@ interface AccountRepositoryInterface*/public function expandWithDoubles(Collection $accounts): Collection;+ /**+ * @param string $number+ * @param array $types+ *+ * @return Account|null+ */+ public function findByAccountNumber(string $number, array $types): ?Account;+/*** @param string $iban* @param array $typesdiff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.phpindex 968eef3507..46af4a383c 100644--- a/app/Services/Internal/Support/JournalServiceTrait.php+++ b/app/Services/Internal/Support/JournalServiceTrait.php@@ -79,6 +79,7 @@ trait JournalServiceTrait$result = $this->findAccountById($data, $expectedTypes[$transactionType]);$result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]);$result = $this->findAccountByIban($result, $data, $expectedTypes[$transactionType]);+ $result = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]);$result = $this->createAccount($result, $data, $expectedTypes[$transactionType][0]);return $this->getCashAccount($result, $data, $expectedTypes[$transactionType]);@@ -160,6 +161,34 @@ trait JournalServiceTraitreturn $account;}+ /**+ * @param Account|null $account+ * @param array $data+ * @param array $types+ *+ * @return Account|null+ */+ private function findAccountByNumber(?Account $account, array $data, array $types): ?Account+ {+ // third attempt, find by account number+ if (null === $account && null !== $data['number']) {+ Log::debug(sprintf('Searching for account number "%s".', $data['number']));+ // find by preferred type.+ $source = $this->accountRepository->findByAccountNumber($data['number'], [$types[0]]);++ // or any expected type.+ $source = $source ?? $this->accountRepository->findByAccountNumber($data['iban'], $types);++ if (null !== $source) {+ Log::debug(sprintf('Found account: #%d, %s', $source->id, $source->name));++ $account = $source;+ }+ }++ return $account;+ }+/*** @param Account|null $account* @param array $data