From cfd98a33fe89211286b4a7ed3ba78895aae12b95 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 28 Jun 2018 06:43:59 +0200 Subject: [PATCH] Fix #1463 --- app/Support/ExpandedForm.php | 44 +++++++++++++++++-- config/twigbridge.php | 2 +- .../views/import/file/configure-upload.twig | 2 +- resources/views/piggy-banks/create.twig | 2 +- resources/views/recurring/create.twig | 4 +- resources/views/transactions/convert.twig | 4 +- .../views/transactions/single/create.twig | 4 +- resources/views/transactions/split/edit.twig | 4 +- 8 files changed, 52 insertions(+), 14 deletions(-) diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index a5c5ee8a3f..b6a46453b1 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -45,6 +45,45 @@ use Session; */ class ExpandedForm { + /** + * @param string $name + * @param null $options + * + * @return string + * @throws \Throwable + */ + public function activeAssetAccountList(string $name, $value = null, array $options = []): string + { + // make repositories + /** @var AccountRepositoryInterface $repository */ + $repository = app(AccountRepositoryInterface::class); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + + $assetAccounts = $repository->getActiveAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + // group accounts: + /** @var Account $account */ + foreach ($assetAccounts as $account) { + $balance = app('steam')->balance($account, new Carbon); + $currencyId = (int)$account->getMeta('currency_id'); + $currency = $currencyRepos->findNull($currencyId); + $role = $account->getMeta('accountRole'); + 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 null $value @@ -101,7 +140,6 @@ class ExpandedForm /** * @param string $name - * @param $selected * @param null $options * * @return string @@ -263,7 +301,7 @@ class ExpandedForm // get all currencies: $list = $currencyRepos->get(); $array = [ - 0 => trans('firefly.no_currency') + 0 => trans('firefly.no_currency'), ]; /** @var TransactionCurrency $currency */ foreach ($list as $currency) { @@ -515,7 +553,7 @@ class ExpandedForm */ public function optionsList(string $type, string $name): string { - $html = view('form.options', compact('type', 'name'))->render(); + $html = view('form.options', compact('type', 'name'))->render(); return $html; } diff --git a/config/twigbridge.php b/config/twigbridge.php index 484eafd88a..f6a6e22ae3 100644 --- a/config/twigbridge.php +++ b/config/twigbridge.php @@ -189,7 +189,7 @@ return [ 'date', 'text', 'select', 'balance', 'optionsList', 'checkbox', 'amount', 'tags', 'integer', 'textarea', 'location', 'multiRadio', 'file', 'multiCheckbox', 'staticText', 'amountSmall', 'password', 'nonSelectableBalance', 'nonSelectableAmount', 'number', 'assetAccountList','amountNoCurrency','currencyList','ruleGroupList','assetAccountCheckList','ruleGroupListWithEmpty', - 'piggyBankList','currencyListEmpty' + 'piggyBankList','currencyListEmpty','activeAssetAccountList' ], ], 'Form' => [ diff --git a/resources/views/import/file/configure-upload.twig b/resources/views/import/file/configure-upload.twig index 59de0c8a74..4e1e31c46c 100644 --- a/resources/views/import/file/configure-upload.twig +++ b/resources/views/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.assetAccountList('csv_import_account', importJob.configuration['import-account'], {helpText: trans('import.job_config_uc_account_help')}) }} + {{ ExpandedForm.activeAssetAccountList('csv_import_account', importJob.configuration['import-account'], {helpText: trans('import.job_config_uc_account_help')}) }}

{{ 'optionalFields'|_ }}

diff --git a/resources/views/piggy-banks/create.twig b/resources/views/piggy-banks/create.twig index adae7f9b34..332d8fc041 100644 --- a/resources/views/piggy-banks/create.twig +++ b/resources/views/piggy-banks/create.twig @@ -18,7 +18,7 @@
{{ ExpandedForm.text('name') }} - {{ ExpandedForm.assetAccountList('account_id', null, {label: 'saveOnAccount'|_ }) }} + {{ ExpandedForm.activeAssetAccountList('account_id', null, {label: 'saveOnAccount'|_ }) }} {{ ExpandedForm.amountNoCurrency('targetamount') }}
diff --git a/resources/views/recurring/create.twig b/resources/views/recurring/create.twig index 7af6a7082f..d88455cfaa 100644 --- a/resources/views/recurring/create.twig +++ b/resources/views/recurring/create.twig @@ -89,13 +89,13 @@ {{ ExpandedForm.amountNoCurrency('amount', []) }} {# source account if withdrawal, or if transfer: #} - {{ ExpandedForm.assetAccountList('source_account_id', null, {label: trans('form.asset_source_account')}) }} + {{ ExpandedForm.activeAssetAccountList('source_account_id', null, {label: trans('form.asset_source_account')}) }} {# source account name for deposits: #} {{ ExpandedForm.text('source_account_name', null, {label: trans('form.revenue_account')}) }} {# destination if deposit or transfer: #} - {{ ExpandedForm.assetAccountList('destination_account_id', null, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.activeAssetAccountList('destination_account_id', null, {label: trans('form.asset_destination_account')} ) }} {# destination account name for withdrawals #} {{ ExpandedForm.text('destination_account_name', null, {label: trans('form.expense_account')}) }} diff --git a/resources/views/transactions/convert.twig b/resources/views/transactions/convert.twig index 8ab8cedb31..2406a5ebe6 100644 --- a/resources/views/transactions/convert.twig +++ b/resources/views/transactions/convert.twig @@ -92,7 +92,7 @@

- {{ ExpandedForm.assetAccountList('destination_account_asset', null) }} + {{ ExpandedForm.activeAssetAccountList('destination_account_asset', null) }} {% endif %} @@ -145,7 +145,7 @@

- {{ ExpandedForm.assetAccountList('source_account_asset', null) }} + {{ ExpandedForm.activeAssetAccountList('source_account_asset', null) }} {% endif %} {# FIVE #} diff --git a/resources/views/transactions/single/create.twig b/resources/views/transactions/single/create.twig index f6d8e7b02a..63fa0fc6cc 100644 --- a/resources/views/transactions/single/create.twig +++ b/resources/views/transactions/single/create.twig @@ -33,7 +33,7 @@ {{ ExpandedForm.text('description') }} {# SELECTABLE SOURCE ACCOUNT ONLY FOR WITHDRAWALS AND TRANSFERS #} - {{ ExpandedForm.assetAccountList('source_account_id', null, {label: trans('form.asset_source_account')}) }} + {{ ExpandedForm.activeAssetAccountList('source_account_id', null, {label: trans('form.asset_source_account')}) }} {# FREE FORMAT SOURCE ACCOUNT ONLY FOR DEPOSITS #} {{ ExpandedForm.text('source_account_name', null, {label: trans('form.revenue_account')}) }} @@ -42,7 +42,7 @@ {{ ExpandedForm.text('destination_account_name', null, {label: trans('form.expense_account')}) }} {# SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS #} - {{ ExpandedForm.assetAccountList('destination_account_id', null, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.activeAssetAccountList('destination_account_id', null, {label: trans('form.asset_destination_account')} ) }} {# ALWAYS SHOW AMOUNT #} {{ ExpandedForm.amount('amount') }} diff --git a/resources/views/transactions/split/edit.twig b/resources/views/transactions/split/edit.twig index b4dd2ad247..898b872907 100644 --- a/resources/views/transactions/split/edit.twig +++ b/resources/views/transactions/split/edit.twig @@ -42,12 +42,12 @@ {# show source if withdrawal or transfer #} {% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %} - {{ ExpandedForm.assetAccountList('journal_source_account_id', preFilled.journal_source_account_id) }} + {{ ExpandedForm.activeAssetAccountList('journal_source_account_id', preFilled.journal_source_account_id) }} {% endif %} {# show destination account id, if deposit (is asset): #} {% if preFilled.what == 'deposit' or preFilled.what == 'transfer' %} - {{ ExpandedForm.assetAccountList('journal_destination_account_id', preFilled.journal_destination_account_id) }} + {{ ExpandedForm.activeAssetAccountList('journal_destination_account_id', preFilled.journal_destination_account_id) }} {% endif %} {# show amount and some helper text when making splits: #}