| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * TransactionValidation.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:58:22 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +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. | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +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. | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +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/>. | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Validation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 05:55:31 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | use FireflyIII\Models\AccountType; | 
					
						
							| 
									
										
										
										
											2019-06-02 16:33:25 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-04-15 07:17:48 +02:00
										 |  |  | use Illuminate\Validation\Validator; | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait TransactionValidation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait TransactionValidation | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validates the given account information. Switches on given transaction type. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function validateAccountInformation(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-07 19:36:54 +01:00
										 |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |         Log::debug('Now in validateAccountInformation (TransactionValidation) ()'); | 
					
						
							| 
									
										
										
										
											2020-02-06 21:54:47 +01:00
										 |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  |         $data         = $validator->getData(); | 
					
						
							| 
									
										
										
										
											2019-06-09 09:58:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-08 06:19:21 +02:00
										 |  |  |         $transactionType = $data['type'] ?? 'invalid'; | 
					
						
							| 
									
										
										
										
											2020-02-05 20:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |         Log::debug(sprintf('Going to loop %d transaction(s)', count($transactions))); | 
					
						
							|  |  |  |         /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |          * @var int   $index | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |          * @var array $transaction | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         foreach ($transactions as $index => $transaction) { | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  |             if (!is_int($index)) { | 
					
						
							| 
									
										
										
										
											2020-10-24 16:49:05 +02:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |             $this->validateSingleAccount($validator, $index, $transactionType, $transaction); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getTransactionsArray(Validator $validator): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-04-15 07:17:48 +02:00
										 |  |  |         Log::debug('Now in getTransactionsArray'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $data         = $validator->getData(); | 
					
						
							| 
									
										
										
										
											2023-04-15 07:17:48 +02:00
										 |  |  |         $transactions = []; | 
					
						
							|  |  |  |         if (is_array($data) && array_key_exists('transactions', $data) && is_array($data['transactions'])) { | 
					
						
							|  |  |  |             Log::debug('Transactions key exists and is array.'); | 
					
						
							|  |  |  |             $transactions = $data['transactions']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (is_array($data) && array_key_exists('transactions', $data) && !is_array($data['transactions'])) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Transactions key exists but is NOT array,  its a %s', gettype($data['transactions']))); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // should be impossible to hit this:
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         if (!is_countable($transactions)) { | 
					
						
							|  |  |  |             Log::error(sprintf('Transactions array is not countable, because its a %s', gettype($transactions))); | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-06 15:59:31 +02:00
										 |  |  |         //Log::debug('Returning transactions.', $transactions);
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $transactions; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							|  |  |  |      * @param int       $index | 
					
						
							|  |  |  |      * @param string    $transactionType | 
					
						
							|  |  |  |      * @param array     $transaction | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function validateSingleAccount(Validator $validator, int $index, string $transactionType, array $transaction): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  |         /** @var AccountValidator $accountValidator */ | 
					
						
							|  |  |  |         $accountValidator = app(AccountValidator::class); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |         $transactionType = $transaction['type'] ?? $transactionType; | 
					
						
							|  |  |  |         $accountValidator->setTransactionType($transactionType); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:36:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |         // validate source account.
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         $sourceId     = array_key_exists('source_id', $transaction) ? (int)$transaction['source_id'] : null; | 
					
						
							|  |  |  |         $sourceName   = array_key_exists('source_name', $transaction) ? (string)$transaction['source_name'] : null; | 
					
						
							|  |  |  |         $sourceIban   = array_key_exists('source_iban', $transaction) ? (string)$transaction['source_iban'] : null; | 
					
						
							|  |  |  |         $sourceNumber = array_key_exists('source_number', $transaction) ? (string)$transaction['source_number'] : null; | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         $source       = [ | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             'id'     => $sourceId, | 
					
						
							|  |  |  |             'name'   => $sourceName, | 
					
						
							|  |  |  |             'iban'   => $sourceIban, | 
					
						
							|  |  |  |             'number' => $sourceNumber, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         $validSource  = $accountValidator->validateSource($source); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |         // do something with result:
 | 
					
						
							|  |  |  |         if (false === $validSource) { | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // validate destination account
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |         $destinationId     = array_key_exists('destination_id', $transaction) ? (int)$transaction['destination_id'] : null; | 
					
						
							|  |  |  |         $destinationName   = array_key_exists('destination_name', $transaction) ? (string)$transaction['destination_name'] : null; | 
					
						
							|  |  |  |         $destinationIban   = array_key_exists('destination_iban', $transaction) ? (string)$transaction['destination_iban'] : null; | 
					
						
							|  |  |  |         $destinationNumber = array_key_exists('destination_number', $transaction) ? (string)$transaction['destination_number'] : null; | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         $destination       = [ | 
					
						
							| 
									
										
										
										
											2021-12-18 12:35:17 +01:00
										 |  |  |             'id'     => $destinationId, | 
					
						
							|  |  |  |             'name'   => $destinationName, | 
					
						
							|  |  |  |             'iban'   => $destinationIban, | 
					
						
							|  |  |  |             'number' => $destinationNumber, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         $validDestination  = $accountValidator->validateDestination($destination); | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |         // do something with result:
 | 
					
						
							|  |  |  |         if (false === $validDestination) { | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         // sanity check for reconciliation accounts. They can't both be null.
 | 
					
						
							|  |  |  |         $this->sanityCheckReconciliation($validator, $transactionType, $index, $source, $destination); | 
					
						
							| 
									
										
										
										
											2023-03-07 19:36:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // sanity check for currency information.
 | 
					
						
							|  |  |  |         $this->sanityCheckForeignCurrency($validator, $accountValidator, $transaction, $transactionType, $index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Validator $validator | 
					
						
							|  |  |  |      * @param string    $transactionType | 
					
						
							|  |  |  |      * @param int       $index | 
					
						
							|  |  |  |      * @param array     $source | 
					
						
							|  |  |  |      * @param array     $destination | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function sanityCheckReconciliation(Validator $validator, string $transactionType, int $index, array $source, array $destination): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Now in sanityCheckReconciliation'); | 
					
						
							|  |  |  |         if (TransactionType::RECONCILIATION === ucfirst($transactionType) && | 
					
						
							|  |  |  |             null === $source['id'] && null === $source['name'] && null === $destination['id'] && null === $destination['name'] | 
					
						
							|  |  |  |         ) { | 
					
						
							|  |  |  |             Log::debug('Both are NULL, error!'); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_id', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_name', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (TransactionType::RECONCILIATION === $transactionType && | 
					
						
							|  |  |  |             (null !== $source['id'] || null !== $source['name']) && | 
					
						
							|  |  |  |             (null !== $destination['id'] || null !== $destination['name'])) { | 
					
						
							|  |  |  |             Log::debug('Both are not NULL, error!'); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_id', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.source_name', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |             $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), trans('validation.reconciliation_either_account')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TODO describe this method. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Validator        $validator | 
					
						
							|  |  |  |      * @param AccountValidator $accountValidator | 
					
						
							|  |  |  |      * @param array            $transaction | 
					
						
							|  |  |  |      * @param string           $transactionType | 
					
						
							|  |  |  |      * @param int              $index | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function sanityCheckForeignCurrency( | 
					
						
							|  |  |  |         Validator        $validator, | 
					
						
							|  |  |  |         AccountValidator $accountValidator, | 
					
						
							|  |  |  |         array            $transaction, | 
					
						
							|  |  |  |         string           $transactionType, | 
					
						
							|  |  |  |         int              $index | 
					
						
							|  |  |  |     ): void { | 
					
						
							|  |  |  |         Log::debug('Now in sanityCheckForeignCurrency()'); | 
					
						
							|  |  |  |         if (0 !== $validator->errors()->count()) { | 
					
						
							|  |  |  |             Log::debug('Already have errors, return'); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $accountValidator->source) { | 
					
						
							|  |  |  |             Log::debug('No source, return'); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null === $accountValidator->destination) { | 
					
						
							|  |  |  |             Log::debug('No destination, return'); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $source      = $accountValidator->source; | 
					
						
							|  |  |  |         $destination = $accountValidator->destination; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Source: #%d "%s (%s)"', $source->id, $source->name, $source->accountType->type)); | 
					
						
							|  |  |  |         Log::debug(sprintf('Destination: #%d "%s" (%s)', $destination->id, $destination->name, $source->accountType->type)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$this->isLiabilityOrAsset($source) || !$this->isLiabilityOrAsset($destination)) { | 
					
						
							|  |  |  |             Log::debug('Any account must be liability or asset account to continue.'); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var AccountRepositoryInterface $accountRepository */ | 
					
						
							|  |  |  |         $accountRepository   = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $defaultCurrency     = app('amount')->getDefaultCurrency(); | 
					
						
							|  |  |  |         $sourceCurrency      = $accountRepository->getAccountCurrency($source) ?? $defaultCurrency; | 
					
						
							|  |  |  |         $destinationCurrency = $accountRepository->getAccountCurrency($destination) ?? $defaultCurrency; | 
					
						
							|  |  |  |         // if both accounts have the same currency, continue.
 | 
					
						
							|  |  |  |         if ($sourceCurrency->code === $destinationCurrency->code) { | 
					
						
							|  |  |  |             Log::debug('Both accounts have the same currency, continue.'); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Source account expects %s', $sourceCurrency->code)); | 
					
						
							|  |  |  |         Log::debug(sprintf('Destination account expects %s', $destinationCurrency->code)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Amount is %s', $transaction['amount'])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (TransactionType::DEPOSIT === ucfirst($transactionType)) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Processing as a "%s"', $transactionType)); | 
					
						
							|  |  |  |             // use case: deposit from liability account to an asset account
 | 
					
						
							|  |  |  |             // the foreign amount must be in the currency of the source
 | 
					
						
							|  |  |  |             // the amount must be in the currency of the destination
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // no foreign currency information is present:
 | 
					
						
							|  |  |  |             if (!$this->hasForeignCurrencyInfo($transaction)) { | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_currency')); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // wrong currency information is present
 | 
					
						
							|  |  |  |             $foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false; | 
					
						
							|  |  |  |             $foreignCurrencyId   = (int)($transaction['foreign_currency_id'] ?? 0); | 
					
						
							|  |  |  |             Log::debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); | 
					
						
							|  |  |  |             if ($foreignCurrencyCode !== $sourceCurrency->code && $foreignCurrencyId !== (int)$sourceCurrency->id) { | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.foreign_currency_code', $index), (string)trans('validation.require_foreign_src')); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (TransactionType::TRANSFER === ucfirst($transactionType) || TransactionType::WITHDRAWAL === ucfirst($transactionType)) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Processing as a "%s"', $transactionType)); | 
					
						
							|  |  |  |             // use case: withdrawal from asset account to a liability account.
 | 
					
						
							|  |  |  |             // the foreign amount must be in the currency of the destination
 | 
					
						
							|  |  |  |             // the amount must be in the currency of the source
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // use case: transfer between accounts with different currencies.
 | 
					
						
							|  |  |  |             // the foreign amount must be in the currency of the destination
 | 
					
						
							|  |  |  |             // the amount must be in the currency of the source
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // no foreign currency information is present:
 | 
					
						
							|  |  |  |             if (!$this->hasForeignCurrencyInfo($transaction)) { | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_currency')); | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // wrong currency information is present
 | 
					
						
							|  |  |  |             $foreignCurrencyCode = $transaction['foreign_currency_code'] ?? false; | 
					
						
							|  |  |  |             $foreignCurrencyId   = (int)($transaction['foreign_currency_id'] ?? 0); | 
					
						
							|  |  |  |             Log::debug(sprintf('Foreign currency code seems to be #%d "%s"', $foreignCurrencyId, $foreignCurrencyCode), $transaction); | 
					
						
							|  |  |  |             if ($foreignCurrencyCode !== $destinationCurrency->code && $foreignCurrencyId !== (int)$destinationCurrency->id) { | 
					
						
							|  |  |  |                 Log::debug(sprintf('No match on code, "%s" vs "%s"', $foreignCurrencyCode, $destinationCurrency->code)); | 
					
						
							|  |  |  |                 Log::debug(sprintf('No match on ID, #%d vs #%d', $foreignCurrencyId, $destinationCurrency->id)); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.foreign_amount', $index), (string)trans('validation.require_foreign_dest')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function isLiabilityOrAsset(Account $account): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->isLiability($account) || $this->isAsset($account); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function isLiability(Account $account): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = $account->accountType?->type; | 
					
						
							|  |  |  |         if (in_array($type, config('firefly.valid_liabilities'), true)) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Account $account | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function isAsset(Account $account): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $type = $account->accountType?->type; | 
					
						
							|  |  |  |         return $type === AccountType::ASSET; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $transaction | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function hasForeignCurrencyInfo(array $transaction): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!array_key_exists('foreign_currency_code', $transaction) && !array_key_exists('foreign_currency_id', $transaction)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!array_key_exists('foreign_amount', $transaction)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('' === $transaction['foreign_amount']) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (bccomp('0', $transaction['foreign_amount']) === 0) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validates the given account information. Switches on given transaction type. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * @param Validator        $validator | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param TransactionGroup $transactionGroup | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function validateAccountInformationUpdate(Validator $validator, TransactionGroup $transactionGroup): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Now in validateAccountInformationUpdate()'); | 
					
						
							|  |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** | 
					
						
							|  |  |  |          * @var int   $index | 
					
						
							|  |  |  |          * @var array $transaction | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         foreach ($transactions as $index => $transaction) { | 
					
						
							|  |  |  |             if (!is_int($index)) { | 
					
						
							|  |  |  |                 throw new FireflyException('Invalid data submitted: transaction is not array.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->validateSingleUpdate($validator, $index, $transaction, $transactionGroup); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator        $validator | 
					
						
							|  |  |  |      * @param int              $index | 
					
						
							|  |  |  |      * @param array            $transaction | 
					
						
							|  |  |  |      * @param TransactionGroup $transactionGroup | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     protected function validateSingleUpdate(Validator $validator, int $index, array $transaction, TransactionGroup $transactionGroup): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Now validating single account update in validateSingleUpdate()'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         // if no account types are given, just skip the check.
 | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             !array_key_exists('source_id', $transaction) | 
					
						
							|  |  |  |             && !array_key_exists('source_name', $transaction) | 
					
						
							|  |  |  |             && !array_key_exists('destination_id', $transaction) | 
					
						
							|  |  |  |             && !array_key_exists('destination_name', $transaction)) { | 
					
						
							|  |  |  |             Log::debug('No account data has been submitted so will not validating account info.'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         // create validator:
 | 
					
						
							|  |  |  |         /** @var AccountValidator $accountValidator */ | 
					
						
							|  |  |  |         $accountValidator = app(AccountValidator::class); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         // get the transaction type using the original transaction group:
 | 
					
						
							|  |  |  |         $accountValidator->setTransactionType($this->getTransactionType($transactionGroup, [])); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |         // validate if the submitted source ID/name/iban/number are valid
 | 
					
						
							|  |  |  |         if ( | 
					
						
							|  |  |  |             array_key_exists('source_id', $transaction) || | 
					
						
							|  |  |  |             array_key_exists('source_name', $transaction) || | 
					
						
							|  |  |  |             array_key_exists('source_iban', $transaction) || | 
					
						
							|  |  |  |             array_key_exists('source_number', $transaction) | 
					
						
							|  |  |  |         ) { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             Log::debug('Will try to validate source account information.'); | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             $sourceId     = (int)($transaction['source_id'] ?? 0); | 
					
						
							|  |  |  |             $sourceName   = $transaction['source_name'] ?? null; | 
					
						
							|  |  |  |             $sourceIban   = $transaction['source_iban'] ?? null; | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |             $sourceNumber = $transaction['source_number'] ?? null; | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             $validSource  = $accountValidator->validateSource( | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |                 ['id' => $sourceId, 'name' => $sourceName, 'iban' => $sourceIban, 'number' => $sourceNumber] | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2023-03-12 18:18:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             // do something with result:
 | 
					
						
							|  |  |  |             if (false === $validSource) { | 
					
						
							|  |  |  |                 app('log')->warning('Looks like the source account is not valid so complain to the user about it.'); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError); | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |                 $validator->errors()->add(sprintf('transactions.%d.source_iban', $index), $accountValidator->sourceError); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.source_number', $index), $accountValidator->sourceError); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             Log::debug('Source account info is valid.'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             array_key_exists('destination_id', $transaction) || | 
					
						
							|  |  |  |             array_key_exists('destination_name', $transaction) || | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             array_key_exists('destination_iban', $transaction) || | 
					
						
							| 
									
										
										
										
											2023-06-24 08:27:28 +02:00
										 |  |  |             array_key_exists('destination_number', $transaction) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ) { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             Log::debug('Will try to validate destination account information.'); | 
					
						
							|  |  |  |             // at this point the validator may not have a source account, because it was never submitted for validation.
 | 
					
						
							|  |  |  |             // must add it ourselves or the validator can never check if the destination is correct.
 | 
					
						
							|  |  |  |             // the $transaction array must have a journal id or it's just one, this was validated before.
 | 
					
						
							|  |  |  |             if (null === $accountValidator->source) { | 
					
						
							|  |  |  |                 Log::debug('Account validator has no source account, must find it.'); | 
					
						
							|  |  |  |                 $source = $this->getOriginalSource($transaction, $transactionGroup); | 
					
						
							|  |  |  |                 if (null !== $source) { | 
					
						
							|  |  |  |                     Log::debug('Found a source!'); | 
					
						
							|  |  |  |                     $accountValidator->source = $source; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-03-07 19:36:54 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             $destinationId     = (int)($transaction['destination_id'] ?? 0); | 
					
						
							|  |  |  |             $destinationName   = $transaction['destination_name'] ?? null; | 
					
						
							|  |  |  |             $destinationIban   = $transaction['destination_iban'] ?? null; | 
					
						
							|  |  |  |             $destinationNumber = $transaction['destination_number'] ?? null; | 
					
						
							|  |  |  |             $array             = ['id' => $destinationId, 'name' => $destinationName, 'iban' => $destinationIban, 'number' => $destinationNumber]; | 
					
						
							|  |  |  |             $validDestination  = $accountValidator->validateDestination($array); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             // do something with result:
 | 
					
						
							|  |  |  |             if (false === $validDestination) { | 
					
						
							|  |  |  |                 app('log')->warning('Looks like the destination account is not valid so complain to the user about it.'); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); | 
					
						
							|  |  |  |                 $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             Log::debug('Destination account info is valid.'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         Log::debug('Done with validateSingleUpdate().'); | 
					
						
							| 
									
										
										
										
											2023-03-11 07:09:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param TransactionGroup $group | 
					
						
							|  |  |  |      * @param array            $transactions | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getTransactionType(TransactionGroup $group, array $transactions): string | 
					
						
							| 
									
										
										
										
											2020-03-15 15:31:59 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $transactions[0]['type'] ?? strtolower($group->transactionJournals()->first()->transactionType->type); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-05 20:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param array            $transaction | 
					
						
							|  |  |  |      * @param TransactionGroup $transactionGroup | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return Account|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getOriginalSource(array $transaction, TransactionGroup $transactionGroup): ?Account | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (1 === $transactionGroup->transactionJournals->count()) { | 
					
						
							|  |  |  |             $journal = $transactionGroup->transactionJournals->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $journal->transactions()->where('amount', '<', 0)->first()->account; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /** @var TransactionJournal $journal */ | 
					
						
							|  |  |  |         foreach ($transactionGroup->transactionJournals as $journal) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |             if ((int)$journal->id === (int)$transaction['transaction_journal_id']) { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |                 return $journal->transactions()->where('amount', '<', 0)->first()->account; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 16:49:05 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Adds an error to the validator when there are no transactions in the array of data. | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2020-10-24 16:49:05 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function validateOneRecurrenceTransaction(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Now in validateOneRecurrenceTransaction()'); | 
					
						
							|  |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // need at least one transaction
 | 
					
						
							|  |  |  |         if (0 === count($transactions)) { | 
					
						
							|  |  |  |             $validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction')); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-24 16:49:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Adds an error to the validator when there are no transactions in the array of data. | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function validateOneTransaction(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Now in validateOneTransaction'); | 
					
						
							|  |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							|  |  |  |             Log::debug('Validator already has errors, so return.'); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  |         // need at least one transaction
 | 
					
						
							|  |  |  |         if (0 === count($transactions)) { | 
					
						
							|  |  |  |             $validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction')); | 
					
						
							|  |  |  |             Log::debug('Added error: at_least_one_transaction.'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Added NO errors.'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     public function validateTransactionArray(Validator $validator): void | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  |         foreach ($transactions as $key => $value) { | 
					
						
							|  |  |  |             if (!is_int($key)) { | 
					
						
							|  |  |  |                 $validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction')); | 
					
						
							|  |  |  |                 Log::debug('Added error: at_least_one_transaction.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * All types of splits must be equal. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Validator $validator | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateTransactionTypes(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('Now in validateTransactionTypes()'); | 
					
						
							|  |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							| 
									
										
										
										
											2018-07-05 18:02:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $types = []; | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $types[] = $transaction['type'] ?? 'invalid'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $unique = array_unique($types); | 
					
						
							|  |  |  |         if (count($unique) > 1) { | 
					
						
							|  |  |  |             $validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal')); | 
					
						
							| 
									
										
										
										
											2021-03-10 06:34:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $first = $unique[0] ?? 'invalid'; | 
					
						
							|  |  |  |         if ('invalid' === $first) { | 
					
						
							|  |  |  |             $validator->errors()->add('transactions.0.type', (string)trans('validation.invalid_transaction_type')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-06 08:10:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * All types of splits must be equal. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Validator $validator | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validateTransactionTypesForUpdate(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Now in validateTransactionTypesForUpdate()'); | 
					
						
							|  |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  |         $types        = []; | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $originalType = $this->getOriginalType((int)($transaction['transaction_journal_id'] ?? 0)); | 
					
						
							|  |  |  |             // if type is not set, fall back to the type of the journal, if one is given.
 | 
					
						
							|  |  |  |             $types[] = $transaction['type'] ?? $originalType; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $unique = array_unique($types); | 
					
						
							|  |  |  |         if (count($unique) > 1) { | 
					
						
							|  |  |  |             app('log')->warning('Add error for mismatch transaction types.'); | 
					
						
							|  |  |  |             $validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal')); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         Log::debug('No errors in validateTransactionTypesForUpdate()'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param int $journalId | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getOriginalType(int $journalId): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (0 === $journalId) { | 
					
						
							|  |  |  |             return 'invalid'; | 
					
						
							| 
									
										
										
										
											2019-08-26 19:09:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var TransactionJournal|null $journal */ | 
					
						
							|  |  |  |         $journal = TransactionJournal::with(['transactionType'])->find($journalId); | 
					
						
							|  |  |  |         if (null !== $journal) { | 
					
						
							|  |  |  |             return strtolower($journal->transactionType->type); | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 'invalid'; | 
					
						
							| 
									
										
										
										
											2019-06-04 20:42:11 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator $validator | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function validateEqualAccounts(Validator $validator): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-07 19:36:54 +01:00
										 |  |  |         if ($validator->errors()->count() > 0) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-22 11:05:16 +01:00
										 |  |  |         Log::debug('Now in validateEqualAccounts()'); | 
					
						
							| 
									
										
										
										
											2020-03-19 15:16:59 +01:00
										 |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							| 
									
										
										
										
											2020-02-05 20:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |         // needs to be split
 | 
					
						
							|  |  |  |         if (count($transactions) < 2) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $type    = $transactions[0]['type'] ?? 'withdrawal'; | 
					
						
							|  |  |  |         $sources = []; | 
					
						
							|  |  |  |         $dests   = []; | 
					
						
							|  |  |  |         foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |             $sources[] = sprintf('%d-%s', $transaction['source_id'] ?? 0, $transaction['source_name'] ?? ''); | 
					
						
							|  |  |  |             $dests[]   = sprintf('%d-%s', $transaction['destination_id'] ?? 0, $transaction['destination_name'] ?? ''); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $sources = array_unique($sources); | 
					
						
							|  |  |  |         $dests   = array_unique($dests); | 
					
						
							|  |  |  |         switch ($type) { | 
					
						
							| 
									
										
										
										
											2020-10-03 17:53:23 +02:00
										 |  |  |             default: | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |             case 'withdrawal': | 
					
						
							|  |  |  |                 if (count($sources) > 1) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                     $validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'deposit': | 
					
						
							|  |  |  |                 if (count($dests) > 1) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                     $validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case'transfer': | 
					
						
							|  |  |  |                 if (count($sources) > 1 || count($dests) > 1) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                     $validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							|  |  |  |                     $validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param Validator        $validator | 
					
						
							|  |  |  |      * @param TransactionGroup $transactionGroup | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void | 
					
						
							| 
									
										
										
										
											2019-04-06 11:08:46 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         Log::debug('Now in validateEqualAccountsForUpdate()'); | 
					
						
							|  |  |  |         $transactions = $this->getTransactionsArray($validator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (2 !== count($transactions)) { | 
					
						
							|  |  |  |             Log::debug('Less than 2 transactions, do nothing.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $type = $this->getTransactionType($transactionGroup, $transactions); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-04 05:49:32 +01:00
										 |  |  |         // compare source IDs, destination IDs, source names and destination names.
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         // I think I can get away with one combination being equal, as long as the rest
 | 
					
						
							|  |  |  |         // of the code picks up on this as well.
 | 
					
						
							|  |  |  |         // either way all fields must be blank or all equal
 | 
					
						
							| 
									
										
										
										
											2022-10-02 20:13:32 +02:00
										 |  |  |         // but if IDs are equal don't bother with the names.
 | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |         $comparison = $this->collectComparisonData($transactions); | 
					
						
							|  |  |  |         $result     = $this->compareAccountData($type, $comparison); | 
					
						
							|  |  |  |         if (false === $result) { | 
					
						
							|  |  |  |             if ('withdrawal' === $type) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 $validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if ('deposit' === $type) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 $validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             if ('transfer' === $type) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:40 +01:00
										 |  |  |                 $validator->errors()->add('transactions.0.source_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							|  |  |  |                 $validator->errors()->add('transactions.0.destination_id', (string)trans('validation.all_accounts_equal')); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning('Add error about equal accounts.'); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug('No errors found in validateEqualAccountsForUpdate'); | 
					
						
							| 
									
										
										
										
											2020-10-19 06:41:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $transactions | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function collectComparisonData(array $transactions): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $fields     = ['source_id', 'destination_id', 'source_name', 'destination_name']; | 
					
						
							|  |  |  |         $comparison = []; | 
					
						
							|  |  |  |         foreach ($fields as $field) { | 
					
						
							|  |  |  |             $comparison[$field] = []; | 
					
						
							|  |  |  |             /** @var array $transaction */ | 
					
						
							|  |  |  |             foreach ($transactions as $transaction) { | 
					
						
							|  |  |  |                 // source or destination may be omitted. If this is the case, use the original source / destination name + ID.
 | 
					
						
							|  |  |  |                 $originalData = $this->getOriginalData((int)($transaction['transaction_journal_id'] ?? 0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // get field.
 | 
					
						
							|  |  |  |                 $comparison[$field][] = $transaction[$field] ?? $originalData[$field]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $comparison; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param int $journalId | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getOriginalData(int $journalId): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return = [ | 
					
						
							|  |  |  |             'source_id'        => 0, | 
					
						
							|  |  |  |             'source_name'      => '', | 
					
						
							|  |  |  |             'destination_id'   => 0, | 
					
						
							|  |  |  |             'destination_name' => '', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         if (0 === $journalId) { | 
					
						
							|  |  |  |             return $return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /** @var Transaction $source */ | 
					
						
							|  |  |  |         $source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first(); | 
					
						
							|  |  |  |         if (null !== $source) { | 
					
						
							|  |  |  |             $return['source_id']   = $source->account_id; | 
					
						
							|  |  |  |             $return['source_name'] = $source->account->name; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /** @var Transaction $destination */ | 
					
						
							|  |  |  |         $destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first(); | 
					
						
							|  |  |  |         if (null !== $source) { | 
					
						
							|  |  |  |             $return['destination_id']   = $destination->account_id; | 
					
						
							|  |  |  |             $return['destination_name'] = $destination->account->name; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $type | 
					
						
							|  |  |  |      * @param array  $comparison | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function compareAccountData(string $type, array $comparison): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return match ($type) { | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |             default    => $this->compareAccountDataWithdrawal($comparison), | 
					
						
							|  |  |  |             'deposit'  => $this->compareAccountDataDeposit($comparison), | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'transfer' => $this->compareAccountDataTransfer($comparison), | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $comparison | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function compareAccountDataWithdrawal(array $comparison): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['source_id'])) { | 
					
						
							|  |  |  |             // source ID's are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['source_name'])) { | 
					
						
							|  |  |  |             // source names are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function arrayEqual(array $array): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return 1 === count(array_unique($array)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $comparison | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function compareAccountDataDeposit(array $comparison): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['destination_id'])) { | 
					
						
							|  |  |  |             // destination ID's are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['destination_name'])) { | 
					
						
							|  |  |  |             // destination names are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $comparison | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function compareAccountDataTransfer(array $comparison): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['source_id'])) { | 
					
						
							|  |  |  |             // source ID's are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['source_name'])) { | 
					
						
							|  |  |  |             // source names are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['destination_id'])) { | 
					
						
							|  |  |  |             // destination ID's are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->arrayEqual($comparison['destination_name'])) { | 
					
						
							|  |  |  |             // destination names are equal, return void.
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |