Skip to content

Firefly III: a personal finances manager

Make reports slightly more multi currency.

James Coled5773ab5d0c5
diff --git a/app/Helpers/Collection/BillLine.php b/app/Helpers/Collection/BillLine.php
index eb1e4c8fee..2a11629aeb 100644
--- a/app/Helpers/Collection/BillLine.php
+++ b/app/Helpers/Collection/BillLine.php
@@ -24,6 +24,7 @@ namespace FireflyIII\Helpers\Collection;
use Carbon\Carbon;
use FireflyIII\Models\Bill as BillModel;
+use FireflyIII\Models\TransactionCurrency;
/**
* Class BillLine.
@@ -42,6 +43,8 @@ class BillLine
protected $max;
/** @var string What was the min amount. */
protected $min;
+ /** @var TransactionCurrency The transaction currency */
+ private $currency;
/** @var Carbon Latest date that payment is expected. */
private $endOfPayDate;
/** @var Carbon Date of last hit */
@@ -99,6 +102,22 @@ class BillLine
$this->bill = $bill;
}
+ /**
+ * @return TransactionCurrency
+ */
+ public function getCurrency(): TransactionCurrency
+ {
+ return $this->currency;
+ }
+
+ /**
+ * @param TransactionCurrency $currency
+ */
+ public function setCurrency(TransactionCurrency $currency): void
+ {
+ $this->currency = $currency;
+ }
+
/**
* End of pay date getter.
*
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index 42ed961e02..ef1b771c9c 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -98,6 +98,7 @@ class ReportHelper implements ReportHelperInterface
$billLine = new BillLine;
$billLine->setBill($bill);
+ $billLine->setCurrency($bill->transactionCurrency);
$billLine->setPayDate($payDate);
$billLine->setEndOfPayDate($endOfPayPeriod);
$billLine->setMin((string)$bill->amount_min);
diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php
index 3939baf11b..3c0ef13f07 100644
--- a/app/Repositories/Account/AccountTasker.php
+++ b/app/Repositories/Account/AccountTasker.php
@@ -24,8 +24,10 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
+use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@@ -68,18 +70,28 @@ class AccountTasker implements AccountTaskerInterface
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
+ /** @var CurrencyRepositoryInterface $currencyRepository */
+ $currencyRepository = app(CurrencyRepositoryInterface::class);
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+
$return = [
+ 'currencies' => [],
'start' => '0',
'end' => '0',
'difference' => '0',
'accounts' => [],
];
+ /** @var Account $account */
foreach ($accounts as $account) {
- $id = $account->id;
- $entry = [
+ $id = $account->id;
+ $currencyId = (int)$repository->getMetaValue($account, 'currency_id');
+ $currency = $currencyRepository->findNull($currencyId);
+ $return['currencies'][] = $currencyId;
+ $entry = [
'name' => $account->name,
'id' => $account->id,
+ 'currency' => $currency ?? $defaultCurrency,
'start_balance' => '0',
'end_balance' => '0',
];
@@ -100,7 +112,7 @@ class AccountTasker implements AccountTaskerInterface
$return['accounts'][$id] = $entry;
}
-
+ $return['currencies'] = count(array_unique($return['currencies']));
$return['difference'] = bcsub($return['end'], $return['start']);
return $return;
@@ -206,29 +218,51 @@ class AccountTasker implements AccountTaskerInterface
*/
private function groupByOpposing(Collection $transactions): array
{
- $expenses = [];
- // join the result together:
+ $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
+ /** @var CurrencyRepositoryInterface $currencyRepos */
+ $currencyRepos = app(CurrencyRepositoryInterface::class);
+ $currencies = [$defaultCurrency->id => $defaultCurrency,];
+ $expenses = [];
+ $countAccounts = []; // if count remains 0 use original name, not the name with the currency.
+
+
+ /** @var Transaction $transaction */
foreach ($transactions as $transaction) {
- $opposingId = $transaction->opposing_account_id;
- $name = $transaction->opposing_account_name;
- if (!isset($expenses[$opposingId])) {
- $expenses[$opposingId] = [
- 'id' => $opposingId,
- 'name' => $name,
- 'sum' => '0',
- 'average' => '0',
- 'count' => 0,
+ $opposingId = (int)$transaction->opposing_account_id;
+ $currencyId = (int)$transaction->transaction_currency_id;
+ $key = sprintf('%s-%s', $opposingId, $currencyId);
+ $name = sprintf('%s (%s)', $transaction->opposing_account_name, $transaction->transaction_currency_code);
+ $countAccounts[$opposingId] = isset($countAccounts[$opposingId]) ? $countAccounts[$opposingId] + 1 : 1;
+ if (!isset($expenses[$key])) {
+ $currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepos->findNull($currencyId);
+ $expenses[$key] = [
+ 'id' => $opposingId,
+ 'name' => $name,
+ 'original' => $transaction->opposing_account_name,
+ 'sum' => '0',
+ 'average' => '0',
+ 'currencies' => [],
+ 'single_currency' => $currencies[$currencyId],
+ 'count' => 0,
];
}
- $expenses[$opposingId]['sum'] = bcadd($expenses[$opposingId]['sum'], $transaction->transaction_amount);
- ++$expenses[$opposingId]['count'];
+ $expenses[$key]['currencies'][] = (int)$transaction->transaction_currency_id;
+ $expenses[$key]['sum'] = bcadd($expenses[$key]['sum'], $transaction->transaction_amount);
+ ++$expenses[$key]['count'];
}
// do averages:
$keys = array_keys($expenses);
foreach ($keys as $key) {
+ $opposingId = $expenses[$key]['id'];
+ if(1===$countAccounts[$opposingId]) {
+ $expenses[$key]['name'] = $expenses[$key]['original'];
+ }
+
if ($expenses[$key]['count'] > 1) {
$expenses[$key]['average'] = bcdiv($expenses[$key]['sum'], (string)$expenses[$key]['count']);
}
+ $expenses[$key]['currencies'] = \count(array_unique($expenses[$key]['currencies']));
+ $expenses[$key]['all_currencies'] = \count($currencies);
}
return $expenses;
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index b651b885b7..d9578b8cad 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -148,7 +148,7 @@ class BillRepository implements BillRepositoryInterface
public function getBillsForAccounts(Collection $accounts): Collection
{
$fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.match', 'bills.amount_min',
- 'bills.amount_max', 'bills.date', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active', 'bills.name_encrypted',
+ 'bills.amount_max', 'bills.date','bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active', 'bills.name_encrypted',
'bills.match_encrypted',];
$ids = $accounts->pluck('id')->toArray();
$set = $this->user->bills()
diff --git a/app/Support/Http/Controllers/RenderPartialViews.php b/app/Support/Http/Controllers/RenderPartialViews.php
index 85a2ca3568..06958f437d 100644
--- a/app/Support/Http/Controllers/RenderPartialViews.php
+++ b/app/Support/Http/Controllers/RenderPartialViews.php
@@ -27,6 +27,7 @@ namespace FireflyIII\Support\Http\Controllers;
use FireflyIII\Helpers\Collection\BalanceLine;
use FireflyIII\Helpers\Report\PopupReportInterface;
use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Budget;
use FireflyIII\Models\Tag;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
@@ -102,6 +103,7 @@ trait RenderPartialViews
break;
case BalanceLine::ROLE_DEFAULTROLE === $role && null === $budget && null !== $account:
// normal row without a budget:
+ $budget = new Budget;
$journals = $popupHelper->balanceForNoBudget($account, $attributes);
$budget->name = (string)trans('firefly.no_budget');
break;
@@ -156,7 +158,7 @@ trait RenderPartialViews
$budget = $budgetRepository->findNull((int)$attributes['budgetId']);
if (null === $budget) {
- return 'This is an unknown budget. Apologies.';
+ $budget = new Budget;
}
$journals = $popupHelper->byBudget($budget, $attributes);
try {
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 3ddb4c3e13..c5a09fd81a 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -1013,6 +1013,8 @@ return [
'in_out_per_category' => 'Earned and spent per category',
'out_per_budget' => 'Spent per budget',
'select_expense_revenue' => 'Select expense/revenue account',
+ 'multi_currency_report_sum' => 'Because this list contains accounts with multiple currencies, the sum(s) you see may not make sense. The report will always fall back to your default currency.',
+ 'sum_in_default_currency' => 'The sum will always be in your default currency.',
// charts:
'chart' => 'Chart',
diff --git a/resources/views/popup/report/budget-spent-amount.twig b/resources/views/popup/report/budget-spent-amount.twig
index fa88bae9ae..79b3fc8156 100644
--- a/resources/views/popup/report/budget-spent-amount.twig
+++ b/resources/views/popup/report/budget-spent-amount.twig
@@ -9,6 +9,11 @@
<div class="modal-body">
{% set hideBudget = true %}
{% include 'popup/list/journals' %}
+ <p>
+ <small class="text-warning">
+ {{ 'sum_in_default_currency'|_ }}
+ </small>
+ </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{{ 'close'|_ }}</button>
diff --git a/resources/views/reports/partials/accounts.twig b/resources/views/reports/partials/accounts.twig
index f45376f26e..34ef9d6798 100644
--- a/resources/views/reports/partials/accounts.twig
+++ b/resources/views/reports/partials/accounts.twig
@@ -13,10 +13,15 @@
<td data-value="{{ account.name }}">
<a href="{{ route('accounts.show',account.id) }}" title="{{ account.name }}">{{ account.name }}</a>
</td>
- <td class="hidden-xs" data-value="{{ account.start_balance }}" style="text-align: right;">{{ account.start_balance|formatAmount }}</td>
- <td class="hidden-xs" data-value="{{ account.end_balance }}" style="text-align: right;">{{ account.end_balance|formatAmount }}</td>
+ <td class="hidden-xs" data-value="{{ account.start_balance }}" style="text-align: right;">
+ {{ formatAmountByCurrency(account.currency, account.start_balance) }}
+ </td>
+ <td class="hidden-xs" data-value="{{ account.end_balance }}" style="text-align: right;">
+ {{ formatAmountByCurrency(account.currency, account.end_balance) }}</td>
<td style="text-align: right;"
- data-value="{{ (account.end_balance - account.start_balance) }}">{{ (account.end_balance - account.start_balance)|formatAmount }}</td>
+ data-value="{{ (account.end_balance - account.start_balance) }}">
+ {{ formatAmountByCurrency(account.currency, (account.end_balance - account.start_balance)) }}
+ </td>
</tr>
{% endfor %}
</tbody>
@@ -27,5 +32,10 @@
<td class="hidden-xs" style="text-align: right;">{{ accountReport.end|formatAmount }}</td>
<td style="text-align: right;">{{ accountReport.difference|formatAmount }}</td>
</tr>
+ {% if accountReport.currencies > 1 %}
+ <tr>
+ <td colspan="4"><small class="text-warning">{{ 'multi_currency_report_sum'|_ }}</small></td>
+ </tr>
+ {% endif %}
</tfoot>
</table>
diff --git a/resources/views/reports/partials/bills.twig b/resources/views/reports/partials/bills.twig
index 86f826036b..f6a90b5081 100644
--- a/resources/views/reports/partials/bills.twig
+++ b/resources/views/reports/partials/bills.twig
@@ -22,14 +22,14 @@
{{ trans('firefly.bill_expected_between', {start: line.getPayDate.formatLocalized(monthAndDayFormat), end: line.getEndOfPayDate.formatLocalized(monthAndDayFormat) }) }}
</small>
</td>
- <td class="hidden-xs" data-value="{{ line.getMin }}" style="text-align: right;">{{ line.getMin|formatAmount }}</td>
- <td class="hidden-xs" data-value="{{ line.getMax }}" style="text-align: right;">{{ line.getMax|formatAmount }}</td>
+ <td class="hidden-xs" data-value="{{ line.getMin }}" style="text-align: right;">{{ formatAmountByCurrency(line.getCurrency, line.getMin) }}</td>
+ <td class="hidden-xs" data-value="{{ line.getMax }}" style="text-align: right;">{{ formatAmountByCurrency(line.getCurrency, line.getMax) }}</td>
{# if bill is hit, show hit amount #}
{% if line.isHit %}
<td data-value="{{ line.getAmount }}" style="text-align: right;">
<a href="{{ route('transactions.show', line.getTransactionJournalId) }}">
- {{ line.getAmount|formatAmount }}
+ {{ formatAmountByCurrency(line.getCurrency, line.getAmount) }}
</a>
</td>
{% endif %}
@@ -42,7 +42,7 @@
{% endif %}
<td data-value="{{ (line.getMax - line.getAmount) }}" style="text-align: right;" class="hidden-xs">
{% if line.isHit %}
- {{ (line.getMax + line.getAmount)|formatAmount }}
+ {{ formatAmountByCurrency(line.getCurrency, (line.getMax + line.getAmount)) }}
{% endif %}
</td>
</tr>
diff --git a/resources/views/reports/partials/income-expenses.twig b/resources/views/reports/partials/income-expenses.twig
index c33cd81c49..6d804feceb 100644
--- a/resources/views/reports/partials/income-expenses.twig
+++ b/resources/views/reports/partials/income-expenses.twig
@@ -26,7 +26,11 @@
{% endif %}
</td>
<td data-value="{{ entry.sum }}" style="text-align: right;">
- {{ (entry.sum)|formatAmount }}
+ {% if entry.currencies == 1 %}
+ {{ formatAmountByCurrency(entry.single_currency, entry.sum) }}
+ {% else %}
+ {{ (entry.sum)|formatAmount }}
+ {% endif %}
</td>
<td class="hidden-xs" data-value="{{ entry.average }}" style="text-align: right;">
{% if entry.count > 1 %}
@@ -54,5 +58,8 @@
<td><em>{{ 'sum'|_ }}</em></td>
<td style="text-align: right;">{{ (sum)|formatAmount }}</td>
</tr>
+ <tr>
+ <td colspan="4"><small class="text-warning">{{ 'sum_in_default_currency'|_ }}</small></td>
+ </tr>
</tfoot>
</table>
diff --git a/resources/views/reports/partials/operations.twig b/resources/views/reports/partials/operations.twig
index ef501ba3a2..b77387d38d 100644
--- a/resources/views/reports/partials/operations.twig
+++ b/resources/views/reports/partials/operations.twig
@@ -11,4 +11,9 @@
<td>{{ 'difference'|_ }}</td>
<td style="text-align: right;">{{ (incomeSum + expensesSum)|formatAmount }}</td>
</tr>
+ <tr>
+ <td colspan="2">
+ <small class="text-warning">{{ 'sum_in_default_currency'|_ }}</small>
+ </td>
+ </tr>
</table>