2018-02-22 13:07:14 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* UniqueIban.php
|
2020-02-16 06:56:25 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-22 13:07:14 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-22 13:07:14 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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.
|
2018-02-22 13:07:14 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-22 13:07:14 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-22 13:07:14 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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/>.
|
2018-02-22 13:07:14 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Rules;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Account;
|
2018-03-19 02:16:54 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2023-04-28 02:06:05 -05:00
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2018-02-22 13:07:14 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class UniqueIban
|
|
|
|
*/
|
2023-04-28 02:06:05 -05:00
|
|
|
class UniqueIban implements ValidationRule
|
2018-02-22 13:07:14 -06:00
|
|
|
{
|
2021-01-26 12:27:49 -06:00
|
|
|
private ?Account $account;
|
2023-04-28 02:27:14 -05:00
|
|
|
private array $expectedTypes;
|
2018-03-19 02:16:54 -05:00
|
|
|
|
2018-02-22 13:07:14 -06:00
|
|
|
/**
|
|
|
|
* Create a new rule instance.
|
|
|
|
*/
|
2018-03-19 02:16:54 -05:00
|
|
|
public function __construct(?Account $account, ?string $expectedType)
|
2018-02-22 13:07:14 -06:00
|
|
|
{
|
2023-04-28 02:27:14 -05:00
|
|
|
$this->account = $account;
|
|
|
|
$this->expectedTypes = [];
|
|
|
|
if (null === $expectedType) {
|
2023-04-28 02:06:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->expectedTypes = [$expectedType];
|
2021-01-26 12:27:49 -06:00
|
|
|
// a very basic fix to make sure we get the correct account type:
|
|
|
|
if ('expense' === $expectedType) {
|
2023-04-28 02:06:05 -05:00
|
|
|
$this->expectedTypes = [AccountType::EXPENSE];
|
2021-01-26 12:27:49 -06:00
|
|
|
}
|
|
|
|
if ('revenue' === $expectedType) {
|
2023-04-28 02:06:05 -05:00
|
|
|
$this->expectedTypes = [AccountType::REVENUE];
|
2021-01-26 12:27:49 -06:00
|
|
|
}
|
|
|
|
if ('asset' === $expectedType) {
|
2023-04-28 02:06:05 -05:00
|
|
|
$this->expectedTypes = [AccountType::ASSET];
|
|
|
|
}
|
|
|
|
if ('liabilities' === $expectedType) {
|
|
|
|
$this->expectedTypes = [AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
2021-01-26 12:27:49 -06:00
|
|
|
}
|
2018-02-22 13:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation error message.
|
|
|
|
*/
|
2018-07-25 23:10:17 -05:00
|
|
|
public function message(): string
|
2018-02-22 13:07:14 -06:00
|
|
|
{
|
2022-12-29 12:42:26 -06:00
|
|
|
return (string)trans('validation.unique_iban_for_user');
|
2018-02-22 13:07:14 -06:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:35:52 -06:00
|
|
|
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
2023-06-21 05:34:58 -05:00
|
|
|
{
|
|
|
|
if (!$this->passes($attribute, $value)) {
|
|
|
|
$fail((string)trans('validation.unique_iban_for_user'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:07:14 -06:00
|
|
|
/**
|
|
|
|
* Determine if the validation rule passes.
|
|
|
|
*
|
2023-06-21 05:34:58 -05:00
|
|
|
* @param string $attribute
|
|
|
|
* @param mixed $value
|
2018-02-22 13:07:14 -06:00
|
|
|
*
|
2023-11-30 10:28:44 -06:00
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
2018-02-22 13:07:14 -06:00
|
|
|
*/
|
2018-07-25 23:10:17 -05:00
|
|
|
public function passes($attribute, $value): bool
|
2018-02-22 13:07:14 -06:00
|
|
|
{
|
|
|
|
if (!auth()->check()) {
|
2021-09-18 03:26:12 -05:00
|
|
|
return true;
|
2018-02-22 13:07:14 -06:00
|
|
|
}
|
2023-04-28 02:06:05 -05:00
|
|
|
if (0 === count($this->expectedTypes)) {
|
2021-09-18 03:26:12 -05:00
|
|
|
return true;
|
2018-03-19 02:16:54 -05:00
|
|
|
}
|
2018-07-25 23:10:17 -05:00
|
|
|
$maxCounts = $this->getMaxOccurrences();
|
2018-02-22 13:07:14 -06:00
|
|
|
|
2018-03-19 02:16:54 -05:00
|
|
|
foreach ($maxCounts as $type => $max) {
|
2018-07-25 23:10:17 -05:00
|
|
|
$count = $this->countHits($type, $value);
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(sprintf('Count for "%s" and IBAN "%s" is %d', $type, $value, $count));
|
2018-03-19 02:16:54 -05:00
|
|
|
if ($count > $max) {
|
2023-10-29 00:33:43 -05:00
|
|
|
app('log')->debug(
|
2018-03-19 02:16:54 -05:00
|
|
|
sprintf(
|
2023-04-28 02:06:05 -05:00
|
|
|
'IBAN "%s" is in use with %d account(s) of type "%s", which is too much for expected types "%s"',
|
2022-10-30 08:24:37 -05:00
|
|
|
$value,
|
|
|
|
$count,
|
|
|
|
$type,
|
2023-11-04 08:18:49 -05:00
|
|
|
implode(', ', $this->expectedTypes)
|
2018-03-19 02:16:54 -05:00
|
|
|
)
|
|
|
|
);
|
2018-02-22 13:07:14 -06:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-25 23:10:17 -05:00
|
|
|
|
2023-06-21 05:34:58 -05:00
|
|
|
private function getMaxOccurrences(): array
|
2018-07-25 23:10:17 -05:00
|
|
|
{
|
2023-06-21 05:34:58 -05:00
|
|
|
$maxCounts = [
|
|
|
|
AccountType::ASSET => 0,
|
|
|
|
AccountType::EXPENSE => 0,
|
|
|
|
AccountType::REVENUE => 0,
|
|
|
|
'liabilities' => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (in_array('expense', $this->expectedTypes, true) || in_array(AccountType::EXPENSE, $this->expectedTypes, true)) {
|
|
|
|
// IBAN should be unique amongst expense and asset accounts.
|
|
|
|
// may appear once in revenue accounts
|
|
|
|
$maxCounts[AccountType::REVENUE] = 1;
|
|
|
|
}
|
|
|
|
if (in_array('revenue', $this->expectedTypes, true) || in_array(AccountType::REVENUE, $this->expectedTypes, true)) {
|
|
|
|
// IBAN should be unique amongst revenue and asset accounts.
|
|
|
|
// may appear once in expense accounts
|
|
|
|
$maxCounts[AccountType::EXPENSE] = 1;
|
2018-07-25 23:10:17 -05:00
|
|
|
}
|
2023-06-21 05:34:58 -05:00
|
|
|
|
|
|
|
return $maxCounts;
|
2018-07-25 23:10:17 -05:00
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
|
|
|
|
private function countHits(string $type, string $iban): int
|
|
|
|
{
|
2023-04-28 02:06:05 -05:00
|
|
|
$typesArray = [$type];
|
2023-04-28 02:27:14 -05:00
|
|
|
if ('liabilities' === $type) {
|
2023-04-28 02:06:05 -05:00
|
|
|
$typesArray = [AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE];
|
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
$query
|
2024-01-01 07:43:56 -06:00
|
|
|
= auth()->user()
|
2024-03-06 18:29:08 -06:00
|
|
|
->accounts()
|
|
|
|
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
|
|
|
|
->where('accounts.iban', $iban)
|
|
|
|
->whereIn('account_types.type', $typesArray)
|
2023-12-20 12:35:52 -06:00
|
|
|
;
|
2021-03-21 03:15:40 -05:00
|
|
|
|
|
|
|
if (null !== $this->account) {
|
|
|
|
$query->where('accounts.id', '!=', $this->account->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->count();
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|