Files
firefly-iii/app/Factory/AccountFactory.php

369 lines
14 KiB
PHP
Raw Normal View History

<?php
2018-05-11 10:08:34 +02:00
/**
* AccountFactory.php
2020-02-16 14:00:57 +01: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 10:08:34 +02:00
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Events\StoredAccount;
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;
2021-03-13 12:01:01 +01:00
use FireflyIII\Services\Internal\Update\AccountUpdateService;
use FireflyIII\User;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
/**
2018-02-19 19:44:46 +01:00
* Factory to create or return accounts.
*
* Class AccountFactory
*/
class AccountFactory
{
2022-10-30 14:24:10 +01:00
use AccountServiceTrait;
use LocationServiceTrait;
2019-06-22 10:25:34 +02:00
2021-03-13 12:01:01 +01:00
protected AccountRepositoryInterface $accountRepository;
protected array $validAssetFields;
protected array $validCCFields;
protected array $validFields;
private array $canHaveOpeningBalance;
private array $canHaveVirtual;
2021-03-13 12:01:01 +01:00
private User $user;
/**
2018-12-21 16:38:10 +01:00
* AccountFactory constructor.
*/
public function __construct()
{
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->canHaveVirtual = config('firefly.can_have_virtual_amounts');
$this->canHaveOpeningBalance = config('firefly.can_have_opening_balance');
$this->validAssetFields = config('firefly.valid_asset_fields');
$this->validCCFields = config('firefly.valid_cc_fields');
$this->validFields = config('firefly.valid_account_fields');
}
2023-05-29 13:56:55 +02:00
/**
2021-03-21 09:15:40 +01:00
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2021-03-21 09:15:40 +01:00
public function findOrCreate(string $accountName, string $accountType): Account
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('findOrCreate("%s", "%s")', $accountName, $accountType));
2021-04-05 10:56:08 +02:00
$type = $this->accountRepository->getAccountTypeByType($accountType);
2021-04-05 10:56:08 +02:00
if (null === $type) {
throw new FireflyException(sprintf('Cannot find account type "%s"', $accountType));
}
2021-03-21 09:15:40 +01:00
$return = $this->user->accounts->where('account_type_id', $type->id)->where('name', $accountName)->first();
if (null === $return) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Found nothing. Will create a new one.');
2021-03-21 09:15:40 +01:00
$return = $this->create(
2021-04-05 10:56:08 +02:00
[
2021-04-05 21:52:55 +02:00
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
2021-04-05 21:52:55 +02:00
'name' => $accountName,
'account_type_id' => $type->id,
'account_type_name' => null,
'virtual_balance' => '0',
'iban' => null,
'active' => true,
2021-04-05 10:56:08 +02:00
]
2021-03-21 09:15:40 +01:00
);
}
return $return;
}
/**
2023-06-21 12:34:58 +02:00
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2023-06-21 12:34:58 +02:00
public function create(array $data): Account
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in AccountFactory::create()');
2023-06-21 12:34:58 +02:00
$type = $this->getAccountType($data);
$data['iban'] = $this->filterIban($data['iban'] ?? null);
// account may exist already:
$return = $this->find($data['name'], $type->type);
2023-06-21 12:34:58 +02:00
if (null !== $return) {
return $return;
}
$return = $this->createAccount($type, $data);
2023-06-21 12:34:58 +02:00
event(new StoredAccount($return));
return $return;
}
/**
2021-05-24 08:50:17 +02:00
* @throws FireflyException
*/
2021-04-05 10:56:08 +02:00
protected function getAccountType(array $data): ?AccountType
{
2022-12-29 19:41:57 +01:00
$accountTypeId = array_key_exists('account_type_id', $data) ? (int)$data['account_type_id'] : 0;
2021-04-05 10:56:08 +02:00
$accountTypeName = array_key_exists('account_type_name', $data) ? $data['account_type_name'] : null;
$result = null;
// find by name or ID
if ($accountTypeId > 0) {
$result = AccountType::find($accountTypeId);
}
2021-04-05 10:56:08 +02:00
if (null !== $accountTypeName) {
$result = $this->accountRepository->getAccountTypeByType($accountTypeName);
}
// try with type:
if (null === $result) {
2021-04-05 10:56:08 +02:00
$types = config(sprintf('firefly.accountTypeByIdentifier.%s', $accountTypeName)) ?? [];
if (0 !== count($types)) {
2018-08-05 20:42:45 +02:00
$result = AccountType::whereIn('type', $types)->first();
2018-08-04 17:30:47 +02:00
}
2018-03-24 18:55:02 +01:00
}
if (null === $result) {
2022-10-30 14:44:49 +01:00
app('log')->warning(sprintf('Found NO account type based on %d and "%s"', $accountTypeId, $accountTypeName));
2023-12-20 19:35:52 +01:00
2021-04-05 10:56:08 +02:00
throw new FireflyException(sprintf('AccountFactory::create() was unable to find account type #%d ("%s").', $accountTypeId, $accountTypeName));
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found account type based on %d and "%s": "%s"', $accountTypeId, $accountTypeName, $result->type));
2021-03-28 11:46:23 +02:00
2018-03-24 18:55:02 +01:00
return $result;
}
public function find(string $accountName, string $accountType): ?Account
{
app('log')->debug(sprintf('Now in AccountFactory::find("%s", "%s")', $accountName, $accountType));
$type = AccountType::whereType($accountType)->first();
// @var Account|null
return $this->user->accounts()->where('account_type_id', $type->id)->where('name', $accountName)->first();
}
2021-03-21 09:15:40 +01:00
/**
2022-03-29 15:10:05 +02:00
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2021-04-05 10:56:08 +02:00
private function createAccount(AccountType $type, array $data): Account
{
$this->accountRepository->resetAccountOrder();
// create it:
$virtualBalance = array_key_exists('virtual_balance', $data) ? $data['virtual_balance'] : null;
$active = array_key_exists('active', $data) ? $data['active'] : true;
2022-12-29 19:41:57 +01:00
$databaseData = [
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
2022-12-29 19:41:57 +01:00
'account_type_id' => $type->id,
'name' => $data['name'],
'order' => 25000,
'virtual_balance' => $virtualBalance,
'active' => $active,
'iban' => $data['iban'],
2021-04-05 10:56:08 +02:00
];
// fix virtual balance when it's empty
2022-12-29 19:41:57 +01:00
if ('' === (string)$databaseData['virtual_balance']) {
2021-04-05 10:56:08 +02:00
$databaseData['virtual_balance'] = null;
}
2022-12-27 07:01:13 +01:00
// remove virtual balance when not an asset account
2021-04-05 10:56:08 +02:00
if (!in_array($type->type, $this->canHaveVirtual, true)) {
$databaseData['virtual_balance'] = null;
}
// create account!
$account = Account::create($databaseData);
2023-08-30 15:57:59 +02:00
Log::channel('audit')->info(sprintf('Account #%d ("%s") has been created.', $account->id, $account->name));
2021-04-05 10:56:08 +02:00
// update meta data:
$data = $this->cleanMetaDataArray($account, $data);
2021-04-05 10:56:08 +02:00
$this->storeMetaData($account, $data);
2022-12-27 07:01:13 +01:00
// create opening balance (only asset accounts)
try {
$this->storeOpeningBalance($account, $data);
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
2022-12-27 07:01:13 +01:00
// create credit liability data (only liabilities)
try {
$this->storeCreditLiability($account, $data);
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
}
2021-04-05 10:56:08 +02:00
// create notes
$notes = array_key_exists('notes', $data) ? $data['notes'] : '';
2021-04-05 10:56:08 +02:00
$this->updateNote($account, $notes);
// create location
$this->storeNewLocation($account, $data);
// set order
$this->storeOrder($account, $data);
// refresh and return
$account->refresh();
return $account;
}
/**
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2023-06-21 12:34:58 +02:00
private function cleanMetaDataArray(Account $account, array $data): array
2021-04-05 10:56:08 +02:00
{
$currencyId = array_key_exists('currency_id', $data) ? (int)$data['currency_id'] : 0;
$currencyCode = array_key_exists('currency_code', $data) ? (string)$data['currency_code'] : '';
$accountRole = array_key_exists('account_role', $data) ? (string)$data['account_role'] : null;
$currency = $this->getCurrency($currencyId, $currencyCode);
2023-06-21 12:34:58 +02:00
// only asset account may have a role:
2023-12-20 19:35:52 +01:00
if (AccountType::ASSET !== $account->accountType->type) {
2023-06-21 12:34:58 +02:00
$accountRole = '';
2021-04-10 07:57:04 +02:00
}
2023-06-21 12:34:58 +02:00
// only liability may have direction:
if (array_key_exists('liability_direction', $data) && !in_array($account->accountType->type, config('firefly.valid_liabilities'), true)) {
$data['liability_direction'] = null;
}
$data['account_role'] = $accountRole;
$data['currency_id'] = $currency->id;
return $data;
2021-04-05 10:56:08 +02:00
}
private function storeMetaData(Account $account, array $data): void
{
$fields = $this->validFields;
2023-12-20 19:35:52 +01:00
if (AccountType::ASSET === $account->accountType->type) {
2021-04-05 10:56:08 +02:00
$fields = $this->validAssetFields;
}
2023-12-20 19:35:52 +01:00
if (AccountType::ASSET === $account->accountType->type && 'ccAsset' === $data['account_role']) {
2021-04-10 07:57:04 +02:00
$fields = $this->validCCFields;
2021-04-05 10:56:08 +02:00
}
// remove currency_id if necessary.
$type = $account->accountType->type;
$list = config('firefly.valid_currency_account_types');
if (!in_array($type, $list, true)) {
2022-10-30 14:24:10 +01:00
$pos = array_search('currency_id', $fields, true);
2023-12-20 19:35:52 +01:00
if (false !== $pos) {
unset($fields[$pos]);
}
}
2021-04-05 10:56:08 +02:00
/** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class);
foreach ($fields as $field) {
// if the field is set but NULL, skip it.
// if the field is set but "", update it.
2021-04-07 07:28:43 +02:00
if (array_key_exists($field, $data) && null !== $data[$field]) {
2021-04-05 10:56:08 +02:00
// convert boolean value:
if (is_bool($data[$field]) && false === $data[$field]) {
2021-04-10 07:57:04 +02:00
$data[$field] = 0;
2021-04-05 10:56:08 +02:00
}
if (true === $data[$field]) {
2021-04-10 07:57:04 +02:00
$data[$field] = 1;
2021-04-05 10:56:08 +02:00
}
2022-12-29 19:41:57 +01:00
$factory->crud($account, $field, (string)$data[$field]);
2021-04-05 10:56:08 +02:00
}
}
}
/**
* @throws FireflyException
2021-04-05 10:56:08 +02:00
*/
2023-06-04 06:30:22 +02:00
private function storeOpeningBalance(Account $account, array $data): void
2021-04-05 10:56:08 +02:00
{
$accountType = $account->accountType->type;
if (in_array($accountType, $this->canHaveOpeningBalance, true)) {
2021-04-05 10:56:08 +02:00
if ($this->validOBData($data)) {
$openingBalance = $data['opening_balance'];
$openingBalanceDate = $data['opening_balance_date'];
$this->updateOBGroupV2($account, $openingBalance, $openingBalanceDate);
2021-04-05 10:56:08 +02:00
}
if (!$this->validOBData($data)) {
$this->deleteOBGroup($account);
}
}
}
/**
2023-06-21 12:34:58 +02:00
* @throws FireflyException
*/
private function storeCreditLiability(Account $account, array $data): void
{
2023-10-29 06:33:43 +01:00
app('log')->debug('storeCreditLiability');
2023-06-21 12:34:58 +02:00
$account->refresh();
$accountType = $account->accountType->type;
$direction = $this->accountRepository->getMetaValue($account, 'liability_direction');
$valid = config('firefly.valid_liabilities');
if (in_array($accountType, $valid, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Is a liability with credit ("i am owed") direction.');
2023-06-21 12:34:58 +02:00
if ($this->validOBData($data)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Has valid CL data.');
2023-06-21 12:34:58 +02:00
$openingBalance = $data['opening_balance'];
$openingBalanceDate = $data['opening_balance_date'];
// store credit transaction.
$this->updateCreditTransaction($account, $direction, $openingBalance, $openingBalanceDate);
}
if (!$this->validOBData($data)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Does NOT have valid CL data, deletr any CL transaction.');
2023-06-21 12:34:58 +02:00
$this->deleteCreditTransaction($account);
}
}
}
/**
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-04-05 10:56:08 +02:00
*/
private function storeOrder(Account $account, array $data): void
{
$accountType = $account->accountType->type;
$maxOrder = $this->accountRepository->maxOrder($accountType);
$order = null;
2021-04-05 10:56:08 +02:00
if (!array_key_exists('order', $data)) {
$order = $maxOrder + 1;
}
if (array_key_exists('order', $data)) {
2022-12-29 19:41:57 +01:00
$order = (int)($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']);
2021-04-05 10:56:08 +02:00
$order = 0 === $order ? $maxOrder + 1 : $order;
}
$updateService = app(AccountUpdateService::class);
$updateService->setUser($account->user);
$updateService->update($account, ['order' => $order]);
}
public function setUser(User $user): void
{
$this->user = $user;
$this->accountRepository->setUser($user);
}
2018-03-05 19:35:58 +01:00
}