mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-08-29 09:16:15 +00:00
Complex but workable code for #865
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
@@ -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
|
||||
*/
|
||||
|
@@ -89,6 +89,10 @@
|
||||
<td>{{ 'total_amount'|_ }}</td>
|
||||
<td>
|
||||
{{ journalAmount(journal) }}
|
||||
|
||||
{% if transactions|length > 1 %}
|
||||
({{ journalTotalAmount(journal) }})
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
Reference in New Issue
Block a user