From 5329e026dcb80e64e1b4eb386a56585d61ed948d Mon Sep 17 00:00:00 2001
From: James Cole
Date: Tue, 6 Jun 2017 07:18:09 +0200
Subject: [PATCH] Fixed various currency displays.
---
.../Report/Audit/MonthReportGenerator.php | 19 ++++++++++++++-----
app/Helpers/Collector/JournalCollector.php | 3 +++
app/Support/Twig/AmountFormat.php | 18 ++++++++++++++++++
resources/views/accounts/show.twig | 4 ++--
resources/views/reports/audit/report.twig | 4 ++--
.../reports/partials/journals-audit.twig | 4 ++--
6 files changed, 41 insertions(+), 11 deletions(-)
diff --git a/app/Generator/Report/Audit/MonthReportGenerator.php b/app/Generator/Report/Audit/MonthReportGenerator.php
index 0b12e60fe0..d02815963a 100644
--- a/app/Generator/Report/Audit/MonthReportGenerator.php
+++ b/app/Generator/Report/Audit/MonthReportGenerator.php
@@ -19,6 +19,7 @@ use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Steam;
@@ -147,6 +148,8 @@ class MonthReportGenerator implements ReportGeneratorInterface
*/
private function getAuditReport(Account $account, Carbon $date): array
{
+ /** @var CurrencyRepositoryInterface $currencyRepos */
+ $currencyRepos = app(CurrencyRepositoryInterface::class);
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
@@ -155,15 +158,21 @@ class MonthReportGenerator implements ReportGeneratorInterface
$journals = $journals->reverse();
$dayBeforeBalance = Steam::balance($account, $date);
$startBalance = $dayBeforeBalance;
-
+ $currency = $currencyRepos->find(intval($account->getMeta('currency_id')));
/** @var Transaction $journal */
foreach ($journals as $transaction) {
$transaction->before = $startBalance;
- $transactionAmount = $transaction->transaction_amount;
- $newBalance = bcadd($startBalance, $transactionAmount);
- $transaction->after = $newBalance;
- $startBalance = $newBalance;
+ $transactionAmount = $transaction->transaction_amount;
+
+ if ($currency->id === $transaction->foreign_currency_id) {
+ $transactionAmount = $transaction->transaction_foreign_amount;
+ }
+
+ $newBalance = bcadd($startBalance, $transactionAmount);
+ $transaction->after = $newBalance;
+ $startBalance = $newBalance;
+ $transaction->currency = $currency;
}
/*
diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php
index 2fbc389eab..7edc9a5164 100644
--- a/app/Helpers/Collector/JournalCollector.php
+++ b/app/Helpers/Collector/JournalCollector.php
@@ -72,11 +72,14 @@ class JournalCollector implements JournalCollectorInterface
'transactions.transaction_journal_id',
'transactions.amount as transaction_amount',
+
+ 'transactions.transaction_currency_id as transaction_currency_id',
'transaction_currencies.code as transaction_currency_code',
'transaction_currencies.symbol as transaction_currency_symbol',
'transaction_currencies.decimal_places as transaction_currency_dp',
'transactions.foreign_amount as transaction_foreign_amount',
+ 'transactions.foreign_currency_id as foreign_currency_id',
'foreign_currencies.code as foreign_currency_code',
'foreign_currencies.symbol as foreign_currency_symbol',
'foreign_currencies.decimal_places as foreign_currency_dp',
diff --git a/app/Support/Twig/AmountFormat.php b/app/Support/Twig/AmountFormat.php
index ba7011f902..db267e4401 100644
--- a/app/Support/Twig/AmountFormat.php
+++ b/app/Support/Twig/AmountFormat.php
@@ -52,6 +52,7 @@ class AmountFormat extends Twig_Extension
$this->formatDestinationBefore(),
$this->formatSourceAfter(),
$this->formatSourceBefore(),
+ $this->formatAmountByCurrency(),
];
}
@@ -107,6 +108,23 @@ class AmountFormat extends Twig_Extension
);
}
+ /**
+ * Will format the amount by the currency related to the given account.
+ *
+ * @return Twig_SimpleFunction
+ */
+ protected function formatAmountByCurrency(): Twig_SimpleFunction
+ {
+ return new Twig_SimpleFunction(
+ 'formatAmountByCurrency', function (TransactionCurrency $currency, string $amount, bool $coloured = true): string {
+
+ return app('amount')->formatAnything($currency, $amount, $coloured);
+
+
+ }, ['is_safe' => ['html']]
+ );
+ }
+
/**
* @return Twig_SimpleFilter
*/
diff --git a/resources/views/accounts/show.twig b/resources/views/accounts/show.twig
index 40da0b960c..41f430b756 100644
--- a/resources/views/accounts/show.twig
+++ b/resources/views/accounts/show.twig
@@ -119,13 +119,13 @@
{% if period.spent != 0 %}
| {{ 'spent'|_ }} |
- {{ period.spent|formatAmount }} |
+ {{ formatAmountByCurrency(currency, period.spent) }} |
{% endif %}
{% if period.earned != 0 %}
| {{ 'earned'|_ }} |
- {{ period.earned|formatAmount }} |
+ {{ formatAmountByCurrency(currency, period.earned) }} |
{% endif %}
diff --git a/resources/views/reports/audit/report.twig b/resources/views/reports/audit/report.twig
index 01444e9212..0a4b8b4e52 100644
--- a/resources/views/reports/audit/report.twig
+++ b/resources/views/reports/audit/report.twig
@@ -59,7 +59,7 @@
account_name: account.name,
url: url,
end: auditData[account.id].end,
- balance: auditData[account.id].endBalance|formatAmount
+ balance: formatAmountByAccount(account,auditData[account.id].endBalance)
})|raw }}
{% include 'reports.partials.journals-audit' with {'journals': auditData[account.id].journals,'account':account} %}
@@ -69,7 +69,7 @@
account_name: account.name,
url: url,
end: auditData[account.id].dayBefore,
- balance: auditData[account.id].dayBeforeBalance|formatAmount
+ balance: formatAmountByAccount(account, auditData[account.id].dayBeforeBalance)
})|raw }}
diff --git a/resources/views/reports/partials/journals-audit.twig b/resources/views/reports/partials/journals-audit.twig
index 4f0f794f96..d5da12c69f 100644
--- a/resources/views/reports/partials/journals-audit.twig
+++ b/resources/views/reports/partials/journals-audit.twig
@@ -57,11 +57,11 @@
{% endif %}
- {{ transaction.before|formatAmount }} |
+ {{ formatAmountByCurrency(transaction.currency, transaction.before) }} |
{{ transactionAmount(transaction) }}
|
- {{ transaction.after|formatAmount }} |
+ {{ formatAmountByCurrency(transaction.currency, transaction.after) }} |
{{ transaction.date.formatLocalized(monthAndDayFormat) }} |
|