Complex but workable code for #865

This commit is contained in:
James Cole
2017-09-26 10:59:17 +02:00
parent d99adb515a
commit 6f55049fb6
3 changed files with 70 additions and 0 deletions

View File

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

View File

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

View File

@@ -89,6 +89,10 @@
<td>{{ 'total_amount'|_ }}</td>
<td>
{{ journalAmount(journal) }}
{% if transactions|length > 1 %}
({{ journalTotalAmount(journal) }})
{% endif %}
</td>
</tr>
<tr>