firefly-iii/app/Helpers/Csv/Converter/AssetAccountIban.php

79 lines
2.2 KiB
PHP
Raw Normal View History

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;
2016-02-13 06:13:22 -06:00
use Carbon\Carbon;
2015-07-05 07:37:36 -05:00
use FireflyIII\Models\Account;
/**
* Class AssetAccountIban
*
* @package FireflyIII\Helpers\Csv\Converter
*/
class AssetAccountIban extends BasicConverter implements ConverterInterface
{
/**
2016-02-18 03:04:26 -06:00
* @return Account
2015-07-05 07:37:36 -05:00
*/
2016-02-18 03:04:26 -06:00
public function convert(): Account
2015-07-05 07:37:36 -05:00
{
2016-02-18 03:04:26 -06:00
2015-07-05 07:37:36 -05:00
// 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:
2016-02-13 06:13:22 -06:00
$account = $this->findAccount();
2015-07-05 14:47:59 -05:00
2016-02-18 03:04:26 -06:00
if (is_null($account->id)) {
2015-07-06 18:07:19 -05:00
// create it if doesn't exist.
2016-02-13 06:13:22 -06:00
$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);
2015-07-06 18:07:19 -05:00
}
2015-07-05 14:47:59 -05:00
return $account;
2015-07-05 07:37:36 -05:00
}
2016-02-18 03:04:26 -06:00
return new Account;
2015-07-05 07:37:36 -05:00
}
2015-07-06 18:07:19 -05:00
/**
2016-02-18 03:04:26 -06:00
* @return Account
2015-07-06 18:07:19 -05:00
*/
2016-02-18 03:04:26 -06:00
protected function findAccount(): Account
2015-07-06 18:07:19 -05:00
{
$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;
}
}
2016-02-18 03:04:26 -06:00
return new Account;
2015-07-06 18:07:19 -05:00
}
2015-07-09 14:26:40 -05:00
}