This commit is contained in:
James Cole 2021-04-22 06:18:46 +02:00
parent ec03017eca
commit 388d19b99c
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
3 changed files with 67 additions and 14 deletions

View File

@ -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 AccountRepositoryInterface
public 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 AccountRepositoryInterface
return $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.*']);
}
}

View File

@ -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 $types

View File

@ -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 JournalServiceTrait
return $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