| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * TransferValidation.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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait TransferValidation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait TransferValidation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateTransferDestination(array $array): bool | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |         $accountId   = array_key_exists('id', $array) ? $array['id'] : null; | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         $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; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateTransferDestination', $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 transfer can't be created.
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->destError = (string)trans('validation.transfer_dest_need_data'); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('Both values are NULL, cant create transfer destination.'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 20:07:09 +01:00
										 |  |  |         // or try to find the account:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $search      = $this->findExistingAccount($validTypes, $array); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         if (null === $search) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->destError = (string)trans('validation.transfer_dest_bad_data', ['id' => $accountId, 'name' => $accountName]); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |         $this->setDestination($search); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // must not be the same as the source account
 | 
					
						
							|  |  |  |         if (null !== $this->source && $this->source->id === $this->destination->id) { | 
					
						
							|  |  |  |             $this->sourceError = 'Source and destination are the same.'; | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             $this->destError   = 'Source and destination are the same.'; | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     abstract protected function canCreateTypes(array $accountTypes): bool; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     abstract protected function findExistingAccount(array $validTypes, array $data): ?Account; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |     protected function validateTransferSource(array $array): bool | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02: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; | 
					
						
							| 
									
										
										
										
											2023-07-14 06:07:16 +02:00
										 |  |  |         $accountNumber = array_key_exists('number', $array) ? $array['number'] : null; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateTransferSource', $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    = array_keys($this->combinations[$this->transactionType]); | 
					
						
							| 
									
										
										
										
											2023-07-14 06:07:16 +02: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 we return false,
 | 
					
						
							|  |  |  |             // because the source of a withdrawal can't be created.
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->sourceError = (string)trans('validation.transfer_source_need_data'); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning('Not a valid source, need more data.'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // otherwise try to find the account:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $search        = $this->findExistingAccount($validTypes, $array); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  |         if (null === $search) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             $this->sourceError = (string)trans('validation.transfer_source_bad_data', ['id' => $accountId, 'name' => $accountName]); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning('Not a valid source, cant find it.', $validTypes); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |         $this->setSource($search); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Valid source!'); | 
					
						
							| 
									
										
										
										
											2020-03-21 08:11:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-30 07:33:06 +02:00
										 |  |  | } |