From 5130ba7ea457ace5bbd46b31d890fb618d153b09 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 15 Jul 2016 22:37:47 +0200 Subject: [PATCH] Working IBAN account import thing. --- app/Crud/Account/AccountCrud.php | 18 +++++++++++++++ app/Crud/Account/AccountCrudInterface.php | 7 ++++++ app/Import/Converter/AssetAccountIban.php | 28 ++++++++++++++++++----- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/app/Crud/Account/AccountCrud.php b/app/Crud/Account/AccountCrud.php index c7ffb509df..e9c4046b2f 100644 --- a/app/Crud/Account/AccountCrud.php +++ b/app/Crud/Account/AccountCrud.php @@ -82,6 +82,24 @@ class AccountCrud implements AccountCrudInterface return $account; } + /** + * @param string $iban + * + * @return Account + */ + public function findByIban(string $iban): Account + { + $accounts = $this->user->accounts()->where('iban', '!=', "")->get(); + /** @var Account $account */ + foreach ($accounts as $account) { + if ($account->iban === $iban) { + return $account; + } + } + + return new Account; + } + /** * @param array $accountIds * diff --git a/app/Crud/Account/AccountCrudInterface.php b/app/Crud/Account/AccountCrudInterface.php index a325e3e6d2..8d3ac26634 100644 --- a/app/Crud/Account/AccountCrudInterface.php +++ b/app/Crud/Account/AccountCrudInterface.php @@ -37,6 +37,13 @@ interface AccountCrudInterface */ public function find(int $accountId): Account; + /** + * @param string $iban + * + * @return Account + */ + public function findByIban(string $iban): Account; + /** * @param array $accountIds * diff --git a/app/Import/Converter/AssetAccountIban.php b/app/Import/Converter/AssetAccountIban.php index a0fcd9e40d..f469a8ef92 100644 --- a/app/Import/Converter/AssetAccountIban.php +++ b/app/Import/Converter/AssetAccountIban.php @@ -12,6 +12,7 @@ declare(strict_types = 1); namespace FireflyIII\Import\Converter; use FireflyIII\Crud\Account\AccountCrudInterface; +use FireflyIII\Models\Account; use Log; /** @@ -25,8 +26,9 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface /** * @param $value * + * @return Account */ - public function convert($value) + public function convert($value): Account { Log::debug('Going to convert value ' . $value); @@ -35,14 +37,28 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface if (isset($this->mapping[$value])) { - Log::debug('Found account in mapping. Should exist.',['value' => $value]); - $account = $repository->find(intval($value)); - Log::debug('Found account ', ['id' => $account->id]); + Log::debug('Found account in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); + $account = $repository->find(intval($this->mapping[$value])); + if (!is_null($account->id)) { + Log::debug('Found account by ID', ['id' => $account->id]); + return $account; + } } - Log::debug('Given map is ', $this->mapping); + // not mapped? Still try to find it first: + $account = $repository->findByIban($value); + if (!is_null($account->id)) { + Log::debug('Found account by IBAN', ['id' => $account->id]); - exit; + return $account; + } + + + $account = $repository->store( + ['name' => $value, 'iban' => $value, 'user' => $this->user->id, 'accountType' => 'asset', 'virtualBalance' => 0, 'active' => true] + ); + + return $account; } } \ No newline at end of file