Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New converters for #180 (Tag and accounts)
James Colefa38c975b668
diff --git a/app/Helpers/Csv/Converter/OpposingAccountIban.php b/app/Helpers/Csv/Converter/OpposingAccountIban.phpindex 87e2c10ec1..a500b3c898 100644--- a/app/Helpers/Csv/Converter/OpposingAccountIban.php+++ b/app/Helpers/Csv/Converter/OpposingAccountIban.php@@ -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;- }}diff --git a/app/Helpers/Csv/Converter/OpposingAccountId.php b/app/Helpers/Csv/Converter/OpposingAccountId.phpindex cf85478056..3c11791fb8 100644--- a/app/Helpers/Csv/Converter/OpposingAccountId.php+++ b/app/Helpers/Csv/Converter/OpposingAccountId.php@@ -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;diff --git a/app/Helpers/Csv/Converter/OpposingAccountName.php b/app/Helpers/Csv/Converter/OpposingAccountName.phpindex 0a2a1b8880..b55c7e1bf9 100644--- a/app/Helpers/Csv/Converter/OpposingAccountName.php+++ b/app/Helpers/Csv/Converter/OpposingAccountName.php@@ -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 {diff --git a/app/Helpers/Csv/Converter/TagsComma.php b/app/Helpers/Csv/Converter/TagsComma.phpindex 02ac2bc14a..6c8715b01c 100644--- a/app/Helpers/Csv/Converter/TagsComma.php+++ b/app/Helpers/Csv/Converter/TagsComma.php@@ -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']);diff --git a/app/Helpers/Csv/Converter/TagsSpace.php b/app/Helpers/Csv/Converter/TagsSpace.phpindex c0e7b5c4f8..72f41503dc 100644--- a/app/Helpers/Csv/Converter/TagsSpace.php+++ b/app/Helpers/Csv/Converter/TagsSpace.php@@ -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']);diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.phpindex 0fbdd19f22..6d8738107e 100644--- a/app/Repositories/Account/AccountRepository.php+++ b/app/Repositories/Account/AccountRepository.php@@ -112,11 +112,15 @@ class AccountRepository implements AccountRepositoryInterfacepublic 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) {