typeIcon()]; return $filters; } /** * @return array */ public function getFunctions() { $functions = [ $this->invalidJournal(), $this->relevantTags() ]; return $functions; } /** * Returns the name of the extension. * * @return string The extension name */ public function getName() { return 'FireflyIII\Support\Twig\Journals'; } /** * @return Twig_SimpleFilter */ protected function typeIcon() { return new Twig_SimpleFilter( 'typeIcon', function (TransactionJournal $journal) { $cache = new CacheProperties(); $cache->addProperty($journal->id); $cache->addProperty('typeIcon'); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } switch (true) { case $journal->isWithdrawal(): $txt = ''; break; case $journal->isDeposit(): $txt = ''; break; case $journal->isTransfer(): $txt = ''; break; case $journal->isOpeningBalance(): $txt = ''; break; default: $txt = ''; break; } $cache->store($txt); return $txt; }, ['is_safe' => ['html']] ); } /** * @return Twig_SimpleFunction */ protected function invalidJournal() { return new Twig_SimpleFunction( 'invalidJournal', function (TransactionJournal $journal) { if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) { return true; } return false; } ); } /** * @return Twig_SimpleFunction */ protected function relevantTags() { return new Twig_SimpleFunction( 'relevantTags', function (TransactionJournal $journal) { $cache = new CacheProperties; $cache->addProperty('relevantTags'); $cache->addProperty($journal->id); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } $count = $journal->tags->count(); $string = ''; if ($count === 0) { $string = $this->relevantTagsNoTags($journal); } if ($count === 1) { $string = $this->relevantTagsSingle($journal); } if ($count > 1) { $string = $this->relevantTagsMulti($journal); } $cache->store($string); return $string; } ); } /** * @param TransactionJournal $journal * * @return string */ protected function relevantTagsNoTags(TransactionJournal $journal) { return app('amount')->formatJournal($journal); } /** * @param TransactionJournal $journal * * @return string */ protected function relevantTagsSingle(TransactionJournal $journal) { $tag = $journal->tags()->first(); return $this->formatJournalByTag($journal, $tag); } /** * @param TransactionJournal $journal * @param Tag $tag * * @return string */ protected function formatJournalByTag(TransactionJournal $journal, Tag $tag) { if ($tag->tagMode == 'balancingAct') { // return tag formatted for a "balancing act", even if other // tags are present. $amount = app('amount')->format($journal->amount_positive, false); $string = ' ' . $tag->tag . ''; return $string; } if ($tag->tagMode == 'advancePayment') { if ($journal->isDeposit()) { $amount = app('amount')->formatJournal($journal, false); $string = ' ' . $tag->tag . ''; return $string; } /* * AdvancePayment with a withdrawal will show the amount with a link to * the tag. The TransactionJournal should properly calculate the amount. */ if ($journal->isWithdrawal()) { $amount = app('amount')->formatJournal($journal); $string = '' . $amount . ''; return $string; } } return $this->relevantTagsNoTags($journal); } /** * If a transaction journal has multiple tags, we'll have to gamble. FF3 * does not yet block adding multiple 'special' tags so we must wing it. * * We grab the first special tag (for advancePayment and for balancingAct * and try to format those. If they're not present (it's all normal tags), * we can format like any other journal. * * @param TransactionJournal $journal * * @return string */ protected function relevantTagsMulti(TransactionJournal $journal) { $firstBalancingAct = $journal->tags()->where('tagMode', 'balancingAct')->first(); if ($firstBalancingAct) { return $this->formatJournalByTag($journal, $firstBalancingAct); } $firstAdvancePayment = $journal->tags()->where('tagMode', 'advancePayment')->first(); if ($firstAdvancePayment) { return $this->formatJournalByTag($journal, $firstAdvancePayment); } return $this->relevantTagsNoTags($journal); } }