firefly-iii/app/Validation/AccountValidator.php

327 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* AccountValidator.php
2020-02-16 06:58:22 -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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Validation;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionType;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as UserGroupAccountRepositoryInterface;
use FireflyIII\User;
2020-03-21 02:11:14 -05:00
use FireflyIII\Validation\Account\DepositValidation;
use FireflyIII\Validation\Account\LiabilityValidation;
2020-03-21 02:11:14 -05:00
use FireflyIII\Validation\Account\OBValidation;
use FireflyIII\Validation\Account\ReconciliationValidation;
use FireflyIII\Validation\Account\TransferValidation;
use FireflyIII\Validation\Account\WithdrawalValidation;
/**
* Class AccountValidator
*/
class AccountValidator
{
2022-10-30 08:24:37 -05:00
use DepositValidation;
use LiabilityValidation;
2023-11-04 08:18:49 -05:00
use OBValidation;
use ReconciliationValidation;
use TransferValidation;
use WithdrawalValidation;
public bool $createMode;
public string $destError;
public ?Account $destination;
public ?Account $source;
public string $sourceError;
private AccountRepositoryInterface $accountRepository;
private array $combinations;
private string $transactionType;
private bool $useUserGroupRepository = false;
private UserGroupAccountRepositoryInterface $userGroupAccountRepository;
/**
* AccountValidator constructor.
*/
public function __construct()
{
$this->createMode = false;
$this->destError = 'No error yet.';
$this->sourceError = 'No error yet.';
$this->combinations = config('firefly.source_dests');
$this->source = null;
$this->destination = null;
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->userGroupAccountRepository = app(UserGroupAccountRepositoryInterface::class);
}
2021-03-21 03:15:40 -05:00
public function getSource(): ?Account
{
return $this->source;
}
2023-05-29 06:56:55 -05:00
public function setSource(?Account $account): void
{
if (null === $account) {
2023-10-29 00:33:43 -05:00
app('log')->debug('AccountValidator source is set to NULL');
2023-05-29 06:56:55 -05:00
}
if (null !== $account) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type));
2023-05-29 06:56:55 -05:00
}
$this->source = $account;
}
public function setDestination(?Account $account): void
{
if (null === $account) {
2023-10-29 00:33:43 -05:00
app('log')->debug('AccountValidator destination is set to NULL');
2023-05-29 06:56:55 -05:00
}
if (null !== $account) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type));
2023-05-29 06:56:55 -05:00
}
$this->destination = $account;
}
public function setTransactionType(string $transactionType): void
{
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Transaction type for validator is now "%s".', ucfirst($transactionType)));
$this->transactionType = ucfirst($transactionType);
}
public function setUser(User $user): void
{
$this->accountRepository->setUser($user);
$this->useUserGroupRepository = false;
}
public function setUserGroup(UserGroup $userGroup): void
{
$this->userGroupAccountRepository->setUserGroup($userGroup);
$this->useUserGroupRepository = true;
}
2021-12-18 05:35:17 -06:00
public function validateDestination(array $array): bool
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in AccountValidator::validateDestination()', $array);
if (null === $this->source) {
2023-10-29 00:32:00 -05:00
app('log')->error('Source is NULL, always FALSE.');
$this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.';
return false;
}
2023-12-20 12:35:52 -06:00
switch ($this->transactionType) {
default:
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType);
2023-10-29 00:32:00 -05:00
app('log')->error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
$result = false;
2023-12-20 12:35:52 -06:00
break;
case TransactionType::WITHDRAWAL:
$result = $this->validateWithdrawalDestination($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::DEPOSIT:
$result = $this->validateDepositDestination($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::TRANSFER:
$result = $this->validateTransferDestination($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::OPENING_BALANCE:
$result = $this->validateOBDestination($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
2021-04-10 10:26:00 -05:00
case TransactionType::LIABILITY_CREDIT:
$result = $this->validateLCDestination($array);
2023-12-20 12:35:52 -06:00
2021-04-10 10:26:00 -05:00
break;
2023-12-20 12:35:52 -06:00
2019-06-22 22:53:01 -05:00
case TransactionType::RECONCILIATION:
$result = $this->validateReconciliationDestination($array);
2023-12-20 12:35:52 -06:00
2019-06-22 22:53:01 -05:00
break;
}
return $result;
}
2021-12-18 05:35:17 -06:00
public function validateSource(array $array): bool
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in AccountValidator::validateSource()', $array);
2023-12-20 12:35:52 -06:00
switch ($this->transactionType) {
default:
2023-10-29 00:32:00 -05:00
app('log')->error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will do a generic check.', $this->transactionType));
2021-12-18 05:35:17 -06:00
$result = $this->validateGenericSource($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::WITHDRAWAL:
2021-12-18 05:35:17 -06:00
$result = $this->validateWithdrawalSource($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::DEPOSIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateDepositSource($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::TRANSFER:
2021-12-18 05:35:17 -06:00
$result = $this->validateTransferSource($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
case TransactionType::OPENING_BALANCE:
2021-12-18 05:35:17 -06:00
$result = $this->validateOBSource($array);
2023-12-20 12:35:52 -06:00
break;
2023-12-20 12:35:52 -06:00
2021-04-10 10:26:00 -05:00
case TransactionType::LIABILITY_CREDIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateLCSource($array);
2023-12-20 12:35:52 -06:00
2021-04-10 10:26:00 -05:00
break;
2019-06-22 22:53:01 -05:00
case TransactionType::RECONCILIATION:
2023-10-29 00:33:43 -05:00
app('log')->debug('Calling validateReconciliationSource');
2021-12-18 05:35:17 -06:00
$result = $this->validateReconciliationSource($array);
2023-12-20 12:35:52 -06:00
2019-06-22 22:53:01 -05:00
break;
}
return $result;
}
2020-03-21 02:11:14 -05:00
protected function canCreateTypes(array $accountTypes): bool
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Can we create any of these types?', $accountTypes);
2023-12-20 12:35:52 -06:00
/** @var string $accountType */
foreach ($accountTypes as $accountType) {
if ($this->canCreateType($accountType)) {
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('YES, we can create a %s', $accountType));
2019-06-07 23:19:21 -05:00
return true;
}
}
2023-10-29 00:33:43 -05:00
app('log')->debug('NO, we cant create any of those.');
return false;
}
2023-06-21 05:34:58 -05:00
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
}
/**
2023-12-22 13:12:38 -06:00
* It's a long and fairly complex method, but I don't mind.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
2023-12-22 13:12:38 -06:00
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function findExistingAccount(array $validTypes, array $data, bool $inverse = false): ?Account
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in findExistingAccount', $data);
app('log')->debug('The search will be reversed!');
2021-12-18 05:35:17 -06:00
$accountId = array_key_exists('id', $data) ? $data['id'] : null;
$accountIban = array_key_exists('iban', $data) ? $data['iban'] : null;
$accountNumber = array_key_exists('number', $data) ? $data['number'] : null;
$accountName = array_key_exists('name', $data) ? $data['name'] : null;
// find by ID
2021-12-18 05:35:17 -06:00
if (null !== $accountId && $accountId > 0) {
$first = $this->getRepository()->find($accountId);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
2023-12-20 12:35:52 -06:00
return $first;
}
}
2021-12-18 05:35:17 -06:00
// find by iban
2022-12-29 12:42:40 -06:00
if (null !== $accountIban && '' !== (string)$accountIban) {
$first = $this->getRepository()->findByIbanNull($accountIban, $validTypes);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
2023-12-20 12:35:52 -06:00
2021-12-18 05:35:17 -06:00
return $first;
}
}
// find by number
2022-12-29 12:42:40 -06:00
if (null !== $accountNumber && '' !== (string)$accountNumber) {
$first = $this->getRepository()->findByAccountNumber($accountNumber, $validTypes);
$accountType = null === $first ? 'invalid' : $first->accountType->type;
$check = in_array($accountType, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
2023-12-20 12:35:52 -06:00
2021-12-18 05:35:17 -06:00
return $first;
}
}
// find by name:
2022-12-29 12:42:40 -06:00
if ('' !== (string)$accountName) {
$first = $this->getRepository()->findByName($accountName, $validTypes);
if (null !== $first) {
app('log')->debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
2023-12-20 12:35:52 -06:00
return $first;
}
}
app('log')->debug('Found nothing!');
return null;
}
2023-12-20 12:35:52 -06:00
private function getRepository(): AccountRepositoryInterface|UserGroupAccountRepositoryInterface
{
if ($this->useUserGroupRepository) {
return $this->userGroupAccountRepository;
}
return $this->accountRepository;
}
2019-08-17 05:09:03 -05:00
}