| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * DepositValidation.php | 
					
						
							|  |  |  |  * Copyright (c) 2020 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\Validation\Account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  | use FireflyIII\Enums\AccountTypeEnum; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\AccountType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait DepositValidation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait DepositValidation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateDepositDestination(array $array): bool | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $result      = null; | 
					
						
							|  |  |  |         $accountId   = array_key_exists('id', $array) ? $array['id'] : null; | 
					
						
							|  |  |  |         $accountName = array_key_exists('name', $array) ? $array['name'] : null; | 
					
						
							| 
									
										
										
										
											2023-06-12 20:24:45 +02:00
										 |  |  |         $accountIban = array_key_exists('iban', $array) ? $array['iban'] : null; | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateDepositDestination', $array); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // source can be any of the following types.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $validTypes  = $this->combinations[$this->transactionType][$this->source->accountType->type] ?? []; | 
					
						
							| 
									
										
										
										
											2023-06-12 20:24:45 +02:00
										 |  |  |         if (null === $accountId && null === $accountName && null === $accountIban && false === $this->canCreateTypes($validTypes)) { | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             // if both values are NULL we return false,
 | 
					
						
							|  |  |  |             // because the destination of a deposit can't be created.
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $this->destError = (string) trans('validation.deposit_dest_need_data'); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('Both values are NULL, cant create deposit destination.'); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $result          = false; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         // if the account can be created anyway we don't need to search.
 | 
					
						
							|  |  |  |         if (null === $result && true === $this->canCreateTypes($validTypes)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Can create some of these types, so return true.'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             $result = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null === $result) { | 
					
						
							|  |  |  |             // otherwise try to find the account:
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             $search = $this->findExistingAccount($validTypes, $array); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             if (null === $search) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug('findExistingAccount() returned NULL, so the result is false.'); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $this->destError = (string) trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |                 $result          = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (null !== $search) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('findExistingAccount() returned #%d ("%s"), so the result is true.', $search->id, $search->name)); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |                 $this->setDestination($search); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $result = true; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('validateDepositDestination will return %s', var_export($result, true))); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     abstract protected function canCreateTypes(array $accountTypes): bool; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     abstract protected function findExistingAccount(array $validTypes, array $data): ?Account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Pretty complex unfortunately. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-03 15:53:10 +01:00
										 |  |  |      * @SuppressWarnings("PHPMD.NPathComplexity") | 
					
						
							|  |  |  |      * @SuppressWarnings("PHPMD.CyclomaticComplexity") | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateDepositSource(array $array): bool | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $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; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateDepositSource', $array); | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |         // null = we found nothing at all or didn't even search
 | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  |         // false = invalid results
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $result        = null; | 
					
						
							| 
									
										
										
										
											2023-01-14 07:39:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         // source can be any of the following types.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $validTypes    = array_keys($this->combinations[$this->transactionType]); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         if (null === $accountId | 
					
						
							|  |  |  |             && null === $accountName | 
					
						
							|  |  |  |             && null === $accountIban | 
					
						
							|  |  |  |             && null === $accountNumber | 
					
						
							|  |  |  |             && false === $this->canCreateTypes($validTypes)) { | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             // if both values are NULL return false,
 | 
					
						
							|  |  |  |             // because the source of a deposit can't be created.
 | 
					
						
							|  |  |  |             // (this never happens).
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $this->sourceError = (string) trans('validation.deposit_source_need_data'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             $result            = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 05:58:39 +02:00
										 |  |  |         // if there is an iban, it can only be in use by a valid source type, or we will fail.
 | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |         if (null !== $accountIban && '' !== $accountIban) { | 
					
						
							| 
									
										
										
										
											2023-11-30 17:28:44 +01:00
										 |  |  |             app('log')->debug('Check if there is not already another account with this IBAN'); | 
					
						
							| 
									
										
										
										
											2023-09-12 05:58:39 +02:00
										 |  |  |             $existing = $this->findExistingAccount($validTypes, ['iban' => $accountIban], true); | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             if (null !== $existing) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $this->sourceError = (string) trans('validation.deposit_src_iban_exists'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-23 11:07:50 +02:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         // if the user submits an ID, but that ID is not of the correct type,
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         // return false.
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         if (null !== $accountId) { | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  |             $search = $this->getRepository()->find($accountId); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('User submitted an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type)); | 
					
						
							|  |  |  |                 app('log')->debug(sprintf('Firefly III accepts ID #%d as valid account data.', $accountId)); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug('ID result is not null and seems valid, save as source account.'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |                 $this->setSource($search); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $result = true; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if user submits an IBAN:
 | 
					
						
							|  |  |  |         if (null !== $accountIban) { | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  |             $search = $this->getRepository()->findByIbanNull($accountIban, $validTypes); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('User submitted IBAN ("%s"), which is a "%s", so this is not a valid source.', $accountIban, $search->accountType->type)); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                 $result = false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug('IBAN result is not null and seems valid, save as source account.'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |                 $this->setSource($search); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $result = true; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if user submits a number:
 | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |         if (null !== $accountNumber && '' !== $accountNumber) { | 
					
						
							| 
									
										
										
										
											2023-09-21 15:50:49 +02:00
										 |  |  |             $search = $this->getRepository()->findByAccountNumber($accountNumber, $validTypes); | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug( | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |                     sprintf('User submitted number ("%s"), which is a "%s", so this is not a valid source.', $accountNumber, $search->accountType->type) | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |                 $result = false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug('Number result is not null and seems valid, save as source account.'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |                 $this->setSource($search); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $result = true; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if the account can be created anyway we don't need to search.
 | 
					
						
							|  |  |  |         if (null === $result && true === $this->canCreateTypes($validTypes)) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $result               = true; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // set the source to be a (dummy) revenue account.
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             $account              = new Account(); | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  |             $accountType          = AccountType::whereType(AccountTypeEnum::REVENUE->value)->first(); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             $account->accountType = $accountType; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             $this->setSource($account); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-23 19:11:25 +02:00
										 |  |  |         return $result ?? false; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-30 07:33:06 +02:00
										 |  |  | } |