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

165 lines
5.3 KiB
PHP
Raw Normal View History

2015-05-01 15:44:35 -05:00
<?php
namespace FireflyIII\Support\Twig;
2015-05-02 15:12:26 -05:00
use App;
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-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
/**
2015-05-17 02:35:49 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2015-05-02 15:05:18 -05:00
* @return array
*/
2015-05-01 15:44:35 -05:00
public function getFilters()
{
$filters = [];
$filters[] = 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-03 11:22:47 -05:00
$txt = '<span class="glyphicon glyphicon-arrow-left" title="' . trans('firefly.withdrawal') . '"></span>';
break;
2015-05-17 02:35:49 -05:00
case 'Deposit':
2015-06-03 11:22:47 -05:00
$txt = '<span class="glyphicon glyphicon-arrow-right" title="' . trans('firefly.deposit') . '"></span>';
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-03 11:22:47 -05:00
$txt = '<span class="glyphicon glyphicon-ban-circle" title="' . trans('firefly.openingBalance') . '"></span>';
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']]
);
return $filters;
}
2015-05-02 15:05:18 -05:00
/**
2015-05-17 02:35:49 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
2015-05-02 15:05:18 -05:00
* @return array
*/
2015-05-01 15:44:35 -05:00
public function getFunctions()
{
$functions = [];
$functions[] = 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;
}
);
$functions[] = 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);
if($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2015-05-02 15:12:26 -05:00
if ($journal->tags->count() == 0) {
2015-06-05 12:02:23 -05:00
$string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
2015-05-02 15:05:18 -05:00
}
2015-05-17 09:12:00 -05:00
2015-05-02 15:12:26 -05:00
foreach ($journal->tags as $tag) {
2015-05-02 15:13:37 -05:00
if ($tag->tagMode == 'balancingAct') {
2015-05-17 08:24:30 -05:00
// return tag formatted for a "balancing act", even if other
// tags are present.
2015-05-24 00:43:48 -05:00
$amount = App::make('amount')->format($journal->actual_amount, false);
2015-06-05 12:02:23 -05:00
$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>';
2015-06-05 12:02:23 -05:00
$cache->store($string);
return $string;
2015-05-17 09:12:00 -05:00
}
2015-05-02 15:12:26 -05:00
2015-05-17 09:12:00 -05:00
/*
* 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);
2015-06-05 12:02:23 -05:00
$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>';
2015-06-05 12:02:23 -05:00
$cache->store($string);
return $string;
2015-05-17 09:12:00 -05:00
}
/*
* 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);
2015-05-20 12:55:53 -05:00
2015-06-05 12:02:23 -05:00
$string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
$cache->store($string);
return $string;
2015-05-02 15:12:26 -05:00
}
2015-05-17 09:12:00 -05:00
if ($tag->tagMode == 'nothing') {
2015-05-17 08:24:30 -05:00
// return the amount:
2015-06-05 12:02:23 -05:00
$string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
2015-05-17 08:24:30 -05:00
}
2015-05-02 15:12:26 -05:00
}
return 'TODO: ' . $journal->amount;
2015-05-01 15:44:35 -05:00
}
);
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'FireflyIII\Support\Twig\Journals';
}
}