2018-02-18 12:55:35 -06:00
|
|
|
<?php
|
2018-05-11 03:08:34 -05:00
|
|
|
|
2018-02-18 12:55:35 -06:00
|
|
|
/**
|
|
|
|
* AccountFactory.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
|
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
2018-02-18 12:55:35 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-02-18 12:55:35 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2018-02-21 14:06:59 -06:00
|
|
|
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
|
2018-02-18 12:55:35 -06:00
|
|
|
use FireflyIII\User;
|
2018-08-04 10:30:47 -05:00
|
|
|
use Log;
|
2018-02-18 12:55:35 -06:00
|
|
|
|
|
|
|
/**
|
2018-02-19 12:44:46 -06:00
|
|
|
* Factory to create or return accounts.
|
|
|
|
*
|
2018-02-18 12:55:35 -06:00
|
|
|
* Class AccountFactory
|
|
|
|
*/
|
|
|
|
class AccountFactory
|
|
|
|
{
|
2018-09-26 23:26:03 -05:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
use AccountServiceTrait;
|
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2018-09-06 05:29:32 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 12:55:35 -06:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Account
|
2018-07-06 12:06:08 -05:00
|
|
|
* @throws FireflyException
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
2018-02-18 12:55:35 -06:00
|
|
|
*/
|
|
|
|
public function create(array $data): Account
|
|
|
|
{
|
2018-07-06 12:06:08 -05:00
|
|
|
$type = $this->getAccountType($data['account_type_id'], $data['accountType']);
|
|
|
|
|
|
|
|
if (null === $type) {
|
|
|
|
throw new FireflyException(
|
|
|
|
sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'], $data['accountType'])
|
|
|
|
);
|
|
|
|
}
|
2018-02-21 14:06:59 -06:00
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
$data['iban'] = $this->filterIban($data['iban']);
|
2018-02-21 14:06:59 -06:00
|
|
|
|
|
|
|
// account may exist already:
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = $this->find($data['name'], $type->type);
|
2018-02-21 14:06:59 -06:00
|
|
|
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
if (null === $return) {
|
|
|
|
// create it:
|
|
|
|
$databaseData
|
|
|
|
= [
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'account_type_id' => $type->id,
|
|
|
|
'name' => $data['name'],
|
|
|
|
'virtual_balance' => $data['virtualBalance'] ?? '0',
|
|
|
|
'active' => true === $data['active'],
|
|
|
|
'iban' => $data['iban'],
|
|
|
|
];
|
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
// remove virtual balance when not an asset account or a liability
|
|
|
|
$canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
|
|
|
|
if (!\in_array($type->type, $canHaveVirtual, true)) {
|
2018-07-06 12:06:08 -05:00
|
|
|
$databaseData['virtual_balance'] = '0';
|
|
|
|
}
|
2018-02-21 14:06:59 -06:00
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
// fix virtual balance when it's empty
|
|
|
|
if ('' === $databaseData['virtual_balance']) {
|
|
|
|
$databaseData['virtual_balance'] = '0';
|
|
|
|
}
|
2018-04-03 12:15:06 -05:00
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = Account::create($databaseData);
|
|
|
|
$this->updateMetaData($return, $data);
|
2018-02-21 14:06:59 -06:00
|
|
|
|
2018-08-04 10:30:47 -05:00
|
|
|
if (\in_array($type->type, $canHaveVirtual, true)) {
|
2018-07-06 12:06:08 -05:00
|
|
|
if ($this->validIBData($data)) {
|
|
|
|
$this->updateIB($return, $data);
|
|
|
|
}
|
|
|
|
if (!$this->validIBData($data)) {
|
|
|
|
$this->deleteIB($return);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->updateNote($return, $data['notes'] ?? '');
|
2018-02-21 14:06:59 -06:00
|
|
|
}
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
return $return;
|
2018-02-21 14:06:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accountName
|
|
|
|
* @param string $accountType
|
|
|
|
*
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
|
|
|
public function find(string $accountName, string $accountType): ?Account
|
|
|
|
{
|
|
|
|
$type = AccountType::whereType($accountType)->first();
|
|
|
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = null;
|
2018-02-21 14:06:59 -06:00
|
|
|
/** @var Account $object */
|
|
|
|
foreach ($accounts as $object) {
|
|
|
|
if ($object->name === $accountName) {
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = $object;
|
|
|
|
break;
|
2018-02-21 14:06:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
return $return;
|
2018-02-18 12:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-24 00:18:33 -05:00
|
|
|
*
|
2018-02-18 12:55:35 -06:00
|
|
|
* @param string $accountName
|
|
|
|
* @param string $accountType
|
|
|
|
*
|
|
|
|
* @return Account
|
2018-07-06 12:06:08 -05:00
|
|
|
* @throws FireflyException
|
2018-02-18 12:55:35 -06:00
|
|
|
*/
|
|
|
|
public function findOrCreate(string $accountName, string $accountType): Account
|
|
|
|
{
|
2018-09-26 23:26:03 -05:00
|
|
|
Log::debug(sprintf('Searching for "%s" of type "%s"', $accountName, $accountType));
|
2018-02-18 12:55:35 -06:00
|
|
|
$type = AccountType::whereType($accountType)->first();
|
|
|
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = null;
|
2018-09-26 23:26:03 -05:00
|
|
|
|
|
|
|
Log::debug(sprintf('Account type is #%d', $type->id));
|
|
|
|
|
2018-02-18 12:55:35 -06:00
|
|
|
/** @var Account $object */
|
|
|
|
foreach ($accounts as $object) {
|
|
|
|
if ($object->name === $accountName) {
|
2018-09-26 23:26:03 -05:00
|
|
|
Log::debug(sprintf('Found account #%d "%s".', $object->id, $object->name));
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = $object;
|
|
|
|
break;
|
2018-02-18 12:55:35 -06:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 12:06:08 -05:00
|
|
|
if (null === $return) {
|
2018-09-26 23:26:03 -05:00
|
|
|
Log::debug('Found nothing. Will create a new one.');
|
2018-07-06 12:06:08 -05:00
|
|
|
$return = $this->create(
|
|
|
|
[
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'name' => $accountName,
|
|
|
|
'account_type_id' => $type->id,
|
|
|
|
'accountType' => null,
|
|
|
|
'virtualBalance' => '0',
|
|
|
|
'iban' => null,
|
|
|
|
'active' => true,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2018-02-18 12:55:35 -06:00
|
|
|
|
2018-07-06 12:06:08 -05:00
|
|
|
return $return;
|
2018-02-18 12:55:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2018-02-21 14:06:59 -06:00
|
|
|
/**
|
|
|
|
* @param int|null $accountTypeId
|
|
|
|
* @param null|string $accountType
|
|
|
|
*
|
|
|
|
* @return AccountType|null
|
2018-07-06 12:06:08 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2018-02-21 14:06:59 -06:00
|
|
|
*/
|
|
|
|
protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
|
|
|
|
{
|
2018-04-02 07:42:07 -05:00
|
|
|
$accountTypeId = (int)$accountTypeId;
|
2018-07-06 12:06:08 -05:00
|
|
|
$result = null;
|
2018-02-21 14:06:59 -06:00
|
|
|
if ($accountTypeId > 0) {
|
2018-07-06 12:06:08 -05:00
|
|
|
$result = AccountType::find($accountTypeId);
|
2018-02-21 14:06:59 -06:00
|
|
|
}
|
2018-07-06 12:06:08 -05:00
|
|
|
if (null === $result) {
|
2018-08-04 10:30:47 -05:00
|
|
|
Log::debug(sprintf('No account type found by ID, continue search for "%s".', $accountType));
|
|
|
|
/** @var array $types */
|
|
|
|
$types = config('firefly.accountTypeByIdentifier.' . $accountType) ?? [];
|
|
|
|
if (\count($types) > 0) {
|
2018-08-05 13:42:45 -05:00
|
|
|
Log::debug(sprintf('%d accounts in list from config', \count($types)), $types);
|
|
|
|
$result = AccountType::whereIn('type', $types)->first();
|
2018-08-04 10:30:47 -05:00
|
|
|
}
|
2018-07-06 12:06:08 -05:00
|
|
|
if (null === $result && null !== $accountType) {
|
|
|
|
// try as full name:
|
|
|
|
$result = AccountType::whereType($accountType)->first();
|
|
|
|
}
|
2018-03-24 12:55:02 -05:00
|
|
|
}
|
2018-02-21 14:06:59 -06:00
|
|
|
|
2018-03-24 12:55:02 -05:00
|
|
|
return $result;
|
2018-02-21 14:06:59 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|