2015-07-05 07:37:36 -05:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-07-05 07:37:36 -05:00
|
|
|
namespace FireflyIII\Helpers\Csv\Converter;
|
|
|
|
|
|
|
|
use Auth;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AssetAccountIban
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers\Csv\Converter
|
|
|
|
*/
|
|
|
|
class AssetAccountIban extends BasicConverter implements ConverterInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public function convert()
|
|
|
|
{
|
|
|
|
// 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]);
|
|
|
|
|
|
|
|
return $account;
|
|
|
|
}
|
2015-07-05 14:47:59 -05:00
|
|
|
if (strlen($this->value) > 0) {
|
|
|
|
// find or create new account:
|
2015-07-06 18:07:19 -05:00
|
|
|
$account = $this->findAccount();
|
2015-07-05 14:47:59 -05:00
|
|
|
$accountType = AccountType::where('type', 'Asset account')->first();
|
|
|
|
|
2015-07-06 18:07:19 -05:00
|
|
|
if (is_null($account)) {
|
|
|
|
// create it if doesn't exist.
|
2016-02-11 21:59:14 -06:00
|
|
|
$account = Account::firstOrCreateEncrypted( // See issue #180
|
2015-07-06 18:07:19 -05:00
|
|
|
[
|
|
|
|
'name' => $this->value,
|
|
|
|
'iban' => $this->value,
|
|
|
|
'user_id' => Auth::user()->id,
|
|
|
|
'account_type_id' => $accountType->id,
|
|
|
|
'active' => 1,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2015-07-05 14:47:59 -05:00
|
|
|
|
|
|
|
return $account;
|
2015-07-05 07:37:36 -05:00
|
|
|
}
|
|
|
|
|
2015-07-05 14:47:59 -05:00
|
|
|
return null;
|
2015-07-05 07:37:36 -05:00
|
|
|
}
|
2015-07-06 18:07:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
protected function findAccount()
|
|
|
|
{
|
|
|
|
$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 null;
|
|
|
|
}
|
2015-07-09 14:26:40 -05:00
|
|
|
}
|