Skip to content

Firefly III: a personal finances manager

Copied (not yet removed) findByName

James Cole717c1d080e5d
diff --git a/app/Crud/Account/AccountCrud.php b/app/Crud/Account/AccountCrud.php
index 25aae371a9..9ba5b99116 100644
--- a/app/Crud/Account/AccountCrud.php
+++ b/app/Crud/Account/AccountCrud.php
@@ -52,6 +52,8 @@ class AccountCrud implements AccountCrudInterface
}
/**
+ * WILL BE REMOVED.
+ *
* @param string $name
* @param array $types
*
diff --git a/app/Crud/Account/AccountCrudInterface.php b/app/Crud/Account/AccountCrudInterface.php
index 2f1ee936ac..b6c0466868 100644
--- a/app/Crud/Account/AccountCrudInterface.php
+++ b/app/Crud/Account/AccountCrudInterface.php
@@ -26,6 +26,8 @@ interface AccountCrudInterface
{
/**
+ * WILL BE REMOVED.
+ *
* @param string $name
* @param array $types
*
diff --git a/app/Import/Converter/AssetAccountName.php b/app/Import/Converter/AssetAccountName.php
index d99a9c3285..6ad981c067 100644
--- a/app/Import/Converter/AssetAccountName.php
+++ b/app/Import/Converter/AssetAccountName.php
@@ -62,7 +62,7 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
}
// not mapped? Still try to find it first:
- $account = $crud->findByName($value, [AccountType::ASSET]);
+ $account = $repository->findByName($value, [AccountType::ASSET]);
if (!is_null($account->id)) {
Log::debug('Found asset account by name', ['value' => $value, 'id' => $account->id]);
diff --git a/app/Import/Converter/AssetAccountNumber.php b/app/Import/Converter/AssetAccountNumber.php
index e0f0a40f2e..647e18f7a6 100644
--- a/app/Import/Converter/AssetAccountNumber.php
+++ b/app/Import/Converter/AssetAccountNumber.php
@@ -69,7 +69,7 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
// try to find by the name we would give it:
$accountName = 'Asset account with number ' . e($value);
- $account = $crud->findByName($accountName, [AccountType::ASSET]);
+ $account = $repository->findByName($accountName, [AccountType::ASSET]);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
$this->setCertainty(50);
diff --git a/app/Import/Converter/OpposingAccountName.php b/app/Import/Converter/OpposingAccountName.php
index 567fbdb2f8..f869a22e06 100644
--- a/app/Import/Converter/OpposingAccountName.php
+++ b/app/Import/Converter/OpposingAccountName.php
@@ -61,7 +61,7 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
}
// not mapped? Still try to find it first:
- $account = $crud->findByName($value, []);
+ $account = $repository->findByName($value, []);
if (!is_null($account->id)) {
Log::debug('Found opposing account by name', ['id' => $account->id]);
Log::info(
diff --git a/app/Import/Converter/OpposingAccountNumber.php b/app/Import/Converter/OpposingAccountNumber.php
index 751b35fecc..725813a0b2 100644
--- a/app/Import/Converter/OpposingAccountNumber.php
+++ b/app/Import/Converter/OpposingAccountNumber.php
@@ -72,7 +72,7 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface
// try to find by the name we would give it:
$accountName = 'Import account with number ' . e($value);
- $account = $crud->findByName($accountName, [AccountType::IMPORT]);
+ $account = $repository->findByName($accountName, [AccountType::IMPORT]);
if (!is_null($account->id)) {
Log::debug('Found account by name', ['id' => $account->id]);
$this->setCertainty(50);
diff --git a/app/Import/ImportValidator.php b/app/Import/ImportValidator.php
index 53a075f630..8045fa38e2 100644
--- a/app/Import/ImportValidator.php
+++ b/app/Import/ImportValidator.php
@@ -19,6 +19,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\TransactionType;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
@@ -175,13 +176,17 @@ class ImportValidator
return $account;
}
// find it first by new type:
- /** @var AccountCrudInterface $repository */
- $repository = app(AccountCrudInterface::class, [$this->user]);
- $result = $repository->findByName($account->name, [$type]);
+ /** @var AccountCrudInterface $crud */
+ $crud = app(AccountCrudInterface::class, [$this->user]);
+
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class, [$this->user]);
+
+ $result = $repository->findByName($account->name, [$type]);
if (is_null($result->id)) {
// can convert account:
Log::debug(sprintf('No account named %s of type %s, create new account.', $account->name, $type));
- $result = $repository->store(
+ $result = $crud->store(
[
'user' => $this->user->id,
'accountType' => config('firefly.shortNamesByFullName.' . $type),
@@ -211,12 +216,16 @@ class ImportValidator
private function fallbackExpenseAccount(): Account
{
- /** @var AccountCrudInterface $repository */
- $repository = app(AccountCrudInterface::class, [$this->user]);
- $name = 'Unknown expense account';
- $result = $repository->findByName($name, [AccountType::EXPENSE]);
+ /** @var AccountCrudInterface $crud */
+ $crud = app(AccountCrudInterface::class, [$this->user]);
+
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class, [$this->user]);
+
+ $name = 'Unknown expense account';
+ $result = $repository->findByName($name, [AccountType::EXPENSE]);
if (is_null($result->id)) {
- $result = $repository->store(
+ $result = $crud->store(
['name' => $name, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'expense', 'virtualBalance' => 0,
'active' => true]
);
@@ -231,12 +240,18 @@ class ImportValidator
private function fallbackRevenueAccount(): Account
{
- /** @var AccountCrudInterface $repository */
- $repository = app(AccountCrudInterface::class, [$this->user]);
- $name = 'Unknown revenue account';
- $result = $repository->findByName($name, [AccountType::REVENUE]);
+ /** @var AccountCrudInterface $crud */
+ $crud = app(AccountCrudInterface::class, [$this->user]);
+
+ /** @var AccountRepositoryInterface $repository */
+ $repository = app(AccountRepositoryInterface::class, [$this->user]);
+
+ $name = 'Unknown revenue account';
+ $result = $repository->findByName($name, [AccountType::REVENUE]);
+
+
if (is_null($result->id)) {
- $result = $repository->store(
+ $result = $crud->store(
['name' => $name, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'revenue', 'virtualBalance' => 0,
'active' => true]
);
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index cc4dba77b8..cec23d1239 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -20,6 +20,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\User;
use Illuminate\Support\Collection;
+use Log;
/**
@@ -146,6 +147,37 @@ class AccountRepository implements AccountRepositoryInterface
return new Account;
}
+ /**
+ * @param string $name
+ * @param array $types
+ *
+ * @return Account
+ */
+ public function findByName(string $name, array $types): Account
+ {
+ $query = $this->user->accounts();
+
+ if (count($types) > 0) {
+ $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id');
+ $query->whereIn('account_types.type', $types);
+
+ }
+ Log::debug(sprintf('Searching for account named %s of the following type(s)', $name), ['types' => $types]);
+
+ $accounts = $query->get(['accounts.*']);
+ /** @var Account $account */
+ foreach ($accounts as $account) {
+ if ($account->name === $name) {
+ Log::debug(sprintf('Found #%d (%s) with type id %d', $account->id, $account->name, $account->account_type_id));
+
+ return $account;
+ }
+ }
+ Log::debug('Found nothing.');
+
+ return new Account;
+ }
+
/**
* Returns the date of the very first transaction in this account.
*
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 59396e215c..fdcfb38aed 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -33,6 +33,14 @@ interface AccountRepositoryInterface
*/
public function count(array $types): int;
+ /**
+ * @param string $name
+ * @param array $types
+ *
+ * @return Account
+ */
+ public function findByName(string $name, array $types): Account;
+
/**
* Moved here from account CRUD.
*