Files
firefly-iii/app/Support/Twig/Journal.php

242 lines
6.7 KiB
PHP
Raw Normal View History

2015-05-01 22:44:35 +02:00
<?php
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Tag;
2015-05-01 22:44:35 +02:00
use FireflyIII\Models\TransactionJournal;
2015-06-03 18:22:47 +02:00
use FireflyIII\Support\CacheProperties;
2015-05-01 22:44:35 +02:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2015-05-02 22:12:26 +02:00
2015-05-01 22:44:35 +02:00
/**
2015-06-29 09:23:39 +02:00
* @codeCoverageIgnore
*
2015-05-02 11:32:45 +02:00
* Class Journal
2015-05-01 22:44:35 +02:00
*
* @package FireflyIII\Support\Twig
*/
2015-05-02 11:32:45 +02:00
class Journal extends Twig_Extension
2015-05-01 22:44:35 +02:00
{
2015-05-02 22:05:18 +02:00
/**
* @return array
*/
2015-05-01 22:44:35 +02:00
public function getFilters()
{
2015-06-06 17:40:41 +02:00
$filters = [$this->typeIcon()];
2015-05-01 22:44:35 +02:00
2015-06-06 17:40:41 +02:00
return $filters;
}
/**
* @return array
*/
public function getFunctions()
{
$functions = [
$this->invalidJournal(),
2016-01-15 23:12:52 +01:00
$this->relevantTags(),
2015-06-06 17:40:41 +02:00
];
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) {
2015-06-03 18:22:47 +02:00
2015-06-03 21:25:11 +02:00
$cache = new CacheProperties();
$cache->addProperty($journal->id);
$cache->addProperty('typeIcon');
if ($cache->has()) {
2015-06-04 21:35:36 +02:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 18:22:47 +02:00
}
switch (true) {
case $journal->isWithdrawal():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa fa-long-arrow-left fa-fw" title="' . trans('firefly.withdrawal') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
case $journal->isDeposit():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa fa-long-arrow-right fa-fw" title="' . trans('firefly.deposit') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
case $journal->isTransfer():
2015-06-03 18:22:47 +02:00
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case $journal->isOpeningBalance():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa-fw fa fa-ban" title="' . trans('firefly.openingBalance') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
2015-05-17 09:35:49 +02:00
default:
2015-06-03 18:22:47 +02:00
$txt = '';
break;
2015-05-01 22:44:35 +02:00
}
2015-06-03 21:25:11 +02:00
$cache->store($txt);
2015-06-03 18:22:47 +02:00
return $txt;
2015-05-01 22:44:35 +02:00
}, ['is_safe' => ['html']]
);
}
2015-05-02 22:05:18 +02:00
/**
2015-06-06 17:40:41 +02:00
* @return Twig_SimpleFunction
2015-05-02 22:05:18 +02:00
*/
2015-06-06 17:40:41 +02:00
protected function invalidJournal()
2015-05-01 22:44:35 +02:00
{
2015-06-06 17:40:41 +02:00
return new Twig_SimpleFunction(
'invalidJournal', function (TransactionJournal $journal) {
2015-05-01 22:44:35 +02:00
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
return false;
}
);
2015-06-06 17:40:41 +02:00
}
2015-05-01 22:44:35 +02:00
2015-06-06 17:40:41 +02:00
/**
* @return Twig_SimpleFunction
*/
protected function relevantTags()
{
return new Twig_SimpleFunction(
'relevantTags', function (TransactionJournal $journal) {
2015-06-05 19:02:23 +02:00
$cache = new CacheProperties;
$cache->addProperty('relevantTags');
$cache->addProperty($journal->id);
2015-06-06 17:40:41 +02:00
if ($cache->has()) {
2015-06-05 19:02:23 +02:00
return $cache->get(); // @codeCoverageIgnore
}
$count = $journal->tags->count();
$string = '';
2015-06-05 19:02:23 +02:00
if ($count === 0) {
$string = $this->relevantTagsNoTags($journal);
2015-05-02 22:05:18 +02:00
}
2015-05-17 16:12:00 +02:00
if ($count === 1) {
$string = $this->relevantTagsSingle($journal);
}
2015-05-17 16:12:00 +02:00
if ($count > 1) {
$string = $this->relevantTagsMulti($journal);
2015-05-02 22:12:26 +02:00
}
$cache->store($string);
2015-05-02 22:12:26 +02:00
return $string;
2015-05-01 22:44:35 +02:00
}
);
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
protected function relevantTagsNoTags(TransactionJournal $journal)
{
2015-07-07 19:09:45 +02:00
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.
2015-09-25 20:40:24 +02:00
$amount = app('amount')->format($journal->amount_positive, false);
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>';
return $string;
}
if ($tag->tagMode == 'advancePayment') {
if ($journal->isDeposit()) {
2015-07-07 19:09:45 +02:00
$amount = app('amount')->formatJournal($journal, false);
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
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()) {
2015-07-07 19:09:45 +02:00
$amount = app('amount')->formatJournal($journal);
$string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
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);
}
}