Add the ability to make transactions to and from liability accounts.

This commit is contained in:
James Cole
2018-08-25 22:10:10 +02:00
parent 07cfba1b3a
commit 7dc72a2894
8 changed files with 104 additions and 21 deletions

View File

@@ -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.
*

View File

@@ -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' => [

View File

@@ -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')}) }}

View File

@@ -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')}) }}

View File

@@ -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 #}
<!-- A -->

View File

@@ -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 #}

View File

@@ -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: #}

View File

@@ -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');