diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index 8002dea61a..0a23e02557 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -365,19 +365,29 @@ class TransactionJournalFactory $this->accountValidator->setTransactionType($transactionType); // validate source account. - $sourceId = $data['source_id'] ? (int)$data['source_id'] : null; - $sourceName = $data['source_name'] ? (string)$data['source_name'] : null; - $validSource = $this->accountValidator->validateSource($sourceId, $sourceName, null); + $array = [ + 'id' => $data['source_id'] ? (int)$data['source_id'] : null, + 'name' => $data['source_name'] ? (string)$data['source_name'] : null, + 'iban' => $data['source_iban'] ? (string)$data['source_iban'] : null, + 'number' => $data['source_number'] ? (string)$data['source_number'] : null, + ]; + $validSource = $this->accountValidator->validateSource($array); // do something with result: if (false === $validSource) { throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); } Log::debug('Source seems valid.'); + // validate destination account - $destinationId = $data['destination_id'] ? (int)$data['destination_id'] : null; - $destinationName = $data['destination_name'] ? (string)$data['destination_name'] : null; - $validDestination = $this->accountValidator->validateDestination($destinationId, $destinationName, null); + $array = [ + 'id' => $data['destination_id'] ? (int)$data['destination_id'] : null, + 'name' => $data['destination_name'] ? (string)$data['destination_name'] : null, + 'iban' => $data['destination_iban'] ? (string)$data['destination_iban'] : null, + 'number' => $data['destination_number'] ? (string)$data['destination_number'] : null, + ]; + + $validDestination = $this->accountValidator->validateDestination($array); // do something with result: if (false === $validDestination) { throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError)); diff --git a/app/Http/Controllers/Transaction/ConvertController.php b/app/Http/Controllers/Transaction/ConvertController.php index d0c42883e4..b69f30f399 100644 --- a/app/Http/Controllers/Transaction/ConvertController.php +++ b/app/Http/Controllers/Transaction/ConvertController.php @@ -336,8 +336,8 @@ class ConvertController extends Controller $sourceName = '' === $sourceName ? null : (string)$sourceName; $destinationId = '' === $destinationId || null === $destinationId ? null : (int)$destinationId; $destinationName = '' === $destinationName ? null : (string)$destinationName; - $validSource = $validator->validateSource($sourceId, $sourceName, null); - $validDestination = $validator->validateDestination($destinationId, $destinationName, null); + $validSource = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName,]); + $validDestination = $validator->validateDestination(['id' => $destinationId, 'name' => $destinationName,]); if (false === $validSource) { throw new FireflyException(sprintf(trans('firefly.convert_invalid_source'), $journal->id)); diff --git a/app/Http/Requests/RecurrenceFormRequest.php b/app/Http/Requests/RecurrenceFormRequest.php index 3ef914e280..a68ad2c351 100644 --- a/app/Http/Requests/RecurrenceFormRequest.php +++ b/app/Http/Requests/RecurrenceFormRequest.php @@ -295,7 +295,7 @@ class RecurrenceFormRequest extends FormRequest */ public function validateAccountInformation(Validator $validator): void { - Log::debug('Now in validateAccountInformation()'); + Log::debug('Now in validateAccountInformation (RecurrenceFormRequest)()'); /** @var AccountValidator $accountValidator */ $accountValidator = app(AccountValidator::class); $data = $validator->getData(); @@ -326,7 +326,7 @@ class RecurrenceFormRequest extends FormRequest break; } // validate source account. - $validSource = $accountValidator->validateSource($sourceId, null, null); + $validSource = $accountValidator->validateSource(['id' => $sourceId,]); // do something with result: if (false === $validSource) { @@ -338,7 +338,7 @@ class RecurrenceFormRequest extends FormRequest } // validate destination account - $validDestination = $accountValidator->validateDestination($destinationId, null, null); + $validDestination = $accountValidator->validateDestination(['id' => $destinationId,]); // do something with result: if (false === $validDestination) { $message = (string)trans('validation.generic_invalid_destination'); diff --git a/app/Rules/IsTransferAccount.php b/app/Rules/IsTransferAccount.php index ec3d9fbf52..16897ed59a 100644 --- a/app/Rules/IsTransferAccount.php +++ b/app/Rules/IsTransferAccount.php @@ -60,14 +60,14 @@ class IsTransferAccount implements Rule $validator->setTransactionType(TransactionType::TRANSFER); $validator->setUser(auth()->user()); - $validAccount = $validator->validateSource(null, (string)$value, null); + $validAccount = $validator->validateSource(['name' => (string)$value,]); if (true === $validAccount) { Log::debug('Found account based on name. Return true.'); // found by name, use repos to return. return true; } - $validAccount = $validator->validateSource((int)$value, null, null); + $validAccount = $validator->validateSource(['id' => (int)$value,]); Log::debug(sprintf('Search by id (%d), result is %s.', (int)$value, var_export($validAccount, true))); return false !== $validAccount; diff --git a/app/Services/Internal/Support/JournalServiceTrait.php b/app/Services/Internal/Support/JournalServiceTrait.php index 92389b9b76..3ab8eff0bd 100644 --- a/app/Services/Internal/Support/JournalServiceTrait.php +++ b/app/Services/Internal/Support/JournalServiceTrait.php @@ -77,9 +77,9 @@ trait JournalServiceTrait Log::debug(sprintf($message, $transactionType, $direction, implode(', ', $expectedTypes[$transactionType] ?? ['UNKNOWN']), $direction)); $result = $this->findAccountById($data, $expectedTypes[$transactionType]); - $result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]); $result = $this->findAccountByIban($result, $data, $expectedTypes[$transactionType]); $result = $this->findAccountByNumber($result, $data, $expectedTypes[$transactionType]); + $result = $this->findAccountByName($result, $data, $expectedTypes[$transactionType]); $result = $this->createAccount($result, $data, $expectedTypes[$transactionType][0]); return $this->getCashAccount($result, $data, $expectedTypes[$transactionType]); diff --git a/app/Services/Internal/Support/RecurringTransactionTrait.php b/app/Services/Internal/Support/RecurringTransactionTrait.php index 316ada2b88..7333a6e1aa 100644 --- a/app/Services/Internal/Support/RecurringTransactionTrait.php +++ b/app/Services/Internal/Support/RecurringTransactionTrait.php @@ -136,11 +136,12 @@ trait RecurringTransactionTrait $validator = app(AccountValidator::class); $validator->setUser($recurrence->user); $validator->setTransactionType($recurrence->transactionType->type); - if (!$validator->validateSource($source->id, null, null)) { + + if (!$validator->validateSource(['id' => $source->id])) { throw new FireflyException(sprintf('Source invalid: %s', $validator->sourceError)); } - if (!$validator->validateDestination($destination->id, null, null)) { + if (!$validator->validateDestination(['id' => $destination->id])) { throw new FireflyException(sprintf('Destination invalid: %s', $validator->destError)); } if (array_key_exists('foreign_amount', $array) && '' === (string)$array['foreign_amount']) { diff --git a/app/Services/Internal/Update/JournalUpdateService.php b/app/Services/Internal/Update/JournalUpdateService.php index 423c2311ce..c703202d2f 100644 --- a/app/Services/Internal/Update/JournalUpdateService.php +++ b/app/Services/Internal/Update/JournalUpdateService.php @@ -215,7 +215,7 @@ class JournalUpdateService $validator->setTransactionType($expectedType); $validator->setUser($this->transactionJournal->user); - $result = $validator->validateSource($sourceId, $sourceName, null); + $result = $validator->validateSource(['id' =>$sourceId]); Log::debug(sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))); // See reference nr. 95 @@ -309,7 +309,7 @@ class JournalUpdateService $validator->setTransactionType($expectedType); $validator->setUser($this->transactionJournal->user); $validator->source = $this->getValidSourceAccount(); - $result = $validator->validateDestination($destId, $destName, null); + $result = $validator->validateDestination(['id' => $destId]); Log::debug(sprintf('hasValidDestinationAccount(%d, "%s") will return %s', $destId, $destName, var_export($result, true))); // See reference nr. 96 diff --git a/app/Validation/Account/DepositValidation.php b/app/Validation/Account/DepositValidation.php index d168e3add6..2987e85a5c 100644 --- a/app/Validation/Account/DepositValidation.php +++ b/app/Validation/Account/DepositValidation.php @@ -41,23 +41,24 @@ trait DepositValidation /** * @param array $validTypes - * @param int $accountId - * @param string $accountName + * @param array $data * * @return Account|null */ - abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account; + abstract protected function findExistingAccount(array $validTypes, array $data): ?Account; /** - * @param int|null $accountId - * @param mixed $accountName + * @param array $array * * @return bool */ - protected function validateDepositDestination(?int $accountId, $accountName): bool + protected function validateDepositDestination(array $array): bool { - $result = null; - Log::debug(sprintf('Now in validateDepositDestination(%d, "%s")', $accountId, $accountName)); + $result = null; + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + + Log::debug('Now in validateDepositDestination', $array); // source can be any of the following types. $validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? []; @@ -76,7 +77,7 @@ trait DepositValidation if (null === $result) { // otherwise try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes, $array); if (null === $search) { Log::debug('findExistingAccount() returned NULL, so the result is false.'); $this->destError = (string)trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); @@ -89,20 +90,23 @@ trait DepositValidation } } $result = $result ?? false; - Log::debug(sprintf('validateDepositDestination(%d, "%s") will return %s', $accountId, $accountName, var_export($result, true))); + Log::debug(sprintf('validateDepositDestination will return %s', var_export($result, true))); return $result; } /** - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateDepositSource(?int $accountId, ?string $accountName): bool + protected function validateDepositSource(array $array): bool { - Log::debug(sprintf('Now in validateDepositSource(%d, "%s")', $accountId, $accountName)); + $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 validateDepositSource', $array); $result = null; // source can be any of the following types. $validTypes = array_keys($this->combinations[$this->transactionType]); @@ -114,12 +118,32 @@ trait DepositValidation $result = false; } - // if the user submits an ID only but that ID is not of the correct type, + // if the user submits an ID, but that ID is not of the correct type, // return false. - if (null !== $accountId && null === $accountName) { + if (null !== $accountId) { $search = $this->accountRepository->find($accountId); if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { - Log::debug(sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type)); + Log::debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type)); + $result = false; + } + } + + // if user submits an IBAN: + if (null !== $accountIban) { + $search = $this->accountRepository->findByIbanNull($accountIban, $validTypes); + if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { + Log::debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type)); + $result = false; + } + } + + // if user submits a number: + if (null !== $accountNumber) { + $search = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes); + if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { + Log::debug( + sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type) + ); $result = false; } } diff --git a/app/Validation/Account/LiabilityValidation.php b/app/Validation/Account/LiabilityValidation.php index 2c19b5e0a1..019792f58e 100644 --- a/app/Validation/Account/LiabilityValidation.php +++ b/app/Validation/Account/LiabilityValidation.php @@ -36,14 +36,15 @@ trait LiabilityValidation { /** - * @param int|null $accountId + * @param array $array * * @return bool */ - protected function validateLCDestination(?int $accountId): bool + protected function validateLCDestination(array $array): bool { - Log::debug(sprintf('Now in validateLCDestination(%d)', $accountId)); + Log::debug('Now in validateLCDestination', $array); $result = null; + $accountId = array_key_exists('id', $array) ? $array['id'] : null; $validTypes = config('firefly.valid_liabilities'); if (null === $accountId) { @@ -74,14 +75,15 @@ trait LiabilityValidation /** * Source of an liability credit must be a liability. * - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateLCSource(?string $accountName): bool + protected function validateLCSource(array $array): bool { + $accountName = array_key_exists('name', $array) ? $array['name'] : null; $result = true; - Log::debug(sprintf('Now in validateLCDestination("%s")', $accountName)); + Log::debug('Now in validateLCDestination', $array); if ('' === $accountName || null === $accountName) { $result = false; } diff --git a/app/Validation/Account/OBValidation.php b/app/Validation/Account/OBValidation.php index 8b934cda2f..9b4b09841a 100644 --- a/app/Validation/Account/OBValidation.php +++ b/app/Validation/Account/OBValidation.php @@ -41,15 +41,16 @@ trait OBValidation abstract protected function canCreateTypes(array $accountTypes): bool; /** - * @param int|null $accountId - * @param mixed $accountName + * @param array $array * * @return bool */ - protected function validateOBDestination(?int $accountId, $accountName): bool + protected function validateOBDestination(array $array): bool { - $result = null; - Log::debug(sprintf('Now in validateOBDestination(%d, "%s")', $accountId, $accountName)); + $result = null; + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + Log::debug('Now in validateOBDestination', $array); // source can be any of the following types. $validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? []; @@ -68,7 +69,7 @@ trait OBValidation if (null === $result) { // otherwise try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes, $array); if (null === $search) { Log::debug('findExistingAccount() returned NULL, so the result is false.', $validTypes); $this->destError = (string)trans('validation.ob_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); @@ -90,15 +91,15 @@ trait OBValidation * Source of an opening balance can either be an asset account * or an "initial balance account". The latter can be created. * - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateOBSource(?int $accountId, ?string $accountName): bool + protected function validateOBSource(array $array): bool { - Log::debug(sprintf('Now in validateOBSource(%d, "%s")', $accountId, $accountName)); - Log::debug(sprintf('The account name is null: %s', var_export(null === $accountName, true))); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + Log::debug('Now in validateOBSource', $array); $result = null; // source can be any of the following types. $validTypes = array_keys($this->combinations[$this->transactionType]); diff --git a/app/Validation/Account/ReconciliationValidation.php b/app/Validation/Account/ReconciliationValidation.php index d8e97de566..87048e7331 100644 --- a/app/Validation/Account/ReconciliationValidation.php +++ b/app/Validation/Account/ReconciliationValidation.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Validation\Account; +use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use Log; @@ -32,15 +33,17 @@ use Log; */ trait ReconciliationValidation { - + public ?Account $destination; + public ?Account $source; /** - * @param int|null $accountId + * @param array $array * * @return bool */ - protected function validateReconciliationDestination(?int $accountId): bool + protected function validateReconciliationDestination(array $array): bool { - Log::debug('Now in validateReconciliationDestination'); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + Log::debug('Now in validateReconciliationDestination', $array); if (null === $accountId) { Log::debug('Return FALSE'); @@ -80,13 +83,14 @@ trait ReconciliationValidation } /** - * @param int|null $accountId + * @param array $array * * @return bool */ - protected function validateReconciliationSource(?int $accountId): bool + protected function validateReconciliationSource(array $array): bool { - Log::debug('In validateReconciliationSource'); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + Log::debug('In validateReconciliationSource', $array); if (null === $accountId) { Log::debug('Return FALSE'); diff --git a/app/Validation/Account/TransferValidation.php b/app/Validation/Account/TransferValidation.php index e075f86727..3360500f60 100644 --- a/app/Validation/Account/TransferValidation.php +++ b/app/Validation/Account/TransferValidation.php @@ -40,22 +40,22 @@ trait TransferValidation /** * @param array $validTypes - * @param int $accountId - * @param string $accountName + * @param array $data * * @return Account|null */ - abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account; + abstract protected function findExistingAccount(array $validTypes, array $data): ?Account; /** - * @param int|null $accountId - * @param mixed $accountName + * @param array $array * * @return bool */ - protected function validateTransferDestination(?int $accountId, $accountName): bool + protected function validateTransferDestination(array $array): bool { - Log::debug(sprintf('Now in validateTransferDestination(%d, "%s")', $accountId, $accountName)); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + Log::debug('Now in validateTransferDestination', $array); // source can be any of the following types. $validTypes = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? []; if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { @@ -68,7 +68,7 @@ trait TransferValidation } // or try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes,$array); if (null === $search) { $this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); @@ -88,14 +88,15 @@ trait TransferValidation } /** - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateTransferSource(?int $accountId, ?string $accountName): bool + protected function validateTransferSource(array $array): bool { - Log::debug(sprintf('Now in validateTransferSource(%d, "%s")', $accountId, $accountName)); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + Log::debug('Now in validateTransferSource', $array); // source can be any of the following types. $validTypes = array_keys($this->combinations[$this->transactionType]); if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { @@ -108,7 +109,7 @@ trait TransferValidation } // otherwise try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes, $array); if (null === $search) { $this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]); Log::warning('Not a valid source, cant find it.', $validTypes); diff --git a/app/Validation/Account/WithdrawalValidation.php b/app/Validation/Account/WithdrawalValidation.php index 49878cb253..48e66f8bde 100644 --- a/app/Validation/Account/WithdrawalValidation.php +++ b/app/Validation/Account/WithdrawalValidation.php @@ -41,22 +41,22 @@ trait WithdrawalValidation /** * @param array $validTypes - * @param int $accountId - * @param string $accountName + * @param array $data * * @return Account|null */ - abstract protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account; + abstract protected function findExistingAccount(array $validTypes, array $data): ?Account; /** - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateGenericSource(?int $accountId, ?string $accountName): bool + protected function validateGenericSource(array $array): bool { - Log::debug(sprintf('Now in validateGenericSource(%d, "%s")', $accountId, $accountName)); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + Log::debug('Now in validateGenericSource', $array); // source can be any of the following types. $validTypes = [AccountType::ASSET, AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { @@ -69,7 +69,7 @@ trait WithdrawalValidation } // otherwise try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes, $array); if (null === $search) { $this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]); Log::warning('Not a valid source. Cant find it.', $validTypes); @@ -83,14 +83,15 @@ trait WithdrawalValidation } /** - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateWithdrawalDestination(?int $accountId, ?string $accountName): bool + protected function validateWithdrawalDestination(array $array): bool { - Log::debug(sprintf('Now in validateWithdrawalDestination(%d, "%s")', $accountId, $accountName)); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : 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 && false === $this->canCreateTypes($validTypes)) { @@ -120,14 +121,16 @@ trait WithdrawalValidation } /** - * @param int|null $accountId - * @param string|null $accountName + * @param array $array * * @return bool */ - protected function validateWithdrawalSource(?int $accountId, ?string $accountName): bool + protected function validateWithdrawalSource(array $array): bool { - Log::debug(sprintf('Now in validateWithdrawalSource(%d, "%s")', $accountId, $accountName)); + $accountId = array_key_exists('id', $array) ? $array['id'] : null; + $accountName = array_key_exists('name', $array) ? $array['name'] : null; + + Log::debug('Now in validateWithdrawalSource', $array); // source can be any of the following types. $validTypes = array_keys($this->combinations[$this->transactionType]); if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { @@ -140,7 +143,7 @@ trait WithdrawalValidation } // otherwise try to find the account: - $search = $this->findExistingAccount($validTypes, (int)$accountId, (string)$accountName); + $search = $this->findExistingAccount($validTypes, $array); if (null === $search) { $this->sourceError = (string)trans('validation.withdrawal_source_bad_data', ['id' => $accountId, 'name' => $accountName]); Log::warning('Not a valid source. Cant find it.', $validTypes); diff --git a/app/Validation/AccountValidator.php b/app/Validation/AccountValidator.php index d440cfd786..b416fba64e 100644 --- a/app/Validation/AccountValidator.php +++ b/app/Validation/AccountValidator.php @@ -95,15 +95,13 @@ class AccountValidator } /** - * @param int|null $accountId - * @param string|null $accountName - * @param string|null $accountIban + * @param array $array * * @return bool */ - public function validateDestination(?int $accountId, ?string $accountName, ?string $accountIban): bool + public function validateDestination(array $array): bool { - Log::debug(sprintf('Now in AccountValidator::validateDestination(%d, "%s", "%s")', $accountId, $accountName, $accountIban)); + Log::debug('Now in AccountValidator::validateDestination()', $array); if (null === $this->source) { Log::error('Source is NULL, always FALSE.'); $this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.'; @@ -119,22 +117,22 @@ class AccountValidator break; case TransactionType::WITHDRAWAL: - $result = $this->validateWithdrawalDestination($accountId, $accountName); + $result = $this->validateWithdrawalDestination($array); break; case TransactionType::DEPOSIT: - $result = $this->validateDepositDestination($accountId, $accountName); + $result = $this->validateDepositDestination($array); break; case TransactionType::TRANSFER: - $result = $this->validateTransferDestination($accountId, $accountName); + $result = $this->validateTransferDestination($array); break; case TransactionType::OPENING_BALANCE: - $result = $this->validateOBDestination($accountId, $accountName); + $result = $this->validateOBDestination($array); break; case TransactionType::LIABILITY_CREDIT: - $result = $this->validateLCDestination($accountId); + $result = $this->validateLCDestination($array); break; case TransactionType::RECONCILIATION: - $result = $this->validateReconciliationDestination($accountId); + $result = $this->validateReconciliationDestination($array); break; } @@ -142,39 +140,37 @@ class AccountValidator } /** - * @param int|null $accountId - * @param string|null $accountName - * @param string|null $accountIban + * @param array $array * * @return bool */ - public function validateSource(?int $accountId, ?string $accountName, ?string $accountIban): bool + public function validateSource(array $array): bool { - Log::debug(sprintf('Now in AccountValidator::validateSource(%d, "%s", "%s")', $accountId, $accountName, $accountIban)); + Log::debug('Now in AccountValidator::validateSource()', $array); switch ($this->transactionType) { default: Log::error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will do a generic check.', $this->transactionType)); - $result = $this->validateGenericSource($accountId, $accountName); + $result = $this->validateGenericSource($array); break; case TransactionType::WITHDRAWAL: - $result = $this->validateWithdrawalSource($accountId, $accountName); + $result = $this->validateWithdrawalSource($array); break; case TransactionType::DEPOSIT: - $result = $this->validateDepositSource($accountId, $accountName); + $result = $this->validateDepositSource($array); break; case TransactionType::TRANSFER: - $result = $this->validateTransferSource($accountId, $accountName); + $result = $this->validateTransferSource($array); break; case TransactionType::OPENING_BALANCE: - $result = $this->validateOBSource($accountId, $accountName); + $result = $this->validateOBSource($array); break; case TransactionType::LIABILITY_CREDIT: - $result = $this->validateLCSource($accountName); + $result = $this->validateLCSource($array); break; case TransactionType::RECONCILIATION: Log::debug('Calling validateReconciliationSource'); - $result = $this->validateReconciliationSource($accountId); + $result = $this->validateReconciliationSource($array); break; } @@ -203,22 +199,43 @@ class AccountValidator } /** - * @param array $validTypes - * @param int $accountId - * @param string $accountName + * @param array $validTypes + * @param array $data * * @return Account|null */ - protected function findExistingAccount(array $validTypes, int $accountId, string $accountName): ?Account + protected function findExistingAccount(array $validTypes, array $data): ?Account { + Log::debug('Now in findExistingAccount', $data); + $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; + $accountName = array_key_exists('name', $data) ? $data['name'] : null; + // find by ID - if ($accountId > 0) { + if (null !== $accountId && $accountId > 0) { $first = $this->accountRepository->find($accountId); if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) { return $first; } } + // find by iban + if (null !== $accountIban && '' !== $accountIban) { + $first = $this->accountRepository->findByIbanNull($accountIban, $validTypes); + if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) { + return $first; + } + } + + // find by number + if (null !== $accountNumber && '' !== $accountNumber) { + $first = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes); + if ((null !== $first) && in_array($first->accountType->type, $validTypes, true)) { + return $first; + } + } + // find by name: if ('' !== $accountName) { return $this->accountRepository->findByName($accountName, $validTypes); diff --git a/app/Validation/RecurrenceValidation.php b/app/Validation/RecurrenceValidation.php index 1469bcbf0f..7340b9163a 100644 --- a/app/Validation/RecurrenceValidation.php +++ b/app/Validation/RecurrenceValidation.php @@ -105,7 +105,7 @@ trait RecurrenceValidation // validate destination account $destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null; $destinationName = $transaction['destination_name'] ?? null; - $validDestination = $accountValidator->validateDestination($destinationId, $destinationName, null); + $validDestination = $accountValidator->validateDestination(['id' => $destinationId, 'name' => $destinationName,]); // do something with result: if (false === $validDestination) { $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); diff --git a/app/Validation/TransactionValidation.php b/app/Validation/TransactionValidation.php index fe26e44eca..276c735fb7 100644 --- a/app/Validation/TransactionValidation.php +++ b/app/Validation/TransactionValidation.php @@ -42,7 +42,7 @@ trait TransactionValidation */ public function validateAccountInformation(Validator $validator): void { - Log::debug('Now in validateAccountInformation()'); + Log::debug('Now in validateAccountInformation (TransactionValidation) ()'); $transactions = $this->getTransactionsArray($validator); $data = $validator->getData(); @@ -100,10 +100,17 @@ trait TransactionValidation $accountValidator->setTransactionType($transactionType); // validate source account. - $sourceId = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null; - $sourceName = array_key_exists('source_name', $transaction) ? (string)$transaction['source_name'] : null; - $sourceIban = array_key_exists('source_iban', $transaction) ? (string)$transaction['source_iban'] : null; - $validSource = $accountValidator->validateSource($sourceId, $sourceName, $sourceIban); + $sourceId = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null; + $sourceName = array_key_exists('source_name', $transaction) ? (string)$transaction['source_name'] : null; + $sourceIban = array_key_exists('source_iban', $transaction) ? (string)$transaction['source_iban'] : null; + $sourceNumber = array_key_exists('source_number', $transaction) ? (string)$transaction['source_number'] : null; + $array = [ + 'id' => $sourceId, + 'name' => $sourceName, + 'iban' => $sourceIban, + 'number' => $sourceNumber, + ]; + $validSource = $accountValidator->validateSource($array); // do something with result: if (false === $validSource) { @@ -113,10 +120,17 @@ trait TransactionValidation return; } // validate destination account - $destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null; - $destinationName = array_key_exists('destination_name', $transaction) ? (string)$transaction['destination_name'] : null; - $destinationIban = array_key_exists('destination_iban', $transaction) ? (string)$transaction['destination_iban'] : null; - $validDestination = $accountValidator->validateDestination($destinationId, $destinationName, $destinationIban); + $destinationId = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null; + $destinationName = array_key_exists('destination_name', $transaction) ? (string)$transaction['destination_name'] : null; + $destinationIban = array_key_exists('destination_iban', $transaction) ? (string)$transaction['destination_iban'] : null; + $destinationNumber = array_key_exists('destination_number', $transaction) ? (string)$transaction['destination_number'] : null; + $array = [ + 'id' => $destinationId, + 'name' => $destinationName, + 'iban' => $destinationIban, + 'number' => $destinationNumber, + ]; + $validDestination = $accountValidator->validateDestination($array); // do something with result: if (false === $validDestination) { $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); @@ -204,7 +218,8 @@ trait TransactionValidation } $destinationId = (int)($transaction['destination_id'] ?? 0); $destinationName = $transaction['destination_name'] ?? null; - $validDestination = $accountValidator->validateDestination($destinationId, $destinationName, null); + $array = ['id' => $destinationId, 'name' => $destinationName,]; + $validDestination = $accountValidator->validateDestination($array); // do something with result: if (false === $validDestination) { Log::warning('Looks like the destination account is not valid so complain to the user about it.');