Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New converters for #180
James Coled9a4840e37ec
diff --git a/app/Helpers/Csv/Converter/AccountId.php b/app/Helpers/Csv/Converter/AccountId.phpindex 00912809bd..a25a599f1f 100644--- a/app/Helpers/Csv/Converter/AccountId.php+++ b/app/Helpers/Csv/Converter/AccountId.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 AccountId@@ -19,22 +18,16 @@ class AccountId extends BasicConverter implements ConverterInterface*/public function convert(): Account{+ /** @var AccountRepositoryInterface $repository */+ $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');+// is mapped? Then it's easy!if (isset($this->mapped[$this->index][$this->value])) {-/** @var Account $account */- $account = Auth::user()->accounts()->find($this->mapped[$this->index][$this->value]);+ $account = $repository->find($this->mapped[$this->index][$this->value]);} else {-/** @var Account $account */- $account = Auth::user()->accounts()->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;- }+ $account = $repository->find($this->value);}return $account;diff --git a/app/Helpers/Csv/Converter/AssetAccountIban.php b/app/Helpers/Csv/Converter/AssetAccountIban.phpindex d506b2d052..cec0a730e0 100644--- a/app/Helpers/Csv/Converter/AssetAccountIban.php+++ b/app/Helpers/Csv/Converter/AssetAccountIban.php@@ -5,6 +5,7 @@ namespace FireflyIII\Helpers\Csv\Converter;use Auth;use Carbon\Carbon;use FireflyIII\Models\Account;+use FireflyIII\Repositories\Account\AccountRepositoryInterface;/*** Class AssetAccountIban@@ -19,58 +20,46 @@ class AssetAccountIban extends BasicConverter implements ConverterInterface*/public function convert(): Account{+ /** @var AccountRepositoryInterface $repository */+ $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');// is mapped? Then it's easy!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;}+if (strlen($this->value) > 0) {// 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)) {- // 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);+ return $entry;+ }}+ // 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.- return $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) {+ $account = $repository->store($accountData);- return $entry;- }+ return $account;}return new Account;diff --git a/app/Helpers/Csv/Converter/AssetAccountName.php b/app/Helpers/Csv/Converter/AssetAccountName.phpindex 407e54fc6d..681e168021 100644--- a/app/Helpers/Csv/Converter/AssetAccountName.php+++ b/app/Helpers/Csv/Converter/AssetAccountName.php@@ -5,6 +5,7 @@ namespace FireflyIII\Helpers\Csv\Converter;use Auth;use Carbon\Carbon;use FireflyIII\Models\Account;+use FireflyIII\Repositories\Account\AccountRepositoryInterface;/*** Class AssetAccountName@@ -19,14 +20,19 @@ class AssetAccountName extends BasicConverter implements ConverterInterface*/public function convert(){+ /** @var AccountRepositoryInterface $repository */+ $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');+// is mapped? Then it's easy!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;}++// 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 */foreach ($set as $entry) {if ($entry->name == $this->value) {@@ -35,8 +41,6 @@ class AssetAccountName extends BasicConverter implements ConverterInterface}// create it if doesnt exist.-- $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');$accountData = ['name' => $this->value,'accountType' => 'asset',diff --git a/app/Helpers/Csv/Converter/AssetAccountNumber.php b/app/Helpers/Csv/Converter/AssetAccountNumber.phpindex 803fc9763f..4db084b240 100644--- a/app/Helpers/Csv/Converter/AssetAccountNumber.php+++ b/app/Helpers/Csv/Converter/AssetAccountNumber.php@@ -13,6 +13,7 @@ namespace FireflyIII\Helpers\Csv\Converter;use Auth;use Carbon\Carbon;use FireflyIII\Models\Account;+use FireflyIII\Repositories\Account\AccountRepositoryInterface;/*** Class AssetAccountNumber@@ -27,9 +28,12 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface*/public function convert(){+ /** @var AccountRepositoryInterface $repository */+ $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');+// is mapped? Then it's easy!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;}@@ -37,53 +41,38 @@ class AssetAccountNumber extends BasicConverter implements ConverterInterface$value = $this->value ?? '';if (strlen($value) > 0) {// find or create new account:- $account = $this->findAccount();-- if (is_null($account->id)) {- // create it if doesn't exist.- $repository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');+ $set = $repository->getAccounts(['Default account', 'Asset account']);+ /** @var Account $entry */+ foreach ($set as $entry) {+ $accountNumber = $entry->getMeta('accountNumber');+ if ($accountNumber == $this->value) {+ return $entry;+ }+ }- $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.+ $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);- }+ $account = $repository->store($accountData);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;- }}diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.phpindex 20fcfdc67b..0fbdd19f22 100644--- a/app/Repositories/Account/AccountRepository.php+++ b/app/Repositories/Account/AccountRepository.php@@ -34,7 +34,7 @@ class AccountRepository implements AccountRepositoryInterface/** @var User */private $user;- /** @var array */+ /** @var array */private $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber'];/**@@ -78,15 +78,18 @@ class AccountRepository implements AccountRepositoryInterface}/**- * @deprecated- ** @param $accountId** @return 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;}/**