From 6e2978231bf0d909603ef6e9c7c23d87a1c38c1d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 10 Aug 2019 15:09:44 +0200 Subject: [PATCH] Refactor the expandedform methods. First commit to see how Scrutinizer likes this. This commit will break most views. --- app/Providers/FireflyServiceProvider.php | 8 + app/Support/ExpandedForm.php | 772 +----------------- app/Support/Facades/AccountForm.php | 44 + app/Support/Form/AccountForm.php | 380 +++++++++ app/Support/Form/CurrencyForm.php | 219 +++++ app/Support/Form/FormSupport.php | 144 ++++ app/Support/Form/PiggyBankForm.php | 60 ++ app/Support/Form/RuleForm.php | 88 ++ config/app.php | 2 + config/twigbridge.php | 33 +- .../v1/import/file/configure-upload.twig | 2 +- resources/views/v1/piggy-banks/create.twig | 2 +- resources/views/v1/recurring/create.twig | 7 +- resources/views/v1/transactions/convert.twig | 162 ---- .../views/v1/transactions/split/edit.twig | 337 -------- 15 files changed, 970 insertions(+), 1290 deletions(-) create mode 100644 app/Support/Facades/AccountForm.php create mode 100644 app/Support/Form/AccountForm.php create mode 100644 app/Support/Form/CurrencyForm.php create mode 100644 app/Support/Form/FormSupport.php create mode 100644 app/Support/Form/PiggyBankForm.php create mode 100644 app/Support/Form/RuleForm.php delete mode 100644 resources/views/v1/transactions/split/edit.twig diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index d297c39294..4d6957ff89 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -55,6 +55,7 @@ use FireflyIII\Services\Password\Verifier; use FireflyIII\Support\Amount; use FireflyIII\Support\ExpandedForm; use FireflyIII\Support\FireflyConfig; +use FireflyIII\Support\Form\AccountForm; use FireflyIII\Support\Navigation; use FireflyIII\Support\Preferences; use FireflyIII\Support\Steam; @@ -147,6 +148,13 @@ class FireflyServiceProvider extends ServiceProvider } ); + $this->app->bind( + 'accountform', + static function () { + return new AccountForm; + } + ); + // chart generator: $this->app->bind(GeneratorInterface::class, ChartJsGenerator::class); diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index 603127fe04..a938b73572 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -35,6 +35,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; +use FireflyIII\Support\Form\FormSupport; use Form; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; @@ -52,221 +53,7 @@ use Throwable; */ class ExpandedForm { - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function activeAssetAccountList(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - $accountList = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - // group accounts: - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); - $currency = $currencyRepos->findNull($currencyId); - $role = $repository->getMetaValue($account, 'account_role'); - if ('' === $role) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - - if (null === $currency) { - $currency = $defaultCurrency; - } - - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - return $this->select($name, $grouped, $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function activeLongAccountList(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - $accountList = $repository->getActiveAccountsByType( - [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,] - ); - $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - // group accounts: - - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - - $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; - - $role = $repository->getMetaValue($account, 'account_role'); - - if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - - if (in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore - } - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - - return $this->select($name, $grouped, $value, $options); - } - - /** - * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. - * - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function activeWithdrawalDestinations(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - - $accountList = $repository->getActiveAccountsByType( - [ - AccountType::MORTGAGE, - AccountType::DEBT, - AccountType::CREDITCARD, - AccountType::LOAN, - AccountType::EXPENSE, - ] - ); - $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - - // add cash account first: - $cash = $repository->getCashAccount(); - $key = (string)trans('firefly.cash_account_type'); - $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); - - // group accounts: - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; - $role = (string)$repository->getMetaValue($account, 'account_role'); - if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - if ('no_account_type' === $role && AccountType::EXPENSE === $account->accountType->type) { - $role = 'expense_account'; // @codeCoverageIgnore - - } - - if (in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore - } - - if (null === $currency) { - $currency = $defaultCurrency; - } - - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - return $this->select($name, $grouped, $value, $options); - } - - /** - * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. - * - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function activeDepositDestinations(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - - $accountList = $repository->getActiveAccountsByType( - [ - AccountType::MORTGAGE, - AccountType::DEBT, - AccountType::CREDITCARD, - AccountType::LOAN, - AccountType::REVENUE, - ] - ); - $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - - // add cash account first: - $cash = $repository->getCashAccount(); - $key = (string)trans('firefly.cash_account_type'); - $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); - - // group accounts: - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; - $role = (string)$repository->getMetaValue($account, 'account_role'); - if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - if ('no_account_type' === $role && AccountType::REVENUE === $account->accountType->type) { - $role = 'revenue_account'; // @codeCoverageIgnore - - } - - if (in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore - } - - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - return $this->select($name, $grouped, $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function amount(string $name, $value = null, array $options = null): string - { - return $this->currencyField($name, 'amount', $value, $options); - } - + use FormSupport; /** * @param string $name * @param mixed $value @@ -299,114 +86,6 @@ class ExpandedForm return $html; } - /** - * @param string $name - * @param array $options - * - * @return string - * - */ - public function assetAccountCheckList(string $name, array $options = null): string - { - $options = $options ?? []; - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $selected = request()->old($name) ?? []; - - // get all asset accounts: - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - $assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); - $grouped = []; - // group accounts: - /** @var Account $account */ - foreach ($assetAccounts as $account) { - $role = $repository->getMetaValue($account, 'account_role'); - if (null === $role) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name; - } - - unset($options['class']); - try { - $html = view('form.assetAccountCheckList', compact('classes', 'selected', 'name', 'label', 'options', 'grouped'))->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage())); - $html = 'Could not render assetAccountCheckList.'; - } - - return $html; - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function assetAccountList(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - $accountList = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - // group accounts: - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); - $currency = $currencyRepos->findNull($currencyId); - $role = (string)$repository->getMetaValue($account, 'account_role'); - if ('' === $role) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - - if (null === $currency) { - $currency = $defaultCurrency; - } - - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - return $this->select($name, $grouped, $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - * @throws FireflyException - */ - public function balance(string $name, $value = null, array $options = null): string - { - return $this->currencyField($name, 'balance', $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - * @throws FireflyException - */ - public function balanceAll(string $name, $value = null, array $options = null): string - { - return $this->allCurrencyField($name, 'balance', $value, $options); - } - /** * @param string $name * @param int $value @@ -443,56 +122,6 @@ class ExpandedForm return $html; } - /** @noinspection MoreThanThreeArgumentsInspection */ - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function currencyList(string $name, $value = null, array $options = null): string - { - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - // get all currencies: - $list = $currencyRepos->get(); - $array = []; - /** @var TransactionCurrency $currency */ - foreach ($list as $currency) { - $array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; - } - - return $this->select($name, $array, $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function currencyListEmpty(string $name, $value = null, array $options = null): string - { - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - // get all currencies: - $list = $currencyRepos->get(); - $array = [ - 0 => (string)trans('firefly.no_currency'), - ]; - /** @var TransactionCurrency $currency */ - foreach ($list as $currency) { - $array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; - } - - return $this->select($name, $array, $value, $options); - } - /** * @param string $name * @param mixed $value @@ -592,81 +221,6 @@ class ExpandedForm return $html; } - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function longAccountList(string $name, $value = null, array $options = null): string - { - // make repositories - /** @var AccountRepositoryInterface $repository */ - $repository = app(AccountRepositoryInterface::class); - /** @var CurrencyRepositoryInterface $currencyRepos */ - $currencyRepos = app(CurrencyRepositoryInterface::class); - - $accountList = $repository->getAccountsByType( - [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,] - ); - $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; - $defaultCurrency = app('amount')->getDefaultCurrency(); - $grouped = []; - // group accounts: - /** @var Account $account */ - foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); - $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); - $currency = $currencyRepos->findNull($currencyId); - $role = (string)$repository->getMetaValue($account, 'account_role'); // TODO bad form for currency - if ('' === $role) { - $role = 'no_account_type'; // @codeCoverageIgnore - } - - if (in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore - } - - if (null === $currency) { - $currency = $defaultCurrency; - } - - $key = (string)trans('firefly.opt_group_' . $role); - $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; - } - - return $this->select($name, $grouped, $value, $options); - } - - /** - * Takes any collection and tries to make a sensible select list compatible array of it. - * - * @param \Illuminate\Support\Collection $set - * - * @return array - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - public function makeSelectList(Collection $set): array - { - $selectList = []; - $fields = ['title', 'name', 'description']; - /** @var Eloquent $entry */ - foreach ($set as $entry) { - $entryId = (int)$entry->id; - $title = null; - - foreach ($fields as $field) { - if (isset($entry->$field) && null === $title) { - $title = $entry->$field; - } - } - $selectList[$entryId] = $title; - } - - return $selectList; - } - /** * @param \Illuminate\Support\Collection $set * @@ -821,110 +375,6 @@ class ExpandedForm return $html; } - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function piggyBankList(string $name, $value = null, array $options = null): string - { - - // make repositories - /** @var PiggyBankRepositoryInterface $repository */ - $repository = app(PiggyBankRepositoryInterface::class); - $piggyBanks = $repository->getPiggyBanksWithAmount(); - $array = [ - 0 => (string)trans('firefly.none_in_select_list'), - ]; - /** @var PiggyBank $piggy */ - foreach ($piggyBanks as $piggy) { - $array[$piggy->id] = $piggy->name; - } - - return $this->select($name, $array, $value, $options); - } - - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - */ - public function ruleGroupList(string $name, $value = null, array $options = null): string - { - /** @var RuleGroupRepositoryInterface $groupRepos */ - $groupRepos = app(RuleGroupRepositoryInterface::class); - - // get all currencies: - $list = $groupRepos->get(); - $array = []; - /** @var RuleGroup $group */ - foreach ($list as $group) { - $array[$group->id] = $group->title; - } - - return $this->select($name, $array, $value, $options); - } - - /** - * @param string $name - * @param null $value - * @param array|null $options - * - * @return HtmlString - */ - public function ruleGroupListWithEmpty(string $name, $value = null, array $options = null): HtmlString - { - $options = $options ?? []; - $options['class'] = 'form-control'; - /** @var RuleGroupRepositoryInterface $groupRepos */ - $groupRepos = app(RuleGroupRepositoryInterface::class); - - // get all currencies: - $list = $groupRepos->get(); - $array = [ - 0 => (string)trans('firefly.none_in_select_list'), - ]; - /** @var RuleGroup $group */ - foreach ($list as $group) { - if (isset($options['hidden']) && (int)$options['hidden'] !== $group->id) { - $array[$group->id] = $group->title; - } - } - - return Form::select($name, $array, $value, $options); - } - - /** @noinspection MoreThanThreeArgumentsInspection */ - /** - * @param string $name - * @param array $list - * @param mixed $selected - * @param array $options - * - * @return string - */ - public function select(string $name, array $list = null, $selected = null, array $options = null): string - { - $list = $list ?? []; - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $selected = $this->fillFieldValue($name, $selected); - unset($options['autocomplete'], $options['placeholder']); - try { - $html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Could not render select(): %s', $e->getMessage())); - $html = 'Could not render select.'; - } - - return $html; - } - /** * @param string $name * @param mixed $value @@ -948,31 +398,6 @@ class ExpandedForm return $html; } - /** - * @param string $name - * @param mixed $value - * @param array $options - * - * @return string - * - */ - public function tags(string $name, $value = null, array $options = null): string - { - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $value = $this->fillFieldValue($name, $value); - $options['data-role'] = 'tagsinput'; - try { - $html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Could not render tags(): %s', $e->getMessage())); - $html = 'Could not render tags.'; - } - - return $html; - } - /** * @param string $name * @param mixed $value @@ -1026,197 +451,4 @@ class ExpandedForm return $html; } - - /** @noinspection MoreThanThreeArgumentsInspection */ - - /** - * @param string $name - * @param string $view - * @param mixed $value - * @param array $options - * - * @return string - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - protected function allCurrencyField(string $name, string $view, $value = null, array $options = null): string - { - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $value = $this->fillFieldValue($name, $value); - $options['step'] = 'any'; - $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); - /** @var Collection $currencies */ - $currencies = app('amount')->getAllCurrencies(); - unset($options['currency'], $options['placeholder']); - - // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) - $preFilled = session('preFilled'); - $key = 'amount_currency_id_' . $name; - $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; - - Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); - - // find this currency in set of currencies: - foreach ($currencies as $currency) { - if ($currency->id === $sentCurrencyId) { - $defaultCurrency = $currency; - Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); - break; - } - } - - // make sure value is formatted nicely: - if (null !== $value && '' !== $value) { - $value = round($value, $defaultCurrency->decimal_places); - } - try { - $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); - $html = 'Could not render currencyField.'; - } - - return $html; - } - - /** @noinspection MoreThanThreeArgumentsInspection */ - - /** - * @param string $name - * @param string $view - * @param mixed $value - * @param array $options - * - * @return string - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - protected function currencyField(string $name, string $view, $value = null, array $options = null): string - { - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $value = $this->fillFieldValue($name, $value); - $options['step'] = 'any'; - $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); - /** @var Collection $currencies */ - $currencies = app('amount')->getCurrencies(); - unset($options['currency'], $options['placeholder']); - - // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) - $preFilled = session('preFilled'); - $key = 'amount_currency_id_' . $name; - $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; - - Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); - - // find this currency in set of currencies: - foreach ($currencies as $currency) { - if ($currency->id === $sentCurrencyId) { - $defaultCurrency = $currency; - Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); - break; - } - } - - // make sure value is formatted nicely: - if (null !== $value && '' !== $value) { - $value = round($value, $defaultCurrency->decimal_places); - } - try { - $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); - $html = 'Could not render currencyField.'; - } - - return $html; - } - - /** - * @param $name - * @param $label - * @param array $options - * - * @return array - */ - protected function expandOptionArray(string $name, $label, array $options = null): array - { - $options = $options ?? []; - $name = str_replace('[]', '', $name); - $options['class'] = 'form-control'; - $options['id'] = 'ffInput_' . $name; - $options['autocomplete'] = 'off'; - $options['placeholder'] = ucfirst($label); - - return $options; - } - - /** - * @param string $name - * @param $value - * - * @return mixed - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - protected function fillFieldValue(string $name, $value = null) - { - if (app('session')->has('preFilled')) { - $preFilled = session('preFilled'); - $value = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value; - } - - try { - if (null !== request()->old($name)) { - $value = request()->old($name); - } - } catch (RuntimeException $e) { - // don't care about session errors. - Log::debug(sprintf('Run time: %s', $e->getMessage())); - } - - if ($value instanceof Carbon) { - $value = $value->format('Y-m-d'); - } - - return $value; - } - - /** - * @param $name - * - * @return string - */ - protected function getHolderClasses(string $name): string - { - // Get errors from session: - /** @var MessageBag $errors */ - $errors = session('errors'); - $classes = 'form-group'; - - if (null !== $errors && $errors->has($name)) { - $classes = 'form-group has-error has-feedback'; - } - - return $classes; - } - - /** @noinspection MoreThanThreeArgumentsInspection */ - - /** - * @param $name - * @param $options - * - * @return mixed - */ - protected function label(string $name, array $options = null): string - { - $options = $options ?? []; - if (isset($options['label'])) { - return $options['label']; - } - $name = str_replace('[]', '', $name); - - return (string)trans('form.' . $name); - } } diff --git a/app/Support/Facades/AccountForm.php b/app/Support/Facades/AccountForm.php new file mode 100644 index 0000000000..5e2328a42d --- /dev/null +++ b/app/Support/Facades/AccountForm.php @@ -0,0 +1,44 @@ +. + */ +declare(strict_types=1); + +namespace FireflyIII\Support\Facades; + +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Facade; + +/** + * @codeCoverageIgnore + * Class ExpandedForm. + * + */ +class AccountForm extends Facade +{ + /** + * Get the registered name of the component. + * + * @return string + */ + protected static function getFacadeAccessor(): string + { + return 'accountform'; + } +} diff --git a/app/Support/Form/AccountForm.php b/app/Support/Form/AccountForm.php new file mode 100644 index 0000000000..e5e07422d3 --- /dev/null +++ b/app/Support/Form/AccountForm.php @@ -0,0 +1,380 @@ +. + */ + +namespace FireflyIII\Support\Form; + + +use Carbon\Carbon; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use Log; +use Throwable; + +/** + * Class AccountForm + * + * All form methods that are account related. + * + * TODO describe all methods. + * TODO optimize repositories and methods. + */ +class AccountForm +{ + use FormSupport; + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function activeAssetAccountList(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + $accountList = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + // group accounts: + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); + $currency = $currencyRepos->findNull($currencyId); + $role = $repository->getMetaValue($account, 'account_role'); + if ('' === $role) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + + if (null === $currency) { + $currency = $defaultCurrency; + } + + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + return $this->select($name, $grouped, $value, $options); + } + + + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function activeLongAccountList(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $accountList = $repository->getActiveAccountsByType( + [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,] + ); + $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + // group accounts: + + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + + $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; + + $role = $repository->getMetaValue($account, 'account_role'); + + if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + + if (in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore + } + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + + return $this->select($name, $grouped, $value, $options); + } + + /** + * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. + * + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function activeWithdrawalDestinations(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + + $accountList = $repository->getActiveAccountsByType( + [ + AccountType::MORTGAGE, + AccountType::DEBT, + AccountType::CREDITCARD, + AccountType::LOAN, + AccountType::EXPENSE, + ] + ); + $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + + // add cash account first: + $cash = $repository->getCashAccount(); + $key = (string)trans('firefly.cash_account_type'); + $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); + + // group accounts: + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; + $role = (string)$repository->getMetaValue($account, 'account_role'); + if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + if ('no_account_type' === $role && AccountType::EXPENSE === $account->accountType->type) { + $role = 'expense_account'; // @codeCoverageIgnore + + } + + if (in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore + } + + if (null === $currency) { + $currency = $defaultCurrency; + } + + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + return $this->select($name, $grouped, $value, $options); + } + + /** + * Grouped dropdown list of all accounts that are valid as the destination of a withdrawal. + * + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function activeDepositDestinations(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + + $accountList = $repository->getActiveAccountsByType( + [ + AccountType::MORTGAGE, + AccountType::DEBT, + AccountType::CREDITCARD, + AccountType::LOAN, + AccountType::REVENUE, + ] + ); + $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + + // add cash account first: + $cash = $repository->getCashAccount(); + $key = (string)trans('firefly.cash_account_type'); + $grouped[$key][$cash->id] = sprintf('(%s)', (string)trans('firefly.cash')); + + // group accounts: + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; + $role = (string)$repository->getMetaValue($account, 'account_role'); + if ('' === $role && !in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + if ('no_account_type' === $role && AccountType::REVENUE === $account->accountType->type) { + $role = 'revenue_account'; // @codeCoverageIgnore + + } + + if (in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore + } + + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + return $this->select($name, $grouped, $value, $options); + } + + + /** + * @param string $name + * @param array $options + * + * @return string + * + */ + public function assetAccountCheckList(string $name, array $options = null): string + { + $options = $options ?? []; + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $selected = request()->old($name) ?? []; + + // get all asset accounts: + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + $assetAccounts = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); + $grouped = []; + // group accounts: + /** @var Account $account */ + foreach ($assetAccounts as $account) { + $role = $repository->getMetaValue($account, 'account_role'); + if (null === $role) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name; + } + + unset($options['class']); + try { + $html = view('form.assetAccountCheckList', compact('classes', 'selected', 'name', 'label', 'options', 'grouped'))->render(); + } catch (Throwable $e) { + Log::debug(sprintf('Could not render assetAccountCheckList(): %s', $e->getMessage())); + $html = 'Could not render assetAccountCheckList.'; + } + + return $html; + } + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function assetAccountList(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + $accountList = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + // group accounts: + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); + $currency = $currencyRepos->findNull($currencyId); + $role = (string)$repository->getMetaValue($account, 'account_role'); + if ('' === $role) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + + if (null === $currency) { + $currency = $defaultCurrency; + } + + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + return $this->select($name, $grouped, $value, $options); + } + + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function longAccountList(string $name, $value = null, array $options = null): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + $accountList = $repository->getAccountsByType( + [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,] + ); + $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + // group accounts: + /** @var Account $account */ + foreach ($accountList as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currencyId = (int)$repository->getMetaValue($account, 'currency_id'); + $currency = $currencyRepos->findNull($currencyId); + $role = (string)$repository->getMetaValue($account, 'account_role'); // TODO bad form for currency + if ('' === $role) { + $role = 'no_account_type'; // @codeCoverageIgnore + } + + if (in_array($account->accountType->type, $liabilityTypes, true)) { + $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore + } + + if (null === $currency) { + $currency = $defaultCurrency; + } + + $key = (string)trans('firefly.opt_group_' . $role); + $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')'; + } + + return $this->select($name, $grouped, $value, $options); + } +} \ No newline at end of file diff --git a/app/Support/Form/CurrencyForm.php b/app/Support/Form/CurrencyForm.php new file mode 100644 index 0000000000..6f16f4230c --- /dev/null +++ b/app/Support/Form/CurrencyForm.php @@ -0,0 +1,219 @@ +. + */ + +namespace FireflyIII\Support\Form; + + +use Amount as Amt; +use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use Illuminate\Support\Collection; +use Log; +use Throwable; + +/** + * Class CurrencyForm + * + * All currency related form methods. + * + * TODO cleanup and describe. + */ +class CurrencyForm +{ + use FormSupport; + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function currencyList(string $name, $value = null, array $options = null): string + { + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + // get all currencies: + $list = $currencyRepos->get(); + $array = []; + /** @var TransactionCurrency $currency */ + foreach ($list as $currency) { + $array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; + } + + return $this->select($name, $array, $value, $options); + } + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function currencyListEmpty(string $name, $value = null, array $options = null): string + { + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + // get all currencies: + $list = $currencyRepos->get(); + $array = [ + 0 => (string)trans('firefly.no_currency'), + ]; + /** @var TransactionCurrency $currency */ + foreach ($list as $currency) { + $array[$currency->id] = $currency->name . ' (' . $currency->symbol . ')'; + } + + return $this->select($name, $array, $value, $options); + } + + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + * @throws FireflyException + */ + public function balanceAll(string $name, $value = null, array $options = null): string + { + return $this->allCurrencyField($name, 'balance', $value, $options); + } + + /** + * @param string $name + * @param string $view + * @param mixed $value + * @param array $options + * + * @return string + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + protected function allCurrencyField(string $name, string $view, $value = null, array $options = null): string + { + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $value = $this->fillFieldValue($name, $value); + $options['step'] = 'any'; + $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); + /** @var Collection $currencies */ + $currencies = app('amount')->getAllCurrencies(); + unset($options['currency'], $options['placeholder']); + + // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) + $preFilled = session('preFilled'); + $key = 'amount_currency_id_' . $name; + $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; + + Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); + + // find this currency in set of currencies: + foreach ($currencies as $currency) { + if ($currency->id === $sentCurrencyId) { + $defaultCurrency = $currency; + Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); + break; + } + } + + // make sure value is formatted nicely: + if (null !== $value && '' !== $value) { + $value = round($value, $defaultCurrency->decimal_places); + } + try { + $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); + } catch (Throwable $e) { + Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); + $html = 'Could not render currencyField.'; + } + + return $html; + } + + /** + * @param string $name + * @param string $view + * @param mixed $value + * @param array $options + * + * @return string + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + protected function currencyField(string $name, string $view, $value = null, array $options = null): string + { + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $value = $this->fillFieldValue($name, $value); + $options['step'] = 'any'; + $defaultCurrency = $options['currency'] ?? Amt::getDefaultCurrency(); + /** @var Collection $currencies */ + $currencies = app('amount')->getCurrencies(); + unset($options['currency'], $options['placeholder']); + + // perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount) + $preFilled = session('preFilled'); + $key = 'amount_currency_id_' . $name; + $sentCurrencyId = isset($preFilled[$key]) ? (int)$preFilled[$key] : $defaultCurrency->id; + + Log::debug(sprintf('Sent currency ID is %d', $sentCurrencyId)); + + // find this currency in set of currencies: + foreach ($currencies as $currency) { + if ($currency->id === $sentCurrencyId) { + $defaultCurrency = $currency; + Log::debug(sprintf('default currency is now %s', $defaultCurrency->code)); + break; + } + } + + // make sure value is formatted nicely: + if (null !== $value && '' !== $value) { + $value = round($value, $defaultCurrency->decimal_places); + } + try { + $html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); + } catch (Throwable $e) { + Log::debug(sprintf('Could not render currencyField(): %s', $e->getMessage())); + $html = 'Could not render currencyField.'; + } + + return $html; + } + + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function amount(string $name, $value = null, array $options = null): string + { + return $this->currencyField($name, 'amount', $value, $options); + } + +} \ No newline at end of file diff --git a/app/Support/Form/FormSupport.php b/app/Support/Form/FormSupport.php new file mode 100644 index 0000000000..4f3ef06336 --- /dev/null +++ b/app/Support/Form/FormSupport.php @@ -0,0 +1,144 @@ +. + */ + +namespace FireflyIII\Support\Form; + +use Carbon\Carbon; +use Illuminate\Support\MessageBag; +use Log; +use RuntimeException; +use Throwable; + +/** + * Trait FormSupport + */ +trait FormSupport +{ + /** + * @param string $name + * @param array $list + * @param mixed $selected + * @param array $options + * + * @return string + */ + public function select(string $name, array $list = null, $selected = null, array $options = null): string + { + $list = $list ?? []; + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $selected = $this->fillFieldValue($name, $selected); + unset($options['autocomplete'], $options['placeholder']); + try { + $html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); + } catch (Throwable $e) { + Log::debug(sprintf('Could not render select(): %s', $e->getMessage())); + $html = 'Could not render select.'; + } + + return $html; + } + + /** + * @param $name + * @param $label + * @param array $options + * + * @return array + */ + protected function expandOptionArray(string $name, $label, array $options = null): array + { + $options = $options ?? []; + $name = str_replace('[]', '', $name); + $options['class'] = 'form-control'; + $options['id'] = 'ffInput_' . $name; + $options['autocomplete'] = 'off'; + $options['placeholder'] = ucfirst($label); + + return $options; + } + + /** + * @param string $name + * @param $value + * + * @return mixed + */ + protected function fillFieldValue(string $name, $value = null) + { + if (app('session')->has('preFilled')) { + $preFilled = session('preFilled'); + $value = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value; + } + + try { + if (null !== request()->old($name)) { + $value = request()->old($name); + } + } catch (RuntimeException $e) { + // don't care about session errors. + Log::debug(sprintf('Run time: %s', $e->getMessage())); + } + + if ($value instanceof Carbon) { + $value = $value->format('Y-m-d'); + } + + return $value; + } + + /** + * @param $name + * + * @return string + */ + protected function getHolderClasses(string $name): string + { + // Get errors from session: + /** @var MessageBag $errors */ + $errors = session('errors'); + $classes = 'form-group'; + + if (null !== $errors && $errors->has($name)) { + $classes = 'form-group has-error has-feedback'; + } + + return $classes; + } + + /** + * @param $name + * @param $options + * + * @return mixed + */ + protected function label(string $name, array $options = null): string + { + $options = $options ?? []; + if (isset($options['label'])) { + return $options['label']; + } + $name = str_replace('[]', '', $name); + + return (string)trans('form.' . $name); + } +} \ No newline at end of file diff --git a/app/Support/Form/PiggyBankForm.php b/app/Support/Form/PiggyBankForm.php new file mode 100644 index 0000000000..38fae973a8 --- /dev/null +++ b/app/Support/Form/PiggyBankForm.php @@ -0,0 +1,60 @@ +. + */ + +namespace FireflyIII\Support\Form; + + +use FireflyIII\Models\PiggyBank; +use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; + +/** + * Class PiggyBankForm + * + * TODO cleanup and describe. + */ +class PiggyBankForm +{ + use FormSupport; + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function piggyBankList(string $name, $value = null, array $options = null): string + { + + // make repositories + /** @var PiggyBankRepositoryInterface $repository */ + $repository = app(PiggyBankRepositoryInterface::class); + $piggyBanks = $repository->getPiggyBanksWithAmount(); + $array = [ + 0 => (string)trans('firefly.none_in_select_list'), + ]; + /** @var PiggyBank $piggy */ + foreach ($piggyBanks as $piggy) { + $array[$piggy->id] = $piggy->name; + } + + return $this->select($name, $array, $value, $options); + } +} \ No newline at end of file diff --git a/app/Support/Form/RuleForm.php b/app/Support/Form/RuleForm.php new file mode 100644 index 0000000000..1cf57d9c16 --- /dev/null +++ b/app/Support/Form/RuleForm.php @@ -0,0 +1,88 @@ +. + */ + +namespace FireflyIII\Support\Form; + + +use FireflyIII\Models\RuleGroup; +use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; +use Form; +use Illuminate\Support\HtmlString; + +/** + * Class RuleForm + * TODO cleanup and describe. + */ +class RuleForm +{ + use FormSupport; + /** + * @param string $name + * @param mixed $value + * @param array $options + * + * @return string + */ + public function ruleGroupList(string $name, $value = null, array $options = null): string + { + /** @var RuleGroupRepositoryInterface $groupRepos */ + $groupRepos = app(RuleGroupRepositoryInterface::class); + + // get all currencies: + $list = $groupRepos->get(); + $array = []; + /** @var RuleGroup $group */ + foreach ($list as $group) { + $array[$group->id] = $group->title; + } + + return $this->select($name, $array, $value, $options); + } + + /** + * @param string $name + * @param null $value + * @param array|null $options + * + * @return HtmlString + */ + public function ruleGroupListWithEmpty(string $name, $value = null, array $options = null): HtmlString + { + $options = $options ?? []; + $options['class'] = 'form-control'; + /** @var RuleGroupRepositoryInterface $groupRepos */ + $groupRepos = app(RuleGroupRepositoryInterface::class); + + // get all currencies: + $list = $groupRepos->get(); + $array = [ + 0 => (string)trans('firefly.none_in_select_list'), + ]; + /** @var RuleGroup $group */ + foreach ($list as $group) { + if (isset($options['hidden']) && (int)$options['hidden'] !== $group->id) { + $array[$group->id] = $group->title; + } + } + + return Form::select($name, $array, $value, $options); + } +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index f95262a3b1..7941e75248 100644 --- a/config/app.php +++ b/config/app.php @@ -21,6 +21,7 @@ declare(strict_types=1); + use FireflyIII\Providers\ImportServiceProvider; @@ -145,6 +146,7 @@ return [ 'Amount' => \FireflyIII\Support\Facades\Amount::class, 'Steam' => \FireflyIII\Support\Facades\Steam::class, 'ExpandedForm' => \FireflyIII\Support\Facades\ExpandedForm::class, + 'AccountForm' => \FireflyIII\Support\Facades\AccountForm::class, 'Google2FA' => PragmaRX\Google2FALaravel\Facade::class, ], diff --git a/config/twigbridge.php b/config/twigbridge.php index 02f8f8c206..30e7604efc 100644 --- a/config/twigbridge.php +++ b/config/twigbridge.php @@ -21,17 +21,17 @@ * along with Firefly III. If not, see . */ -use TwigBridge\Extension\Laravel\Url; -use TwigBridge\Extension\Laravel\Str; -use TwigBridge\Extension\Laravel\Translator; -use TwigBridge\Extension\Laravel\Session; +use TwigBridge\Extension\Laravel\Auth; +use TwigBridge\Extension\Laravel\Config; use TwigBridge\Extension\Laravel\Dump; use TwigBridge\Extension\Laravel\Input; -use TwigBridge\Extension\Laravel\Config; -use TwigBridge\Extension\Laravel\Auth; +use TwigBridge\Extension\Laravel\Session; +use TwigBridge\Extension\Laravel\Str; +use TwigBridge\Extension\Laravel\Translator; +use TwigBridge\Extension\Laravel\Url; +use TwigBridge\Extension\Loader\Facades; use TwigBridge\Extension\Loader\Filters; use TwigBridge\Extension\Loader\Functions; -use TwigBridge\Extension\Loader\Facades; use TwigBridge\Twig\Template; /** @@ -193,16 +193,21 @@ return [ 'Steam', 'Config', 'Request', + 'Form' => ['is_safe' => ['input', 'select', 'checkbox', 'model', 'open', 'radio', 'textarea', 'file',],], 'ExpandedForm' => [ - 'is_safe' => ['date', 'text', 'select', 'balance', 'optionsList', 'checkbox', 'amount', 'tags', 'integer', 'textarea', 'location', 'file', - 'staticText', 'password', 'nonSelectableAmount', 'number', 'assetAccountList', 'amountNoCurrency', 'currencyList', - 'ruleGroupList', 'assetAccountCheckList', 'ruleGroupListWithEmpty', 'piggyBankList', 'currencyListEmpty', - 'activeAssetAccountList', 'percentage', 'activeLongAccountList', 'longAccountList','balanceAll', - 'activeWithdrawalDestinations','activeDepositDestinations' + 'is_safe' => [ + 'date', 'text', 'select', 'balance', 'optionsList', 'checkbox', 'amount', 'tags', 'integer', 'textarea', 'location', 'file', 'staticText', + 'password', 'nonSelectableAmount', 'number', 'assetAccountList', 'amountNoCurrency', 'currencyList', 'ruleGroupList', + 'assetAccountCheckList', 'ruleGroupListWithEmpty', 'piggyBankList', 'currencyListEmpty', 'percentage', + 'activeLongAccountList', 'longAccountList', 'balanceAll', 'activeWithdrawalDestinations', 'activeDepositDestinations', - ],], - 'Form' => ['is_safe' => ['input', 'select', 'checkbox', 'model', 'open', 'radio', 'textarea', 'file',], + ], + ], + 'AccountForm' => [ + 'is_safe' => [ + 'activeAssetAccountList', 'activeLongAccountList', + ], ], ], diff --git a/resources/views/v1/import/file/configure-upload.twig b/resources/views/v1/import/file/configure-upload.twig index 73d00f3ab4..2a3c5eef92 100644 --- a/resources/views/v1/import/file/configure-upload.twig +++ b/resources/views/v1/import/file/configure-upload.twig @@ -38,7 +38,7 @@ {{ ExpandedForm.checkbox('has_headers',1,importJob.configuration['has-headers'],{helpText: trans('import.job_config_uc_header_help')}) }} {{ ExpandedForm.text('date_format',importJob.configuration['date-format'],{helpText: trans('import.job_config_uc_date_help', {dateExample: phpdate('Ymd')}) }) }} {{ ExpandedForm.select('csv_delimiter', data.delimiters, importJob.configuration['delimiter'], {helpText: trans('import.job_config_uc_delimiter_help') } ) }} - {{ ExpandedForm.activeAssetAccountList('csv_import_account', importJob.configuration['import-account'], {helpText: trans('import.job_config_uc_account_help')}) }} + {{ AccountForm.activeAssetAccountList('csv_import_account', importJob.configuration['import-account'], {helpText: trans('import.job_config_uc_account_help')}) }}

{{ 'optionalFields'|_ }}

diff --git a/resources/views/v1/piggy-banks/create.twig b/resources/views/v1/piggy-banks/create.twig index e9208bb055..db388cbeb0 100644 --- a/resources/views/v1/piggy-banks/create.twig +++ b/resources/views/v1/piggy-banks/create.twig @@ -18,7 +18,7 @@
{{ ExpandedForm.text('name') }} - {{ ExpandedForm.activeAssetAccountList('account_id', null, {label: 'saveOnAccount'|_ }) }} + {{ AccountForm.activeAssetAccountList('account_id', null, {label: 'saveOnAccount'|_ }) }} {{ ExpandedForm.amountNoCurrency('targetamount') }}
diff --git a/resources/views/v1/recurring/create.twig b/resources/views/v1/recurring/create.twig index 654aecc5da..51616a2595 100644 --- a/resources/views/v1/recurring/create.twig +++ b/resources/views/v1/recurring/create.twig @@ -90,16 +90,13 @@ {{ ExpandedForm.amountNoCurrency('amount', []) }} {# source account if withdrawal, or if transfer: #} - {{ ExpandedForm.activeLongAccountList('source_id', null, {label: trans('form.asset_source_account')}) }} - - {# source account name for deposits: #} - {#{{ ExpandedForm.text('source_name', null, {label: trans('form.revenue_account')}) }}#} + {{ AccountForm.activeLongAccountList('source_id', null, {label: trans('form.asset_source_account')}) }} {# for deposits, a drop down with revenue accounts, loan debt cash and mortgage #} {{ ExpandedForm.activeDepositDestinations('deposit_source_id', null, {label: trans('form.deposit_source_id')}) }} {# destination if deposit or transfer: #} - {{ ExpandedForm.activeLongAccountList('destination_id', null, {label: trans('form.asset_destination_account')} ) }} + {{ AccountForm.activeLongAccountList('destination_id', null, {label: trans('form.asset_destination_account')} ) }} {# destination account name for withdrawals #} {#{{ ExpandedForm.text('destination_name', null, {label: trans('form.expense_account')}) }} #} diff --git a/resources/views/v1/transactions/convert.twig b/resources/views/v1/transactions/convert.twig index ab228f72fe..eba06b4339 100644 --- a/resources/views/v1/transactions/convert.twig +++ b/resources/views/v1/transactions/convert.twig @@ -314,168 +314,6 @@ {{ ExpandedForm.staticText('destination_account_asset', ''~destinationAccount.name|escape~'') }} {% endif %} #} - - {# ONE #} - {# - {% if journalType.type == 'Withdrawal' and destinationType.type == 'Deposit' %} - ONE - {% endif %} - {% if journalType.type == 'Withdrawal' and destinationType.type == 'Deposit' %} -

- {{ trans('firefly.convert_explanation_withdrawal_deposit', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} - -

-

- {{ 'convert_please_set_revenue_source'|_ }} - -

- {% if destinationAccount.accountType.type == "Cash account" %} - {{ ExpandedForm.text('source_account_revenue', '') }} - {% else %} - {{ ExpandedForm.text('source_account_revenue', destinationAccount.name|escape) }} - {% endif %} - {% endif %} - #} - {# TWO #} - {# - {% if journalType.type == 'Withdrawal' and destinationType.type == 'Transfer' %} -

- {{ trans('firefly.convert_explanation_withdrawal_transfer', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} -

- -

- - {{ 'convert_please_set_asset_destination'|_ }} - - -

- {{ ExpandedForm.activeLongAccountList('destination_account_asset', null) }} - - {% endif %} - #} - {# THREE #} - {# - {% if journalType.type == 'Deposit' and destinationType.type == 'Withdrawal' %} -

- - {{ trans('firefly.convert_explanation_deposit_withdrawal', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} - -

-

- - {{ 'convert_please_set_expense_destination'|_ }} - - -

- {% if sourceAccount.accountType.type == "Cash account" %} - {{ ExpandedForm.text('destination_account_expense', '') }} - {% else %} - {{ ExpandedForm.text('destination_account_expense', destinationAccount.name|escape) }} - {% endif %} - - {% endif %} - #} - {# FOUR #} - {# - {% if journalType.type == 'Deposit' and destinationType.type == 'Transfer' %} - -

- - {{ trans('firefly.convert_explanation_deposit_transfer', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} - -

-

- - {{ 'convert_please_set_asset_source'|_ }} - - -

- {{ ExpandedForm.activeLongAccountList('source_account_asset', null) }} - {% endif %} - #} - {# FIVE #} - {# - {% if journalType.type == 'Transfer' and destinationType.type == 'Withdrawal' %} - -

- - {{ trans('firefly.convert_explanation_transfer_withdrawal', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} - -

- -

- - {{ 'convert_please_set_expense_destination'|_ }} - -

- - {{ ExpandedForm.text('destination_account_expense', destinationAccount.name|escape) }} - - {% endif %} - #} - {# SIX #} - {# - {% if journalType.type == 'Transfer' and destinationType.type == 'Deposit' %} - - -

- - {{ trans('firefly.convert_explanation_transfer_deposit', - { - amount: positiveAmount|formatAmount, - sourceRoute: route('accounts.show', [sourceAccount.id]), - sourceName: sourceAccount.name|escape, - destinationRoute: route('accounts.show', [destinationAccount.id]), - destinationName: destinationAccount.name|escape, - })|raw }} - -

- -

- - {{ 'convert_please_set_revenue_source'|_ }} - -

- - {{ ExpandedForm.text('source_account_revenue', sourceAccount.name|escape) }} - - {% endif %} - #}