. */ declare(strict_types=1); namespace FireflyIII\Support\Twig\Extension; use FireflyIII\Models\Transaction as TransactionModel; use FireflyIII\Models\TransactionJournal as JournalModel; use FireflyIII\Models\TransactionType; use FireflyIII\Support\SingleCacheProperties; use Twig_Extension; /** * Class TransactionJournal */ class TransactionJournal extends Twig_Extension { /** * @param JournalModel $journal * * @return string */ public function totalAmount(JournalModel $journal): string { $cache = new SingleCacheProperties; $cache->addProperty('total-amount'); $cache->addProperty($journal->id); $cache->addProperty($journal->updated_at); if ($cache->has()) { return $cache->get(); } $transactions = $journal->transactions()->where('amount', '>', 0)->get(); $totals = []; $type = $journal->transactionType->type; /** @var TransactionModel $transaction */ foreach ($transactions as $transaction) { $currencyId = $transaction->transaction_currency_id; $currency = $transaction->transactionCurrency; if (!isset($totals[$currencyId])) { $totals[$currencyId] = [ 'amount' => '0', 'currency' => $currency, ]; } $totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']); if (null !== $transaction->foreign_currency_id) { $foreignId = $transaction->foreign_currency_id; $foreign = $transaction->foreignCurrency; if (!isset($totals[$foreignId])) { $totals[$foreignId] = [ 'amount' => '0', 'currency' => $foreign, ]; } $totals[$foreignId]['amount'] = bcadd($transaction->foreign_amount, $totals[$foreignId]['amount']); } } $array = []; foreach ($totals as $total) { if (TransactionType::WITHDRAWAL === $type) { $total['amount'] = bcmul($total['amount'], '-1'); } $array[] = app('amount')->formatAnything($total['currency'], $total['amount']); } $txt = join(' / ', $array); $cache->store($txt); return $txt; } }