Files
firefly-iii/app/Validation/Account/DepositValidation.php

197 lines
8.3 KiB
PHP
Raw Normal View History

2020-03-21 08:11:14 +01:00
<?php
/**
* DepositValidation.php
* Copyright (c) 2020 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\Account;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
/**
* Trait DepositValidation
*/
trait DepositValidation
{
2023-05-29 13:56:55 +02:00
/**
2023-06-21 12:34:58 +02:00
* @param array $array
2020-03-21 08:11:14 +01:00
*
* @return bool
*/
2021-12-18 12:35:17 +01:00
protected function validateDepositDestination(array $array): bool
2020-03-21 08:11:14 +01:00
{
2021-12-18 12:35:17 +01:00
$result = null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
2023-06-12 20:24:45 +02:00
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
2021-12-18 12:35:17 +01:00
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateDepositDestination', $array);
2020-03-21 08:11:14 +01:00
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
2023-06-12 20:24:45 +02:00
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
2020-03-21 08:11:14 +01:00
// if both values are NULL we return false,
// because the destination of a deposit can't be created.
2022-12-29 19:42:40 +01:00
$this->destError = (string)trans('validation.deposit_dest_need_data');
2023-10-29 06:32:00 +01:00
app('log')->error('Both values are NULL, cant create deposit destination.');
2020-03-21 08:11:14 +01:00
$result = false;
}
// if the account can be created anyway we don't need to search.
if (null === $result && true === $this->canCreateTypes($validTypes)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Can create some of these types, so return true.');
2020-03-21 08:11:14 +01:00
$result = true;
}
if (null === $result) {
// otherwise try to find the account:
2021-12-18 12:35:17 +01:00
$search = $this->findExistingAccount($validTypes, $array);
2020-03-21 08:11:14 +01:00
if (null === $search) {
2023-10-29 06:33:43 +01:00
app('log')->debug('findExistingAccount() returned NULL, so the result is false.');
2022-12-29 19:42:40 +01:00
$this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
2020-03-21 08:11:14 +01:00
$result = false;
}
if (null !== $search) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name));
2023-03-11 07:09:27 +01:00
$this->setDestination($search);
2023-05-29 13:56:55 +02:00
$result = true;
2020-03-21 08:11:14 +01:00
}
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('validateDepositDestination will return %s', var_export($result, true)));
2020-03-21 08:11:14 +01:00
return $result;
}
2022-12-29 19:42:40 +01:00
/**
2023-06-21 12:34:58 +02:00
* @param array $accountTypes
*
* @return bool
*/
abstract protected function canCreateTypes(array $accountTypes): bool;
/**
* @param array $validTypes
* @param array $data
*
* @return Account|null
*/
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
/**
* @param array $array
2020-03-21 08:11:14 +01:00
*
* @return bool
*/
2021-12-18 12:35:17 +01:00
protected function validateDepositSource(array $array): bool
2020-03-21 08:11:14 +01:00
{
2021-12-18 12:35:17 +01:00
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateDepositSource', $array);
2023-03-11 07:09:27 +01:00
// null = we found nothing at all or didn't even search
// false = invalid results
2020-03-21 08:11:14 +01:00
$result = null;
2020-03-21 08:11:14 +01:00
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
if (null === $accountId &&
null === $accountName &&
null === $accountIban &&
null === $accountNumber &&
false === $this->canCreateTypes($validTypes)) {
2020-03-21 08:11:14 +01:00
// if both values are NULL return false,
// because the source of a deposit can't be created.
// (this never happens).
2022-12-29 19:42:40 +01:00
$this->sourceError = (string)trans('validation.deposit_source_need_data');
2020-03-21 08:11:14 +01:00
$result = false;
}
// if there is an iban, it can only be in use by a valid source type, or we will fail.
2023-07-15 16:02:42 +02:00
if (null !== $accountIban && '' !== $accountIban) {
app('log')->debug('Check if there is not already another account with this IBAN');
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
2023-07-15 16:02:42 +02:00
if (null !== $existing) {
$this->sourceError = (string)trans('validation.deposit_src_iban_exists');
2023-06-23 11:07:50 +02:00
return false;
}
}
2021-12-18 12:35:17 +01:00
// if the user submits an ID, but that ID is not of the correct type,
2020-03-21 08:11:14 +01:00
// return false.
2021-12-18 12:35:17 +01:00
if (null !== $accountId) {
$search = $this->getRepository()->find($accountId);
2020-03-21 08:11:14 +01:00
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type));
app('log')->debug(sprintf('Firefly III accepts ID #%d as valid account data.', $accountId));
2021-12-18 12:35:17 +01:00
}
2023-03-11 07:09:27 +01:00
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('ID result is not null and seems valid, save as source account.');
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-05-29 13:56:55 +02:00
$result = true;
2023-03-11 07:09:27 +01:00
}
2021-12-18 12:35:17 +01:00
}
// if user submits an IBAN:
if (null !== $accountIban) {
$search = $this->getRepository()->findByIbanNull($accountIban, $validTypes);
2021-12-18 12:35:17 +01:00
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type));
2021-12-18 12:35:17 +01:00
$result = false;
}
2023-03-11 07:09:27 +01:00
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('IBAN result is not null and seems valid, save as source account.');
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-05-29 13:56:55 +02:00
$result = true;
2023-03-11 07:09:27 +01:00
}
2021-12-18 12:35:17 +01:00
}
// if user submits a number:
2023-03-11 07:09:27 +01:00
if (null !== $accountNumber && '' !== $accountNumber) {
$search = $this->getRepository()->findByAccountNumber($accountNumber, $validTypes);
2021-12-18 12:35:17 +01:00
if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug(
2021-12-18 12:35:17 +01:00
sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type)
);
2020-03-21 08:11:14 +01:00
$result = false;
}
2023-03-11 07:09:27 +01:00
if (null !== $search && in_array($search->accountType->type, $validTypes, true)) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Number result is not null and seems valid, save as source account.');
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-05-29 13:56:55 +02:00
$result = true;
2023-03-11 07:09:27 +01:00
}
2020-03-21 08:11:14 +01:00
}
// if the account can be created anyway we don't need to search.
if (null === $result && true === $this->canCreateTypes($validTypes)) {
$result = true;
// set the source to be a (dummy) revenue account.
2022-10-30 14:24:37 +01:00
$account = new Account();
2020-03-21 08:11:14 +01:00
$accountType = AccountType::whereType(AccountType::REVENUE)->first();
$account->accountType = $accountType;
2023-03-11 07:09:27 +01:00
$this->setSource($account);
2020-03-21 08:11:14 +01:00
}
2021-03-21 09:15:40 +01:00
2020-10-23 19:11:25 +02:00
return $result ?? false;
2020-03-21 08:11:14 +01:00
}
}