From a41d7378ef34d224dfbb4d484e944fd2db9c1c57 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Apr 2021 06:41:21 +0200 Subject: [PATCH] Some fixes in the account list --- .../Controllers/Account/IndexController.php | 21 +++++++++----- .../Support/CreditRecalculateService.php | 29 +++++++++++++++++-- resources/lang/en_US/firefly.php | 3 ++ resources/views/v1/list/accounts.twig | 10 +++++-- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Account/IndexController.php b/app/Http/Controllers/Account/IndexController.php index ac0c95de4a..c80673db05 100644 --- a/app/Http/Controllers/Account/IndexController.php +++ b/app/Http/Controllers/Account/IndexController.php @@ -160,14 +160,19 @@ class IndexController extends Controller $accounts->each( function (Account $account) use ($activities, $startBalances, $endBalances) { // TODO lots of queries executed in this block. - $account->lastActivityDate = $this->isInArray($activities, $account->id); - $account->startBalance = $this->isInArray($startBalances, $account->id); - $account->endBalance = $this->isInArray($endBalances, $account->id); - $account->difference = bcsub($account->endBalance, $account->startBalance); - $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', ''); - $account->interestPeriod = (string)trans(sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period'))); - $account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type)); - $account->location = $this->repository->getLocation($account); + $account->lastActivityDate = $this->isInArray($activities, $account->id); + $account->startBalance = $this->isInArray($startBalances, $account->id); + $account->endBalance = $this->isInArray($endBalances, $account->id); + $account->difference = bcsub($account->endBalance, $account->startBalance); + $account->interest = number_format((float)$this->repository->getMetaValue($account, 'interest'), 4, '.', ''); + $account->interestPeriod = (string)trans( + sprintf('firefly.interest_calc_%s', $this->repository->getMetaValue($account, 'interest_period')) + ); + $account->accountTypeString = (string)trans(sprintf('firefly.account_type_%s', $account->accountType->type)); + $account->location = $this->repository->getLocation($account); + + $account->liability_direction = $this->repository->getMetaValue($account, 'liability_direction'); + $account->current_debt = $this->repository->getMetaValue($account, 'current_debt'); } ); // make paginator: diff --git a/app/Services/Internal/Support/CreditRecalculateService.php b/app/Services/Internal/Support/CreditRecalculateService.php index 2f925730d0..c4d2a8a457 100644 --- a/app/Services/Internal/Support/CreditRecalculateService.php +++ b/app/Services/Internal/Support/CreditRecalculateService.php @@ -28,6 +28,7 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionGroup; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; @@ -101,13 +102,18 @@ class CreditRecalculateService // get opening balance (if present) $this->repository->setUser($account->user); $startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0'; - + $leftOfDebt = app('steam')->positive($startOfDebt); /** @var AccountMetaFactory $factory */ $factory = app(AccountMetaFactory::class); $factory->crud($account, 'start_of_debt', $startOfDebt); - $factory->crud($account, 'current_debt', $startOfDebt); - // update meta data: + // now loop all transactions (except opening balance and credit thing) + $transactions = $account->transactions()->get(); + /** @var Transaction $transaction */ + foreach ($transactions as $transaction) { + $leftOfDebt = $this->processTransaction($transaction, $leftOfDebt); + } + $factory->crud($account, 'current_debt', $leftOfDebt); Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id)); @@ -230,5 +236,22 @@ class CreditRecalculateService $this->group = $group; } + /** + * @param Transaction $transaction + * @param string $amount + * + * @return string + */ + private function processTransaction(Transaction $transaction, string $amount): string + { + $journal = $transaction->transactionJournal; + $type = $journal->transactionType->type; + if (in_array($type, [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER], true)) { + $amount = bcadd($amount, bcmul($transaction->amount, '-1')); + } + + return $amount; + } + } \ No newline at end of file diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index c305df31b8..c3c5f46972 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -1322,6 +1322,8 @@ return [ 'account_type_Credit card' => 'Credit card', 'liability_direction_credit' => 'I am owed this debt', 'liability_direction_debit' => 'I owe this debt to somebody else', + 'liability_direction_credit_short' => 'Owed this debt', + 'liability_direction_debit_short' => 'Owe this debt', 'Liability credit' => 'Liability credit', 'budgets' => 'Budgets', 'tags' => 'Tags', @@ -1393,6 +1395,7 @@ return [ 'splitByAccount' => 'Split by account', 'coveredWithTags' => 'Covered with tags', 'leftInBudget' => 'Left in budget', + 'left_in_debt' => 'Left', 'sumOfSums' => 'Sum of sums', 'noCategory' => '(no category)', 'notCharged' => 'Not charged (yet)', diff --git a/resources/views/v1/list/accounts.twig b/resources/views/v1/list/accounts.twig index 8c487ff83d..03c2694001 100644 --- a/resources/views/v1/list/accounts.twig +++ b/resources/views/v1/list/accounts.twig @@ -11,12 +11,15 @@ {% endif %} {% if objectType == 'liabilities' %} {{ trans('list.liability_type') }} + {{ trans('form.liability_direction') }} {{ trans('list.interest') }} ({{ trans('list.interest_period') }}) {% endif %} {{ trans('form.account_number') }} {{ trans('list.currentBalance') }} {% if objectType == 'liabilities' %} - Left to pay off + + {{ trans('firefly.left_in_debt') }} + {% endif %} {{ trans('list.active') }} {# hide last activity to make room for other stuff #} @@ -54,6 +57,7 @@ {% endif %} {% if objectType == 'liabilities' %} {{ account.accountTypeString }} + {{ trans('firefly.liability_direction_'~account.liability_direction~'_short') }} {{ account.interest }}% ({{ account.interestPeriod|lower }}) {% endif %} {{ account.iban }}{% if account.iban == '' %}{{ accountGetMetaField(account, 'account_number') }}{% endif %} @@ -63,8 +67,8 @@ {% if objectType == 'liabilities' %} - - Test + + {{ formatAmountByAccount(account, account.current_debt, false) }} {% endif %}