From 9d409a7412d13a17bb53d3bfb909c0da2e58d82c Mon Sep 17 00:00:00 2001 From: Aniruddha Maru Date: Sat, 25 Jul 2020 14:45:40 -0700 Subject: [PATCH] Fix recurring transactions create - If there's a lot of accounts to calculate balances for, then recurring transactions create page doesn't load. Partly because it has to calculate a lot of balances, but partly because the cache isn't being used at all because date is `new Date` rather than say, end of month. Fix: Change Steam balance calculator to always default cache using end of month. Since cache is 'invalidated' upon any edit, there's no reason to use current datetime anywhere its not explicitly required by user flow. Fix: Don't calculate balances for revenue / expense accounts since those are unbounded. Issue: #3597 --- .../Transaction/ConvertController.php | 4 +- app/Support/Form/AccountForm.php | 216 +++++------------- app/Support/Steam.php | 22 +- 3 files changed, 76 insertions(+), 166 deletions(-) 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