| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * CurrencyValidation.php | 
					
						
							| 
									
										
										
										
											2020-06-30 19:05:35 +02:00
										 |  |  |  * Copyright (c) 2020 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 06:00:36 +01:00
										 |  |  | namespace FireflyIII\Validation; | 
					
						
							| 
									
										
										
										
											2021-04-06 08:51:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2025-10-05 12:59:43 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait CurrencyValidation | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This trait contains validation methods that have to do with currencies. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait CurrencyValidation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-02 12:56:48 +01:00
										 |  |  |     public const string TEST = 'Test'; | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * If the transactions contain foreign amounts, there must also be foreign currency information. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function validateForeignCurrencyInformation(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-09 06:33:23 +01:00
										 |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							| 
									
										
										
										
											2023-03-07 19:36:54 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now in validateForeignCurrencyInformation()'); | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($transactions as $index => $transaction) { | 
					
						
							| 
									
										
										
										
											2023-01-29 15:29:42 +01:00
										 |  |  |             if (!is_array($transaction)) { | 
					
						
							| 
									
										
										
										
											2023-01-21 12:21:06 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!array_key_exists('foreign_amount', $transaction) && !array_key_exists('foreign_currency_id', $transaction) && !array_key_exists('foreign_currency_code', $transaction)) { | 
					
						
							|  |  |  |                 Log::debug('validateForeignCurrencyInformation: no foreign currency information present at all.'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 07:26:12 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $foreignAmount = (string) ($transaction['foreign_amount'] ?? ''); | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |             $foreignId     = $transaction['foreign_currency_id'] ?? null; | 
					
						
							|  |  |  |             $foreignCode   = $transaction['foreign_currency_code'] ?? null; | 
					
						
							| 
									
										
										
										
											2024-01-04 15:42:00 +01:00
										 |  |  |             if ('' === $foreignAmount) { | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |                 Log::debug('validateForeignCurrencyInformation: foreign amount is "".'); | 
					
						
							|  |  |  |                 if ( | 
					
						
							|  |  |  |                     (array_key_exists('foreign_currency_id', $transaction) || array_key_exists('foreign_currency_code', $transaction)) | 
					
						
							|  |  |  |                     && (null !== $foreignId || null !== $foreignCode) | 
					
						
							|  |  |  |                 ) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     $validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_amount')); | 
					
						
							|  |  |  |                     $validator->errors()->add('transactions.'.$index.'.foreign_currency_id', (string) trans('validation.require_currency_amount')); | 
					
						
							|  |  |  |                     $validator->errors()->add('transactions.'.$index.'.foreign_currency_code', (string) trans('validation.require_currency_amount')); | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 07:26:12 +01:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 11:09:40 +01:00
										 |  |  |             $compare       = bccomp('0', $foreignAmount); | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |             if (-1 === $compare) { | 
					
						
							|  |  |  |                 Log::debug('validateForeignCurrencyInformation: array contains foreign amount info.'); | 
					
						
							|  |  |  |                 if (!array_key_exists('foreign_currency_id', $transaction) && !array_key_exists('foreign_currency_code', $transaction)) { | 
					
						
							|  |  |  |                     Log::debug('validateForeignCurrencyInformation: array contains NO foreign currency info.'); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     $validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_info')); | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             if (0 === $compare && ('' !== (string) $foreignId || '' !== (string) $foreignCode)) { | 
					
						
							| 
									
										
										
										
											2024-01-06 08:33:38 +01:00
										 |  |  |                 Log::debug('validateForeignCurrencyInformation: array contains foreign currency info, but zero amount.'); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 $validator->errors()->add('transactions.'.$index.'.foreign_currency_id', (string) trans('validation.require_currency_amount')); | 
					
						
							|  |  |  |                 $validator->errors()->add('transactions.'.$index.'.foreign_currency_code', (string) trans('validation.require_currency_amount')); | 
					
						
							|  |  |  |                 $validator->errors()->add('transactions.'.$index.'.foreign_amount', (string) trans('validation.require_currency_amount')); | 
					
						
							| 
									
										
										
										
											2020-03-21 06:00:36 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     abstract protected function getTransactionsArray(Validator $validator): array; | 
					
						
							| 
									
										
										
										
											2020-05-30 07:33:06 +02:00
										 |  |  | } |