2020-03-21 08:11:14 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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;
|
2021-03-10 06:34:03 +01:00
|
|
|
use FireflyIII\Models\AccountType;
|
2020-03-21 08:11:14 +01:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait WithdrawalValidation
|
|
|
|
*/
|
|
|
|
trait WithdrawalValidation
|
|
|
|
{
|
2021-09-18 10:26:12 +02:00
|
|
|
/**
|
|
|
|
* @param array $accountTypes
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
abstract protected function canCreateTypes(array $accountTypes): bool;
|
|
|
|
|
|
|
|
/**
|
2022-03-29 15:00:29 +02:00
|
|
|
* @param array $validTypes
|
2021-12-18 12:35:17 +01:00
|
|
|
* @param array $data
|
2021-09-18 10:26:12 +02:00
|
|
|
*
|
|
|
|
* @return Account|null
|
|
|
|
*/
|
2021-12-18 12:35:17 +01:00
|
|
|
abstract protected function findExistingAccount(array $validTypes, array $data): ?Account;
|
2021-09-18 10:26:12 +02:00
|
|
|
|
2020-03-21 08:11:14 +01:00
|
|
|
/**
|
2021-12-18 12:35:17 +01:00
|
|
|
* @param array $array
|
2020-03-21 08:11:14 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
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;
|
|
|
|
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];
|
|
|
|
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
|
|
|
// if both values are NULL we return TRUE
|
|
|
|
// because we assume the user doesnt want to submit / change anything.
|
2022-03-29 15:00:29 +02: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:
|
2021-12-18 12:35:17 +01:00
|
|
|
$search = $this->findExistingAccount($validTypes, $array);
|
2021-03-21 09:15:40 +01:00
|
|
|
if (null === $search) {
|
2022-03-29 15:00:29 +02: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;
|
|
|
|
}
|
|
|
|
$this->source = $search;
|
|
|
|
Log::debug('Valid source account!');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-21 08:11:14 +01:00
|
|
|
|
|
|
|
/**
|
2021-12-18 12:35:17 +01:00
|
|
|
* @param array $array
|
2020-03-21 08:11:14 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-12-18 12:35:17 +01:00
|
|
|
protected function validateWithdrawalDestination(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;
|
|
|
|
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] ?? [];
|
|
|
|
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
|
|
|
// if both values are NULL return false,
|
|
|
|
// because the destination of a withdrawal can never be created automatically.
|
2022-03-29 15:00:29 +02: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) {
|
2021-06-30 06:17:38 +02:00
|
|
|
$found = $this->accountRepository->find($accountId);
|
2020-03-21 08:11:14 +01:00
|
|
|
if (null !== $found) {
|
|
|
|
$type = $found->accountType->type;
|
|
|
|
if (in_array($type, $validTypes, true)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-29 15:00:29 +02: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 the account can be created anyway don't need to search.
|
|
|
|
return true === $this->canCreateTypes($validTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-18 12:35:17 +01:00
|
|
|
* @param array $array
|
2020-03-21 08:11:14 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-12-18 12:35:17 +01:00
|
|
|
protected function validateWithdrawalSource(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;
|
|
|
|
|
|
|
|
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]);
|
|
|
|
if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) {
|
|
|
|
// if both values are NULL we return false,
|
|
|
|
// because the source of a withdrawal can't be created.
|
2022-03-29 15:00:29 +02: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:
|
2021-12-18 12:35:17 +01:00
|
|
|
$search = $this->findExistingAccount($validTypes, $array);
|
2020-03-21 08:11:14 +01:00
|
|
|
if (null === $search) {
|
2022-03-29 15:00:29 +02: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;
|
|
|
|
}
|
|
|
|
$this->source = $search;
|
|
|
|
Log::debug('Valid source account!');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-30 07:33:06 +02:00
|
|
|
}
|