Files
firefly-iii/app/Helpers/Csv/Converter/AssetAccountName.php

60 lines
1.8 KiB
PHP
Raw Normal View History

2015-07-05 14:37:36 +02:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-07-05 14:37:36 +02:00
namespace FireflyIII\Helpers\Csv\Converter;
2015-07-05 21:47:59 +02:00
2015-07-05 14:37:36 +02:00
use Auth;
2016-02-13 13:13:22 +01:00
use Carbon\Carbon;
2015-07-05 14:37:36 +02:00
use FireflyIII\Models\Account;
2016-04-01 13:03:38 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-07-05 14:37:36 +02:00
/**
* Class AssetAccountName
*
* @package FireflyIII\Helpers\Csv\Converter
*/
2015-07-05 21:47:59 +02:00
class AssetAccountName extends BasicConverter implements ConverterInterface
2015-07-05 14:37:36 +02:00
{
/**
* @return Account|null
*/
2016-04-06 16:37:28 +02:00
public function convert(): Account
2015-07-05 14:37:36 +02:00
{
2016-04-01 13:03:38 +02:00
/** @var AccountRepositoryInterface $repository */
2016-05-01 15:05:29 +02:00
$repository = app(AccountRepositoryInterface::class);
2016-05-20 09:45:24 +02:00
$crud = app('FireflyIII\Crud\Account\AccountCrudInterface');
2016-05-20 09:25:17 +02:00
2015-07-05 14:37:36 +02:00
if (isset($this->mapped[$this->index][$this->value])) {
2016-05-20 09:45:24 +02:00
$account = $crud->find(intval($this->mapped[$this->index][$this->value]));
2015-07-05 14:37:36 +02:00
return $account;
}
2016-04-01 13:03:38 +02:00
2016-05-13 15:53:39 +02:00
$set = $repository->getAccountsByType(['Default account', 'Asset account']);
2015-07-05 14:37:36 +02:00
/** @var Account $entry */
foreach ($set as $entry) {
if ($entry->name == $this->value) {
return $entry;
}
}
2016-02-13 13:13:22 +01:00
$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.
];
2016-05-20 09:25:17 +02:00
$account = $crud->store($accountData);
2015-07-05 14:37:36 +02:00
return $account;
}
2015-07-09 21:26:40 +02:00
}