From 8246d901e77838881231c08179896ca9a31e2fc0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 1 Sep 2019 19:17:46 +0200 Subject: [PATCH] Fix #2513 --- .../Report/Audit/MonthReportGenerator.php | 140 +++++++++--------- resources/views/v1/reports/audit/report.twig | 22 +-- .../v1/reports/partials/journals-audit.twig | 11 +- 3 files changed, 92 insertions(+), 81 deletions(-) diff --git a/app/Generator/Report/Audit/MonthReportGenerator.php b/app/Generator/Report/Audit/MonthReportGenerator.php index 53b9b13670..23abef23a3 100644 --- a/app/Generator/Report/Audit/MonthReportGenerator.php +++ b/app/Generator/Report/Audit/MonthReportGenerator.php @@ -94,6 +94,79 @@ class MonthReportGenerator implements ReportGeneratorInterface return $result; } + /** + * Get the audit report. + * + * @param Account $account + * @param Carbon $date + * + * @return array + * + * @throws FireflyException + */ + public function getAuditReport(Account $account, Carbon $date): array + { + /** @var AccountRepositoryInterface $accountRepository */ + $accountRepository = app(AccountRepositoryInterface::class); + $accountRepository->setUser($account->user); + + /** @var JournalRepositoryInterface $journalRepository */ + $journalRepository = app(JournalRepositoryInterface::class); + $journalRepository->setUser($account->user); + + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + $collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end)->withAccountInformation(); + $journals = $collector->getExtractedJournals(); + $journals = array_reverse($journals, true); + + $dayBeforeBalance = app('steam')->balance($account, $date); + $startBalance = $dayBeforeBalance; + $currency = $accountRepository->getAccountCurrency($account); + + if (null === $currency) { + throw new FireflyException('Unexpected NULL value in account currency preference.'); // @codeCoverageIgnore + } + + foreach ($journals as $index => $journal) { + $journals[$index]['balance_before'] = $startBalance; + $transactionAmount = $journal['amount']; + + // make sure amount is in the right "direction". + if ($account->id === $journal['destination_account_id']) { + $transactionAmount = app('steam')->positive($journal['amount']); + } + + if ($currency->id === $journal['foreign_currency_id']) { + $transactionAmount = $journal['foreign_amount']; + } + + $newBalance = bcadd($startBalance, $transactionAmount); + $journals[$index]['balance_after'] = $newBalance; + $startBalance = $newBalance; + + // add meta dates for each journal. + $journals[$index]['interest_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'interest_date'); + $journals[$index]['book_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'book_date'); + $journals[$index]['process_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'process_date'); + $journals[$index]['due_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'due_date'); + $journals[$index]['payment_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'payment_date'); + $journals[$index]['invoice_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'invoice_date'); + + } + + $return = [ + 'journals' => $journals, + 'exists' => count($journals) > 0, + 'end' => $this->end->formatLocalized((string)trans('config.month_and_day')), + 'endBalance' => app('steam')->balance($account, $this->end), + 'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')), + 'dayBeforeBalance' => $dayBeforeBalance, + ]; + + return $return; + } + /** * Account collection setter. * @@ -191,71 +264,4 @@ class MonthReportGenerator implements ReportGeneratorInterface { return $this; } - - /** - * Get the audit report. - * - * @param Account $account - * @param Carbon $date - * - * @return array - * - * @throws FireflyException - */ - public function getAuditReport(Account $account, Carbon $date): array - { - /** @var AccountRepositoryInterface $accountRepository */ - $accountRepository = app(AccountRepositoryInterface::class); - $accountRepository->setUser($account->user); - - /** @var JournalRepositoryInterface $journalRepository */ - $journalRepository = app(JournalRepositoryInterface::class); - $journalRepository->setUser($account->user); - - /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); - $collector->setAccounts(new Collection([$account]))->setRange($this->start, $this->end) - ->withAccountInformation(); - $journals = $collector->getExtractedJournals(); - $dayBeforeBalance = app('steam')->balance($account, $date); - $startBalance = $dayBeforeBalance; - $currency = $accountRepository->getAccountCurrency($account); - - if (null === $currency) { - throw new FireflyException('Unexpected NULL value in account currency preference.'); // @codeCoverageIgnore - } - - foreach ($journals as $index => $journal) { - $journals[$index]['balance_before'] = $startBalance; - $transactionAmount = $journal['amount']; - - if ($currency->id === $journal['foreign_currency_id']) { - $transactionAmount = $journal['foreign_amount']; - } - - $newBalance = bcadd($startBalance, $transactionAmount); - $journals[$index]['balance_after'] = $newBalance; - $startBalance = $newBalance; - - // add meta dates for each journal. - $journals[$index]['interest_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'interest_date'); - $journals[$index]['book_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'book_date'); - $journals[$index]['process_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'process_date'); - $journals[$index]['due_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'due_date'); - $journals[$index]['payment_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'payment_date'); - $journals[$index]['invoice_date'] = $journalRepository->getMetaDateById($journal['transaction_journal_id'], 'invoice_date'); - - } - - $return = [ - 'journals' => $journals, - 'exists' => count($journals) > 0, - 'end' => $this->end->formatLocalized((string)trans('config.month_and_day')), - 'endBalance' => app('steam')->balance($account, $this->end), - 'dayBefore' => $date->formatLocalized((string)trans('config.month_and_day')), - 'dayBeforeBalance' => $dayBeforeBalance, - ]; - - return $return; - } } diff --git a/resources/views/v1/reports/audit/report.twig b/resources/views/v1/reports/audit/report.twig index 6dd1bcbfbc..bbd5a372c2 100644 --- a/resources/views/v1/reports/audit/report.twig +++ b/resources/views/v1/reports/audit/report.twig @@ -52,6 +52,17 @@ {% else %}
+

+ {{ trans('firefly.audit_end_balance', + { + account_name: account.name|escape, + url: url, + end: auditData[account.id].dayBefore, + balance: formatAmountByAccount(account, auditData[account.id].dayBeforeBalance) + })|raw }} +

+ {% include 'reports.partials.journals-audit' with {'journals': auditData[account.id].journals,'account':account} %} +

{{ trans('firefly.audit_end_balance', @@ -61,17 +72,6 @@ end: auditData[account.id].end, balance: formatAmountByAccount(account,auditData[account.id].endBalance) })|raw }} - -

- {% include 'reports.partials.journals-audit' with {'journals': auditData[account.id].journals,'account':account} %} -

- {{ trans('firefly.audit_end_balance', - { - account_name: account.name|escape, - url: url, - end: auditData[account.id].dayBefore, - balance: formatAmountByAccount(account, auditData[account.id].dayBeforeBalance) - })|raw }}

{% endif %} diff --git a/resources/views/v1/reports/partials/journals-audit.twig b/resources/views/v1/reports/partials/journals-audit.twig index 0310f3ca64..b864699fe5 100644 --- a/resources/views/v1/reports/partials/journals-audit.twig +++ b/resources/views/v1/reports/partials/journals-audit.twig @@ -57,11 +57,11 @@ {% endif %} {% if journal.transaction_type_type == 'Reconciliation' %} - XX + {% endif %} {% if journal.transaction_type_type == 'Opening balance' %} - XX + {% endif %} @@ -80,7 +80,12 @@ {{ formatAmountBySymbol(journal.balance_before, journal.currency_symbol, journal.currency_decimal_places) }} - {{ formatAmountBySymbol(journal.amount, journal.currency_symbol, journal.currency_decimal_places) }} + {% if account.id == journal.destination_account_id and journal.transaction_type_type == 'Opening balance' %} + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places) }} + {% else %} + {{ formatAmountBySymbol(journal.amount, journal.currency_symbol, journal.currency_decimal_places) }} + {% endif %} +