Copied (not yet removed) findByName

This commit is contained in:
James Cole 2016-10-10 07:20:49 +02:00
parent 0ae9afd325
commit 717c1d080e
9 changed files with 77 additions and 18 deletions

View File

@ -52,6 +52,8 @@ class AccountCrud implements AccountCrudInterface
}
/**
* WILL BE REMOVED.
*
* @param string $name
* @param array $types
*

View File

@ -26,6 +26,8 @@ interface AccountCrudInterface
{
/**
* WILL BE REMOVED.
*
* @param string $name
* @param array $types
*

View File

@ -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]);

View File

@ -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);

View File

@ -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(

View File

@ -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);

View File

@ -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]
);

View File

@ -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.
*

View File

@ -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.
*