From 778af9f4e2f4c4473fe8d496e1a3d547063eb4b3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 28 Aug 2020 05:51:02 +0200 Subject: [PATCH] Fix #3735 --- .../Controllers/Rule/CreateController.php | 48 +----------- .../Http/Controllers/ModelInformation.php | 77 +++++++++++-------- .../Http/Controllers/RuleManagement.php | 40 ++++++++++ config/firefly.php | 2 + resources/views/v1/transactions/show.twig | 31 ++++++++ 5 files changed, 120 insertions(+), 78 deletions(-) diff --git a/app/Http/Controllers/Rule/CreateController.php b/app/Http/Controllers/Rule/CreateController.php index 2cc612fe08..744198e36a 100644 --- a/app/Http/Controllers/Rule/CreateController.php +++ b/app/Http/Controllers/Rule/CreateController.php @@ -49,8 +49,7 @@ class CreateController extends Controller { use RuleManagement, ModelInformation; - /** @var RuleRepositoryInterface Rule repository */ - private $ruleRepos; + private RuleRepositoryInterface $ruleRepos; /** * RuleController constructor. @@ -76,8 +75,8 @@ class CreateController extends Controller /** * Create a new rule. It will be stored under the given $ruleGroup. * - * @param Request $request - * @param RuleGroup $ruleGroup + * @param Request $request + * @param RuleGroup|null $ruleGroup * * @return Factory|View */ @@ -290,45 +289,4 @@ class CreateController extends Controller return $redirect; } - - /** - * @param array $submittedOperators - * @return array - */ - private function parseFromOperators(array $submittedOperators): array - { - // TODO duplicated code. - $operators = config('firefly.search.operators'); - $renderedEntries = []; - $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); - - $index = 0; - foreach ($submittedOperators as $operator) { - try { - $renderedEntries[] = view( - 'rules.partials.trigger', - [ - 'oldTrigger' => OperatorQuerySearch::getRootOperator($operator['type']), - 'oldValue' => $operator['value'], - 'oldChecked' => 1 === (int) ($oldTrigger['stop_processing'] ?? '0'), - 'count' => $index + 1, - 'triggers' => $triggers, - ] - )->render(); - } catch (Throwable $e) { - Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage())); - Log::error($e->getTraceAsString()); - } - $index++; - } - - return $renderedEntries; - } } diff --git a/app/Support/Http/Controllers/ModelInformation.php b/app/Support/Http/Controllers/ModelInformation.php index 2c0df9b44b..603589506c 100644 --- a/app/Support/Http/Controllers/ModelInformation.php +++ b/app/Support/Http/Controllers/ModelInformation.php @@ -70,7 +70,7 @@ trait ModelInformation } /** - * @codeCoverageIgnore + * @codeCoverageIgnore * * @return string[] * @@ -86,9 +86,9 @@ trait ModelInformation $mortgage = $repository->getAccountTypeByType(AccountType::MORTGAGE); /** @noinspection NullPointerExceptionInspection */ $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)), + $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); @@ -103,7 +103,7 @@ trait ModelInformation { $roles = []; foreach (config('firefly.accountRoles') as $role) { - $roles[$role] = (string)trans(sprintf('firefly.account_role_%s', $role)); + $roles[$role] = (string) trans(sprintf('firefly.account_role_%s', $role)); } return $roles; @@ -122,8 +122,8 @@ trait ModelInformation $triggers = ['currency_is', 'amount_more', 'amount_less', 'description_contains']; $values = [ $bill->transactionCurrency()->first()->name, - round((float)$bill->amount_min, 12), - round((float)$bill->amount_max, 12), + round((float) $bill->amount_min, 12), + round((float) $bill->amount_max, 12), $bill->name, ]; foreach ($triggers as $index => $trigger) { @@ -160,10 +160,21 @@ trait ModelInformation */ private function getTriggersForJournal(TransactionJournal $journal): array { - $result = []; - $triggers = []; - $values = []; - $index = 0; + // TODO duplicated code. + $operators = config('firefly.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 $source */ @@ -174,66 +185,65 @@ trait ModelInformation return $result; } // type - $triggers[$index] = 'transaction_type'; - $values[$index] = $journal->transactionType->type; + $journalTriggers[$index] = 'transaction_type'; + $values[$index] = $journal->transactionType->type; $index++; // currency - $triggers[$index] = 'currency_is'; - $values[$index] = sprintf('%s (%s)', $journal->transactionCurrency->name, $journal->transactionCurrency->code); + $journalTriggers[$index] = 'currency_is'; + $values[$index] = sprintf('%s (%s)', $journal->transactionCurrency->name, $journal->transactionCurrency->code); $index++; // amount_exactly: - $triggers[$index] = 'amount_exactly'; - $values[$index] = $destination->amount; + $journalTriggers[$index] = 'amount_exactly'; + $values[$index] = $destination->amount; $index++; // description_is: - $triggers[$index] = 'description_is'; - $values[$index] = $journal->description; + $journalTriggers[$index] = 'description_is'; + $values[$index] = $journal->description; $index++; // from_account_is - $triggers[$index] = 'from_account_is'; - $values[$index] = $source->account->name; + $journalTriggers[$index] = 'source_account_is'; + $values[$index] = $source->account->name; $index++; // to_account_is - $triggers[$index] = 'to_account_is'; - $values[$index] = $destination->account->name; + $journalTriggers[$index] = 'destination_account_is'; + $values[$index] = $destination->account->name; $index++; // category (if) $category = $journal->categories()->first(); if (null !== $category) { - $triggers[$index] = 'category_is'; - $values[$index] = $category->name; + $journalTriggers[$index] = 'category_is'; + $values[$index] = $category->name; $index++; } // budget (if) $budget = $journal->budgets()->first(); if (null !== $budget) { - $triggers[$index] = 'budget_is'; - $values[$index] = $budget->name; + $journalTriggers[$index] = 'budget_is'; + $values[$index] = $budget->name; $index++; } // tags (if) $tags = $journal->tags()->get(); /** @var Tag $tag */ foreach ($tags as $tag) { - $triggers[$index] = 'tag_is'; - $values[$index] = $tag->tag; + $journalTriggers[$index] = 'tag_is'; + $values[$index] = $tag->tag; $index++; } // notes (if) $notes = $journal->notes()->first(); if (null !== $notes) { - $triggers[$index] = 'notes_are'; - $values[$index] = $notes->text; - $index++; + $journalTriggers[$index] = 'notes_are'; + $values[$index] = $notes->text; } - foreach ($triggers as $index => $trigger) { + foreach ($journalTriggers as $index => $trigger) { try { $string = view( 'rules.partials.trigger', @@ -242,6 +252,7 @@ trait ModelInformation 'oldValue' => $values[$index], 'oldChecked' => false, 'count' => $index + 1, + 'triggers' => $triggers, ] )->render(); // @codeCoverageIgnoreStart diff --git a/app/Support/Http/Controllers/RuleManagement.php b/app/Support/Http/Controllers/RuleManagement.php index 5f24fd161e..d4d98886bb 100644 --- a/app/Support/Http/Controllers/RuleManagement.php +++ b/app/Support/Http/Controllers/RuleManagement.php @@ -36,6 +36,46 @@ use Throwable; */ trait RuleManagement { + /** + * @param array $submittedOperators + * @return array + */ + protected function parseFromOperators(array $submittedOperators): array + { + // TODO duplicated code. + $operators = config('firefly.search.operators'); + $renderedEntries = []; + $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); + + $index = 0; + foreach ($submittedOperators as $operator) { + try { + $renderedEntries[] = view( + 'rules.partials.trigger', + [ + 'oldTrigger' => OperatorQuerySearch::getRootOperator($operator['type']), + 'oldValue' => $operator['value'], + 'oldChecked' => 1 === (int) ($oldTrigger['stop_processing'] ?? '0'), + 'count' => $index + 1, + 'triggers' => $triggers, + ] + )->render(); + } catch (Throwable $e) { + Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage())); + Log::error($e->getTraceAsString()); + } + $index++; + } + + return $renderedEntries; + } /** * diff --git a/config/firefly.php b/config/firefly.php index 0977b273c7..d1362741c8 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -455,8 +455,10 @@ return [ 'description_ends' => ['alias' => false, 'needs_context' => true,], 'description_contains' => ['alias' => false, 'needs_context' => true,], 'description_is' => ['alias' => false, 'needs_context' => true,], + 'currency_is' => ['alias' => false, 'needs_context' => true,], 'foreign_currency_is' => ['alias' => false, 'needs_context' => true,], + 'has_attachments' => ['alias' => false, 'needs_context' => false,], 'has_no_category' => ['alias' => false, 'needs_context' => false,], 'has_any_category' => ['alias' => false, 'needs_context' => false,], diff --git a/resources/views/v1/transactions/show.twig b/resources/views/v1/transactions/show.twig index cb00395bcc..72bcdfd1a5 100644 --- a/resources/views/v1/transactions/show.twig +++ b/resources/views/v1/transactions/show.twig @@ -15,9 +15,12 @@ {{ 'actions'|_}}