firefly-iii/app/Factory/AccountFactory.php

235 lines
8.0 KiB
PHP
Raw Normal View History

<?php
2018-05-11 03:08:34 -05:00
/**
* AccountFactory.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
use FireflyIII\Services\Internal\Support\LocationServiceTrait;
use FireflyIII\User;
2018-08-04 10:30:47 -05:00
use Log;
/**
2018-02-19 12:44:46 -06:00
* Factory to create or return accounts.
*
* Class AccountFactory
*/
class AccountFactory
{
use AccountServiceTrait, LocationServiceTrait;
2019-06-22 03:25:34 -05:00
/** @var AccountRepositoryInterface */
protected $accountRepository;
protected array $validAssetFields;
protected array $validCCFields;
protected array $validFields;
private array $canHaveVirtual;
private User $user;
/**
2018-12-21 09:38:10 -06:00
* AccountFactory constructor.
2019-02-13 10:38:41 -06:00
*
2018-12-21 09:38:10 -06:00
* @codeCoverageIgnore
*/
public function __construct()
{
if ('testing' === config('app.env')) {
2019-06-07 11:20:15 -05:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
$this->validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
}
/**
* @param array $data
*
* @throws FireflyException
* @return Account
*/
public function create(array $data): Account
{
$type = $this->getAccountType($data['account_type_id'], $data['account_type']);
if (null === $type) {
throw new FireflyException(
sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $data['account_type_id'], $data['account_type'])
);
}
$data['iban'] = $this->filterIban($data['iban'] ?? null);
// account may exist already:
Log::debug('Data array is as follows', $data);
$return = $this->find($data['name'], $type->type);
if (null === $return) {
// create it:
$databaseData
= [
'user_id' => $this->user->id,
'account_type_id' => $type->id,
'name' => $data['name'],
'order' => $data['order'] ?? 0,
2020-04-13 00:57:32 -05:00
'virtual_balance' => $data['virtual_balance'] ?? null,
'active' => true === $data['active'],
'iban' => $data['iban'],
];
$currency = $this->getCurrency((int) ($data['currency_id'] ?? null), (string) ($data['currency_code'] ?? null));
2018-12-21 09:38:10 -06:00
unset($data['currency_code']);
$data['currency_id'] = $currency->id;
2018-08-04 10:30:47 -05:00
// remove virtual balance when not an asset account or a liability
if (!in_array($type->type, $this->canHaveVirtual, true)) {
2020-04-13 00:57:32 -05:00
$databaseData['virtual_balance'] = null;
}
// fix virtual balance when it's empty
2020-07-31 05:45:02 -05:00
if ('' === (string) $databaseData['virtual_balance']) {
2020-04-13 00:57:32 -05:00
$databaseData['virtual_balance'] = null;
}
2018-04-03 12:15:06 -05:00
$return = Account::create($databaseData);
$this->updateMetaData($return, $data);
// if it can have a virtual balance, it can also have an opening balance.
if (in_array($type->type, $this->canHaveVirtual, true)) {
if ($this->validOBData($data)) {
$this->updateOBGroup($return, $data);
}
if (!$this->validOBData($data)) {
$this->deleteOBGroup($return);
}
}
$this->updateNote($return, $data['notes'] ?? '');
2019-12-28 12:49:33 -06:00
// store location
$this->storeNewLocation($return, $data);
}
return $return;
}
/**
* @param string $accountName
* @param string $accountType
*
* @return Account|null
*/
public function find(string $accountName, string $accountType): ?Account
{
$type = AccountType::whereType($accountType)->first();
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
}
/**
* @param string $accountName
* @param string $accountType
*
* @throws FireflyException
* @return Account
*/
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));
/** @var AccountType $type */
$type = AccountType::whereType($accountType)->first();
$return = $this->user->accounts->where('account_type_id', $type->id)
->where('name', $accountName)->first();
if (null === $return) {
2018-09-26 23:26:03 -05:00
Log::debug('Found nothing. Will create a new one.');
$return = $this->create(
[
'user_id' => $this->user->id,
'name' => $accountName,
'account_type_id' => $type->id,
'account_type' => null,
'virtual_balance' => '0',
'iban' => null,
'active' => true,
]
);
}
return $return;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param int|null $accountTypeId
* @param null|string $accountType
*
* @return AccountType|null
*/
protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
{
$accountTypeId = (int) $accountTypeId;
$result = null;
if ($accountTypeId > 0) {
$result = AccountType::find($accountTypeId);
}
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) {
Log::debug(sprintf('%d accounts in list from config', count($types)), $types);
2018-08-05 13:42:45 -05:00
$result = AccountType::whereIn('type', $types)->first();
2018-08-04 10:30:47 -05:00
}
if (null === $result && null !== $accountType) {
// try as full name:
$result = AccountType::whereType($accountType)->first();
}
2018-03-24 12:55:02 -05:00
}
if (null === $result) {
Log::warning(sprintf('Found NO account type based on %d and "%s"', $accountTypeId, $accountType));
}
if (null !== $result) {
Log::debug(sprintf('Found account type based on %d and "%s": "%s"', $accountTypeId, $accountType, $result->type));
}
2018-03-24 12:55:02 -05:00
return $result;
}
2018-03-05 12:35:58 -06:00
}