mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-05 21:53:08 -06:00
79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types = 1);
|
|
namespace FireflyIII\Helpers\Csv\Converter;
|
|
|
|
use Auth;
|
|
use Carbon\Carbon;
|
|
use FireflyIII\Models\Account;
|
|
|
|
/**
|
|
* Class AssetAccountIban
|
|
*
|
|
* @package FireflyIII\Helpers\Csv\Converter
|
|
*/
|
|
class AssetAccountIban extends BasicConverter implements ConverterInterface
|
|
{
|
|
|
|
/**
|
|
* @return Account
|
|
*/
|
|
public function convert(): Account
|
|
{
|
|
|
|
// 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;
|
|
}
|
|
if (strlen($this->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');
|
|
$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 $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) {
|
|
|
|
return $entry;
|
|
}
|
|
}
|
|
|
|
return new Account;
|
|
}
|
|
}
|