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

223 lines
7.9 KiB
PHP
Raw Normal View History

2015-05-01 22:44:35 +02:00
<?php
/**
* Journal.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-05-01 22:44:35 +02:00
namespace FireflyIII\Support\Twig;
use FireflyIII\Models\Account;
2016-12-22 18:19:50 +01:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
2015-05-01 22:44:35 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
2017-11-05 19:49:20 +01:00
use FireflyIII\Support\Twig\Extension\TransactionJournal as TransactionJournalExtension;
2015-05-01 22:44:35 +02:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2017-11-05 19:49:20 +01:00
2015-05-01 22:44:35 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class Journal.
2015-05-01 22:44:35 +02:00
*/
2015-05-02 11:32:45 +02:00
class Journal extends Twig_Extension
2015-05-01 22:44:35 +02:00
{
/**
* @return Twig_SimpleFunction
*/
public function getDestinationAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'destinationAccount',
function (TransactionJournal $journal) {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('destination-account-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2017-11-15 10:52:29 +01:00
$list = $journal->destinationAccountList();
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
2017-11-15 12:25:49 +01:00
if (AccountType::CASH === $entry->accountType->type) {
2017-11-15 10:52:29 +01:00
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
2017-11-15 10:52:29 +01:00
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
2017-11-15 10:52:29 +01:00
return $result;
}
);
}
2015-05-01 22:44:35 +02:00
2015-05-02 22:05:18 +02:00
/**
* @return array
*/
2016-02-06 15:01:26 +01:00
public function getFilters(): array
2015-05-01 22:44:35 +02:00
{
$filters = [
new Twig_SimpleFilter('journalTotalAmount', [TransactionJournalExtension::class, 'totalAmount'], ['is_safe' => ['html']]),
];
2015-05-01 22:44:35 +02:00
2015-06-06 17:40:41 +02:00
return $filters;
}
/**
* @return array
*/
2016-02-06 15:01:26 +01:00
public function getFunctions(): array
2015-06-06 17:40:41 +02:00
{
$functions = [
$this->getSourceAccount(),
$this->getDestinationAccount(),
2016-05-15 09:00:49 +02:00
$this->journalBudgets(),
$this->journalCategories(),
2015-06-06 17:40:41 +02:00
];
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
2016-02-06 15:01:26 +01:00
public function getName(): string
2015-06-06 17:40:41 +02:00
{
return 'FireflyIII\Support\Twig\Journals';
}
2016-02-06 15:01:26 +01:00
/**
* @return Twig_SimpleFunction
*/
public function getSourceAccount(): Twig_SimpleFunction
2016-02-06 15:01:26 +01:00
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'sourceAccount',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('source-account-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2017-11-15 10:52:29 +01:00
$list = $journal->sourceAccountList();
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
2017-11-15 12:25:49 +01:00
if (AccountType::CASH === $entry->accountType->type) {
2017-11-15 10:52:29 +01:00
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($entry->name), route('accounts.show', $entry->id));
}
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
2017-11-15 10:52:29 +01:00
return $result;
}
2016-02-06 15:01:26 +01:00
);
}
2016-05-15 09:00:49 +02:00
/**
* @return Twig_SimpleFunction
*/
public function journalBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'journalBudgets',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('budget-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
2016-05-15 09:00:49 +02:00
2017-11-15 10:52:29 +01:00
$budgets = [];
// get all budgets:
foreach ($journal->budgets as $budget) {
2016-12-28 05:16:55 +01:00
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
2016-05-15 09:00:49 +02:00
}
2017-11-15 10:52:29 +01:00
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->budgets as $budget) {
$budgets[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($budget->name), route('budgets.show', $budget->id));
}
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
2016-05-15 09:00:49 +02:00
2017-11-15 10:52:29 +01:00
return $string;
}
2016-05-15 09:00:49 +02:00
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
2017-11-15 10:52:29 +01:00
'journalCategories',
function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('category-string');
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$categories = [];
// get all categories for the journal itself (easy):
foreach ($journal->categories as $category) {
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
}
2017-11-15 12:25:49 +01:00
if (0 === count($categories)) {
2017-11-15 10:52:29 +01:00
$set = Category::distinct()->leftJoin('category_transaction', 'categories.id', '=', 'category_transaction.category_id')
2017-11-15 11:33:07 +01:00
->leftJoin('transactions', 'category_transaction.transaction_id', '=', 'transactions.id')
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('categories.user_id', $journal->user_id)
->where('transaction_journals.id', $journal->id)
->get(['categories.*']);
2017-11-15 10:52:29 +01:00
/** @var Category $category */
foreach ($set as $category) {
$categories[] = sprintf('<a title="%1$s" href="%2$s">%1$s</a>', e($category->name), route('categories.show', $category->id));
}
2016-05-15 09:00:49 +02:00
}
2017-11-15 10:52:29 +01:00
$string = join(', ', array_unique($categories));
$cache->store($string);
2016-05-15 09:00:49 +02:00
2017-11-15 10:52:29 +01:00
return $string;
}
2016-05-15 09:00:49 +02:00
);
}
}