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

154 lines
6.9 KiB
PHP
Raw Normal View History

2020-03-21 08:11:14 +01:00
<?php
2020-03-21 08:11:14 +01:00
/**
* WithdrawalValidation.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;
2020-03-21 08:11:14 +01:00
/**
* Trait WithdrawalValidation
*/
trait WithdrawalValidation
{
2021-12-18 12:35:17 +01:00
protected function validateGenericSource(array $array): bool
2021-03-21 09:15:40 +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;
2023-06-12 20:24:45 +02:00
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateGenericSource', $array);
2021-03-21 09:15:40 +01:00
// source can be any of the following types.
$validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
2023-06-12 20:24:45 +02:00
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
2021-03-21 09:15:40 +01:00
// if both values are NULL we return TRUE
2023-06-12 20:24:45 +02:00
// because we assume the user doesn't want to submit / change anything.
2022-12-29 19:42:40 +01:00
$this->sourceError = (string)trans('validation.withdrawal_source_need_data');
2022-10-30 14:44:49 +01:00
app('log')->warning('[a] Not a valid source. Need more data.');
2020-03-21 08:11:14 +01:00
2021-03-21 09:15:40 +01:00
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, $array);
2021-03-21 09:15:40 +01:00
if (null === $search) {
2022-12-29 19:42:40 +01:00
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
2022-10-30 14:44:49 +01:00
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
2021-03-21 09:15:40 +01:00
return false;
}
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-10-29 06:33:43 +01:00
app('log')->debug('Valid source account!');
2021-03-21 09:15:40 +01:00
return true;
}
2020-03-21 08:11:14 +01:00
2023-06-21 12:34:58 +02:00
abstract protected function canCreateTypes(array $accountTypes): bool;
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
2021-12-18 12:35:17 +01:00
protected function validateWithdrawalDestination(array $array): bool
2020-03-21 08:11:14 +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 validateWithdrawalDestination()', $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] ?? [];
app('log')->debug('Source type can be: ', $validTypes);
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 destination of a withdrawal can never be created automatically.
2022-12-29 19:42:40 +01:00
$this->destError = (string)trans('validation.withdrawal_dest_need_data');
2020-03-21 08:11:14 +01:00
return false;
}
// if there's an ID it must be of the "validTypes".
if (null !== $accountId && 0 !== $accountId) {
$found = $this->getRepository()->find($accountId);
2020-03-21 08:11:14 +01:00
if (null !== $found) {
$type = $found->accountType->type;
2020-03-21 08:11:14 +01:00
if (in_array($type, $validTypes, true)) {
2023-03-11 07:09:27 +01:00
$this->setDestination($found);
2023-12-20 19:35:52 +01:00
2020-03-21 08:11:14 +01:00
return true;
}
// todo explain error in log message.
2022-12-29 19:42:40 +01:00
$this->destError = (string)trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
2020-03-21 08:11:14 +01:00
return false;
}
}
// if there is an iban, it can only be in use by a valid destination type, or we will fail.
// the inverse of $validTypes is
2023-07-15 16:02:42 +02:00
if (null !== $accountIban && '' !== $accountIban) {
2023-06-23 10:57:26 +02:00
app('log')->debug('Check if there is not already an account with this IBAN');
// the inverse flag reverses the search, searching for everything that is NOT a valid type.
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
2023-07-15 16:02:42 +02:00
if (null !== $existing) {
2023-06-23 10:57:26 +02:00
$this->destError = (string)trans('validation.withdrawal_dest_iban_exists');
2023-12-20 19:35:52 +01:00
2023-06-23 10:57:26 +02:00
return false;
}
}
2020-03-21 08:11:14 +01:00
// if the account can be created anyway don't need to search.
return true === $this->canCreateTypes($validTypes);
}
2021-12-18 12:35:17 +01:00
protected function validateWithdrawalSource(array $array): bool
2020-03-21 08:11:14 +01:00
{
2023-05-29 13:56:55 +02: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;
2021-12-18 12:35:17 +01:00
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in validateWithdrawalSource', $array);
2020-03-21 08:11:14 +01:00
// source can be any of the following types.
$validTypes = array_keys($this->combinations[$this->transactionType]);
2023-05-06 15:59:31 +02:00
if (null === $accountId && null === $accountName && null === $accountNumber && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
2020-03-21 08:11:14 +01:00
// if both values are NULL we return false,
// because the source of a withdrawal can't be created.
2022-12-29 19:42:40 +01:00
$this->sourceError = (string)trans('validation.withdrawal_source_need_data');
2022-10-30 14:44:49 +01:00
app('log')->warning('[b] Not a valid source. Need more data.');
2020-03-21 08:11:14 +01:00
return false;
}
// otherwise try to find the account:
$search = $this->findExistingAccount($validTypes, $array);
2020-03-21 08:11:14 +01:00
if (null === $search) {
2022-12-29 19:42:40 +01:00
$this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]);
2022-10-30 14:44:49 +01:00
app('log')->warning('Not a valid source. Cant find it.', $validTypes);
2020-03-21 08:11:14 +01:00
return false;
}
2023-03-11 07:09:27 +01:00
$this->setSource($search);
2023-10-29 06:33:43 +01:00
app('log')->debug('Valid source account!');
2020-03-21 08:11:14 +01:00
return true;
}
}