mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-14 00:04:24 +00:00
Accounts could not share number. Fix #6165
This commit is contained in:
@@ -589,23 +589,41 @@ class FireflyValidator extends Validator
|
|||||||
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
|
||||||
->whereNull('accounts.deleted_at')
|
->whereNull('accounts.deleted_at')
|
||||||
->where('accounts.user_id', auth()->user()->id)
|
->where('accounts.user_id', auth()->user()->id)
|
||||||
->where('account_meta.name', 'account_number');
|
->where('account_meta.name', 'account_number')
|
||||||
|
->where('account_meta.data', json_encode($value));
|
||||||
|
|
||||||
if ($accountId > 0) {
|
if ($accountId > 0) {
|
||||||
// exclude current account from check.
|
// exclude current account from check.
|
||||||
$query->where('account_meta.account_id', '!=', $accountId);
|
$query->where('account_meta.account_id', '!=', $accountId);
|
||||||
}
|
}
|
||||||
$set = $query->get(['account_meta.*']);
|
$set = $query->get(['account_meta.*']);
|
||||||
|
$count = $set->count();
|
||||||
/** @var AccountMeta $entry */
|
if (0 === $count) {
|
||||||
foreach ($set as $entry) {
|
return true;
|
||||||
if ($entry->data === $value) {
|
}
|
||||||
|
if ($count > 1) {
|
||||||
|
// pretty much impossible but still.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
$type = $this->data['objectType'] ?? 'unknown';
|
||||||
|
if ('expense' !== $type && 'revenue' !== $type) {
|
||||||
|
Log::warning(sprintf('Account number "%s" is not unique and account type "%s" cannot share its account number.', $value, $type));
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
Log::debug(sprintf('Account number "%s" is not unique but account type "%s" may share its account number.', $value, $type));
|
||||||
|
// one other account with this account number.
|
||||||
|
/** @var AccountMeta $entry */
|
||||||
|
foreach ($set as $entry) {
|
||||||
|
$otherAccount = $entry->account;
|
||||||
|
$otherType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $otherAccount->accountType->type));
|
||||||
|
if (('expense' === $otherType || 'revenue' === $otherType) && $otherType !== $type) {
|
||||||
|
Log::debug(sprintf('The other account with this account number is a "%s" so return true.', $otherType));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Log::debug(sprintf('The other account with this account number is a "%s" so return false.', $otherType));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $attribute
|
* @param $attribute
|
||||||
|
Reference in New Issue
Block a user