Skip to content

Firefly III: a personal finances manager

Clean up events

James Cole54df0d44f78b
diff --git a/app/Events/Model/TransactionGroup/TriggeredStoredTransactionGroup.php b/app/Events/Model/TransactionGroup/TriggeredStoredTransactionGroup.php
deleted file mode 100644
index 916ddd34e6..0000000000
--- a/app/Events/Model/TransactionGroup/TriggeredStoredTransactionGroup.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/*
- * TriggeredStoredTransactionGroup.php
- * Copyright (c) 2025 [email protected]
- *
- * This file is part of Firefly III (https://github.com/firefly-iii).
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
-namespace FireflyIII\Events\Model\TransactionGroup;
-
-use FireflyIII\Events\Event;
-use FireflyIII\Models\RuleGroup;
-use FireflyIII\Models\TransactionGroup;
-use Illuminate\Queue\SerializesModels;
-
-/**
- * @deprecated
- */
-class TriggeredStoredTransactionGroup extends Event
-{
- use SerializesModels;
-
- public ?RuleGroup $ruleGroup = null;
-
- /**
- * Create a new event instance.
- */
- public function __construct(
- public TransactionGroup $transactionGroup,
- ?RuleGroup $ruleGroup = null
- ) {
- $this->ruleGroup = $ruleGroup;
- }
-}
diff --git a/app/Http/Controllers/RuleGroup/ExecutionController.php b/app/Http/Controllers/RuleGroup/ExecutionController.php
index fce16d4f26..7351d9aed2 100644
--- a/app/Http/Controllers/RuleGroup/ExecutionController.php
+++ b/app/Http/Controllers/RuleGroup/ExecutionController.php
@@ -26,16 +26,15 @@ namespace FireflyIII\Http\Controllers\RuleGroup;
use Carbon\Carbon;
use Exception;
-use FireflyIII\Events\Model\TransactionGroup\TriggeredStoredTransactionGroup;
-use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SelectTransactionsRequest;
use FireflyIII\Models\RuleGroup;
-use FireflyIII\Models\TransactionGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
+use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
+use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
-use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
@@ -44,7 +43,8 @@ use Illuminate\View\View;
*/
class ExecutionController extends Controller
{
- private readonly AccountRepositoryInterface $repository;
+ private readonly AccountRepositoryInterface $repository;
+ private readonly RuleGroupRepositoryInterface $ruleGroupRepository;
/**
* ExecutionController constructor.
@@ -52,11 +52,13 @@ class ExecutionController extends Controller
public function __construct()
{
parent::__construct();
- $this->repository = app(AccountRepositoryInterface::class);
+ $this->repository = app(AccountRepositoryInterface::class);
+ $this->ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
$this->middleware(function ($request, $next) {
- app('view')->share('title', (string) trans('firefly.rules'));
+ app('view')->share('title', (string)trans('firefly.rules'));
app('view')->share('mainTitleIcon', 'fa-random');
$this->repository->setUser(auth()->user());
+ $this->ruleGroupRepository->setUser(auth()->user());
return $next($request);
});
@@ -70,44 +72,37 @@ class ExecutionController extends Controller
public function execute(SelectTransactionsRequest $request, RuleGroup $ruleGroup): RedirectResponse
{
Log::debug(sprintf('You have selected rule group #%d', $ruleGroup->id));
- // Get parameters specified by the user
- $accounts = $request->get('accounts');
- $set = new Collection();
- if (is_array($accounts)) {
- $set = $this->repository->getAccountsById($accounts);
- }
- /** @var GroupCollectorInterface $collector */
- $collector = app(GroupCollectorInterface::class);
- if (count($set) > 0) {
- $collector->setAccounts($set);
- }
+ // start code
+ /** @var User $user */
+ $user = auth()->user();
+ $accounts = implode(',', $request->get('accounts'));
+ // create new rule engine:
+ $newRuleEngine = app(RuleEngineInterface::class);
+ $newRuleEngine->setUser($user);
// add date operators.
if (null !== $request->get('start')) {
$startDate = new Carbon($request->get('start'));
- $collector->setStart($startDate);
+ $newRuleEngine->addOperator(['type' => 'date_after', 'value' => $startDate->format('Y-m-d')]);
}
if (null !== $request->get('end')) {
$endDate = new Carbon($request->get('end'));
- $collector->setEnd($endDate);
- }
- $final = $collector->getGroups();
- $ids = $final->pluck('id')->toArray();
- Log::debug(sprintf('Found %d groups collected from %d account(s)', $final->count(), $set->count()));
- foreach (array_chunk($ids, 1337) as $setOfIds) {
- Log::debug(sprintf('Now processing %d groups', count($setOfIds)));
- $groups = TransactionGroup::whereIn('id', $setOfIds)->get();
-
- /** @var TransactionGroup $group */
- foreach ($groups as $group) {
- Log::debug(sprintf('Processing group #%d.', $group->id));
- event(new TriggeredStoredTransactionGroup($group, $ruleGroup));
- }
+ $newRuleEngine->addOperator(['type' => 'date_before', 'value' => $endDate->format('Y-m-d')]);
}
+ // add extra operators:
+ $newRuleEngine->addOperator(['type' => 'account_id', 'value' => $accounts]);
+
+ // set rules:
+ $rules = $this->ruleGroupRepository->getActiveRules($ruleGroup);
+
+ $newRuleEngine->setRules($rules);
+ $newRuleEngine->fire();
+ $resultCount = $newRuleEngine->getResults();
+
// Tell the user that the job is queued
- session()->flash('success', (string) trans('firefly.applied_rule_group_selection', ['title' => $ruleGroup->title]));
+ session()->flash('success', trans_choice('firefly.applied_rule_group_selection', $resultCount, ['title' => $ruleGroup->title]));
return redirect()->route('rules.index');
}
@@ -117,10 +112,10 @@ class ExecutionController extends Controller
*
* @return Factory|View
*/
- public function selectTransactions(RuleGroup $ruleGroup): Factory|\Illuminate\Contracts\View\View
+ public function selectTransactions(RuleGroup $ruleGroup): Factory | \Illuminate\Contracts\View\View
{
- $subTitle = (string) trans('firefly.apply_rule_group_selection', ['title' => $ruleGroup->title]);
+ $subTitle = (string)trans('firefly.apply_rule_group_selection', ['title' => $ruleGroup->title]);
- return view('rules.rule-group.select-transactions', ['ruleGroup' => $ruleGroup, 'subTitle' => $subTitle]);
+ return view('rules.rule-group.select-transactions', ['ruleGroup' => $ruleGroup, 'subTitle' => $subTitle]);
}
}
diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php
index 40a65b9dc3..ae4f8a5d10 100644
--- a/app/Repositories/RuleGroup/RuleGroupRepository.php
+++ b/app/Repositories/RuleGroup/RuleGroupRepository.php
@@ -144,7 +144,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface, UserGroupInte
public function getActiveRules(RuleGroup $group): Collection
{
- return $group->rules()->where('rules.active', true)->get(['rules.*']);
+ return $group->rules()->where('rules.active', true)->orderBy('rules.order', 'ASC')->get(['rules.*']);
}
public function getActiveStoreRules(RuleGroup $group): Collection
diff --git a/app/TransactionRules/Engine/SearchRuleEngine.php b/app/TransactionRules/Engine/SearchRuleEngine.php
index ed78800ac9..cdaa490eb1 100644
--- a/app/TransactionRules/Engine/SearchRuleEngine.php
+++ b/app/TransactionRules/Engine/SearchRuleEngine.php
@@ -100,10 +100,7 @@ class SearchRuleEngine implements RuleEngineInterface
foreach ($this->rules as $rule) { // @phpstan-ignore-line
$result = $this->fireRule($rule);
if ($result && true === $rule->stop_processing) {
- Log::debug(sprintf(
- 'Rule #%d has triggered and executed, but calls to stop processing. Since not in the context of a group, do not stop.',
- $rule->id
- ));
+ Log::debug(sprintf('Rule #%d has triggered and executed, but calls to stop processing. Since not in the context of a group, do not stop.', $rule->id));
}
if (false === $result && true === $rule->stop_processing) {
Log::debug(sprintf('Rule #%d has triggered and changed nothing, but calls to stop processing. Do not stop.', $rule->id));
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 0c0f544447..63324441de 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -825,7 +825,7 @@ return [
'execute' => 'Execute',
'apply_rule_group_selection' => 'Apply rule group ":title" to a selection of your transactions',
'apply_rule_group_selection_intro' => 'Rule groups like ":title" are normally only applied to new or updated transactions, but you can tell Firefly III to run all the rules in this group on a selection of your existing transactions. This can be useful when you have updated a group of rules and you need the changes to be applied to all of your other transactions.',
- 'applied_rule_group_selection' => 'Rule group ":title" has been applied to your selection.',
+ 'applied_rule_group_selection' => '{0} No transactions in your selection were changed by the rules in rule group ":title".|[1] One transaction in your selection was changed by the rules in rule group ":title".|[2,*] :count transactions in your selection were changed by the rules in rule group ":title".',
'rule_run_after_creation' => 'If you check this box, you get the opportunity to run the rule after it has been created.',
'rule_run_after_edit' => 'If you check this box, you get the opportunity to run the rule after it has been updated.',