transactions()->where('transactions.account_id', $account->id)->first(); $amount = $transaction->amount; if ($journal->isWithdrawal()) { $amount = bcmul($amount, '-1'); } $formatted = Amount::format($amount, true); return $formatted . ' (' . Amount::formatJournal($journal) . ')'; } ); } /** * @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[] = '(cash)'; continue; } $array[] = '' . e($entry->name) . ''; } $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->formatPerspective(), $this->journalBudgets(), $this->journalCategories(), $this->transactionBudgets(), $this->transactionCategories(), ]; 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(); } $list = TransactionJournal::sourceAccountList($journal); $array = []; /** @var Account $entry */ foreach ($list as $entry) { if ($entry->accountType->type == 'Cash account') { $array[] = '(cash)'; continue; } $array[] = '' . e($entry->name) . ''; } $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(); } $budgets = []; // get all budgets: foreach ($journal->budgets as $budget) { $budgets[] = '' . e($budget->name) . ''; } // and more! foreach ($journal->transactions as $transaction) { foreach ($transaction->budgets as $budget) { $budgets[] = '' . e($budget->name) . ''; } } $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 budgets: foreach ($journal->categories as $category) { $categories[] = '' . e($category->name) . ''; } // and more! foreach ($journal->transactions as $transaction) { foreach ($transaction->categories as $category) { $categories[] = '' . e($category->name) . ''; } } $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()) { return $cache->get(); } $budgets = []; // get all budgets: foreach ($transaction->budgets as $budget) { $budgets[] = '' . e($budget->name) . ''; } $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()) { return $cache->get(); } $categories = []; // get all budgets: foreach ($transaction->categories as $category) { $categories[] = '' . e($category->name) . ''; } $string = join(', ', array_unique($categories)); $cache->store($string); return $string; } ); } /** * @return Twig_SimpleFilter */ protected function typeIcon(): Twig_SimpleFilter { return new Twig_SimpleFilter( 'typeIcon', function (TransactionJournal $journal): string { switch (true) { case $journal->isWithdrawal(): $txt = ''; break; case $journal->isDeposit(): $txt = ''; break; case $journal->isTransfer(): $txt = ''; break; case $journal->isOpeningBalance(): $txt = ''; break; default: $txt = ''; break; } return $txt; }, ['is_safe' => ['html']] ); } }