diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index a102244030..fc98ff41be 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -64,14 +64,43 @@ class TransactionJournal extends Model */ public function getAmountAttribute() { + $amount = 0; /** @var Transaction $t */ foreach ($this->transactions as $t) { if ($t->amount > 0) { - return floatval($t->amount); + $amount = floatval($t->amount); } } - return 0; + /* + * If the journal has tags, it gets complicated. + */ + if ($this->tags->count() == 0) { + return $amount; + } + // if journal is part of advancePayment AND journal is a withdrawal, + // then journal is being repaid by other journals, so the actual amount will lower: + /** @var Tag $tag */ + $tag = $this->tags()->where('tagMode', 'advancePayment')->first(); + if ($tag && $this->transactionType->type == 'Withdrawal') { + // loop other deposits, remove from our amount. + $others = $tag->transactionJournals()->transactionTypes(['Deposit'])->get(); + foreach ($others as $other) { + $amount -= $other->amount; + } + return $amount; + } + + return $amount; + } + + /** + * @codeCoverageIgnore + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function tags() + { + return $this->belongsToMany('FireflyIII\Models\Tag'); } /** @@ -97,7 +126,6 @@ class TransactionJournal extends Model return $this->transactions()->first()->account; } - /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\HasMany @@ -275,15 +303,6 @@ class TransactionJournal extends Model $this->attributes['encrypted'] = true; } - /** - * @codeCoverageIgnore - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - public function tags() - { - return $this->belongsToMany('FireflyIII\Models\Tag'); - } - /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 583b7e73a9..8a1916e9ca 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -84,32 +84,20 @@ class Amount */ public function formatJournal(TransactionJournal $journal, $coloured = true) { - $showPositive = true; if (is_null($journal->symbol)) { $symbol = $journal->transactionCurrency->symbol; } else { $symbol = $journal->symbol; } - $amount = 0; - - if (is_null($journal->type)) { - $type = $journal->transactionType->type; - } else { - $type = $journal->type; + $amount = $journal->amount; + if ($journal->transactionType->type == 'Withdrawal') { + $amount = $amount * -1; } - - if ($type == 'Withdrawal') { - $showPositive = false; + if ($journal->transactionType->type == 'Transfer' && $coloured) { + return '' . $this->formatWithSymbol($symbol, $amount, false) . ''; } - - foreach ($journal->transactions as $t) { - if (floatval($t->amount) > 0 && $showPositive === true) { - $amount = floatval($t->amount); - break; - } - if (floatval($t->amount) < 0 && $showPositive === false) { - $amount = floatval($t->amount); - } + if ($journal->transactionType->type == 'Transfer' && !$coloured) { + return $this->formatWithSymbol($symbol, $amount, false); } return $this->formatWithSymbol($symbol, $amount, $coloured); diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index 093f617084..b1c7eac663 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -78,16 +78,36 @@ class Journal extends Twig_Extension if ($journal->tags->count() == 0) { return App::make('amount')->formatJournal($journal); } + + foreach ($journal->tags as $tag) { if ($tag->tagMode == 'balancingAct') { // return tag formatted for a "balancing act", even if other // tags are present. $amount = App::make('amount')->formatJournal($journal, false); - return ' ' . $tag->tag . ''; + . '"> ' . $tag->tag . ''; } - if($tag->tagMode == 'nothing') { + + /* + * AdvancePayment with a deposit will show the tag instead of the amount: + */ + if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') { + $amount = App::make('amount')->formatJournal($journal, false); + return ' ' . $tag->tag . ''; + } + /* + * AdvancePayment with a withdrawal will show the amount with a link to + * the tag. The TransactionJournal should properly calculate the amount. + */ + if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Withdrawal') { + $amount = App::make('amount')->formatJournal($journal); + return '' . $amount . ''; + } + + + if ($tag->tagMode == 'nothing') { // return the amount: return App::make('amount')->formatJournal($journal); }