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

60 lines
1.7 KiB
PHP
Raw Normal View History

2015-05-02 02:06:07 -05:00
<?php
namespace FireflyIII\Support\Twig;
use Auth;
use FireflyIII\Models\LimitRepetition;
2015-06-05 12:02:23 -05:00
use FireflyIII\Support\CacheProperties;
2015-05-02 02:06:07 -05:00
use Twig_Extension;
use Twig_SimpleFunction;
/**
2015-06-29 02:23:39 -05:00
* @codeCoverageIgnore
2015-05-02 02:06:07 -05:00
* Class Budget
*
* @package FireflyIII\Support\Twig
*/
class Budget extends Twig_Extension
{
/**
* {@inheritDoc}
*/
public function getFunctions()
{
2015-06-03 14:25:11 -05:00
$functions = [];
2015-05-02 02:06:07 -05:00
$functions[] = new Twig_SimpleFunction(
2015-12-13 03:05:13 -06:00
'spentInRepetition', function (LimitRepetition $repetition) {
2015-06-05 12:02:23 -05:00
$cache = new CacheProperties;
$cache->addProperty($repetition->id);
2015-12-13 03:05:13 -06:00
$cache->addProperty('spentInRepetition');
2015-06-05 12:02:23 -05:00
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2015-05-20 12:55:53 -05:00
$sum
= Auth::user()->transactionjournals()
2015-06-05 12:02:23 -05:00
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->before($repetition->enddate)
->after($repetition->startdate)
->where('limit_repetitions.id', '=', $repetition->id)
->get(['transaction_journals.*'])->sum('amount');
$cache->store($sum);
2015-05-20 12:55:53 -05:00
2015-06-05 12:02:23 -05:00
return $sum;
}
);
2015-05-02 02:06:07 -05:00
return $functions;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'FireflyIII\Support\Twig\Budget';
}
}