Skip to content

Firefly III: a personal finances manager

Fix #4668

James Coledcd123a9ec8d
diff --git a/app/Http/Controllers/Rule/IndexController.php b/app/Http/Controllers/Rule/IndexController.php
index 7731fea3f6..2ce1d05fda 100644
--- a/app/Http/Controllers/Rule/IndexController.php
+++ b/app/Http/Controllers/Rule/IndexController.php
@@ -73,12 +73,10 @@ class IndexController extends Controller
*/
public function index()
{
- /** @var User $user */
- $user = auth()->user();
$this->createDefaultRuleGroup();
$this->createDefaultRule();
$this->ruleGroupRepos->resetOrder();
- $ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules(null);
+ $ruleGroups = $this->ruleGroupRepos->getAllRuleGroupsWithRules(null);
return prefixView('rules.index', compact('ruleGroups'));
}
diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php
index c6747145ca..f1fad15cef 100644
--- a/app/Repositories/RuleGroup/RuleGroupRepository.php
+++ b/app/Repositories/RuleGroup/RuleGroupRepository.php
@@ -258,6 +258,58 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
);
}
+ /**
+ * @param string|null $filter
+ *
+ * @return Collection
+ */
+ public function getAllRuleGroupsWithRules(?string $filter): Collection
+ {
+ $groups = $this->user->ruleGroups()
+ ->orderBy('order', 'ASC')
+ ->with(
+ [
+ 'rules' => static function (HasMany $query) {
+ $query->orderBy('order', 'ASC');
+ },
+ 'rules.ruleTriggers' => static function (HasMany $query) {
+ $query->orderBy('order', 'ASC');
+ },
+ 'rules.ruleActions' => static function (HasMany $query) {
+ $query->orderBy('order', 'ASC');
+ },
+ ]
+ )->get();
+ if (null === $filter) {
+ return $groups;
+ }
+ Log::debug(sprintf('Will filter getRuleGroupsWithRules on "%s".', $filter));
+
+ return $groups->map(
+ function (RuleGroup $group) use ($filter) {
+ Log::debug(sprintf('Now filtering group #%d', $group->id));
+ // filter the rules in the rule group:
+ $group->rules = $group->rules->filter(
+ function (Rule $rule) use ($filter) {
+ Log::debug(sprintf('Now filtering rule #%d', $rule->id));
+ foreach ($rule->ruleTriggers as $trigger) {
+ if ('user_action' === $trigger->trigger_type && $filter === $trigger->trigger_value) {
+ Log::debug(sprintf('Rule #%d triggers on %s, include it.', $rule->id, $filter));
+
+ return true;
+ }
+ }
+ Log::debug(sprintf('Rule #%d does not trigger on %s, do not include it.', $rule->id, $filter));
+
+ return false;
+ }
+ );
+
+ return $group;
+ }
+ );
+ }
+
/**
* @param RuleGroup $group
*
diff --git a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php
index 7d4f7a64de..b3f3b90985 100644
--- a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php
+++ b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php
@@ -114,6 +114,15 @@ interface RuleGroupRepositoryInterface
*/
public function getRuleGroupsWithRules(?string $filter): Collection;
+ /**
+ * Also inactive groups.
+ *
+ * @param string|null $filter
+ *
+ * @return Collection
+ */
+ public function getAllRuleGroupsWithRules(?string $filter): Collection;
+
/**
* @param RuleGroup $group
*
diff --git a/app/TransactionRules/Engine/SearchRuleEngine.php b/app/TransactionRules/Engine/SearchRuleEngine.php
index e9c8df73bd..56a4e6db21 100644
--- a/app/TransactionRules/Engine/SearchRuleEngine.php
+++ b/app/TransactionRules/Engine/SearchRuleEngine.php
@@ -174,6 +174,11 @@ class SearchRuleEngine implements RuleEngineInterface
private function fireRule(Rule $rule): bool
{
Log::debug(sprintf('Now going to fire rule #%d', $rule->id));
+ if (false === $rule->active) {
+ Log::debug(sprintf('Rule #%d is not active!', $rule->id));
+
+ return false;
+ }
if (true === $rule->strict) {
Log::debug(sprintf('Rule #%d is a strict rule.', $rule->id));
@@ -223,7 +228,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug(sprintf('Now in findStrictRule(#%d)', $rule->id ?? 0));
$searchArray = [];
/** @var RuleTrigger $ruleTrigger */
- foreach ($rule->ruleTriggers()->where('active',1)->get() as $ruleTrigger) {
+ foreach ($rule->ruleTriggers()->where('active', 1)->get() as $ruleTrigger) {
// if needs no context, value is different:
$needsContext = config(sprintf('firefly.search.operators.%s.needs_context', $ruleTrigger->trigger_type)) ?? true;
if (false === $needsContext) {
@@ -368,7 +373,7 @@ class SearchRuleEngine implements RuleEngineInterface
{
Log::debug(sprintf('SearchRuleEngine:: Will now execute actions on transaction journal #%d', $transaction['transaction_journal_id']));
/** @var RuleAction $ruleAction */
- foreach ($rule->ruleActions()->where('active',1)->get() as $ruleAction) {
+ foreach ($rule->ruleActions()->where('active', 1)->get() as $ruleAction) {
$break = $this->processRuleAction($ruleAction, $transaction);
if (true === $break) {
break;
@@ -444,7 +449,7 @@ class SearchRuleEngine implements RuleEngineInterface
$total = new Collection;
$count = 0;
/** @var RuleTrigger $ruleTrigger */
- foreach ($rule->ruleTriggers()->where('active',1)->get() as $ruleTrigger) {
+ foreach ($rule->ruleTriggers()->where('active', 1)->get() as $ruleTrigger) {
if ('user_action' === $ruleTrigger->trigger_type) {
Log::debug('Skip trigger type.');
continue;