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

244 lines
6.7 KiB
PHP
Raw Normal View History

2015-05-01 15:44:35 -05:00
<?php
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Tag;
2015-05-01 15:44:35 -05:00
use FireflyIII\Models\TransactionJournal;
2015-06-03 11:22:47 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-01 15:44:35 -05:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2015-05-02 15:12:26 -05:00
2015-05-01 15:44:35 -05:00
/**
2015-06-29 02:23:39 -05:00
* @codeCoverageIgnore
*
2015-05-02 04:32:45 -05:00
* Class Journal
2015-05-01 15:44:35 -05:00
*
* @package FireflyIII\Support\Twig
*/
2015-05-02 04:32:45 -05:00
class Journal extends Twig_Extension
2015-05-01 15:44:35 -05:00
{
2015-05-02 15:05:18 -05:00
/**
* @return array
*/
2015-05-01 15:44:35 -05:00
public function getFilters()
{
2015-06-06 10:40:41 -05:00
$filters = [$this->typeIcon()];
2015-05-01 15:44:35 -05:00
2015-06-06 10:40:41 -05:00
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) {
2015-06-03 11:22:47 -05:00
2015-06-03 14:25:11 -05:00
$cache = new CacheProperties();
$cache->addProperty($journal->id);
$cache->addProperty('typeIcon');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 11:22:47 -05:00
}
2015-05-01 15:44:35 -05:00
$type = $journal->transactionType->type;
2015-05-17 02:35:49 -05:00
switch ($type) {
case 'Withdrawal':
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa fa-long-arrow-left fa-fw" title="' . trans('firefly.withdrawal') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
2015-05-17 02:35:49 -05:00
case 'Deposit':
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa fa-long-arrow-right fa-fw" title="' . trans('firefly.deposit') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
2015-05-17 02:35:49 -05:00
case 'Transfer':
2015-06-03 11:22:47 -05:00
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
2015-05-17 02:35:49 -05:00
case 'Opening balance':
2015-06-23 14:14:21 -05:00
$txt = '<i class="fa-fw fa fa-ban" title="' . trans('firefly.openingBalance') . '"></i>';
2015-06-03 11:22:47 -05:00
break;
2015-05-17 02:35:49 -05:00
default:
2015-06-03 11:22:47 -05:00
$txt = '';
break;
2015-05-01 15:44:35 -05:00
}
2015-06-03 14:25:11 -05:00
$cache->store($txt);
2015-06-03 11:22:47 -05:00
return $txt;
2015-05-01 15:44:35 -05:00
}, ['is_safe' => ['html']]
);
}
2015-05-02 15:05:18 -05:00
/**
2015-06-06 10:40:41 -05:00
* @return Twig_SimpleFunction
2015-05-02 15:05:18 -05:00
*/
2015-06-06 10:40:41 -05:00
protected function invalidJournal()
2015-05-01 15:44:35 -05:00
{
2015-06-06 10:40:41 -05:00
return new Twig_SimpleFunction(
'invalidJournal', function (TransactionJournal $journal) {
2015-05-01 15:44:35 -05:00
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
return false;
}
);
2015-06-06 10:40:41 -05:00
}
2015-05-01 15:44:35 -05:00
2015-06-06 10:40:41 -05:00
/**
* @return Twig_SimpleFunction
*/
protected function relevantTags()
{
return new Twig_SimpleFunction(
'relevantTags', function (TransactionJournal $journal) {
2015-06-05 12:02:23 -05:00
$cache = new CacheProperties;
$cache->addProperty('relevantTags');
$cache->addProperty($journal->id);
2015-06-06 10:40:41 -05:00
if ($cache->has()) {
2015-06-05 12:02:23 -05:00
return $cache->get(); // @codeCoverageIgnore
}
$count = $journal->tags->count();
$string = '';
2015-06-05 12:02:23 -05:00
if ($count === 0) {
$string = $this->relevantTagsNoTags($journal);
2015-05-02 15:05:18 -05:00
}
2015-05-17 09:12:00 -05:00
if ($count === 1) {
$string = $this->relevantTagsSingle($journal);
}
2015-05-17 09:12:00 -05:00
if ($count > 1) {
$string = $this->relevantTagsMulti($journal);
2015-05-02 15:12:26 -05:00
}
$cache->store($string);
2015-05-02 15:12:26 -05:00
return $string;
2015-05-01 15:44:35 -05:00
}
);
}
/**
* @param TransactionJournal $journal
*
* @return string
*/
protected function relevantTagsNoTags(TransactionJournal $journal)
{
2015-07-07 12:09:45 -05: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-07-07 12:09:45 -05:00
$amount = app('amount')->format($journal->actual_amount, 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->transactionType->type == 'Deposit') {
2015-07-07 12:09:45 -05: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->transactionType->type == 'Withdrawal') {
2015-07-07 12:09:45 -05: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);
}
}