Files
firefly-iii/app/Rules/UniqueIban.php

171 lines
5.5 KiB
PHP
Raw Normal View History

<?php
/**
* UniqueIban.php
2020-02-16 13:56:25 +01: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\Rules;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Models\Account;
2024-05-18 06:42:09 +02:00
use FireflyIII\Support\Facades\Steam;
2023-04-28 09:06:05 +02:00
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Class UniqueIban
*/
2023-04-28 09:06:05 +02:00
class UniqueIban implements ValidationRule
{
2021-01-26 19:27:49 +01:00
private ?Account $account;
2023-04-28 09:27:14 +02:00
private array $expectedTypes;
2018-03-19 08:16:54 +01:00
/**
* Create a new rule instance.
*/
2018-03-19 08:16:54 +01:00
public function __construct(?Account $account, ?string $expectedType)
{
2023-04-28 09:27:14 +02:00
$this->account = $account;
$this->expectedTypes = [];
if (null === $expectedType) {
2023-04-28 09:06:05 +02:00
return;
}
$this->expectedTypes = [$expectedType];
2021-01-26 19:27:49 +01:00
// a very basic fix to make sure we get the correct account type:
if ('expense' === $expectedType) {
2025-01-03 09:15:52 +01:00
$this->expectedTypes = [AccountTypeEnum::EXPENSE->value];
2021-01-26 19:27:49 +01:00
}
if ('revenue' === $expectedType) {
2025-01-03 09:15:52 +01:00
$this->expectedTypes = [AccountTypeEnum::REVENUE->value];
2021-01-26 19:27:49 +01:00
}
if ('asset' === $expectedType) {
2025-01-03 09:15:52 +01:00
$this->expectedTypes = [AccountTypeEnum::ASSET->value];
2023-04-28 09:06:05 +02:00
}
if ('liabilities' === $expectedType) {
2025-01-03 09:15:52 +01:00
$this->expectedTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
2021-01-26 19:27:49 +01:00
}
}
/**
* Get the validation error message.
*/
2018-07-26 06:10:17 +02:00
public function message(): string
{
2024-12-22 08:43:12 +01:00
return (string) trans('validation.unique_iban_for_user');
}
2023-12-20 19:35:52 +01:00
public function validate(string $attribute, mixed $value, \Closure $fail): void
2023-06-21 12:34:58 +02:00
{
if (!$this->passes($attribute, $value)) {
2024-12-22 08:43:12 +01:00
$fail((string) trans('validation.unique_iban_for_user'));
2023-06-21 12:34:58 +02:00
}
}
/**
* Determine if the validation rule passes.
*
2023-06-21 12:34:58 +02:00
* @param string $attribute
* @param mixed $value
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
*/
2018-07-26 06:10:17 +02:00
public function passes($attribute, $value): bool
{
if (!auth()->check()) {
return true;
}
2023-04-28 09:06:05 +02:00
if (0 === count($this->expectedTypes)) {
return true;
2018-03-19 08:16:54 +01:00
}
if (is_array($value)) {
2025-01-26 06:30:38 +01:00
return false;
}
$value = (string) $value;
2018-07-26 06:10:17 +02:00
$maxCounts = $this->getMaxOccurrences();
2018-03-19 08:16:54 +01:00
foreach ($maxCounts as $type => $max) {
2024-05-18 06:42:09 +02:00
// make sure to trim the value of $value so all spaces are removed.
$value = Steam::filterSpaces($value);
2018-07-26 06:10:17 +02:00
$count = $this->countHits($type, $value);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
2018-03-19 08:16:54 +01:00
if ($count > $max) {
2023-10-29 06:33:43 +01:00
app('log')->debug(
2018-03-19 08:16:54 +01:00
sprintf(
2023-04-28 09:06:05 +02:00
'IBAN "%s" is in use with %d account(s) of type "%s", which is too much for expected types "%s"',
2022-10-30 14:24:37 +01:00
$value,
$count,
$type,
2023-11-04 14:18:49 +01:00
implode(', ', $this->expectedTypes)
2018-03-19 08:16:54 +01:00
)
);
return false;
}
}
return true;
}
2018-07-26 06:10:17 +02:00
2023-06-21 12:34:58 +02:00
private function getMaxOccurrences(): array
2018-07-26 06:10:17 +02:00
{
2023-06-21 12:34:58 +02:00
$maxCounts = [
2025-01-03 09:15:52 +01:00
AccountTypeEnum::ASSET->value => 0,
AccountTypeEnum::EXPENSE->value => 0,
AccountTypeEnum::REVENUE->value => 0,
'liabilities' => 0,
2023-06-21 12:34:58 +02:00
];
2025-01-03 09:15:52 +01:00
if (in_array('expense', $this->expectedTypes, true) || in_array(AccountTypeEnum::EXPENSE->value, $this->expectedTypes, true)) {
2023-06-21 12:34:58 +02:00
// IBAN should be unique amongst expense and asset accounts.
// may appear once in revenue accounts
2025-01-03 09:15:52 +01:00
$maxCounts[AccountTypeEnum::REVENUE->value] = 1;
2023-06-21 12:34:58 +02:00
}
2025-01-03 09:15:52 +01:00
if (in_array('revenue', $this->expectedTypes, true) || in_array(AccountTypeEnum::REVENUE->value, $this->expectedTypes, true)) {
2023-06-21 12:34:58 +02:00
// IBAN should be unique amongst revenue and asset accounts.
// may appear once in expense accounts
2025-01-03 09:15:52 +01:00
$maxCounts[AccountTypeEnum::EXPENSE->value] = 1;
2018-07-26 06:10:17 +02:00
}
2023-06-21 12:34:58 +02:00
return $maxCounts;
2018-07-26 06:10:17 +02:00
}
2021-03-21 09:15:40 +01:00
private function countHits(string $type, string $iban): int
{
2023-04-28 09:06:05 +02:00
$typesArray = [$type];
2023-04-28 09:27:14 +02:00
if ('liabilities' === $type) {
2025-01-03 09:15:52 +01:00
$typesArray = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
2023-04-28 09:06:05 +02:00
}
2021-03-21 09:15:40 +01:00
$query
= auth()->user()
->accounts()
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.iban', $iban)
->whereIn('account_types.type', $typesArray)
2023-12-20 19:35:52 +01:00
;
2021-03-21 09:15:40 +01:00
if (null !== $this->account) {
$query->where('accounts.id', '!=', $this->account->id);
}
return $query->count();
}
2018-03-05 19:35:58 +01:00
}