Make reports slightly more multi currency.

This commit is contained in:
James Cole 2018-09-06 19:47:29 +02:00
parent 1903292202
commit d5773ab5d0
11 changed files with 111 additions and 26 deletions

View File

@ -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.
*

View File

@ -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);

View File

@ -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;

View File

@ -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()

View File

@ -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 {

View File

@ -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',

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>