| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * OBValidation.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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 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 OBValidation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait OBValidation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateOBDestination(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-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateOBDestination', $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] ?? []; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { | 
					
						
							|  |  |  |             // 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.ob_dest_need_data'); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('Both values are NULL, cant create OB 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.', $validTypes); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $this->destError = (string) trans('validation.ob_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('validateOBDestination(%d, "%s") will return %s', $accountId, $accountName, 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Source of an opening balance can either be an asset account | 
					
						
							|  |  |  |      * or an "initial balance account". The latter can be created. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateOBSource(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; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateOBSource', $array); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $result      = null; | 
					
						
							| 
									
										
										
										
											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]); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $accountId && null === $accountName && false === $this->canCreateTypes($validTypes)) { | 
					
						
							|  |  |  |             // 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.ob_source_need_data'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |             $result            = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // if the user submits an ID only but that ID is not of the correct type,
 | 
					
						
							|  |  |  |         // return false.
 | 
					
						
							|  |  |  |         if (null !== $accountId && null === $accountName) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Source ID is not null, but name is null.'); | 
					
						
							| 
									
										
										
										
											2025-03-08 15:57:59 +01:00
										 |  |  |             $search = $this->accountRepository->find($accountId); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // the source resulted in an account, but it's not of a valid type.
 | 
					
						
							|  |  |  |             if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |                 $message           = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid source.', $accountId, $search->accountType->type); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug($message); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |                 $this->sourceError = $message; | 
					
						
							|  |  |  |                 $result            = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             // the source resulted in an account, AND it's of a valid type.
 | 
					
						
							|  |  |  |             if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |                 app('log')->debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name)); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |                 $this->setSource($search); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $result = true; | 
					
						
							| 
									
										
										
										
											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('Result is still null.'); | 
					
						
							| 
									
										
										
										
											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) initial balance account.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $account              = new Account(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-29 06:36:48 +01:00
										 |  |  |             /** @var AccountType $accountType */ | 
					
						
							| 
									
										
										
										
											2025-01-03 09:15:52 +01:00
										 |  |  |             $accountType          = AccountType::whereType(AccountTypeEnum::INITIAL_BALANCE->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
										 |  |  | } |