. */ declare(strict_types=1); namespace FireflyIII\Validation\Account; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use Log; /** * Trait ReconciliationValidation */ trait ReconciliationValidation { public ?Account $destination; public ?Account $source; /** * @param array $array * * @return bool */ protected function validateReconciliationDestination(array $array): bool { $accountId = array_key_exists('id', $array) ? $array['id'] : null; Log::debug('Now in validateReconciliationDestination', $array); if (null === $accountId) { Log::debug('Return FALSE'); return false; } $result = $this->accountRepository->find($accountId); if (null === $result) { $this->destError = (string) trans('validation.deposit_dest_bad_data', ['id' => $accountId, 'name' => '']); Log::debug('Return FALSE'); return false; } // types depends on type of source: $types = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; // if source is reconciliation, destination can't be. if (null !== $this->source && AccountType::RECONCILIATION === $this->source->accountType->type) { $types = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]; } // if source is not reconciliation, destination MUST be. if (null !== $this->source && in_array( $this->source->accountType->type, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true )) { $types = [AccountType::RECONCILIATION]; } if (in_array($result->accountType->type, $types, true)) { $this->destination = $result; Log::debug('Return TRUE'); return true; } $this->destError = (string) trans('validation.deposit_dest_wrong_type'); Log::debug('Return FALSE'); return false; } /** * @param array $array * * @return bool */ protected function validateReconciliationSource(array $array): bool { $accountId = array_key_exists('id', $array) ? $array['id'] : null; Log::debug('In validateReconciliationSource', $array); if (null === $accountId) { Log::debug('Return FALSE'); return false; } $result = $this->accountRepository->find($accountId); $types = [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE, AccountType::RECONCILIATION]; if (null === $result) { Log::debug('Return FALSE'); return false; } if (in_array($result->accountType->type, $types, true)) { $this->source = $result; Log::debug('Return TRUE'); return true; } Log::debug('Return FALSE'); return false; } }