From 7dc72a2894f057d01b28c011be0510f712e17dc3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 25 Aug 2018 22:10:10 +0200 Subject: [PATCH] Add the ability to make transactions to and from liability accounts. --- app/Support/ExpandedForm.php | 97 +++++++++++++++++-- config/twigbridge.php | 6 +- resources/views/recurring/create.twig | 4 +- resources/views/recurring/edit.twig | 4 +- .../views/transactions/single/create.twig | 4 +- resources/views/transactions/single/edit.twig | 4 +- resources/views/transactions/split/edit.twig | 4 +- .../Controllers/JavascriptControllerTest.php | 2 +- 8 files changed, 104 insertions(+), 21 deletions(-) diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index 79d4fd557c..cfae9d8b1d 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -66,6 +66,46 @@ class ExpandedForm /** @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, '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 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); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + $accountList = $repository->getActiveAccountsByType( [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,] ); @@ -200,8 +240,7 @@ class ExpandedForm /** @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]; + $accountList = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); $defaultCurrency = app('amount')->getDefaultCurrency(); $grouped = []; // group accounts: @@ -215,10 +254,6 @@ class ExpandedForm $role = 'no_account_type'; // @codeCoverageIgnore } - if (\in_array($account->accountType->type, $liabilityTypes, true)) { - $role = 'l_' . $account->accountType->type; // @codeCoverageIgnore - } - if (null === $currency) { $currency = $defaultCurrency; } @@ -243,7 +278,6 @@ class ExpandedForm return $this->currencyField($name, 'balance', $value, $options); } - /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param string $name * @param int $value @@ -280,6 +314,8 @@ class ExpandedForm return $html; } + /** @noinspection MoreThanThreeArgumentsInspection */ + /** * @param string $name * @param mixed $value @@ -427,6 +463,53 @@ 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, 'accountRole'); + 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. * diff --git a/config/twigbridge.php b/config/twigbridge.php index 7d3c3f0091..e6b2803442 100644 --- a/config/twigbridge.php +++ b/config/twigbridge.php @@ -187,9 +187,9 @@ return [ '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' + 'file', 'staticText', 'password', 'nonSelectableAmount', + 'number', 'assetAccountList', 'amountNoCurrency', 'currencyList', 'ruleGroupList', 'assetAccountCheckList', 'ruleGroupListWithEmpty', + 'piggyBankList', 'currencyListEmpty', 'activeAssetAccountList', 'percentage', 'activeLongAccountList', 'longAccountList', ], ], 'Form' => [ diff --git a/resources/views/recurring/create.twig b/resources/views/recurring/create.twig index 578a796afc..d915acf87c 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.activeAssetAccountList('source_id', null, {label: trans('form.asset_source_account')}) }} + {{ 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')}) }} {# destination if deposit or transfer: #} - {{ ExpandedForm.activeAssetAccountList('destination_id', null, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.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/recurring/edit.twig b/resources/views/recurring/edit.twig index 11197d98db..8c3d7fc14a 100644 --- a/resources/views/recurring/edit.twig +++ b/resources/views/recurring/edit.twig @@ -88,13 +88,13 @@ {{ ExpandedForm.amountNoCurrency('amount', array.transactions[0].amount) }} {# source account if withdrawal, or if transfer: #} - {{ ExpandedForm.assetAccountList('source_id', array.transactions[0].source_id, {label: trans('form.asset_source_account')}) }} + {{ ExpandedForm.longAccountList('source_id', array.transactions[0].source_id, {label: trans('form.asset_source_account')}) }} {# source account name for deposits: #} {{ ExpandedForm.text('source_name', array.transactions[0].source_name, {label: trans('form.revenue_account')}) }} {# destination if deposit or transfer: #} - {{ ExpandedForm.assetAccountList('destination_id', array.transactions[0].destination_id, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.longAccountList('destination_id', array.transactions[0].destination_id, {label: trans('form.asset_destination_account')} ) }} {# destination account name for withdrawals #} {{ ExpandedForm.text('destination_name', array.transactions[0].destination_name, {label: trans('form.expense_account')}) }} diff --git a/resources/views/transactions/single/create.twig b/resources/views/transactions/single/create.twig index e12b777faa..7c15c31ff5 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.activeAssetAccountList('source_id', null, {label: trans('form.asset_source_account') }) }} + {{ ExpandedForm.activeLongAccountList('source_id', null, {label: trans('form.asset_source_account') }) }} {# FREE FORMAT SOURCE ACCOUNT ONLY FOR DEPOSITS #} {{ ExpandedForm.text('source_name', null, {label: trans('form.revenue_account')}) }} @@ -42,7 +42,7 @@ {{ ExpandedForm.text('destination_name', null, {label: trans('form.expense_account')}) }} {# SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS #} - {{ ExpandedForm.activeAssetAccountList('destination_id', null, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.activeLongAccountList('destination_id', null, {label: trans('form.asset_destination_account')} ) }} {# ALWAYS SHOW AMOUNT #} diff --git a/resources/views/transactions/single/edit.twig b/resources/views/transactions/single/edit.twig index a43f916046..8df6a248ba 100644 --- a/resources/views/transactions/single/edit.twig +++ b/resources/views/transactions/single/edit.twig @@ -39,7 +39,7 @@ {# SELECTABLE SOURCE ACCOUNT ONLY FOR WITHDRAWALS AND TRANSFERS #} {% if what == 'transfer' or what == 'withdrawal' %} - {{ ExpandedForm.assetAccountList('source_id', data.source_id, {label: trans('form.asset_source_account')}) }} + {{ ExpandedForm.longAccountList('source_id', data.source_id, {label: trans('form.asset_source_account')}) }} {% endif %} {# FREE FORMAT SOURCE ACCOUNT ONLY FOR DEPOSITS #} @@ -54,7 +54,7 @@ {# SELECTABLE DESTINATION ACCOUNT ONLY FOR TRANSFERS AND DEPOSITS #} {% if what == 'transfer' or what == 'deposit' %} - {{ ExpandedForm.assetAccountList('destination_id', data.destination_id, {label: trans('form.asset_destination_account')} ) }} + {{ ExpandedForm.longAccountList('destination_id', data.destination_id, {label: trans('form.asset_destination_account')} ) }} {% endif %} {# ALWAYS SHOW AMOUNT #} diff --git a/resources/views/transactions/split/edit.twig b/resources/views/transactions/split/edit.twig index 9eec4de223..8c8627901f 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.activeAssetAccountList('journal_source_id', preFilled.journal_source_id) }} + {{ ExpandedForm.activeLongAccountList('journal_source_id', preFilled.journal_source_id) }} {% endif %} {# show destination account id, if deposit (is asset): #} {% if preFilled.what == 'deposit' or preFilled.what == 'transfer' %} - {{ ExpandedForm.activeAssetAccountList('journal_destination_id', preFilled.journal_destination_id) }} + {{ ExpandedForm.activeLongAccountList('journal_destination_id', preFilled.journal_destination_id) }} {% endif %} {# show amount and some helper text when making splits: #} diff --git a/tests/Feature/Controllers/JavascriptControllerTest.php b/tests/Feature/Controllers/JavascriptControllerTest.php index 5082f3f7c8..cef17d124c 100644 --- a/tests/Feature/Controllers/JavascriptControllerTest.php +++ b/tests/Feature/Controllers/JavascriptControllerTest.php @@ -62,7 +62,7 @@ class JavascriptControllerTest extends TestCase $account = factory(Account::class)->make(); $accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection([$account])) - ->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once(); + ->withArgs([[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT,AccountType::LOAN,AccountType::MORTGAGE, AccountType::CREDITCARD]])->once(); $currencyRepos->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(new TransactionCurrency); $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');