mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 14:26:58 +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\Events\TriggeredAuditLog;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use FireflyIII\TransactionRules\Traits\RefreshNotesTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AppendDescription.
|
* Class AppendDescription.
|
||||||
|
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class AppendDescription implements ActionInterface
|
class AppendDescription implements ActionInterface
|
||||||
{
|
{
|
||||||
private RuleAction $action;
|
private RuleAction $action;
|
||||||
|
use RefreshNotesTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
@@ -44,6 +47,7 @@ class AppendDescription implements ActionInterface
|
|||||||
|
|
||||||
public function actOnArray(array $journal): bool
|
public function actOnArray(array $journal): bool
|
||||||
{
|
{
|
||||||
|
$this->refreshNotes($journal);
|
||||||
$append = $this->action->getValue($journal);
|
$append = $this->action->getValue($journal);
|
||||||
$description = sprintf('%s %s', $journal['description'], $append);
|
$description = sprintf('%s %s', $journal['description'], $append);
|
||||||
\DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $description]);
|
\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\Note;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use FireflyIII\TransactionRules\Traits\RefreshNotesTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AppendDescriptionToNotes
|
* Class AppendDescriptionToNotes
|
||||||
|
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class AppendDescriptionToNotes implements ActionInterface
|
class AppendDescriptionToNotes implements ActionInterface
|
||||||
{
|
{
|
||||||
|
use RefreshNotesTrait;
|
||||||
private RuleAction $action;
|
private RuleAction $action;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,6 +50,7 @@ class AppendDescriptionToNotes implements ActionInterface
|
|||||||
|
|
||||||
public function actOnArray(array $journal): bool
|
public function actOnArray(array $journal): bool
|
||||||
{
|
{
|
||||||
|
$this->refreshNotes($journal);
|
||||||
/** @var null|TransactionJournal $object */
|
/** @var null|TransactionJournal $object */
|
||||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||||
if (null === $object) {
|
if (null === $object) {
|
||||||
|
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AppendNotes.
|
* Class AppendNotes.
|
||||||
|
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class AppendNotes implements ActionInterface
|
class AppendNotes implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -33,6 +33,7 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AppendNotesToDescription
|
* Class AppendNotesToDescription
|
||||||
|
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class AppendNotesToDescription implements ActionInterface
|
class AppendNotesToDescription implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MoveDescriptionToNotes
|
* Class MoveDescriptionToNotes
|
||||||
|
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class MoveDescriptionToNotes implements ActionInterface
|
class MoveDescriptionToNotes implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -36,6 +36,7 @@ use FireflyIII\Support\Request\ConvertsDataTypes;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MoveNotesToDescription
|
* Class MoveNotesToDescription
|
||||||
|
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class MoveNotesToDescription implements ActionInterface
|
class MoveNotesToDescription implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -29,6 +29,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PrependDescription.
|
* Class PrependDescription.
|
||||||
|
* TODO Can be replaced (and migrated) to action "set description" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class PrependDescription implements ActionInterface
|
class PrependDescription implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PrependNotes.
|
* Class PrependNotes.
|
||||||
|
* TODO Can be replaced (and migrated) to action "set notes" with a prefilled expression
|
||||||
*/
|
*/
|
||||||
class PrependNotes implements ActionInterface
|
class PrependNotes implements ActionInterface
|
||||||
{
|
{
|
||||||
|
@@ -26,15 +26,18 @@ namespace FireflyIII\TransactionRules\Engine;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\Models\Rule;
|
use FireflyIII\Models\Rule;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\RuleGroup;
|
use FireflyIII\Models\RuleGroup;
|
||||||
use FireflyIII\Models\RuleTrigger;
|
use FireflyIII\Models\RuleTrigger;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||||
use FireflyIII\Support\Search\SearchInterface;
|
use FireflyIII\Support\Search\SearchInterface;
|
||||||
use FireflyIII\TransactionRules\Factory\ActionFactory;
|
use FireflyIII\TransactionRules\Factory\ActionFactory;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SearchRuleEngine
|
* Class SearchRuleEngine
|
||||||
@@ -79,8 +82,9 @@ class SearchRuleEngine implements RuleEngineInterface
|
|||||||
}
|
}
|
||||||
$collection = $collection->merge($found);
|
$collection = $collection->merge($found);
|
||||||
}
|
}
|
||||||
|
$result = $collection->unique();
|
||||||
return $collection->unique();
|
app('log')->debug(sprintf('SearchRuleEngine::find() returns %d unique transactions.', $result->count()));
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -432,6 +436,7 @@ class SearchRuleEngine implements RuleEngineInterface
|
|||||||
private function processRuleAction(RuleAction $ruleAction, array $transaction): bool
|
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));
|
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);
|
$actionClass = ActionFactory::getAction($ruleAction);
|
||||||
$result = $actionClass->actOnArray($transaction);
|
$result = $actionClass->actOnArray($transaction);
|
||||||
$journalId = $transaction['transaction_journal_id'] ?? 0;
|
$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',
|
'due_date',
|
||||||
'process_date',
|
'process_date',
|
||||||
'destination_transaction_id',
|
'destination_transaction_id',
|
||||||
|
'notes',
|
||||||
];
|
];
|
||||||
|
|
||||||
private ExpressionLanguage $expressionLanguage;
|
private ExpressionLanguage $expressionLanguage;
|
||||||
@@ -97,7 +98,7 @@ class ActionExpression
|
|||||||
|
|
||||||
private static function isExpression(string $expr): bool
|
private static function isExpression(string $expr): bool
|
||||||
{
|
{
|
||||||
return str_starts_with($expr, '=');
|
return str_starts_with($expr, '=') && strlen($expr) > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validate(): ?SyntaxError
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -185,7 +185,7 @@ class FireflyValidator extends Validator
|
|||||||
// take
|
// take
|
||||||
$first = substr($value, 0, 4);
|
$first = substr($value, 0, 4);
|
||||||
$last = substr($value, 4);
|
$last = substr($value, 4);
|
||||||
$iban = $last . $first;
|
$iban = $last.$first;
|
||||||
$iban = trim(str_replace($search, $replace, $iban));
|
$iban = trim(str_replace($search, $replace, $iban));
|
||||||
if ('' === $iban) {
|
if ('' === $iban) {
|
||||||
return false;
|
return false;
|
||||||
@@ -509,7 +509,7 @@ class FireflyValidator extends Validator
|
|||||||
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
private function validateByAccountTypeString(string $value, array $parameters, string $type): bool
|
||||||
{
|
{
|
||||||
/** @var null|array $search */
|
/** @var null|array $search */
|
||||||
$search = \Config::get('firefly.accountTypeByIdentifier.' . $type);
|
$search = \Config::get('firefly.accountTypeByIdentifier.'.$type);
|
||||||
|
|
||||||
if (null === $search) {
|
if (null === $search) {
|
||||||
return false;
|
return false;
|
||||||
@@ -522,7 +522,8 @@ class FireflyValidator extends Validator
|
|||||||
/** @var null|Account $result */
|
/** @var null|Account $result */
|
||||||
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
$result = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)
|
||||||
->where('name', $value)
|
->where('name', $value)
|
||||||
->first();
|
->first()
|
||||||
|
;
|
||||||
|
|
||||||
return null === $result;
|
return null === $result;
|
||||||
}
|
}
|
||||||
@@ -539,7 +540,8 @@ class FireflyValidator extends Validator
|
|||||||
/** @var null|Account $result */
|
/** @var null|Account $result */
|
||||||
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
$result = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||||
->where('name', $value)
|
->where('name', $value)
|
||||||
->first();
|
->first()
|
||||||
|
;
|
||||||
|
|
||||||
return null === $result;
|
return null === $result;
|
||||||
}
|
}
|
||||||
@@ -557,7 +559,8 @@ class FireflyValidator extends Validator
|
|||||||
|
|
||||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||||
->where('name', $value)
|
->where('name', $value)
|
||||||
->first();
|
->first()
|
||||||
|
;
|
||||||
|
|
||||||
return null === $entry;
|
return null === $entry;
|
||||||
}
|
}
|
||||||
@@ -575,7 +578,8 @@ class FireflyValidator extends Validator
|
|||||||
|
|
||||||
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
$entry = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)
|
||||||
->where('name', $value)
|
->where('name', $value)
|
||||||
->first();
|
->first()
|
||||||
|
;
|
||||||
|
|
||||||
return null === $entry;
|
return null === $entry;
|
||||||
}
|
}
|
||||||
@@ -603,7 +607,8 @@ class FireflyValidator extends Validator
|
|||||||
->whereNull('accounts.deleted_at')
|
->whereNull('accounts.deleted_at')
|
||||||
->where('accounts.user_id', auth()->user()->id)
|
->where('accounts.user_id', auth()->user()->id)
|
||||||
->where('account_meta.name', 'account_number')
|
->where('account_meta.name', 'account_number')
|
||||||
->where('account_meta.data', json_encode($value));
|
->where('account_meta.data', json_encode($value))
|
||||||
|
;
|
||||||
|
|
||||||
if ($accountId > 0) {
|
if ($accountId > 0) {
|
||||||
// exclude current account from check.
|
// exclude current account from check.
|
||||||
@@ -710,7 +715,8 @@ class FireflyValidator extends Validator
|
|||||||
->where('response', $response)
|
->where('response', $response)
|
||||||
->where('delivery', $delivery)
|
->where('delivery', $delivery)
|
||||||
->where('id', '!=', $existingId)
|
->where('id', '!=', $existingId)
|
||||||
->where('url', $url)->count();
|
->where('url', $url)->count()
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -746,7 +752,8 @@ class FireflyValidator extends Validator
|
|||||||
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
$result = \DB::table($table)->where('user_id', auth()->user()->id)->whereNull('deleted_at')
|
||||||
->where('id', '!=', $exclude)
|
->where('id', '!=', $exclude)
|
||||||
->where($field, $value)
|
->where($field, $value)
|
||||||
->first([$field]);
|
->first([$field])
|
||||||
|
;
|
||||||
if (null === $result) {
|
if (null === $result) {
|
||||||
return true; // not found, so true.
|
return true; // not found, so true.
|
||||||
}
|
}
|
||||||
@@ -768,7 +775,8 @@ class FireflyValidator extends Validator
|
|||||||
$query = \DB::table('object_groups')
|
$query = \DB::table('object_groups')
|
||||||
->whereNull('object_groups.deleted_at')
|
->whereNull('object_groups.deleted_at')
|
||||||
->where('object_groups.user_id', auth()->user()->id)
|
->where('object_groups.user_id', auth()->user()->id)
|
||||||
->where('object_groups.title', $value);
|
->where('object_groups.title', $value)
|
||||||
|
;
|
||||||
if (null !== $exclude) {
|
if (null !== $exclude) {
|
||||||
$query->where('object_groups.id', '!=', (int)$exclude);
|
$query->where('object_groups.id', '!=', (int)$exclude);
|
||||||
}
|
}
|
||||||
@@ -787,7 +795,8 @@ class FireflyValidator extends Validator
|
|||||||
{
|
{
|
||||||
$exclude = $parameters[0] ?? null;
|
$exclude = $parameters[0] ?? null;
|
||||||
$query = \DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
|
$query = \DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
|
||||||
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
|
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id)
|
||||||
|
;
|
||||||
if (null !== $exclude) {
|
if (null !== $exclude) {
|
||||||
$query->where('piggy_banks.id', '!=', (int)$exclude);
|
$query->where('piggy_banks.id', '!=', (int)$exclude);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user