addProperty($journal->id); $cache->addProperty('transaction-journal'); $cache->addProperty('destination-account-string'); if ($cache->has()) { return $cache->get(); // @codeCoverageIgnore } $list = $journal->destinationAccountList(); $array = []; /** @var Account $entry */ foreach ($list as $entry) { if ($entry->accountType->type === AccountType::CASH) { $array[] = '(cash)'; continue; } $array[] = sprintf('%1$s', e($entry->name), route('accounts.show', $entry->id)); } $array = array_unique($array); $result = join(', ', $array); $cache->store($result); return $result; } ); } /** * @return array */ public function getFilters(): array { $filters = [ $this->typeIcon(), ]; return $filters; } /** * @return array */ public function getFunctions(): array { $functions = [ $this->getSourceAccount(), $this->getDestinationAccount(), $this->journalBudgets(), $this->journalCategories(), ]; return $functions; } /** * Returns the name of the extension. * * @return string The extension name */ public function getName(): string { return 'FireflyIII\Support\Twig\Journals'; } /** * @return Twig_SimpleFunction */ public function getSourceAccount(): Twig_SimpleFunction { 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(); // @codeCoverageIgnore } $list = $journal->sourceAccountList(); $array = []; /** @var Account $entry */ foreach ($list as $entry) { if ($entry->accountType->type === AccountType::CASH) { $array[] = '(cash)'; continue; } $array[] = sprintf('%1$s', e($entry->name), route('accounts.show', $entry->id)); } $array = array_unique($array); $result = join(', ', $array); $cache->store($result); return $result; } ); } /** * @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()) { return $cache->get(); // @codeCoverageIgnore } $budgets = []; // get all budgets: foreach ($journal->budgets as $budget) { $budgets[] = sprintf('%1$s', e($budget->name), route('budgets.show', $budget->id)); } // and more! foreach ($journal->transactions as $transaction) { foreach ($transaction->budgets as $budget) { $budgets[] = sprintf('%1$s', e($budget->name), route('budgets.show', $budget->id)); } } $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(); // @codeCoverageIgnore } $categories = []; // get all categories for the journal itself (easy): foreach ($journal->categories as $category) { $categories[] = sprintf('%1$s', e($category->name), route('categories.show', $category->id)); } 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') ->where('categories.user_id', $journal->user_id) ->where('transaction_journals.id', $journal->id) ->get(['categories.*']); /** @var Category $category */ foreach ($set as $category) { $categories[] = sprintf('%1$s', e($category->name), route('categories.show', $category->id)); } } $string = join(', ', array_unique($categories)); $cache->store($string); return $string; } ); } /** * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 5. * * @return Twig_SimpleFilter */ protected function typeIcon(): Twig_SimpleFilter { return new Twig_SimpleFilter( 'typeIcon', function (TransactionJournal $journal): string { switch (true) { case $journal->isWithdrawal(): $txt = sprintf('', trans('firefly.withdrawal')); break; case $journal->isDeposit(): $txt = sprintf('', trans('firefly.deposit')); break; case $journal->isTransfer(): $txt = sprintf('', trans('firefly.transfer')); break; case $journal->isOpeningBalance(): $txt = sprintf('', trans('firefly.openingBalance')); break; default: $txt = ''; break; } return $txt; }, ['is_safe' => ['html']] ); } }