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

69 lines
2.0 KiB
PHP
Raw Normal View History

2015-05-02 09:06:07 +02:00
<?php
/**
* Budget.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-05-02 09:06:07 +02:00
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\LimitRepetition;
2015-06-05 19:02:23 +02:00
use FireflyIII\Support\CacheProperties;
2015-05-02 09:06:07 +02:00
use Twig_Extension;
use Twig_SimpleFunction;
/**
* Class Budget
*
* @package FireflyIII\Support\Twig
*/
class Budget extends Twig_Extension
{
/**
* {@inheritDoc}
*/
2016-02-06 10:15:07 +01:00
public function getFunctions(): array
2015-05-02 09:06:07 +02:00
{
2015-06-03 21:25:11 +02:00
$functions = [];
2015-05-02 09:06:07 +02:00
$functions[] = new Twig_SimpleFunction(
2015-12-13 10:05:13 +01:00
'spentInRepetition', function (LimitRepetition $repetition) {
2015-06-05 19:02:23 +02:00
$cache = new CacheProperties;
$cache->addProperty($repetition->id);
2015-12-13 10:05:13 +01:00
$cache->addProperty('spentInRepetition');
2015-06-05 19:02:23 +02:00
if ($cache->has()) {
2016-04-06 09:27:45 +02:00
return $cache->get();
2015-06-05 19:02:23 +02:00
}
2015-05-20 19:55:53 +02:00
$sum
2016-09-16 12:15:58 +02:00
= auth()->user()->transactionJournals()
->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');
2015-06-05 19:02:23 +02:00
$cache->store($sum);
2015-05-20 19:55:53 +02:00
2015-06-05 19:02:23 +02:00
return $sum;
}
);
2015-05-02 09:06:07 +02:00
return $functions;
}
/**
* {@inheritDoc}
*/
2016-02-06 10:15:07 +01:00
public function getName(): string
2015-05-02 09:06:07 +02:00
{
return 'FireflyIII\Support\Twig\Budget';
}
}