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

142 lines
4.7 KiB
PHP
Raw Normal View History

2021-01-26 19:27:49 +01:00
<?php
/**
* UniqueAccountNumber.php
* Copyright (c) 2021 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;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
2023-10-29 17:41:14 +01:00
use Illuminate\Contracts\Validation\ValidationRule;
2021-01-26 19:27:49 +01:00
/**
* Class UniqueAccountNumber
*/
2023-10-29 17:41:14 +01:00
class UniqueAccountNumber implements ValidationRule
2021-01-26 19:27:49 +01:00
{
2024-01-05 14:13:44 +01:00
private ?Account $account;
2024-01-05 14:29:59 +01:00
private ?string $expectedType;
2021-01-26 19:27:49 +01:00
/**
* Create a new rule instance.
*/
public function __construct(?Account $account, ?string $expectedType)
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Constructed UniqueAccountNumber');
2021-01-26 19:27:49 +01:00
$this->account = $account;
$this->expectedType = $expectedType;
// a very basic fix to make sure we get the correct account type:
if ('expense' === $expectedType) {
$this->expectedType = AccountType::EXPENSE;
}
if ('revenue' === $expectedType) {
$this->expectedType = AccountType::REVENUE;
}
if ('asset' === $expectedType) {
$this->expectedType = AccountType::ASSET;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Expected type is "%s"', $this->expectedType));
2021-01-26 19:27:49 +01:00
}
/**
* Get the validation error message.
*/
public function message(): string
{
2022-12-29 19:42:26 +01:00
return (string)trans('validation.unique_account_number_for_user');
2021-01-26 19:27:49 +01:00
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2021-01-26 19:27:49 +01:00
*/
2023-12-20 19:35:52 +01:00
public function validate(string $attribute, mixed $value, \Closure $fail): void
2021-01-26 19:27:49 +01:00
{
if (!auth()->check()) {
2023-10-29 17:41:14 +01:00
return;
2021-01-26 19:27:49 +01:00
}
if (null === $this->expectedType) {
2023-10-29 17:41:14 +01:00
return;
2021-01-26 19:27:49 +01:00
}
$maxCounts = $this->getMaxOccurrences();
foreach ($maxCounts as $type => $max) {
$count = $this->countHits($type, $value);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Count for "%s" and account number "%s" is %d', $type, $value, $count));
2021-01-26 19:27:49 +01:00
if ($count > $max) {
2023-10-29 06:33:43 +01:00
app('log')->debug(
2021-01-26 19:27:49 +01:00
sprintf(
'account number "%s" is in use with %d account(s) of type "%s", which is too much for expected type "%s"',
2022-10-30 14:24:37 +01:00
$value,
$count,
$type,
$this->expectedType
2021-01-26 19:27:49 +01:00
)
);
2023-10-29 17:41:14 +01:00
$fail('validation.unique_account_number_for_user')->translate();
2023-12-20 19:35:52 +01:00
2023-10-29 17:41:14 +01:00
return;
2021-01-26 19:27:49 +01:00
}
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Account number is valid.');
2021-01-26 19:27:49 +01:00
}
private function getMaxOccurrences(): array
{
$maxCounts = [
AccountType::ASSET => 0,
AccountType::EXPENSE => 0,
AccountType::REVENUE => 0,
];
if ('expense' === $this->expectedType || AccountType::EXPENSE === $this->expectedType) {
// IBAN should be unique amongst expense and asset accounts.
// may appear once in revenue accounts
$maxCounts[AccountType::REVENUE] = 1;
}
if ('revenue' === $this->expectedType || AccountType::REVENUE === $this->expectedType) {
// IBAN should be unique amongst revenue and asset accounts.
// may appear once in expense accounts
$maxCounts[AccountType::EXPENSE] = 1;
}
return $maxCounts;
}
2023-06-21 12:34:58 +02:00
private function countHits(string $type, string $accountNumber): int
{
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
2023-12-20 19:35:52 +01:00
->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('accounts.user_id', auth()->user()->id)
->where('account_types.type', $type)
->where('account_meta.name', '=', 'account_number')
->where('account_meta.data', json_encode($accountNumber))
;
2023-06-21 12:34:58 +02:00
if (null !== $this->account) {
$query->where('accounts.id', '!=', $this->account->id);
}
return $query->count();
}
2021-01-26 19:27:49 +01:00
}