. */ declare(strict_types=1); namespace FireflyIII\Validation\Account; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use Log; /** * Trait LiabilityValidation */ trait LiabilityValidation { /** * @param int|null $accountId * * @return bool */ protected function validateLCDestination(?int $accountId): bool { Log::debug(sprintf('Now in validateLCDestination(%d)', $accountId)); $result = null; $validTypes = config('firefly.valid_liabilities'); if (null === $accountId) { $this->sourceError = (string)trans('validation.lc_destination_need_data'); $result = false; } Log::debug('Destination ID is not null.'); $search = $this->accountRepository->find($accountId); // the source resulted in an account, but it's not of a valid type. if (null !== $search && !in_array($search->accountType->type, $validTypes, true)) { $message = sprintf('User submitted only an ID (#%d), which is a "%s", so this is not a valid destination.', $accountId, $search->accountType->type); Log::debug($message); $this->sourceError = $message; $result = false; } // the source resulted in an account, AND it's of a valid type. if (null !== $search && in_array($search->accountType->type, $validTypes, true)) { Log::debug(sprintf('Found account of correct type: #%d, "%s"', $search->id, $search->name)); $this->source = $search; $result = true; } return $result ?? false; } /** * Source of an liability credit must be a liability. * * @param string|null $accountName * * @return bool */ protected function validateLCSource(?string $accountName): bool { $result = true; Log::debug(sprintf('Now in validateLCDestination("%s")', $accountName)); if ('' === $accountName || null === $accountName) { $result = false; } if (true === $result) { // set the source to be a (dummy) revenue account. $account = new Account; $accountType = AccountType::whereType(AccountType::LIABILITY_CREDIT)->first(); $account->accountType = $accountType; $this->source = $account; } return $result; } }