From 6f55049fb6c6064360b5d4161c38d5a879df5c84 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 26 Sep 2017 10:59:17 +0200 Subject: [PATCH] Complex but workable code for #865 --- app/Support/Amount.php | 52 ++++++++++++++++++++++++++ app/Support/Twig/AmountFormat.php | 14 +++++++ resources/views/transactions/show.twig | 4 ++ 3 files changed, 70 insertions(+) diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 56e8792580..49b1872155 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -287,6 +287,58 @@ class Amount } + /** + * @param TransactionJournal $journal + * @param bool $coloured + * + * @return string + */ + public function journalTotalAmount(TransactionJournal $journal, bool $coloured = true): string + { + $transactions = $journal->transactions()->where('amount', '>', 0)->get(); + $totals = []; + $type = $journal->transactionType->type; + /** @var TransactionModel $transaction */ + foreach ($transactions as $transaction) { + // model some fields to fit "transactionAmount()": + $currencyId = $transaction->transaction_currency_id; + + if (!isset($totals[$currencyId])) { + $totals[$currencyId] = [ + 'amount' => '0', + 'symbol' => $transaction->transactionCurrency->symbol, + 'dp' => $transaction->transactionCurrency->decimal_places, + ]; + } + $totals[$currencyId]['amount'] = bcadd($transaction->amount, $totals[$currencyId]['amount']); + + if (!is_null($transaction->foreign_currency_id)) { + $foreignId = $transaction->foreign_currency_id; + if (!isset($totals[$foreignId])) { + $totals[$foreignId] = [ + 'amount' => '0', + 'symbol' => $transaction->foreignCurrency->symbol, + 'dp' => $transaction->foreignCurrency->decimal_places, + ]; + } + $totals[$foreignId]['amount'] = bcadd($transaction->foreign_amount, $totals[$foreignId]['amount']); + } + } + $array = []; + foreach ($totals as $total) { + $currency = new TransactionCurrency; + $currency->symbol = $total['symbol']; + $currency->decimal_places = $total['dp']; + if ($type === TransactionType::WITHDRAWAL) { + $total['amount'] = bcmul($total['amount'], '-1'); + } + $array[] = $this->formatAnything($currency, $total['amount']); + } + + return join(' / ', $array); + + } + /** * This formats a transaction, IF that transaction has been "collected" using the JournalCollector. * diff --git a/app/Support/Twig/AmountFormat.php b/app/Support/Twig/AmountFormat.php index 3f9cd2ebcf..c438b7261a 100644 --- a/app/Support/Twig/AmountFormat.php +++ b/app/Support/Twig/AmountFormat.php @@ -49,6 +49,7 @@ class AmountFormat extends Twig_Extension $this->formatAmountBySymbol(), $this->transactionAmount(), $this->journalAmount(), + $this->journalTotalAmount(), $this->formatDestinationAfter(), $this->formatDestinationBefore(), $this->formatSourceAfter(), @@ -261,6 +262,19 @@ class AmountFormat extends Twig_Extension ); } + /** + * @return Twig_SimpleFunction + */ + protected function journalTotalAmount(): Twig_SimpleFunction + { + return new Twig_SimpleFunction( + 'journalTotalAmount', function (TransactionJournal $journal): string { + + return app('amount')->journalTotalAmount($journal, true); + }, ['is_safe' => ['html']] + ); + } + /** * @return Twig_SimpleFunction */ diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig index c6e0dc7678..8c50f019f6 100644 --- a/resources/views/transactions/show.twig +++ b/resources/views/transactions/show.twig @@ -89,6 +89,10 @@ {{ 'total_amount'|_ }} {{ journalAmount(journal) }} + + {% if transactions|length > 1 %} + ({{ journalTotalAmount(journal) }}) + {% endif %}