This commit is contained in:
Florian Dupret
2020-08-01 18:51:12 +02:00
parent 977c78412f
commit f4b6a63514
2 changed files with 17 additions and 34 deletions

View File

@@ -25,7 +25,6 @@ namespace FireflyIII\Support\Twig;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -63,7 +62,7 @@ class TransactionGroupTwig extends AbstractExtension
{ {
return new TwigFunction( return new TwigFunction(
'groupAmount', 'groupAmount',
function (array $array, Account $account): string { function (array $array): string {
$sums = $array['sums']; $sums = $array['sums'];
$return = []; $return = [];
$first = reset($array['transactions']); $first = reset($array['transactions']);
@@ -79,8 +78,7 @@ class TransactionGroupTwig extends AbstractExtension
$amount = $sum['amount']; $amount = $sum['amount'];
$sourceType = $first['source_account_type'] ?? 'invalid'; $sourceType = $first['source_account_type'] ?? 'invalid';
$sourceAccountId = $first['source_account_id']; $amount = $this->signAmount($amount, $type, $sourceType);
$amount = $this->signAmountFromAccountPOV($amount, $type, $sourceType, $sourceAccountId, $account->id);
$return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored); $return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored);
} }
@@ -172,12 +170,12 @@ class TransactionGroupTwig extends AbstractExtension
{ {
return new TwigFunction( return new TwigFunction(
'journalArrayAmount', 'journalArrayAmount',
function (array $journal, Account $account): string { function (array $array): string {
// if is not a withdrawal, amount positive. // if is not a withdrawal, amount positive.
$result = $this->normalJournalArrayAmount($journal, $account); $result = $this->normalJournalArrayAmount($array);
// now append foreign amount, if any. // now append foreign amount, if any.
if (null !== $journal['foreign_amount']) { if (null !== $array['foreign_amount']) {
$foreign = $this->foreignJournalArrayAmount($journal, $account); $foreign = $this->foreignJournalArrayAmount($array);
$result = sprintf('%s (%s)', $result, $foreign); $result = sprintf('%s (%s)', $result, $foreign);
} }
@@ -213,25 +211,23 @@ class TransactionGroupTwig extends AbstractExtension
/** /**
* Generate foreign amount for transaction from a transaction group. * Generate foreign amount for transaction from a transaction group.
* *
* @param array $journal * @param array $array
* @param Account $account
* *
* @return string * @return string
*/ */
private function foreignJournalArrayAmount(array $journal, Account $account): string private function foreignJournalArrayAmount(array $array): string
{ {
$type = $journal['transaction_type_type'] ?? TransactionType::WITHDRAWAL; $type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $journal['foreign_amount'] ?? '0'; $amount = $array['foreign_amount'] ?? '0';
$colored = true; $colored = true;
$sourceType = $journal['source_account_type'] ?? 'invalid'; $sourceType = $array['source_account_type'] ?? 'invalid';
$sourceAccountId = $journal['source_account_id']; $amount = $this->signAmount($amount, $type, $sourceType);
$amount = $this->signAmountFromAccountPOV($amount, $type, $sourceType, $sourceAccountId, $account->id);
if ($type === TransactionType::TRANSFER) { if ($type === TransactionType::TRANSFER) {
$colored = false; $colored = false;
} }
$result = app('amount')->formatFlat($journal['foreign_currency_symbol'], (int)$journal['foreign_currency_decimal_places'], $amount, $colored); $result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) { if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result); $result = sprintf('<span class="text-info">%s</span>', $result);
} }
@@ -277,14 +273,13 @@ class TransactionGroupTwig extends AbstractExtension
* *
* @return string * @return string
*/ */
private function normalJournalArrayAmount(array $journal, Account $account): string private function normalJournalArrayAmount(array $journal): string
{ {
$type = $journal['transaction_type_type'] ?? TransactionType::WITHDRAWAL; $type = $journal['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $journal['amount'] ?? '0'; $amount = $journal['amount'] ?? '0';
$colored = true; $colored = true;
$sourceType = $journal['source_account_type'] ?? 'invalid'; $sourceType = $journal['source_account_type'] ?? 'invalid';
$sourceAccountId = $journal['source_account_id']; $amount = $this->signAmount($amount, $type, $sourceType);
$amount = $this->signAmount($amount, $type, $sourceType, $sourceAccountId, $account->id);
if ($type === TransactionType::TRANSFER) { if ($type === TransactionType::TRANSFER) {
$colored = false; $colored = false;
@@ -358,16 +353,4 @@ class TransactionGroupTwig extends AbstractExtension
return $amount; return $amount;
} }
private function signAmountFromAccountPOV(string $amount, string $transactionType, string $sourceType, int $sourceAccountId, $displayedAccountId): string {
$amount = $this->signAmount( $amount, $transactionType, $sourceType );
// transfers stay negative from source point of view
if ($transactionType === TransactionType::TRANSFER
&& $sourceAccountId === $displayedAccountId) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
} }

View File

@@ -4,14 +4,14 @@
{% for transaction in group.transactions %} {% for transaction in group.transactions %}
{{ transaction.description }} {{ transaction.description }}
<span class="pull-right small"> <span class="pull-right small">
{{ journalArrayAmount(transaction, account) }} {{ journalArrayAmount(transaction) }}
</span> </span>
<br/> <br/>
{% endfor %} {% endfor %}
{% if group.count > 1 %} {% if group.count > 1 %}
&nbsp; &nbsp;
<span class="pull-right small"> <span class="pull-right small">
{{ groupAmount(group, account) }} {{ groupAmount(group) }}
</span> </span>
{% endif %} {% endif %}
</a> </a>