. */ declare(strict_types=1); namespace FireflyIII\Support\Http\Controllers; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\AccountType; use FireflyIII\Models\Bill; use FireflyIII\Models\Tag; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Log; use Throwable; /** * Trait ModelInformation * */ trait ModelInformation { /** * Get actions based on a bill. * * @param Bill $bill * * @return array */ protected function getActionsForBill(Bill $bill): array // get info and augument { try { $result = view( 'rules.partials.action', [ 'oldAction' => 'link_to_bill', 'oldValue' => $bill->name, 'oldChecked' => false, 'count' => 1, ] )->render(); } catch (Throwable $e) { Log::error(sprintf('Throwable was thrown in getActionsForBill(): %s', $e->getMessage())); Log::error($e->getTraceAsString()); $result = 'Could not render view. See log files.'; throw new FireflyException($result, 0, $e); } return [$result]; } /** * @codeCoverageIgnore * * @return string[] * * @psalm-return array */ protected function getLiabilityTypes(): array { /** @var AccountRepositoryInterface $repository */ $repository = app(AccountRepositoryInterface::class); // types of liability: $debt = $repository->getAccountTypeByType(AccountType::DEBT); $loan = $repository->getAccountTypeByType(AccountType::LOAN); $mortgage = $repository->getAccountTypeByType(AccountType::MORTGAGE); $liabilityTypes = [ $debt->id => (string) trans(sprintf('firefly.account_type_%s', AccountType::DEBT)), $loan->id => (string) trans(sprintf('firefly.account_type_%s', AccountType::LOAN)), $mortgage->id => (string) trans(sprintf('firefly.account_type_%s', AccountType::MORTGAGE)), ]; asort($liabilityTypes); return $liabilityTypes; } /** * @codeCoverageIgnore * @return array */ protected function getRoles(): array { $roles = []; foreach (config('firefly.accountRoles') as $role) { $roles[$role] = (string) trans(sprintf('firefly.account_role_%s', $role)); } return $roles; } /** * Create fake triggers to match the bill's properties * * @param Bill $bill * * @return array */ protected function getTriggersForBill(Bill $bill): array // get info and augument { // TODO duplicate code $operators = config('search.operators'); $triggers = []; foreach ($operators as $key => $operator) { if ('user_action' !== $key && false === $operator['alias']) { $triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key)); } } asort($triggers); $result = []; $billTriggers = ['currency_is', 'amount_more', 'amount_less', 'description_contains']; $values = [ $bill->transactionCurrency()->first()->name, round((float) $bill->amount_min, 24), round((float) $bill->amount_max, 24), $bill->name, ]; foreach ($billTriggers as $index => $trigger) { try { $string = view( 'rules.partials.trigger', [ 'oldTrigger' => $trigger, 'oldValue' => $values[$index], 'oldChecked' => false, 'count' => $index + 1, 'triggers' => $triggers, ] )->render(); } catch (Throwable $e) { Log::debug(sprintf('Throwable was thrown in getTriggersForBill(): %s', $e->getMessage())); Log::debug($e->getTraceAsString()); $string = ''; throw new FireflyException('Could not render trigger', 0, $e); } if ('' !== $string) { $result[] = $string; } } return $result; } /** * @param TransactionJournal $journal * * @return array */ private function getTriggersForJournal(TransactionJournal $journal): array { // See reference nr. 40 $operators = config('search.operators'); $triggers = []; foreach ($operators as $key => $operator) { if ('user_action' !== $key && false === $operator['alias']) { $triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key)); } } asort($triggers); $result = []; $journalTriggers = []; $values = []; $index = 0; // amount, description, category, budget, tags, source, destination, notes, currency type //,type /** @var Transaction|null $source */ $source = $journal->transactions()->where('amount', '<', 0)->first(); /** @var Transaction|null $destination */ $destination = $journal->transactions()->where('amount', '>', 0)->first(); if (null === $destination || null === $source) { return $result; } // type $journalTriggers[$index] = 'transaction_type'; $values[$index] = $journal->transactionType->type; $index++; // currency $journalTriggers[$index] = 'currency_is'; $values[$index] = sprintf('%s (%s)', $journal->transactionCurrency->name, $journal->transactionCurrency->code); $index++; // amount_exactly: $journalTriggers[$index] = 'amount_exactly'; $values[$index] = $destination->amount; $index++; // description_is: $journalTriggers[$index] = 'description_is'; $values[$index] = $journal->description; $index++; // from_account_is $journalTriggers[$index] = 'source_account_is'; $values[$index] = $source->account->name; $index++; // to_account_is $journalTriggers[$index] = 'destination_account_is'; $values[$index] = $destination->account->name; $index++; // category (if) $category = $journal->categories()->first(); if (null !== $category) { $journalTriggers[$index] = 'category_is'; $values[$index] = $category->name; $index++; } // budget (if) $budget = $journal->budgets()->first(); if (null !== $budget) { $journalTriggers[$index] = 'budget_is'; $values[$index] = $budget->name; $index++; } // tags (if) $tags = $journal->tags()->get(); /** @var Tag $tag */ foreach ($tags as $tag) { $journalTriggers[$index] = 'tag_is'; $values[$index] = $tag->tag; $index++; } // notes (if) $notes = $journal->notes()->first(); if (null !== $notes) { $journalTriggers[$index] = 'notes_are'; $values[$index] = $notes->text; } foreach ($journalTriggers as $index => $trigger) { try { $string = view( 'rules.partials.trigger', [ 'oldTrigger' => $trigger, 'oldValue' => $values[$index], 'oldChecked' => false, 'count' => $index + 1, 'triggers' => $triggers, ] )->render(); } catch (Throwable $e) { Log::debug(sprintf('Throwable was thrown in getTriggersForJournal(): %s', $e->getMessage())); Log::debug($e->getTraceAsString()); $string = ''; throw new FireflyException('Could not render trigger', 0, $e); } if ('' !== $string) { $result[] = $string; } } return $result; } }