🤖 Auto commit for release 'develop' on 2026-03-01

This commit is contained in:
JC5
2026-03-01 08:08:15 +01:00
parent 6bae8ab70a
commit bb51baaa38
15 changed files with 133 additions and 132 deletions

View File

@@ -38,6 +38,7 @@ use FireflyIII\TransactionRules\Factory\ActionFactory;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Override;
/**
* Class SearchRuleEngine
@@ -46,12 +47,12 @@ class SearchRuleEngine implements RuleEngineInterface
{
private readonly Collection $groups;
private array $operators = [];
private array $operators = [];
// always collect the triggers from the database, unless indicated otherwise.
private bool $refreshTriggers = true;
private array $resultCount = [];
private bool $refreshTriggers = true;
private array $resultCount = [];
private readonly Collection $rules;
private User $user;
private User $user;
public function __construct()
{
@@ -70,7 +71,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug('SearchRuleEngine::find()');
$collection = new Collection();
foreach ($this->rules as $rule) {
$found = new Collection();
$found = new Collection();
if (true === $rule->strict) {
$found = $this->findStrictRule($rule);
}
@@ -79,7 +80,7 @@ class SearchRuleEngine implements RuleEngineInterface
}
$collection = $collection->merge($found);
}
$result = $collection->unique();
$result = $collection->unique();
Log::debug(sprintf('SearchRuleEngine::find() returns %d unique transactions.', $result->count()));
return $result;
@@ -102,9 +103,9 @@ class SearchRuleEngine implements RuleEngineInterface
$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
));
'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));
@@ -134,6 +135,21 @@ class SearchRuleEngine implements RuleEngineInterface
return count($this->resultCount);
}
#[Override]
public function removeOperator(string $type): void
{
$new = [];
foreach ($this->operators as $operator) {
if ($type === $operator['type']) {
Log::debug(sprintf('Removing operator "%s"', $type));
continue;
}
$new[] = $operator;
}
$this->operators = $new;
}
public function setRefreshTriggers(bool $refreshTriggers): void
{
$this->refreshTriggers = $refreshTriggers;
@@ -170,9 +186,10 @@ class SearchRuleEngine implements RuleEngineInterface
private function addNotes(array $transaction): array
{
$transaction['notes'] = '';
$dbNote = Note::where('noteable_id', (int)$transaction['transaction_journal_id'])
->where('noteable_type', TransactionJournal::class)
->first(['notes.*']);
$dbNote = Note::where('noteable_id', (int) $transaction['transaction_journal_id'])
->where('noteable_type', TransactionJournal::class)
->first(['notes.*'])
;
if (null !== $dbNote) {
$transaction['notes'] = $dbNote->text;
}
@@ -201,12 +218,12 @@ class SearchRuleEngine implements RuleEngineInterface
/** @var RuleTrigger $ruleTrigger */
foreach ($triggers as $ruleTrigger) {
Log::debug(sprintf(
'Now at rule trigger #%d: %s:"%s" (%s).',
$ruleTrigger->id,
$ruleTrigger->trigger_type,
$ruleTrigger->trigger_value,
var_export($ruleTrigger->stop_processing, true)
));
'Now at rule trigger #%d: %s:"%s" (%s).',
$ruleTrigger->id,
$ruleTrigger->trigger_type,
$ruleTrigger->trigger_value,
var_export($ruleTrigger->stop_processing, true)
));
if (false === $ruleTrigger->active) {
Log::debug('Trigger is not active, continue.');
@@ -244,10 +261,10 @@ class SearchRuleEngine implements RuleEngineInterface
$searchEngine->parseQuery(sprintf('%s:%s', $type, $value));
}
$result = $searchEngine->searchTransactions();
$collection = $result->getCollection();
$result = $searchEngine->searchTransactions();
$collection = $result->getCollection();
Log::debug(sprintf('Found in this run, %d transactions', $collection->count()));
$total = $total->merge($collection);
$total = $total->merge($collection);
Log::debug(sprintf('Total collection is now %d transactions', $total->count()));
++$count;
// if trigger says stop processing, do so.
@@ -261,7 +278,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug(sprintf('Done running %d trigger(s)', $count));
// make collection unique
$unique = $total->unique(static function (array $group): string {
$unique = $total->unique(static function (array $group): string {
$str = '';
foreach ($group['transactions'] as $transaction) {
$str = sprintf('%s%d', $str, $transaction['transaction_journal_id']);
@@ -283,8 +300,8 @@ class SearchRuleEngine implements RuleEngineInterface
private function findStrictRule(Rule $rule): Collection
{
Log::debug(sprintf('Now in findStrictRule(#%d)', $rule->id ?? 0));
$searchArray = [];
$triggers = [];
$searchArray = [];
$triggers = [];
if ($this->refreshTriggers) {
$triggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
}
@@ -298,12 +315,12 @@ class SearchRuleEngine implements RuleEngineInterface
continue;
}
$contextSearch = $ruleTrigger->trigger_type;
if (str_starts_with((string)$ruleTrigger->trigger_type, '-')) {
$contextSearch = substr((string)$ruleTrigger->trigger_type, 1);
if (str_starts_with((string) $ruleTrigger->trigger_type, '-')) {
$contextSearch = substr((string) $ruleTrigger->trigger_type, 1);
}
// if the trigger needs no context, value is different:
$needsContext = (bool)(config(sprintf('search.operators.%s.needs_context', $contextSearch)) ?? true);
$needsContext = (bool) (config(sprintf('search.operators.%s.needs_context', $contextSearch)) ?? true);
if (false === $needsContext) {
Log::debug(sprintf('SearchRuleEngine:: add a rule trigger (no context): %s:true', $ruleTrigger->trigger_type));
$searchArray[$ruleTrigger->trigger_type][] = 'true';
@@ -319,7 +336,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug(sprintf('SearchRuleEngine:: add local added operator: %s:"%s"', $operator['type'], $operator['value']));
$searchArray[$operator['type']][] = sprintf('"%s"', $operator['value']);
}
$date = today(config('app.timezone'));
$date = today(config('app.timezone'));
if ($this->hasSpecificJournalTrigger($searchArray)) {
$date = $this->setDateFromJournalTrigger($searchArray);
}
@@ -340,7 +357,7 @@ class SearchRuleEngine implements RuleEngineInterface
}
}
$result = $searchEngine->searchTransactions();
$result = $searchEngine->searchTransactions();
return $result->getCollection();
}
@@ -357,11 +374,7 @@ class SearchRuleEngine implements RuleEngineInterface
}
if (!$group->relationLoaded('rules')) {
Log::debug('Group rules have NOT been pre-loaded, load them NOW.');
$rules = $group
->rules()
->orderBy('rules.order', 'ASC')
->where('rules.active', true)
->get(['rules.*']);
$rules = $group->rules()->orderBy('rules.order', 'ASC')->where('rules.active', true)->get(['rules.*']);
}
Log::debug(sprintf('Going to fire group #%d with %d rule(s)', $group->id, $rules->count()));
@@ -429,7 +442,7 @@ class SearchRuleEngine implements RuleEngineInterface
$this->processResults($rule, $collection);
$result = $collection->count() > 0;
$result = $collection->count() > 0;
if ($result) {
Log::debug(sprintf('SearchRuleEngine:: Done. Rule #%d was triggered (on %d transaction(s)).', $rule->id, $collection->count()));
@@ -455,7 +468,7 @@ class SearchRuleEngine implements RuleEngineInterface
Log::debug('Found a journal_id trigger with 1 journal, true.');
$journalTrigger = true;
}
if ('journal_id' === $triggerName && is_string($values) && !str_contains($values,',')) {
if ('journal_id' === $triggerName && is_string($values) && !str_contains($values, ',')) {
Log::debug('Found a journal_id trigger with 1 journal, true.');
$journalTrigger = true;
}
@@ -464,7 +477,7 @@ class SearchRuleEngine implements RuleEngineInterface
$dateTrigger = true;
}
}
$result = $journalTrigger && $dateTrigger;
$result = $journalTrigger && $dateTrigger;
Log::debug(sprintf('Result of hasSpecificJournalTrigger is %s.', var_export($result, true)));
return $result;
@@ -496,11 +509,11 @@ class SearchRuleEngine implements RuleEngineInterface
if ($result) {
$this->resultCount[$journalId] = array_key_exists($journalId, $this->resultCount) ? $this->resultCount[$journalId]++ : 1;
Log::debug(sprintf(
'Action "%s" on journal #%d was executed, so count a result. Updated transaction journal count is now %d.',
$ruleAction->action_type,
$transaction['transaction_journal_id'] ?? 0,
count($this->resultCount)
));
'Action "%s" on journal #%d was executed, so count a result. Updated transaction journal count is now %d.',
$ruleAction->action_type,
$transaction['transaction_journal_id'] ?? 0,
count($this->resultCount)
));
}
if (false === $result) {
Log::debug(sprintf('Action "%s" reports NO changes were made.', $ruleAction->action_type));
@@ -558,14 +571,14 @@ class SearchRuleEngine implements RuleEngineInterface
$journalId = 0;
foreach ($array as $triggerName => $values) {
if ('journal_id' === $triggerName && is_array($values) && 1 === count($values)) {
$journalId = (int)trim($values[0] ?? '"0"', '"'); // follows format "123".
$journalId = (int) trim($values[0] ?? '"0"', '"'); // follows format "123".
Log::debug(sprintf('Found journal ID #%d', $journalId));
}
}
if (0 !== $journalId) {
$repository = app(JournalRepositoryInterface::class);
$repository->setUser($this->user);
$journal = $repository->find($journalId);
$journal = $repository->find($journalId);
if (null !== $journal) {
$date = $journal->date;
Log::debug(sprintf('Found journal #%d with date %s.', $journal->id, $journal->date->format('Y-m-d')));
@@ -577,19 +590,4 @@ class SearchRuleEngine implements RuleEngineInterface
return today(config('app.timezone'));
}
#[\Override]
public function removeOperator(string $type): void
{
$new = [];
foreach ($this->operators as $operator) {
if ($type === $operator['type']) {
Log::debug(sprintf('Removing operator "%s"', $type));
continue;
}
$new[] = $operator;
}
$this->operators = $new;
}
}