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

134 lines
4.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;
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) {
$type = $journal->transactionType->type;
2015-05-17 02:35:49 -05:00
switch ($type) {
case 'Withdrawal':
return '<span class="glyphicon glyphicon-arrow-left" title="' . trans('firefly.withdrawal') . '"></span>';
break;
case 'Deposit':
return '<span class="glyphicon glyphicon-arrow-right" title="' . trans('firefly.deposit') . '"></span>';
break;
case 'Transfer':
return '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case 'Opening balance':
return '<span class="glyphicon glyphicon-ban-circle" title="' . trans('firefly.openingBalance') . '"></span>';
break;
default:
return '';
break;
2015-05-01 15:44:35 -05:00
}
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) {
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
return false;
}
);
$functions[] = new Twig_SimpleFunction(
'relevantTags', function (TransactionJournal $journal) {
2015-05-02 15:12:26 -05:00
if ($journal->tags->count() == 0) {
2015-05-02 15:05:18 -05:00
return App::make('amount')->formatJournal($journal);
}
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-02 15:12:26 -05:00
$amount = App::make('amount')->formatJournal($journal, false);
2015-05-17 09:12:00 -05:00
return '<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-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-05-02 15:12:26 -05:00
return '<a href="' . route('tags.show', $tag->id) . '" class="label label-success" title="' . $amount
2015-05-18 10:05:22 -05:00
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
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);
return '<a href="' . route('tags.show', $tag->id) . '">' . $amount . '</a>';
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:
return App::make('amount')->formatJournal($journal);
}
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';
}
}