From 9f8c75efc6babac6b80ddae4996493d35a4f925f Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 30 Mar 2018 22:44:37 +0200 Subject: [PATCH] Fix null pointer in account format. --- app/Support/Twig/AmountFormat.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Support/Twig/AmountFormat.php b/app/Support/Twig/AmountFormat.php index 9d78bd8dfe..89ff951b5b 100644 --- a/app/Support/Twig/AmountFormat.php +++ b/app/Support/Twig/AmountFormat.php @@ -24,6 +24,8 @@ namespace FireflyIII\Support\Twig; use FireflyIII\Models\Account as AccountModel; use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use Twig_Extension; use Twig_SimpleFilter; use Twig_SimpleFunction; @@ -82,14 +84,19 @@ class AmountFormat extends Twig_Extension return new Twig_SimpleFunction( 'formatAmountByAccount', function (AccountModel $account, string $amount, bool $coloured = true): string { - $currencyId = intval($account->getMeta('currency_id')); - + /** @var AccountRepositoryInterface $accountRepos */ + $accountRepos = app(AccountRepositoryInterface::class); + /** @var CurrencyRepositoryInterface $currencyRepos */ + $currencyRepos = app(CurrencyRepositoryInterface::class); + $currency = app('amount')->getDefaultCurrency(); + $currencyId = (int)$accountRepos->getMetaValue($account, 'currency_id'); + $accountCurrency = null if (0 !== $currencyId) { - $currency = TransactionCurrency::find($currencyId); - - return app('amount')->formatAnything($currency, $amount, $coloured); + $accountCurrency = $currencyRepos->findNull($currencyId); + } + if (null !== $accountCurrency) { + $currency = $accountCurrency; } - $currency = app('amount')->getDefaultCurrency(); return app('amount')->formatAnything($currency, $amount, $coloured); },