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,60 +20,48 @@ 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. }
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => null,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$account = $repository->store($accountData);
} }
// create it if doesn't exist.
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => null,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$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,53 +41,38 @@ 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 */
foreach ($set as $entry) {
$accountNumber = $entry->getMeta('accountNumber');
if ($accountNumber == $this->value) {
if (is_null($account->id)) { return $entry;
// create it if doesn't exist. }
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => null,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$account = $repository->store($accountData);
} }
$accountData = [
'name' => $this->value,
'accountType' => 'asset',
'virtualBalance' => 0,
'virtualBalanceCurrency' => 1, // hard coded.
'active' => true,
'user' => Auth::user()->id,
'iban' => null,
'accountNumber' => $this->value,
'accountRole' => null,
'openingBalance' => 0,
'openingBalanceDate' => new Carbon,
'openingBalanceCurrency' => 1, // hard coded.
];
$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

@ -34,7 +34,7 @@ class AccountRepository implements AccountRepositoryInterface
/** @var User */ /** @var User */
private $user; private $user;
/** @var array */ /** @var array */
private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber']; private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber'];
/** /**
@ -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;
} }
/** /**