| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountValidator.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:58:22 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02: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. | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02: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/>. | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Validation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 15:23:17 +01:00
										 |  |  | use FireflyIII\Enums\AccountTypeEnum; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  | use FireflyIII\Enums\TransactionTypeEnum; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  | use FireflyIII\Models\UserGroup; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | use FireflyIII\Validation\Account\DepositValidation; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | use FireflyIII\Validation\Account\LiabilityValidation; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | use FireflyIII\Validation\Account\OBValidation; | 
					
						
							|  |  |  | use FireflyIII\Validation\Account\ReconciliationValidation; | 
					
						
							|  |  |  | use FireflyIII\Validation\Account\TransferValidation; | 
					
						
							|  |  |  | use FireflyIII\Validation\Account\WithdrawalValidation; | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountValidator | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountValidator | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |     use DepositValidation; | 
					
						
							|  |  |  |     use LiabilityValidation; | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |     use OBValidation; | 
					
						
							|  |  |  |     use ReconciliationValidation; | 
					
						
							|  |  |  |     use TransferValidation; | 
					
						
							|  |  |  |     use WithdrawalValidation; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |     public bool                        $createMode; | 
					
						
							|  |  |  |     public string                      $destError; | 
					
						
							|  |  |  |     public ?Account                    $destination; | 
					
						
							|  |  |  |     public ?Account                    $source; | 
					
						
							|  |  |  |     public string                      $sourceError; | 
					
						
							|  |  |  |     private AccountRepositoryInterface $accountRepository; | 
					
						
							|  |  |  |     private array                      $combinations; | 
					
						
							|  |  |  |     private string                     $transactionType; | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * AccountValidator constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-03-14 17:45:16 +01:00
										 |  |  |         $this->createMode        = false; | 
					
						
							|  |  |  |         $this->destError         = 'No error yet.'; | 
					
						
							|  |  |  |         $this->sourceError       = 'No error yet.'; | 
					
						
							|  |  |  |         $this->combinations      = config('firefly.source_dests'); | 
					
						
							|  |  |  |         $this->source            = null; | 
					
						
							|  |  |  |         $this->destination       = null; | 
					
						
							|  |  |  |         $this->accountRepository = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     public function getSource(): ?Account | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->source; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     public function setSource(?Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$account instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |             Log::debug('AccountValidator source is set to NULL'); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ($account instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |             Log::debug(sprintf('AccountValidator source is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType?->type)); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->source = $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setDestination(?Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$account instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |             Log::debug('AccountValidator destination is set to NULL'); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ($account instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |             Log::debug(sprintf('AccountValidator destination is set to #%d: "%s" (%s)', $account->id, $account->name, $account->accountType->type)); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->destination = $account; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     public function setTransactionType(string $transactionType): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug(sprintf('Transaction type for validator is now "%s".', ucfirst($transactionType))); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         $this->transactionType = ucfirst($transactionType); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     public function setUser(User $user): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->accountRepository->setUser($user); | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setUserGroup(UserGroup $userGroup): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  |         $this->accountRepository->setUserGroup($userGroup); | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     public function validateDestination(array $array): bool | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('Now in AccountValidator::validateDestination()', $array); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$this->source instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |             Log::error('Source is NULL, always FALSE.'); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |             $this->destError = 'No source account validation has taken place yet. Please do this first or overrule the object.'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         switch ($this->transactionType) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 $this->destError = sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType); | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::error(sprintf('AccountValidator::validateDestination cannot handle "%s", so it will always return false.', $this->transactionType)); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = false; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |             case TransactionTypeEnum::WITHDRAWAL->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateWithdrawalDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:09:15 +01:00
										 |  |  |             case TransactionTypeEnum::DEPOSIT->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateDepositDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:56 +01:00
										 |  |  |             case TransactionTypeEnum::TRANSFER->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateTransferDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::OPENING_BALANCE->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateOBDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-16 13:16:04 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::LIABILITY_CREDIT->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateLCDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:00 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::RECONCILIATION->value: | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $result          = $this->validateReconciliationDestination($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 05:53:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     public function validateSource(array $array): bool | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('Now in AccountValidator::validateSource()', $array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         switch ($this->transactionType) { | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::error(sprintf('AccountValidator::validateSource cannot handle "%s", so it will do a generic check.', $this->transactionType)); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateGenericSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |             case TransactionTypeEnum::WITHDRAWAL->value: | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateWithdrawalSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:09:15 +01:00
										 |  |  |             case TransactionTypeEnum::DEPOSIT->value: | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateDepositSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:56 +01:00
										 |  |  |             case TransactionTypeEnum::TRANSFER->value: | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateTransferSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::OPENING_BALANCE->value: | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateOBSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-16 13:16:04 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::LIABILITY_CREDIT->value: | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateLCSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-10 17:26:00 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:11:20 +01:00
										 |  |  |             case TransactionTypeEnum::RECONCILIATION->value: | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug('Calling validateReconciliationSource'); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = $this->validateReconciliationSource($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-23 05:53:01 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     protected function canCreateTypes(array $accountTypes): bool | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('Can we create any of these types?', $accountTypes); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         /** @var string $accountType */ | 
					
						
							|  |  |  |         foreach ($accountTypes as $accountType) { | 
					
						
							|  |  |  |             if ($this->canCreateType($accountType)) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug(sprintf('YES, we can create a %s', $accountType)); | 
					
						
							| 
									
										
										
										
											2019-06-08 06:19:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('NO, we cant create any of those.'); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function canCreateType(string $accountType): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-11-24 15:23:17 +01:00
										 |  |  |         $canCreate = [AccountTypeEnum::EXPENSE->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::INITIAL_BALANCE->value, AccountTypeEnum::LIABILITY_CREDIT->value]; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if (in_array($accountType, $canCreate, true)) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      * It's a long and fairly complex method, but I don't mind. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.CyclomaticComplexity") | 
					
						
							|  |  |  |      * @SuppressWarnings("PHPMD.BooleanArgumentFlag") | 
					
						
							|  |  |  |      * @SuppressWarnings("PHPMD.NPathComplexity") | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-12 05:58:39 +02:00
										 |  |  |     protected function findExistingAccount(array $validTypes, array $data, bool $inverse = false): ?Account | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('Now in findExistingAccount', [$validTypes, $data]); | 
					
						
							|  |  |  |         Log::debug('The search will be reversed!'); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         // find by ID
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         if (null !== $accountId && $accountId > 0) { | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  |             $first       = $this->accountRepository->find($accountId); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             $accountType = $first instanceof Account ? $first->accountType->type : 'invalid'; | 
					
						
							| 
									
										
										
										
											2023-09-18 17:59:10 +02:00
										 |  |  |             $check       = in_array($accountType, $validTypes, true); | 
					
						
							|  |  |  |             $check       = $inverse ? !$check : $check; // reverse the validation check if necessary.
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if (($first instanceof Account) && $check) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug(sprintf('ID: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban')); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 21:38:35 +02:00
										 |  |  |                 return $first; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         // find by iban
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (null !== $accountIban && '' !== (string) $accountIban) { | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  |             $first       = $this->accountRepository->findByIbanNull($accountIban, $validTypes); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             $accountType = $first instanceof Account ? $first->accountType->type : 'invalid'; | 
					
						
							| 
									
										
										
										
											2023-09-18 17:59:10 +02:00
										 |  |  |             $check       = in_array($accountType, $validTypes, true); | 
					
						
							|  |  |  |             $check       = $inverse ? !$check : $check; // reverse the validation check if necessary.
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if (($first instanceof Account) && $check) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug(sprintf('Iban: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban')); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 return $first; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // find by number
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (null !== $accountNumber && '' !== (string) $accountNumber) { | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  |             $first       = $this->accountRepository->findByAccountNumber($accountNumber, $validTypes); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             $accountType = $first instanceof Account ? $first->accountType->type : 'invalid'; | 
					
						
							| 
									
										
										
										
											2023-09-18 17:59:10 +02:00
										 |  |  |             $check       = in_array($accountType, $validTypes, true); | 
					
						
							|  |  |  |             $check       = $inverse ? !$check : $check; // reverse the validation check if necessary.
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if (($first instanceof Account) && $check) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug(sprintf('Number: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban')); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 return $first; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         // find by name:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if ('' !== (string) $accountName) { | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  |             $first = $this->accountRepository->findByName($accountName, $validTypes); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             if ($first instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |                 Log::debug(sprintf('Name: Found %s account #%d ("%s", IBAN "%s")', $first->accountType->type, $first->id, $first->name, $first->iban ?? 'no iban')); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 15:23:10 +02:00
										 |  |  |                 return $first; | 
					
						
							| 
									
										
										
										
											2023-08-26 18:40:23 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-09-17 07:09:40 +02:00
										 |  |  |         Log::debug('Found nothing in findExistingAccount()'); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 21:38:35 +02:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |