New converters for #180 (Tag and accounts)

This commit is contained in:
James Cole 2016-04-01 14:17:12 +02:00
parent c14fa1021c
commit fa38c975b6
6 changed files with 58 additions and 49 deletions

View File

@ -2,9 +2,8 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Account;
use Log;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
* Class OpposingAccountIban
@ -21,15 +20,23 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
*/
public function convert()
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
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;
} else {
if (strlen($this->value) > 0) {
$account = $this->findAccount();
if (!is_null($account)) {
return $account;
$set = $repository->getAccounts([]);
/** @var Account $account */
foreach ($set as $account) {
if ($account->iban == $this->value) {
return $account;
}
}
}
@ -37,21 +44,4 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
}
}
/**
* @return Account|null
*/
protected function findAccount()
{
$set = Auth::user()->accounts()->get();
/** @var Account $account */
foreach ($set as $account) {
if ($account->iban == $this->value) {
Log::debug('OpposingAccountIban::convert found an Account (#' . $account->id . ': ******) with IBAN ******');
return $account;
}
}
return null;
}
}

View File

@ -2,8 +2,8 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
* Class OpposingAccountId
@ -19,11 +19,14 @@ class OpposingAccountId extends BasicConverter implements ConverterInterface
*/
public function convert()
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
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]);
} else {
$account = Auth::user()->accounts()->find($this->value);
$account = $repository->find($this->value);
}
return $account;

View File

@ -2,8 +2,8 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
/**
* Class OpposingAccountName
@ -20,8 +20,11 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
*/
public function convert()
{
/** @var AccountRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');
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;
} else {

View File

@ -2,8 +2,7 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Tag;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
/**
@ -19,17 +18,22 @@ class TagsComma extends BasicConverter implements ConverterInterface
*/
public function convert()
{
$tags = new Collection;
/** @var TagRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
$tags = new Collection;
$strings = explode(',', $this->value);
foreach ($strings as $string) {
$tag = Tag::firstOrCreateEncrypted( // See issue #180
[
'tag' => $string,
'tagMode' => 'nothing',
'user_id' => Auth::user()->id,
]
);
$data = [
'tag' => $string,
'date' => null,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data); // should validate first?
$tags->push($tag);
}
$tags = $tags->merge($this->data['tags']);

View File

@ -2,8 +2,7 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Csv\Converter;
use Auth;
use FireflyIII\Models\Tag;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Support\Collection;
/**
@ -19,17 +18,23 @@ class TagsSpace extends BasicConverter implements ConverterInterface
*/
public function convert()
{
/** @var TagRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Tag\TagRepositoryInterface');
$tags = new Collection;
$strings = explode(' ', $this->value);
foreach ($strings as $string) {
$tag = Tag::firstOrCreateEncrypted( // See issue #180
[
'tag' => $string,
'tagMode' => 'nothing',
'user_id' => Auth::user()->id,
]
);
$data = [
'tag' => $string,
'date' => null,
'description' => null,
'latitude' => null,
'longitude' => null,
'zoomLevel' => null,
'tagMode' => 'nothing',
];
$tag = $repository->store($data); // should validate first?
$tags->push($tag);
}
$tags = $tags->merge($this->data['tags']);

View File

@ -112,11 +112,15 @@ class AccountRepository implements AccountRepositoryInterface
public function getAccounts(array $types): Collection
{
/** @var Collection $result */
$result = $this->user->accounts()->with(
$query = $this->user->accounts()->with(
['accountmeta' => function (HasMany $query) {
$query->where('name', 'accountRole');
}]
)->accountTypeIn($types)->get(['accounts.*']);
);
if (count($types) > 0) {
$query->accountTypeIn($types);
}
$result = $query->get(['accounts.*']);
$result = $result->sortBy(
function (Account $account) {