mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 02:45:58 +00:00
Copied (not yet removed) findByName
This commit is contained in:
@@ -52,6 +52,8 @@ class AccountCrud implements AccountCrudInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* WILL BE REMOVED.
|
||||||
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param array $types
|
* @param array $types
|
||||||
*
|
*
|
||||||
|
@@ -26,6 +26,8 @@ interface AccountCrudInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* WILL BE REMOVED.
|
||||||
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param array $types
|
* @param array $types
|
||||||
*
|
*
|
||||||
|
@@ -62,7 +62,7 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// not mapped? Still try to find it first:
|
// 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)) {
|
if (!is_null($account->id)) {
|
||||||
Log::debug('Found asset account by name', ['value' => $value, 'id' => $account->id]);
|
Log::debug('Found asset account by name', ['value' => $value, 'id' => $account->id]);
|
||||||
|
|
||||||
|
@@ -69,7 +69,7 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
|
|||||||
|
|
||||||
// try to find by the name we would give it:
|
// try to find by the name we would give it:
|
||||||
$accountName = 'Asset account with number ' . e($value);
|
$accountName = 'Asset account with number ' . e($value);
|
||||||
$account = $crud->findByName($accountName, [AccountType::ASSET]);
|
$account = $repository->findByName($accountName, [AccountType::ASSET]);
|
||||||
if (!is_null($account->id)) {
|
if (!is_null($account->id)) {
|
||||||
Log::debug('Found account by name', ['id' => $account->id]);
|
Log::debug('Found account by name', ['id' => $account->id]);
|
||||||
$this->setCertainty(50);
|
$this->setCertainty(50);
|
||||||
|
@@ -61,7 +61,7 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// not mapped? Still try to find it first:
|
// not mapped? Still try to find it first:
|
||||||
$account = $crud->findByName($value, []);
|
$account = $repository->findByName($value, []);
|
||||||
if (!is_null($account->id)) {
|
if (!is_null($account->id)) {
|
||||||
Log::debug('Found opposing account by name', ['id' => $account->id]);
|
Log::debug('Found opposing account by name', ['id' => $account->id]);
|
||||||
Log::info(
|
Log::info(
|
||||||
|
@@ -72,7 +72,7 @@ class OpposingAccountNumber extends BasicConverter implements ConverterInterface
|
|||||||
|
|
||||||
// try to find by the name we would give it:
|
// try to find by the name we would give it:
|
||||||
$accountName = 'Import account with number ' . e($value);
|
$accountName = 'Import account with number ' . e($value);
|
||||||
$account = $crud->findByName($accountName, [AccountType::IMPORT]);
|
$account = $repository->findByName($accountName, [AccountType::IMPORT]);
|
||||||
if (!is_null($account->id)) {
|
if (!is_null($account->id)) {
|
||||||
Log::debug('Found account by name', ['id' => $account->id]);
|
Log::debug('Found account by name', ['id' => $account->id]);
|
||||||
$this->setCertainty(50);
|
$this->setCertainty(50);
|
||||||
|
@@ -19,6 +19,7 @@ use FireflyIII\Models\Account;
|
|||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\ImportJob;
|
use FireflyIII\Models\ImportJob;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -175,13 +176,17 @@ class ImportValidator
|
|||||||
return $account;
|
return $account;
|
||||||
}
|
}
|
||||||
// find it first by new type:
|
// find it first by new type:
|
||||||
/** @var AccountCrudInterface $repository */
|
/** @var AccountCrudInterface $crud */
|
||||||
$repository = app(AccountCrudInterface::class, [$this->user]);
|
$crud = app(AccountCrudInterface::class, [$this->user]);
|
||||||
$result = $repository->findByName($account->name, [$type]);
|
|
||||||
|
/** @var AccountRepositoryInterface $repository */
|
||||||
|
$repository = app(AccountRepositoryInterface::class, [$this->user]);
|
||||||
|
|
||||||
|
$result = $repository->findByName($account->name, [$type]);
|
||||||
if (is_null($result->id)) {
|
if (is_null($result->id)) {
|
||||||
// can convert account:
|
// can convert account:
|
||||||
Log::debug(sprintf('No account named %s of type %s, create new account.', $account->name, $type));
|
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,
|
'user' => $this->user->id,
|
||||||
'accountType' => config('firefly.shortNamesByFullName.' . $type),
|
'accountType' => config('firefly.shortNamesByFullName.' . $type),
|
||||||
@@ -211,12 +216,16 @@ class ImportValidator
|
|||||||
private function fallbackExpenseAccount(): Account
|
private function fallbackExpenseAccount(): Account
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var AccountCrudInterface $repository */
|
/** @var AccountCrudInterface $crud */
|
||||||
$repository = app(AccountCrudInterface::class, [$this->user]);
|
$crud = app(AccountCrudInterface::class, [$this->user]);
|
||||||
$name = 'Unknown expense account';
|
|
||||||
$result = $repository->findByName($name, [AccountType::EXPENSE]);
|
/** @var AccountRepositoryInterface $repository */
|
||||||
|
$repository = app(AccountRepositoryInterface::class, [$this->user]);
|
||||||
|
|
||||||
|
$name = 'Unknown expense account';
|
||||||
|
$result = $repository->findByName($name, [AccountType::EXPENSE]);
|
||||||
if (is_null($result->id)) {
|
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,
|
['name' => $name, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'expense', 'virtualBalance' => 0,
|
||||||
'active' => true]
|
'active' => true]
|
||||||
);
|
);
|
||||||
@@ -231,12 +240,18 @@ class ImportValidator
|
|||||||
private function fallbackRevenueAccount(): Account
|
private function fallbackRevenueAccount(): Account
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var AccountCrudInterface $repository */
|
/** @var AccountCrudInterface $crud */
|
||||||
$repository = app(AccountCrudInterface::class, [$this->user]);
|
$crud = app(AccountCrudInterface::class, [$this->user]);
|
||||||
$name = 'Unknown revenue account';
|
|
||||||
$result = $repository->findByName($name, [AccountType::REVENUE]);
|
/** @var AccountRepositoryInterface $repository */
|
||||||
|
$repository = app(AccountRepositoryInterface::class, [$this->user]);
|
||||||
|
|
||||||
|
$name = 'Unknown revenue account';
|
||||||
|
$result = $repository->findByName($name, [AccountType::REVENUE]);
|
||||||
|
|
||||||
|
|
||||||
if (is_null($result->id)) {
|
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,
|
['name' => $name, 'iban' => null, 'openingBalance' => 0, 'user' => $this->user->id, 'accountType' => 'revenue', 'virtualBalance' => 0,
|
||||||
'active' => true]
|
'active' => true]
|
||||||
);
|
);
|
||||||
|
@@ -20,6 +20,7 @@ use FireflyIII\Models\Account;
|
|||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,6 +147,37 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
return new Account;
|
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.
|
* Returns the date of the very first transaction in this account.
|
||||||
*
|
*
|
||||||
|
@@ -33,6 +33,14 @@ interface AccountRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function count(array $types): int;
|
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.
|
* Moved here from account CRUD.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user