From f41397eb4365a2fa320bf5105fcd5f5fb6ce0949 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Thu, 7 Mar 2024 17:18:46 -0500 Subject: [PATCH] refactor: add method on RuleAction to compute action value --- app/Models/RuleAction.php | 7 ++++ app/TransactionRules/Actions/AddTag.php | 13 +++---- .../Actions/AppendDescription.php | 12 ++---- app/TransactionRules/Actions/AppendNotes.php | 17 ++++----- .../Actions/ConvertToDeposit.php | 37 ++++++++++--------- .../Actions/ConvertToTransfer.php | 28 +++++++------- .../Actions/ConvertToWithdrawal.php | 37 ++++++++++--------- app/TransactionRules/Actions/LinkToBill.php | 16 ++++---- .../Actions/PrependDescription.php | 12 ++---- app/TransactionRules/Actions/PrependNotes.php | 17 ++++----- app/TransactionRules/Actions/RemoveTag.php | 15 +++----- app/TransactionRules/Actions/SetBudget.php | 10 ++--- app/TransactionRules/Actions/SetCategory.php | 10 ++--- .../Actions/SetDescription.php | 17 ++++----- .../Actions/SetDestinationAccount.php | 11 ++---- app/TransactionRules/Actions/SetNotes.php | 13 +++---- .../Actions/SetSourceAccount.php | 21 +++++------ .../Actions/UpdatePiggybank.php | 18 ++++----- .../Factory/ActionFactory.php | 4 +- 19 files changed, 138 insertions(+), 177 deletions(-) diff --git a/app/Models/RuleAction.php b/app/Models/RuleAction.php index f8f8e5fe12..cf088a9275 100644 --- a/app/Models/RuleAction.php +++ b/app/Models/RuleAction.php @@ -26,6 +26,7 @@ namespace FireflyIII\Models; use Carbon\Carbon; use Eloquent; use FireflyIII\Support\Models\ReturnsIntegerIdTrait; +use FireflyIII\TransactionRules\Expressions\ActionExpression; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; @@ -93,4 +94,10 @@ class RuleAction extends Model get: static fn ($value) => (int)$value, ); } + + public function getValue(array $journal) + { + $expr = new ActionExpression($this->action_value); + return $expr->evaluate($journal); + } } diff --git a/app/TransactionRules/Actions/AddTag.php b/app/TransactionRules/Actions/AddTag.php index dbe22a8bdb..3381c86277 100644 --- a/app/TransactionRules/Actions/AddTag.php +++ b/app/TransactionRules/Actions/AddTag.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Factory\TagFactory; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -37,16 +35,14 @@ use FireflyIII\User; */ class AddTag implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool @@ -58,7 +54,7 @@ class AddTag implements ActionInterface /** @var User $user */ $user = User::find($journal['user_id']); $factory->setUser($user); - $tagName = $this->expr->evaluate($journal); + $tagName = $this->action->getValue($journal); $tag = $factory->findOrCreate($tagName); if (null === $tag) { @@ -71,7 +67,8 @@ class AddTag implements ActionInterface $count = \DB::table('tag_transaction_journal') ->where('tag_id', $tag->id) ->where('transaction_journal_id', $journal['transaction_journal_id']) - ->count(); + ->count() + ; if (0 === $count) { // add to journal: \DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]); diff --git a/app/TransactionRules/Actions/AppendDescription.php b/app/TransactionRules/Actions/AppendDescription.php index f19927b62f..42d6c58151 100644 --- a/app/TransactionRules/Actions/AppendDescription.php +++ b/app/TransactionRules/Actions/AppendDescription.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -27,29 +26,26 @@ namespace FireflyIII\TransactionRules\Actions; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class AppendDescription. */ class AppendDescription implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); - $description = sprintf('%s %s', $journal['description'], $actionValue); + $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]); // event for audit log entry diff --git a/app/TransactionRules/Actions/AppendNotes.php b/app/TransactionRules/Actions/AppendNotes.php index 8b42680dd9..e584a48251 100644 --- a/app/TransactionRules/Actions/AppendNotes.php +++ b/app/TransactionRules/Actions/AppendNotes.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -28,46 +27,44 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class AppendNotes. */ class AppendNotes implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); $dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id']) ->where('noteable_type', TransactionJournal::class) - ->first(['notes.*']); + ->first(['notes.*']) + ; if (null === $dbNote) { $dbNote = new Note(); $dbNote->noteable_id = (int)$journal['transaction_journal_id']; $dbNote->noteable_type = TransactionJournal::class; $dbNote->text = ''; } - app('log')->debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $actionValue, $dbNote->text)); $before = $dbNote->text; - $text = sprintf('%s%s', $dbNote->text, $actionValue); + $append = $this->action->getValue($journal); + $text = sprintf('%s%s', $dbNote->text, $append); $dbNote->text = $text; $dbNote->save(); /** @var TransactionJournal $object */ $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); + app('log')->debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $append, $before)); event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $text)); return true; diff --git a/app/TransactionRules/Actions/ConvertToDeposit.php b/app/TransactionRules/Actions/ConvertToDeposit.php index dbb8ffe9a8..942df9f6a6 100644 --- a/app/TransactionRules/Actions/ConvertToDeposit.php +++ b/app/TransactionRules/Actions/ConvertToDeposit.php @@ -1,5 +1,4 @@ action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); + $actionValue = $this->action->getValue($journal); // make object from array (so the data is fresh). /** @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) { app('log')->error(sprintf('Cannot find journal #%d, cannot convert to deposit.', $journal['transaction_journal_id'])); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found'))); return false; } - $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); + $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to deposit.', $journal['transaction_group_id'])); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group'))); @@ -76,7 +72,7 @@ class ConvertToDeposit implements ActionInterface } app('log')->debug(sprintf('Convert journal #%d to deposit.', $journal['transaction_journal_id'])); - $type = $object->transactionType->type; + $type = $object->transactionType->type; if (TransactionType::DEPOSIT === $type) { app('log')->error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_deposit'))); @@ -128,7 +124,7 @@ class ConvertToDeposit implements ActionInterface * * @throws FireflyException */ - private function convertWithdrawalArray(TransactionJournal $journal, string $actionValue): bool + private function convertWithdrawalArray(TransactionJournal $journal, string $actionValue = ''): bool { $user = $journal->user; @@ -159,20 +155,23 @@ class ConvertToDeposit implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '<', 0) - ->update(['account_id' => $opposingAccount->id]); + ->update(['account_id' => $opposingAccount->id]) + ; // update the destination transaction and put in the original source account ID. \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '>', 0) - ->update(['account_id' => $sourceAccount->id]); + ->update(['account_id' => $sourceAccount->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]); + ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]) + ; app('log')->debug('Converted withdrawal to deposit.'); @@ -214,7 +213,7 @@ class ConvertToDeposit implements ActionInterface * * @throws FireflyException */ - private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool + private function convertTransferArray(TransactionJournal $journal, string $actionValue = ''): bool { $user = $journal->user; @@ -238,20 +237,22 @@ class ConvertToDeposit implements ActionInterface $opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE); } - app('log')->debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $opposingAccount->name)); + app('log')->debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $actionValue, $opposingAccount->name)); // update source transaction(s) to be revenue account \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '<', 0) - ->update(['account_id' => $opposingAccount->id]); + ->update(['account_id' => $opposingAccount->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]); + ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]) + ; app('log')->debug('Converted transfer to deposit.'); diff --git a/app/TransactionRules/Actions/ConvertToTransfer.php b/app/TransactionRules/Actions/ConvertToTransfer.php index bf9ede733f..f2a0438f74 100644 --- a/app/TransactionRules/Actions/ConvertToTransfer.php +++ b/app/TransactionRules/Actions/ConvertToTransfer.php @@ -1,5 +1,4 @@ action = $action; - $this->expr = $expr; } /** @@ -59,7 +55,7 @@ class ConvertToTransfer implements ActionInterface */ public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); + $accountName = $this->action->getValue($journal); // make object from array (so the data is fresh). /** @var null|TransactionJournal $object */ @@ -108,7 +104,7 @@ class ConvertToTransfer implements ActionInterface $expectedType = $this->getDestinationType($journalId); // Deposit? Replace source with account with same type as destination. } - $opposing = $repository->findByName($actionValue, [$expectedType]); + $opposing = $repository->findByName($accountName, [$expectedType]); if (null === $opposing) { app('log')->error( @@ -116,11 +112,11 @@ class ConvertToTransfer implements ActionInterface 'Journal #%d cannot be converted because no valid %s account with name "%s" exists (rule #%d).', $expectedType, $journalId, - $actionValue, + $accountName, $this->action->rule_id ) ); - event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_valid_opposing', ['name' => $actionValue]))); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_valid_opposing', ['name' => $accountName]))); return false; } @@ -214,14 +210,16 @@ class ConvertToTransfer implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '>', 0) - ->update(['account_id' => $opposing->id]); + ->update(['account_id' => $opposing->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]); + ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]) + ; app('log')->debug('Converted withdrawal to transfer.'); @@ -267,14 +265,16 @@ class ConvertToTransfer implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '<', 0) - ->update(['account_id' => $opposing->id]); + ->update(['account_id' => $opposing->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]); + ->update(['transaction_type_id' => $newType->id, 'bill_id' => null]) + ; app('log')->debug('Converted deposit to transfer.'); diff --git a/app/TransactionRules/Actions/ConvertToWithdrawal.php b/app/TransactionRules/Actions/ConvertToWithdrawal.php index 8af0a67a35..79c18a0333 100644 --- a/app/TransactionRules/Actions/ConvertToWithdrawal.php +++ b/app/TransactionRules/Actions/ConvertToWithdrawal.php @@ -1,5 +1,4 @@ action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); + $actionValue = $this->action->getValue($journal); // make object from array (so the data is fresh). /** @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) { app('log')->error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id'])); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found'))); return false; } - $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); + $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id'])); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group'))); @@ -75,7 +71,7 @@ class ConvertToWithdrawal implements ActionInterface return false; } - $type = $object->transactionType->type; + $type = $object->transactionType->type; if (TransactionType::WITHDRAWAL === $type) { app('log')->error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_withdrawal'))); @@ -123,7 +119,7 @@ class ConvertToWithdrawal implements ActionInterface /** * @throws FireflyException */ - private function convertDepositArray(TransactionJournal $journal, string $actionValue): bool + private function convertDepositArray(TransactionJournal $journal, string $actionValue = ''): bool { $user = $journal->user; @@ -139,7 +135,7 @@ class ConvertToWithdrawal implements ActionInterface // get the action value, or use the original source name in case the action value is empty: // this becomes a new or existing (expense) account, which is the destination of the new withdrawal. - $opposingName = '' === $actionValue ? $sourceAccount->name : $actionValue; + $opposingName = '' === $actionValue ? $sourceAccount->name : $actionValue; // we check all possible source account types if one exists: $validTypes = config('firefly.expected_source_types.destination.Withdrawal'); $opposingAccount = $repository->findByName($opposingName, $validTypes); @@ -153,19 +149,22 @@ class ConvertToWithdrawal implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '<', 0) - ->update(['account_id' => $destAccount->id]); + ->update(['account_id' => $destAccount->id]) + ; // update destination transaction(s) to be new expense account. \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '>', 0) - ->update(['account_id' => $opposingAccount->id]); + ->update(['account_id' => $opposingAccount->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id]); + ->update(['transaction_type_id' => $newType->id]) + ; app('log')->debug('Converted deposit to withdrawal.'); @@ -206,7 +205,7 @@ class ConvertToWithdrawal implements ActionInterface * * @throws FireflyException */ - private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool + private function convertTransferArray(TransactionJournal $journal, string $actionValue = ''): bool { // find or create expense account. $user = $journal->user; @@ -236,13 +235,15 @@ class ConvertToWithdrawal implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $journal->id) ->where('amount', '>', 0) - ->update(['account_id' => $opposingAccount->id]); + ->update(['account_id' => $opposingAccount->id]) + ; // change transaction type of journal: $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); \DB::table('transaction_journals') ->where('id', '=', $journal->id) - ->update(['transaction_type_id' => $newType->id]); + ->update(['transaction_type_id' => $newType->id]) + ; app('log')->debug('Converted transfer to withdrawal.'); diff --git a/app/TransactionRules/Actions/LinkToBill.php b/app/TransactionRules/Actions/LinkToBill.php index 6b9a49ed3a..a073c657e1 100644 --- a/app/TransactionRules/Actions/LinkToBill.php +++ b/app/TransactionRules/Actions/LinkToBill.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -30,7 +29,6 @@ use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Bill\BillRepositoryInterface; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -38,16 +36,14 @@ use FireflyIII\User; */ class LinkToBill implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool @@ -58,12 +54,13 @@ class LinkToBill implements ActionInterface /** @var BillRepositoryInterface $repository */ $repository = app(BillRepositoryInterface::class); $repository->setUser($user); - $billName = $this->expr->evaluate($journal); + $billName = $this->action->getValue($journal); $bill = $repository->findByName($billName); if (null !== $bill && TransactionType::WITHDRAWAL === $journal['transaction_type_type']) { $count = \DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id']) - ->where('bill_id', $bill->id)->count(); + ->where('bill_id', $bill->id)->count() + ; if (0 !== $count) { app('log')->error( sprintf( @@ -79,7 +76,8 @@ class LinkToBill implements ActionInterface \DB::table('transaction_journals') ->where('id', '=', $journal['transaction_journal_id']) - ->update(['bill_id' => $bill->id]); + ->update(['bill_id' => $bill->id]) + ; app('log')->debug( sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal['transaction_journal_id'], $bill->id, $bill->name) ); diff --git a/app/TransactionRules/Actions/PrependDescription.php b/app/TransactionRules/Actions/PrependDescription.php index 2be503f475..47fda2aab5 100644 --- a/app/TransactionRules/Actions/PrependDescription.php +++ b/app/TransactionRules/Actions/PrependDescription.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -27,31 +26,26 @@ namespace FireflyIII\TransactionRules\Actions; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class PrependDescription. */ class PrependDescription implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); - $before = $journal['description']; - $after = sprintf('%s%s', $actionValue, $journal['description']); + $after = sprintf('%s%s', $this->action->getValue($journal), $journal['description']); \DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $after]); // journal diff --git a/app/TransactionRules/Actions/PrependNotes.php b/app/TransactionRules/Actions/PrependNotes.php index a9e9756755..31e455a38b 100644 --- a/app/TransactionRules/Actions/PrependNotes.php +++ b/app/TransactionRules/Actions/PrependNotes.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -28,31 +27,28 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class PrependNotes. */ class PrependNotes implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - $actionValue = $this->expr->evaluate($journal); $dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id']) ->where('noteable_type', TransactionJournal::class) - ->first(['notes.*']); + ->first(['notes.*']) + ; if (null === $dbNote) { $dbNote = new Note(); $dbNote->noteable_id = (int)$journal['transaction_journal_id']; @@ -60,8 +56,9 @@ class PrependNotes implements ActionInterface $dbNote->text = ''; } $before = $dbNote->text; - app('log')->debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $actionValue, $dbNote->text)); - $text = sprintf('%s%s', $actionValue, $dbNote->text); + $after = $this->action->getValue($journal); + app('log')->debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $after, $dbNote->text)); + $text = sprintf('%s%s', $after, $dbNote->text); $dbNote->text = $text; $dbNote->save(); diff --git a/app/TransactionRules/Actions/RemoveTag.php b/app/TransactionRules/Actions/RemoveTag.php index 846bcbb05a..a717043ce2 100644 --- a/app/TransactionRules/Actions/RemoveTag.php +++ b/app/TransactionRules/Actions/RemoveTag.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -28,7 +27,6 @@ use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -36,27 +34,25 @@ use FireflyIII\User; */ class RemoveTag implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { - // if tag does not exist, no need to continue: - $name = $this->expr->evaluate($journal); + $name = $this->action->getValue($journal); /** @var User $user */ $user = User::find($journal['user_id']); $tag = $user->tags()->where('tag', $name)->first(); + // if tag does not exist, no need to continue: if (null === $tag) { app('log')->debug( sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal['transaction_journal_id']) @@ -79,7 +75,8 @@ class RemoveTag implements ActionInterface \DB::table('tag_transaction_journal') ->where('transaction_journal_id', $journal['transaction_journal_id']) ->where('tag_id', $tag->id) - ->delete(); + ->delete() + ; /** @var TransactionJournal $object */ $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); diff --git a/app/TransactionRules/Actions/SetBudget.php b/app/TransactionRules/Actions/SetBudget.php index 880d05bed8..067dc93a49 100644 --- a/app/TransactionRules/Actions/SetBudget.php +++ b/app/TransactionRules/Actions/SetBudget.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -37,23 +35,21 @@ use FireflyIII\User; */ class SetBudget implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { /** @var User $user */ $user = User::find($journal['user_id']); - $search = $this->expr->evaluate($journal); + $search = $this->action->getValue($journal); $budget = $user->budgets()->where('name', $search)->first(); if (null === $budget) { diff --git a/app/TransactionRules/Actions/SetCategory.php b/app/TransactionRules/Actions/SetCategory.php index 1c2ef454e3..760f0e01d5 100644 --- a/app/TransactionRules/Actions/SetCategory.php +++ b/app/TransactionRules/Actions/SetCategory.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Factory\CategoryFactory; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -37,23 +35,21 @@ use FireflyIII\User; */ class SetCategory implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { /** @var null|User $user */ $user = User::find($journal['user_id']); - $search = $this->expr->evaluate($journal); + $search = $this->action->getValue($journal); if (null === $user) { app('log')->error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal'))); diff --git a/app/TransactionRules/Actions/SetDescription.php b/app/TransactionRules/Actions/SetDescription.php index 3d55d2f0fe..f423806e5e 100644 --- a/app/TransactionRules/Actions/SetDescription.php +++ b/app/TransactionRules/Actions/SetDescription.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -27,41 +26,39 @@ namespace FireflyIII\TransactionRules\Actions; use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class SetDescription. */ class SetDescription implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { /** @var TransactionJournal $object */ $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); - $before = $journal['description']; - $after = $this->expr->evaluate($journal); + $before = $object->description; + $after = $this->action->getValue($journal); \DB::table('transaction_journals') ->where('id', '=', $journal['transaction_journal_id']) - ->update(['description' => $this->action->action_value]); + ->update(['description' => $after]) + ; app('log')->debug( sprintf( 'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".', $journal['transaction_journal_id'], - $before, + $journal['description'], $after ) ); diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index 6521e02e94..4b8498de8d 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -32,7 +31,6 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator; use FireflyIII\User; /** @@ -41,21 +39,19 @@ use FireflyIII\User; class SetDestinationAccount implements ActionInterface { private RuleAction $action; - private ActionExpressionEvaluator $evaluator; private AccountRepositoryInterface $repository; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator) + public function __construct(RuleAction $action) { $this->action = $action; - $this->evaluator = $evaluator; } public function actOnArray(array $journal): bool { - $accountName = $this->evaluator->evaluate($journal); + $accountName = $this->action->getValue($journal); /** @var User $user */ $user = User::find($journal['user_id']); @@ -132,7 +128,8 @@ class SetDestinationAccount implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $object->id) ->where('amount', '>', 0) - ->update(['account_id' => $newAccount->id]); + ->update(['account_id' => $newAccount->id]) + ; app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id)); diff --git a/app/TransactionRules/Actions/SetNotes.php b/app/TransactionRules/Actions/SetNotes.php index 6d5c86363d..e8e3e92f2c 100644 --- a/app/TransactionRules/Actions/SetNotes.php +++ b/app/TransactionRules/Actions/SetNotes.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -28,29 +27,27 @@ use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Models\Note; use FireflyIII\Models\RuleAction; use FireflyIII\Models\TransactionJournal; -use FireflyIII\TransactionRules\Expressions\ActionExpression; /** * Class SetNotes. */ class SetNotes implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { $dbNote = Note::where('noteable_id', $journal['transaction_journal_id']) - ->where('noteable_type', TransactionJournal::class)->first(); + ->where('noteable_type', TransactionJournal::class)->first() + ; if (null === $dbNote) { $dbNote = new Note(); $dbNote->noteable_id = $journal['transaction_journal_id']; @@ -58,7 +55,7 @@ class SetNotes implements ActionInterface $dbNote->text = ''; } $oldNotes = $dbNote->text; - $newNotes = $this->expr->evaluate($journal); + $newNotes = $this->action->getValue($journal); $dbNote->text = $newNotes; $dbNote->save(); diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.php index dc195b9d61..9da778d2a6 100644 --- a/app/TransactionRules/Actions/SetSourceAccount.php +++ b/app/TransactionRules/Actions/SetSourceAccount.php @@ -19,7 +19,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - declare(strict_types=1); namespace FireflyIII\TransactionRules\Actions; @@ -32,7 +31,6 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; -use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator; use FireflyIII\User; /** @@ -41,21 +39,19 @@ use FireflyIII\User; class SetSourceAccount implements ActionInterface { private RuleAction $action; - private ActionExpressionEvaluator $evaluator; private AccountRepositoryInterface $repository; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator) + public function __construct(RuleAction $action) { $this->action = $action; - $this->evaluator = $evaluator; } public function actOnArray(array $journal): bool { - $accountName = $this->evaluator->evaluate($journal); + $accountName = $this->action->getValue($journal); /** @var User $user */ $user = User::find($journal['user_id']); @@ -124,7 +120,8 @@ class SetSourceAccount implements ActionInterface \DB::table('transactions') ->where('transaction_journal_id', '=', $object->id) ->where('amount', '<', 0) - ->update(['account_id' => $newAccount->id]); + ->update(['account_id' => $newAccount->id]) + ; event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $newAccount->name)); @@ -133,24 +130,24 @@ class SetSourceAccount implements ActionInterface return true; } - private function findAssetAccount(string $type, string $name): ?Account + private function findAssetAccount(string $type, string $accountName): ?Account { // switch on type: $allowed = config(sprintf('firefly.expected_source_types.source.%s', $type)); $allowed = is_array($allowed) ? $allowed : []; app('log')->debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $allowed); - return $this->repository->findByName($name, $allowed); + return $this->repository->findByName($accountName, $allowed); } - private function findDepositSourceAccount(string $name): Account + private function findDepositSourceAccount(string $accountName): Account { $allowed = config('firefly.expected_source_types.source.Deposit'); - $account = $this->repository->findByName($name, $allowed); + $account = $this->repository->findByName($accountName, $allowed); if (null === $account) { // create new revenue account with this name: $data = [ - 'name' => $name, + 'name' => $accountName, 'account_type_name' => 'revenue', 'account_type_id' => null, 'virtual_balance' => 0, diff --git a/app/TransactionRules/Actions/UpdatePiggybank.php b/app/TransactionRules/Actions/UpdatePiggybank.php index 4498dd3f7e..a978ed4946 100644 --- a/app/TransactionRules/Actions/UpdatePiggybank.php +++ b/app/TransactionRules/Actions/UpdatePiggybank.php @@ -31,7 +31,6 @@ use FireflyIII\Models\RuleAction; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; -use FireflyIII\TransactionRules\Expressions\ActionExpression; use FireflyIII\User; /** @@ -39,23 +38,22 @@ use FireflyIII\User; */ class UpdatePiggybank implements ActionInterface { - private RuleAction $action; - private ActionExpression $expr; + private RuleAction $action; /** * TriggerInterface constructor. */ - public function __construct(RuleAction $action, ActionExpression $expr) + public function __construct(RuleAction $action) { $this->action = $action; - $this->expr = $expr; } public function actOnArray(array $journal): bool { + $actionValue = $this->action->getValue($journal); + app('log')->debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id'])); - $piggyBankName = $this->expr->evaluate($journal); // refresh the transaction type. /** @var User $user */ $user = User::find($journal['user_id']); @@ -63,12 +61,12 @@ class UpdatePiggybank implements ActionInterface /** @var TransactionJournal $journalObj */ $journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']); - $piggyBank = $this->findPiggyBank($user, $piggyBankName); + $piggyBank = $this->findPiggyBank($user, $actionValue); if (null === $piggyBank) { app('log')->info( - sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $piggyBankName, $this->action->id, $this->action->rule_id) + sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $actionValue, $this->action->id, $this->action->rule_id) ); - event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $piggyBankName]))); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $actionValue]))); return false; } @@ -130,7 +128,7 @@ class UpdatePiggybank implements ActionInterface $destination->account_id ) ); - event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $piggyBankName]))); + event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $actionValue]))); return false; } diff --git a/app/TransactionRules/Factory/ActionFactory.php b/app/TransactionRules/Factory/ActionFactory.php index c20120d807..83209926c2 100644 --- a/app/TransactionRules/Factory/ActionFactory.php +++ b/app/TransactionRules/Factory/ActionFactory.php @@ -51,9 +51,7 @@ class ActionFactory $class = self::getActionClass($action->action_type); app('log')->debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class)); - $expr = new ActionExpression($action->action_value); - - return new $class($action, $expr); // @phpstan-ignore-line + return new $class($action); // @phpstan-ignore-line } /**