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 @@
+ + {{ 'sum_in_default_currency'|_ }} + +