James Cole
2023-09-12 05:58:39 +02:00
parent 477eebdbe7
commit 19fee6a8fb
3 changed files with 25 additions and 12 deletions

View File

@@ -126,10 +126,10 @@ trait DepositValidation
$result = false;
}
// if there is an iban, it can only be in use by a revenue account, or we will fail.
// if there is an iban, it can only be in use by a valid source type, or we will fail.
if (null !== $accountIban && '' !== $accountIban) {
app('log')->debug('Check if there is not already an account with this IBAN');
$existing = $this->findExistingAccount([AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], ['iban' => $accountIban]);
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
if (null !== $existing) {
$this->sourceError = (string)trans('validation.deposit_src_iban_exists');
return false;

View File

@@ -90,13 +90,15 @@ trait WithdrawalValidation
*/
protected function validateWithdrawalDestination(array $array): bool
{
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountId = array_key_exists('id', $array) ? $array['id'] : null;
$accountName = array_key_exists('name', $array) ? $array['name'] : null;
$accountIban = array_key_exists('iban', $array) ? $array['iban'] : null;
$accountNumber = array_key_exists('number', $array) ? $array['number'] : null;
Log::debug('Now in validateWithdrawalDestination()', $array);
// source can be any of the following types.
$validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? [];
if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) {
app('log')->debug('Source type can be: ', $validTypes);
if (null === $accountId && null === $accountName && null === $accountIban && null === $accountNumber && false === $this->canCreateTypes($validTypes)) {
// if both values are NULL return false,
// because the destination of a withdrawal can never be created automatically.
$this->destError = (string)trans('validation.withdrawal_dest_need_data');
@@ -113,15 +115,18 @@ trait WithdrawalValidation
$this->setDestination($found);
return true;
}
// todo explain error in log message.
$this->destError = (string)trans('validation.withdrawal_dest_bad_data', ['id' => $accountId, 'name' => $accountName]);
return false;
}
}
// if there is an iban, it can only be in use by a revenue account or we will fail.
// if there is an iban, it can only be in use by a valid destination type, or we will fail.
// the inverse of $validTypes is
if (null !== $accountIban && '' !== $accountIban) {
app('log')->debug('Check if there is not already an account with this IBAN');
$existing = $this->findExistingAccount([AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], ['iban' => $accountIban]);
// the inverse flag reverses the search, searching for everything that is NOT a valid type.
$existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true);
if (null !== $existing) {
$this->destError = (string)trans('validation.withdrawal_dest_iban_exists');
return false;

View File

@@ -250,12 +250,14 @@ class AccountValidator
/**
* @param array $validTypes
* @param array $data
* @param bool $inverse
*
* @return Account|null
*/
protected function findExistingAccount(array $validTypes, array $data): ?Account
protected function findExistingAccount(array $validTypes, array $data, bool $inverse = false): ?Account
{
Log::debug('Now in findExistingAccount', $data);
app('log')->debug('The search will be reversed!');
$accountId = array_key_exists('id', $data) ? $data['id'] : null;
$accountIban = array_key_exists('iban', $data) ? $data['iban'] : null;
$accountNumber = array_key_exists('number', $data) ? $data['number'] : null;
@@ -264,7 +266,9 @@ class AccountValidator
// find by ID
if (null !== $accountId && $accountId > 0) {
$first = $this->accountRepository->find($accountId);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
$check = in_array($first->accountType->type, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
@@ -273,7 +277,9 @@ class AccountValidator
// find by iban
if (null !== $accountIban && '' !== (string)$accountIban) {
$first = $this->accountRepository->findByIbanNull($accountIban, $validTypes);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
$check = in_array($first->accountType->type, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}
@@ -282,7 +288,9 @@ class AccountValidator
// find by number
if (null !== $accountNumber && '' !== (string)$accountNumber) {
$first = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes);
if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) {
$check = in_array($first->accountType->type, $validTypes, true);
$check = $inverse ? !$check : $check; // reverse the validation check if necessary.
if ((null !== $first) && $check) {
app('log')->debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban'));
return $first;
}