mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 00:27:30 +00:00
Better validation, can now also use notes in expression.
This commit is contained in:
@@ -26,13 +26,16 @@ namespace FireflyIII\TransactionRules\Actions;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Traits\RefreshNotesTrait;
|
||||
|
||||
/**
|
||||
* Class AppendDescription.
|
||||
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||
*/
|
||||
class AppendDescription implements ActionInterface
|
||||
{
|
||||
private RuleAction $action;
|
||||
use RefreshNotesTrait;
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
@@ -44,6 +47,7 @@ class AppendDescription implements ActionInterface
|
||||
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$this->refreshNotes($journal);
|
||||
$append = $this->action->getValue($journal);
|
||||
$description = sprintf('%s %s', $journal['description'], $append);
|
||||
\DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
||||
|
@@ -29,12 +29,15 @@ use FireflyIII\Events\TriggeredAuditLog;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\TransactionRules\Traits\RefreshNotesTrait;
|
||||
|
||||
/**
|
||||
* Class AppendDescriptionToNotes
|
||||
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||
*/
|
||||
class AppendDescriptionToNotes implements ActionInterface
|
||||
{
|
||||
use RefreshNotesTrait;
|
||||
private RuleAction $action;
|
||||
|
||||
/**
|
||||
@@ -47,6 +50,7 @@ class AppendDescriptionToNotes implements ActionInterface
|
||||
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$this->refreshNotes($journal);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
|
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
|
||||
/**
|
||||
* Class AppendNotes.
|
||||
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||
*/
|
||||
class AppendNotes implements ActionInterface
|
||||
{
|
||||
|
@@ -33,6 +33,7 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||
|
||||
/**
|
||||
* Class AppendNotesToDescription
|
||||
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||
*/
|
||||
class AppendNotesToDescription implements ActionInterface
|
||||
{
|
||||
|
@@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
|
||||
/**
|
||||
* Class MoveDescriptionToNotes
|
||||
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||
*/
|
||||
class MoveDescriptionToNotes implements ActionInterface
|
||||
{
|
||||
|
@@ -36,6 +36,7 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
|
||||
|
||||
/**
|
||||
* Class MoveNotesToDescription
|
||||
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||
*/
|
||||
class MoveNotesToDescription implements ActionInterface
|
||||
{
|
||||
|
@@ -29,6 +29,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
|
||||
/**
|
||||
* Class PrependDescription.
|
||||
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||
*/
|
||||
class PrependDescription implements ActionInterface
|
||||
{
|
||||
|
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
|
||||
/**
|
||||
* Class PrependNotes.
|
||||
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||
*/
|
||||
class PrependNotes implements ActionInterface
|
||||
{
|
||||
|
@@ -26,15 +26,18 @@ namespace FireflyIII\TransactionRules\Engine;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
use FireflyIII\Models\RuleTrigger;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
use FireflyIII\Support\Search\SearchInterface;
|
||||
use FireflyIII\TransactionRules\Factory\ActionFactory;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class SearchRuleEngine
|
||||
@@ -50,10 +53,10 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->rules = new Collection();
|
||||
$this->groups = new Collection();
|
||||
$this->operators = [];
|
||||
$this->resultCount = [];
|
||||
$this->rules = new Collection();
|
||||
$this->groups = new Collection();
|
||||
$this->operators = [];
|
||||
$this->resultCount = [];
|
||||
|
||||
// always collect the triggers from the database, unless indicated otherwise.
|
||||
$this->refreshTriggers = true;
|
||||
@@ -70,7 +73,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
app('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,8 +82,9 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
$collection = $collection->merge($found);
|
||||
}
|
||||
|
||||
return $collection->unique();
|
||||
$result = $collection->unique();
|
||||
app('log')->debug(sprintf('SearchRuleEngine::find() returns %d unique transactions.', $result->count()));
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +93,8 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
private function findStrictRule(Rule $rule): Collection
|
||||
{
|
||||
app('log')->debug(sprintf('Now in findStrictRule(#%d)', $rule->id ?? 0));
|
||||
$searchArray = [];
|
||||
$triggers = [];
|
||||
$searchArray = [];
|
||||
$triggers = [];
|
||||
if ($this->refreshTriggers) {
|
||||
$triggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
|
||||
}
|
||||
@@ -121,7 +125,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
app('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);
|
||||
}
|
||||
@@ -141,7 +145,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
}
|
||||
}
|
||||
|
||||
$result = $searchEngine->searchTransactions();
|
||||
$result = $searchEngine->searchTransactions();
|
||||
|
||||
return $result->getCollection();
|
||||
}
|
||||
@@ -166,7 +170,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
$dateTrigger = true;
|
||||
}
|
||||
}
|
||||
$result = $journalTrigger && $dateTrigger;
|
||||
$result = $journalTrigger && $dateTrigger;
|
||||
app('log')->debug(sprintf('Result of hasSpecificJournalTrigger is %s.', var_export($result, true)));
|
||||
|
||||
return $result;
|
||||
@@ -185,7 +189,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
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;
|
||||
app('log')->debug(sprintf('Found journal #%d with date %s.', $journal->id, $journal->date->format('Y-m-d')));
|
||||
@@ -261,10 +265,10 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
$searchEngine->parseQuery(sprintf('%s:%s', $type, $value));
|
||||
}
|
||||
|
||||
$result = $searchEngine->searchTransactions();
|
||||
$collection = $result->getCollection();
|
||||
$result = $searchEngine->searchTransactions();
|
||||
$collection = $result->getCollection();
|
||||
app('log')->debug(sprintf('Found in this run, %d transactions', $collection->count()));
|
||||
$total = $total->merge($collection);
|
||||
$total = $total->merge($collection);
|
||||
app('log')->debug(sprintf('Total collection is now %d transactions', $total->count()));
|
||||
++$count;
|
||||
// if trigger says stop processing, do so.
|
||||
@@ -278,7 +282,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
app('log')->debug(sprintf('Done running %d trigger(s)', $count));
|
||||
|
||||
// make collection unique
|
||||
$unique = $total->unique(
|
||||
$unique = $total->unique(
|
||||
static function (array $group) {
|
||||
$str = '';
|
||||
foreach ($group['transactions'] as $transaction) {
|
||||
@@ -369,7 +373,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
$this->processResults($rule, $collection);
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: done processing strict rule #%d', $rule->id));
|
||||
|
||||
$result = $collection->count() > 0;
|
||||
$result = $collection->count() > 0;
|
||||
if (true === $result) {
|
||||
app('log')->debug(sprintf('SearchRuleEngine:: rule #%d was triggered (on %d transaction(s)).', $rule->id, $collection->count()));
|
||||
|
||||
@@ -432,6 +436,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
||||
private function processRuleAction(RuleAction $ruleAction, array $transaction): bool
|
||||
{
|
||||
app('log')->debug(sprintf('Executing rule action "%s" with value "%s"', $ruleAction->action_type, $ruleAction->action_value));
|
||||
$transaction = $this->addNotes($transaction);
|
||||
$actionClass = ActionFactory::getAction($ruleAction);
|
||||
$result = $actionClass->actOnArray($transaction);
|
||||
$journalId = $transaction['transaction_journal_id'] ?? 0;
|
||||
@@ -532,4 +537,15 @@ 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.*']);
|
||||
if (null !== $dbNote) {
|
||||
$transaction['notes'] = $dbNote->text;
|
||||
}
|
||||
Log::debug(sprintf('Notes of journal #%d filled in.', $transaction['transaction_journal_id']));
|
||||
return $transaction;
|
||||
}
|
||||
}
|
||||
|
@@ -79,6 +79,7 @@ class ActionExpression
|
||||
'due_date',
|
||||
'process_date',
|
||||
'destination_transaction_id',
|
||||
'notes',
|
||||
];
|
||||
|
||||
private ExpressionLanguage $expressionLanguage;
|
||||
@@ -97,7 +98,7 @@ class ActionExpression
|
||||
|
||||
private static function isExpression(string $expr): bool
|
||||
{
|
||||
return str_starts_with($expr, '=');
|
||||
return str_starts_with($expr, '=') && strlen($expr) > 1;
|
||||
}
|
||||
|
||||
private function validate(): ?SyntaxError
|
||||
|
48
app/TransactionRules/Traits/RefreshNotesTrait.php
Normal file
48
app/TransactionRules/Traits/RefreshNotesTrait.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/*
|
||||
* RefreshNotesTrait.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Traits;
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait RefreshNotesTrait
|
||||
{
|
||||
/**
|
||||
* @param array $transaction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
final protected function refreshNotes(array $transaction): array
|
||||
{
|
||||
$transaction['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;
|
||||
}
|
||||
Log::debug(sprintf('Notes of journal #%d refreshed.', $transaction['transaction_journal_id']));
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user