diff --git a/app/Http/Controllers/Transaction/ConvertController.php b/app/Http/Controllers/Transaction/ConvertController.php index 42c0cee6a2..ae6556038a 100644 --- a/app/Http/Controllers/Transaction/ConvertController.php +++ b/app/Http/Controllers/Transaction/ConvertController.php @@ -260,7 +260,7 @@ class ConvertController extends Controller // group accounts: /** @var Account $account */ foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); + $balance = app('steam')->balance($account); $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; $role = (string) $repository->getMetaValue($account, 'account_role'); if ('' === $role) { @@ -289,7 +289,7 @@ class ConvertController extends Controller // group accounts: /** @var Account $account */ foreach ($accountList as $account) { - $balance = app('steam')->balance($account, new Carbon); + $balance = app('steam')->balance($account); $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; $role = 'l_' . $account->accountType->type; $key = (string) trans('firefly.opt_group_' . $role); diff --git a/app/Support/Form/AccountForm.php b/app/Support/Form/AccountForm.php index b8e4d73289..112df4317c 100644 --- a/app/Support/Form/AccountForm.php +++ b/app/Support/Form/AccountForm.php @@ -26,6 +26,8 @@ namespace FireflyIII\Support\Form; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use Illuminate\Support\Collection; use Log; use Throwable; @@ -41,6 +43,46 @@ class AccountForm { use FormSupport; + private function getAccountsGrouped(array $types, AccountRepositoryInterface $repository = null): array + { + if (null === $repository) { + $repository = $this->getAccountRepository(); + } + $accountList = $repository->getActiveAccountsByType($types); + $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]; + $balanceTypes = [AccountType::ASSET, AccountType::DEFAULT, AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN,]; + $defaultCurrency = app('amount')->getDefaultCurrency(); + $grouped = []; + + /** @var Account $account */ + foreach ($accountList as $account) { + $accountWithBalance = $account->name; + + if (in_array($account->accountType->type, $balanceTypes, true)) { + $balance = app('steam')->balance($account); + $currency = $repository->getAccountCurrency($account) ?? $defaultCurrency; + $formatted = app('amount')->formatAnything($currency, $balance, false); + $accountWithBalance = sprintf('%s (%s)', $account->name, $formatted); + } + $role = (string)$repository->getMetaValue($account, 'account_role'); + if (in_array($account->accountType->type, $liabilityTypes, true)) { + $role = sprintf('l_%s', $account->accountType->type); + } elseif ('' === $role) { + if (AccountType::EXPENSE === $account->accountType->type) { + $role = 'expense_account'; + } elseif (AccountType::REVENUE === $account->accountType->type) { + $role = 'revenue_account'; + } else { + $role = 'no_account_type'; + } + } + $key = (string)trans(sprintf('firefly.opt_group_%s', $role)); + $grouped[$key][$account->id] = $accountWithBalance; + } + + return $grouped; + } + /** * Shows a