New converters for #180

This commit is contained in:
James Cole
2016-04-01 13:03:38 +02:00
parent a93070b98d
commit d9a4840e37
5 changed files with 81 additions and 103 deletions

View File

@@ -2,9 +2,8 @@
declare(strict_types = 1); declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter; namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use Log; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class AccountId * Class AccountId
@@ -19,22 +18,16 @@ class AccountId extends BasicConverter implements ConverterInterface
*/ */
public function convert(): Account public function convert(): Account
{ {
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
/** @var Account $account */ /** @var Account $account */
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); $account = $repository->find($this->mapped[$this->index][$this->value]);
} else { } else {
/** @var Account $account */ /** @var Account $account */
$account = Auth::user()->accounts()->find($this->value); $account = $repository->find($this->value);
if (!is_null($account)) {
Log::debug('Found ' . $account->accountType->type . ' named "******" with ID: ' . $this->value . ' (not mapped) ');
} else {
// new account to prevent TypeErrors.
$account = new Account;
}
} }
return $account; return $account;

View File

@@ -5,6 +5,7 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class AssetAccountIban * Class AssetAccountIban
@@ -19,21 +20,27 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
*/ */
public function convert(): Account public function convert(): Account
{ {
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); $account = $repository->find($this->mapped[$this->index][$this->value]);
return $account; return $account;
} }
if (strlen($this->value) > 0) { if (strlen($this->value) > 0) {
// find or create new account: // find or create new account:
$account = $this->findAccount(); $set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
if (is_null($account->id)) { return $entry;
}
}
// create it if doesn't exist. // create it if doesn't exist.
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$accountData = [ $accountData = [
'name' => $this->value, 'name' => $this->value,
'accountType' => 'asset', 'accountType' => 'asset',
@@ -51,28 +58,10 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface
]; ];
$account = $repository->store($accountData); $account = $repository->store($accountData);
}
return $account; return $account;
} }
return new Account; return new Account;
} }
/**
* @return Account
*/
protected function findAccount(): Account
{
$set = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']);
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->iban == $this->value) {
return $entry;
}
}
return new Account;
}
} }

View File

@@ -5,6 +5,7 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class AssetAccountName * Class AssetAccountName
@@ -19,14 +20,19 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); $account = $repository->find($this->mapped[$this->index][$this->value]);
return $account; return $account;
} }
// find or create new account: // find or create new account:
$set = Auth::user()->accounts()->accountTypeIn(['Asset account', 'Default account'])->get(); $set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */ /** @var Account $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
if ($entry->name == $this->value) { if ($entry->name == $this->value) {
@@ -35,8 +41,6 @@ class AssetAccountName extends BasicConverter implements ConverterInterface
} }
// create it if doesnt exist. // create it if doesnt exist.
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$accountData = [ $accountData = [
'name' => $this->value, 'name' => $this->value,
'accountType' => 'asset', 'accountType' => 'asset',

View File

@@ -13,6 +13,7 @@ namespace FireflyIII\Helpers\Csv\Converter;
use Auth; use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/** /**
* Class AssetAccountNumber * Class AssetAccountNumber
@@ -27,9 +28,12 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
*/ */
public function convert() public function convert()
{ {
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
// is mapped? Then it's easy! // is mapped? Then it's easy!
if (isset($this->mapped[$this->index][$this->value])) { if (isset($this->mapped[$this->index][$this->value])) {
$account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]); $account = $repository->find($this->mapped[$this->index][$this->value]);
return $account; return $account;
} }
@@ -37,12 +41,15 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
$value = $this->value ?? ''; $value = $this->value ?? '';
if (strlen($value) > 0) { if (strlen($value) > 0) {
// find or create new account: // find or create new account:
$account = $this->findAccount(); $set = $repository->getAccounts(['Default account', 'Asset account']);
/** @var Account $entry */
if (is_null($account->id)) { foreach ($set as $entry) {
// create it if doesn't exist. $accountNumber = $entry->getMeta('accountNumber');
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); if ($accountNumber == $this->value) {
return $entry;
}
}
$accountData = [ $accountData = [
'name' => $this->value, 'name' => $this->value,
@@ -61,29 +68,11 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface
]; ];
$account = $repository->store($accountData); $account = $repository->store($accountData);
}
return $account; return $account;
} }
return null; return null; // is this accepted?
} }
/**
* @return Account
*/
protected function findAccount(): Account
{
$set = Auth::user()->accounts()->with(['accountmeta'])->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']);
/** @var Account $entry */
foreach ($set as $entry) {
$accountNumber = $entry->getMeta('accountNumber');
if ($accountNumber == $this->value) {
return $entry;
}
}
return new Account;
}
} }

View File

@@ -78,15 +78,18 @@ class AccountRepository implements AccountRepositoryInterface
} }
/** /**
* @deprecated
*
* @param $accountId * @param $accountId
* *
* @return Account * @return Account
*/ */
public function find(int $accountId): Account public function find(int $accountId): Account
{ {
return $this->user->accounts()->findOrNew($accountId); $account = $this->user->accounts()->find($accountId);
if (is_null($account)) {
$account = new Account;
}
return $account;
} }
/** /**