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

411 lines
13 KiB
PHP
Raw Normal View History

2015-05-01 22:44:35 +02:00
<?php
/**
* Journal.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-01 22:44:35 +02:00
namespace FireflyIII\Support\Twig;
use Amount;
use FireflyIII\Models\Account;
2016-05-20 08:00:35 +02:00
use FireflyIII\Models\Budget as ModelBudget;
use FireflyIII\Models\Category;
2016-05-15 09:00:49 +02:00
use FireflyIII\Models\Transaction;
2015-05-01 22:44:35 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
2015-05-01 22:44:35 +02:00
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
2015-05-02 22:12:26 +02:00
2015-05-01 22:44:35 +02:00
/**
2015-05-02 11:32:45 +02:00
* Class Journal
2015-05-01 22:44:35 +02:00
*
* @package FireflyIII\Support\Twig
*/
2015-05-02 11:32:45 +02:00
class Journal extends Twig_Extension
2015-05-01 22:44:35 +02:00
{
2016-05-15 09:00:49 +02:00
2016-05-17 16:00:27 +02:00
/**
* @return Twig_SimpleFunction
*/
2016-05-17 19:23:23 +02:00
public function formatAccountPerspective(): Twig_SimpleFunction
2016-05-17 16:00:27 +02:00
{
return new Twig_SimpleFunction(
2016-05-17 19:23:23 +02:00
'formatAccountPerspective', function (TransactionJournal $journal, Account $account) {
$cache = new CacheProperties;
$cache->addProperty('formatAccountPerspective');
$cache->addProperty($journal->id);
$cache->addProperty($account->id);
if ($cache->has()) {
return $cache->get();
}
2016-05-17 16:00:27 +02:00
// get the account amount:
2016-05-17 19:23:23 +02:00
$transactions = $journal->transactions()->where('transactions.account_id', $account->id)->get(['transactions.*']);
2016-05-17 16:00:27 +02:00
$amount = '0';
foreach ($transactions as $transaction) {
2016-05-17 19:23:23 +02:00
$amount = bcadd($amount, strval($transaction->amount));
}
if ($journal->isTransfer()) {
$amount = bcmul($amount, '-1');
}
// check if this sum is the same as the journal:
$journalSum = TransactionJournal::amount($journal);
$full = Amount::formatJournal($journal);
2016-05-20 06:58:13 +02:00
if (bccomp($journalSum, $amount) === 0 || bccomp(bcmul($journalSum, '-1'), $amount) === 0) {
2016-05-17 19:23:23 +02:00
$cache->store($full);
return $full;
2016-05-17 16:00:27 +02:00
}
$formatted = Amount::format($amount, true);
2016-05-17 19:23:23 +02:00
if ($journal->isTransfer()) {
$formatted = '<span class="text-info">' . Amount::format($amount) . '</span>';
}
$str = $formatted . ' (' . $full . ')';
$cache->store($str);
return $str;
2016-05-17 16:00:27 +02:00
}
);
}
/**
* @return Twig_SimpleFunction
*/
2016-05-17 19:23:23 +02:00
public function formatBudgetPerspective(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
2016-05-20 08:00:35 +02:00
'formatBudgetPerspective', function (TransactionJournal $journal, ModelBudget $budget) {
2016-05-20 06:58:13 +02:00
$cache = new CacheProperties;
$cache->addProperty('formatBudgetPerspective');
$cache->addProperty($journal->id);
$cache->addProperty($budget->id);
if ($cache->has()) {
return $cache->get();
}
// get the account amount:
2016-05-17 19:23:23 +02:00
$transactions = $journal->transactions()->where('transactions.amount', '<', 0)->get(['transactions.*']);
2016-05-17 16:00:27 +02:00
$amount = '0';
foreach ($transactions as $transaction) {
2016-05-17 19:23:23 +02:00
$currentBudget = $transaction->budgets->first();
if (!is_null($currentBudget) && $currentBudget->id === $budget->id) {
$amount = bcadd($amount, strval($transaction->amount));
}
}
2016-05-20 06:58:13 +02:00
if ($amount === '0') {
$formatted = Amount::formatJournal($journal);
$cache->store($formatted);
2016-05-20 06:58:13 +02:00
return $formatted;
}
$formatted = Amount::format($amount, true) . ' (' . Amount::formatJournal($journal) . ')';
$cache->store($formatted);
2016-05-20 06:58:13 +02:00
return $formatted;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function getDestinationAccount(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'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();
}
$list = TransactionJournal::destinationAccountList($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if ($entry->accountType->type == 'Cash account') {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = '<a title="' . e($entry->name) . '" href="' . route('accounts.show', $entry->id) . '">' . e($entry->name) . '</a>';
}
2016-05-11 17:17:43 +02:00
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
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
{
2015-06-06 17:40:41 +02:00
$filters = [$this->typeIcon()];
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-17 19:23:23 +02:00
$this->formatAccountPerspective(),
2016-05-17 16:00:27 +02:00
$this->formatBudgetPerspective(),
2016-05-15 09:00:49 +02:00
$this->journalBudgets(),
$this->journalCategories(),
$this->transactionBudgets(),
$this->transactionCategories(),
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(
'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();
2016-02-06 15:01:26 +01:00
}
$list = TransactionJournal::sourceAccountList($journal);
$array = [];
/** @var Account $entry */
foreach ($list as $entry) {
if ($entry->accountType->type == 'Cash account') {
$array[] = '<span class="text-success">(cash)</span>';
continue;
}
$array[] = '<a title="' . e($entry->name) . '" href="' . route('accounts.show', $entry->id) . '">' . e($entry->name) . '</a>';
}
2016-05-11 17:17:43 +02:00
$array = array_unique($array);
$result = join(', ', $array);
$cache->store($result);
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(
'journalBudgets', function (TransactionJournal $journal): string {
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('transaction-journal');
$cache->addProperty('budget-string');
if ($cache->has()) {
2016-05-15 12:39:39 +02:00
return $cache->get();
2016-05-15 09:00:49 +02:00
}
$budgets = [];
// get all budgets:
foreach ($journal->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
// and more!
foreach ($journal->transactions as $transaction) {
foreach ($transaction->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function journalCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'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();
}
$categories = [];
// get all categories for the journal itself (easy):
2016-05-15 09:00:49 +02:00
foreach ($journal->categories as $category) {
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name) . '</a>';
}
if (count($categories) === 0) {
$set = Category::distinct()->leftJoin('category_transaction', 'categories.id', '=', 'category_transaction.category_id')
->leftJoin('transactions', 'category_transaction.transaction_id', '=', 'transactions.id')
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
2016-05-22 15:48:34 +02:00
->where('categories.user_id', $journal->user_id)
->where('transaction_journals.id', $journal->id)
->get(['categories.*']);
/** @var Category $category */
foreach ($set as $category) {
2016-05-15 09:00:49 +02:00
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name)
. '</a>';
}
}
2016-05-15 09:00:49 +02:00
$string = join(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function transactionBudgets(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'transactionBudgets', function (Transaction $transaction): string {
$cache = new CacheProperties;
$cache->addProperty($transaction->id);
$cache->addProperty('transaction');
$cache->addProperty('budget-string');
if ($cache->has()) {
2016-05-15 18:36:40 +02:00
return $cache->get();
2016-05-15 09:00:49 +02:00
}
$budgets = [];
// get all budgets:
foreach ($transaction->budgets as $budget) {
$budgets[] = '<a href="' . route('budgets.show', [$budget->id]) . '" title="' . e($budget->name) . '">' . e($budget->name) . '</a>';
}
$string = join(', ', array_unique($budgets));
$cache->store($string);
return $string;
}
);
}
/**
* @return Twig_SimpleFunction
*/
public function transactionCategories(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'transactionCategories', function (Transaction $transaction): string {
$cache = new CacheProperties;
$cache->addProperty($transaction->id);
$cache->addProperty('transaction');
$cache->addProperty('category-string');
if ($cache->has()) {
2016-05-15 18:36:40 +02:00
return $cache->get();
2016-05-15 09:00:49 +02:00
}
$categories = [];
// get all budgets:
foreach ($transaction->categories as $category) {
$categories[] = '<a href="' . route('categories.show', [$category->id]) . '" title="' . e($category->name) . '">' . e($category->name) . '</a>';
}
$string = join(', ', array_unique($categories));
$cache->store($string);
return $string;
}
);
}
2015-06-06 17:40:41 +02:00
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 5.
*
2015-06-06 17:40:41 +02:00
* @return Twig_SimpleFilter
*/
2016-02-06 15:01:26 +01:00
protected function typeIcon(): Twig_SimpleFilter
2015-06-06 17:40:41 +02:00
{
return new Twig_SimpleFilter(
2016-02-06 15:01:26 +01:00
'typeIcon', function (TransactionJournal $journal): string {
2015-06-03 18:22:47 +02:00
switch (true) {
case $journal->isWithdrawal():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa fa-long-arrow-left fa-fw" title="' . trans('firefly.withdrawal') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
case $journal->isDeposit():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa fa-long-arrow-right fa-fw" title="' . trans('firefly.deposit') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
case $journal->isTransfer():
2015-06-03 18:22:47 +02:00
$txt = '<i class="fa fa-fw fa-exchange" title="' . trans('firefly.transfer') . '"></i>';
break;
case $journal->isOpeningBalance():
2015-06-23 21:14:21 +02:00
$txt = '<i class="fa-fw fa fa-ban" title="' . trans('firefly.openingBalance') . '"></i>';
2015-06-03 18:22:47 +02:00
break;
2015-05-17 09:35:49 +02:00
default:
2015-06-03 18:22:47 +02:00
$txt = '';
break;
2015-05-01 22:44:35 +02:00
}
2015-06-03 18:22:47 +02:00
return $txt;
2015-05-01 22:44:35 +02:00
}, ['is_safe' => ['html']]
);
}
}