firefly-iii/app/Validation/AccountValidator.php

263 lines
9.0 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\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
2020-03-21 02:11:14 -05:00
use FireflyIII\Validation\Account\AccountValidatorProperties;
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;
use Log;
/**
* Class AccountValidator
*/
class AccountValidator
{
2021-04-10 10:26:00 -05:00
use AccountValidatorProperties, WithdrawalValidation, DepositValidation, TransferValidation, ReconciliationValidation, OBValidation, LiabilityValidation;
public bool $createMode;
public string $destError;
2021-03-21 03:15:40 -05:00
public ?Account $destination;
public ?Account $source;
public string $sourceError;
private AccountRepositoryInterface $accountRepository;
private array $combinations;
private string $transactionType;
private User $user;
/**
* AccountValidator constructor.
*/
public function __construct()
{
2021-04-06 01:51:27 -05:00
$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);
}
2021-03-21 03:15:40 -05:00
/**
* @return Account|null
*/
public function getSource(): ?Account
{
return $this->source;
}
/**
* @param string $transactionType
*/
public function setTransactionType(string $transactionType): void
{
Log::debug(sprintf('Transaction type for validator is now "%s".', ucfirst($transactionType)));
$this->transactionType = ucfirst($transactionType);
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
$this->accountRepository->setUser($user);
}
/**
2021-12-18 05:35:17 -06:00
* @param array $array
*
* @return bool
*/
2021-12-18 05:35:17 -06:00
public function validateDestination(array $array): bool
{
2021-12-18 05:35:17 -06:00
Log::debug('Now in AccountValidator::validateDestination()', $array);
if (null === $this->source) {
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;
}
switch ($this->transactionType) {
default:
$this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType);
Log::error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType));
$result = false;
break;
case TransactionType::WITHDRAWAL:
2021-12-18 05:35:17 -06:00
$result = $this->validateWithdrawalDestination($array);
break;
case TransactionType::DEPOSIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateDepositDestination($array);
break;
case TransactionType::TRANSFER:
2021-12-18 05:35:17 -06:00
$result = $this->validateTransferDestination($array);
break;
case TransactionType::OPENING_BALANCE:
2021-12-18 05:35:17 -06:00
$result = $this->validateOBDestination($array);
break;
2021-04-10 10:26:00 -05:00
case TransactionType::LIABILITY_CREDIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateLCDestination($array);
2021-04-10 10:26:00 -05:00
break;
2019-06-22 22:53:01 -05:00
case TransactionType::RECONCILIATION:
2021-12-18 05:35:17 -06:00
$result = $this->validateReconciliationDestination($array);
2019-06-22 22:53:01 -05:00
break;
}
return $result;
}
/**
2021-12-18 05:35:17 -06:00
* @param array $array
*
* @return bool
*/
2021-12-18 05:35:17 -06:00
public function validateSource(array $array): bool
{
2021-12-18 05:35:17 -06:00
Log::debug('Now in AccountValidator::validateSource()', $array);
switch ($this->transactionType) {
default:
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);
break;
case TransactionType::WITHDRAWAL:
2021-12-18 05:35:17 -06:00
$result = $this->validateWithdrawalSource($array);
break;
case TransactionType::DEPOSIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateDepositSource($array);
break;
case TransactionType::TRANSFER:
2021-12-18 05:35:17 -06:00
$result = $this->validateTransferSource($array);
break;
case TransactionType::OPENING_BALANCE:
2021-12-18 05:35:17 -06:00
$result = $this->validateOBSource($array);
break;
2021-04-10 10:26:00 -05:00
case TransactionType::LIABILITY_CREDIT:
2021-12-18 05:35:17 -06:00
$result = $this->validateLCSource($array);
2021-04-10 10:26:00 -05:00
break;
2019-06-22 22:53:01 -05:00
case TransactionType::RECONCILIATION:
2019-12-20 00:15:40 -06:00
Log::debug('Calling validateReconciliationSource');
2021-12-18 05:35:17 -06:00
$result = $this->validateReconciliationSource($array);
2019-06-22 22:53:01 -05:00
break;
}
return $result;
}
/**
* @param array $accountTypes
*
* @return bool
*/
2020-03-21 02:11:14 -05:00
protected function canCreateTypes(array $accountTypes): bool
{
2019-06-07 23:19:21 -05:00
Log::debug('Can we create any of these types?', $accountTypes);
/** @var string $accountType */
foreach ($accountTypes as $accountType) {
if ($this->canCreateType($accountType)) {
2019-06-07 23:19:21 -05:00
Log::debug(sprintf('YES, we can create a %s', $accountType));
return true;
}
}
2019-06-07 23:19:21 -05:00
Log::debug('NO, we cant create any of those.');
return false;
}
2022-03-29 08:00:29 -05:00
/**
* @param string $accountType
*
* @return bool
*/
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;
}
/**
2021-12-18 05:35:17 -06:00
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
2021-12-18 05:35:17 -06:00
protected function findExistingAccount(array $validTypes, array $data): ?Account
{
2021-12-18 05:35:17 -06:00
Log::debug('Now in findExistingAccount', $data);
$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) {
2021-06-29 23:17:38 -05:00
$first = $this->accountRepository->find($accountId);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
return $first;
}
}
2021-12-18 05:35:17 -06:00
// find by iban
2022-01-07 12:24:14 -06:00
if (null !== $accountIban && '' !== (string) $accountIban) {
2021-12-18 05:35:17 -06:00
$first = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
return $first;
}
}
// find by number
2022-01-07 12:24:14 -06:00
if (null !== $accountNumber && '' !== (string) $accountNumber) {
2021-12-18 05:35:17 -06:00
$first = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
return $first;
}
}
// find by name:
2022-01-07 12:24:14 -06:00
if ('' !== (string) $accountName) {
return $this->accountRepository->findByName($accountName, $validTypes);
}
return null;
}
2019-08-17 05:09:03 -05:00
}