. */ declare(strict_types=1); namespace FireflyIII\Support\Form; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; /** * Class AccountForm * * All form methods that are account related. * * TODO describe all methods * TODO optimize repositories and methods. */ class AccountForm { use FormSupport; /** * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. */ public function activeDepositDestinations(string $name, mixed $value = null, array $options = null): string { $types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::REVENUE]; $repository = $this->getAccountRepository(); $grouped = $this->getAccountsGrouped($types, $repository); $cash = $repository->getCashAccount(); $key = (string)trans('firefly.cash_account_type'); $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); return $this->select($name, $grouped, $value, $options); } private function getAccountsGrouped(array $types, AccountRepositoryInterface $repository = null): array { if (null === $repository) { $repository = $this->getAccountRepository(); } $accountList = $repository->getActiveAccountsByType($types); $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $grouped = []; /** @var Account $account */ foreach ($accountList as $account) { $role = (string)$repository->getMetaValue($account, 'account_role'); if (in_array($account->accountType->type, $liabilityTypes, true)) { $role = sprintf('l_%s', $account->accountType->type); } if ('' === $role) { $role = 'no_account_type'; if (AccountType::EXPENSE === $account->accountType->type) { $role = 'expense_account'; } if (AccountType::REVENUE === $account->accountType->type) { $role = 'revenue_account'; } } $key = (string)trans(sprintf('firefly.opt_group_%s', $role)); $grouped[$key][$account->id] = $account->name; } return $grouped; } /** * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. */ public function activeWithdrawalDestinations(string $name, mixed $value = null, array $options = null): string { $types = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN, AccountType::EXPENSE]; $repository = $this->getAccountRepository(); $grouped = $this->getAccountsGrouped($types, $repository); $cash = $repository->getCashAccount(); $key = (string)trans('firefly.cash_account_type'); $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); return $this->select($name, $grouped, $value, $options); } /** * Check list of asset accounts. * * @throws FireflyException */ public function assetAccountCheckList(string $name, array $options = null): string { $options ??= []; $label = $this->label($name, $options); $options = $this->expandOptionArray($name, $label, $options); $classes = $this->getHolderClasses($name); $selected = request()->old($name) ?? []; // get all asset accounts: $types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::DEBT]; $grouped = $this->getAccountsGrouped($types); unset($options['class']); try { $html = view('form.assetAccountCheckList', compact('classes', 'selected', 'name', 'label', 'options', 'grouped'))->render(); } catch (\Throwable $e) { app('log')->debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage())); $html = 'Could not render assetAccountCheckList.'; throw new FireflyException($html, 0, $e); } return $html; } /** * Basic list of asset accounts. * * @param mixed $value */ public function assetAccountList(string $name, $value = null, array $options = null): string { $types = [AccountType::ASSET, AccountType::DEFAULT]; $grouped = $this->getAccountsGrouped($types); return $this->select($name, $grouped, $value, $options); } /** * Same list but all liabilities as well. * * @param mixed $value */ public function longAccountList(string $name, $value = null, array $options = null): string { $types = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $grouped = $this->getAccountsGrouped($types); return $this->select($name, $grouped, $value, $options); } }