refactor: add method on RuleAction to compute action value

This commit is contained in:
Michael Thomas
2024-03-07 17:18:46 -05:00
parent 41fc1e8f82
commit f41397eb43
19 changed files with 138 additions and 177 deletions

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Eloquent; use Eloquent;
use FireflyIII\Support\Models\ReturnsIntegerIdTrait; use FireflyIII\Support\Models\ReturnsIntegerIdTrait;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@@ -93,4 +94,10 @@ class RuleAction extends Model
get: static fn ($value) => (int)$value, get: static fn ($value) => (int)$value,
); );
} }
public function getValue(array $journal)
{
$expr = new ActionExpression($this->action_value);
return $expr->evaluate($journal);
}
} }

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Factory\TagFactory; use FireflyIII\Factory\TagFactory;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -37,16 +35,14 @@ use FireflyIII\User;
*/ */
class AddTag implements ActionInterface class AddTag implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
@@ -58,7 +54,7 @@ class AddTag implements ActionInterface
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
$factory->setUser($user); $factory->setUser($user);
$tagName = $this->expr->evaluate($journal); $tagName = $this->action->getValue($journal);
$tag = $factory->findOrCreate($tagName); $tag = $factory->findOrCreate($tagName);
if (null === $tag) { if (null === $tag) {
@@ -71,7 +67,8 @@ class AddTag implements ActionInterface
$count = \DB::table('tag_transaction_journal') $count = \DB::table('tag_transaction_journal')
->where('tag_id', $tag->id) ->where('tag_id', $tag->id)
->where('transaction_journal_id', $journal['transaction_journal_id']) ->where('transaction_journal_id', $journal['transaction_journal_id'])
->count(); ->count()
;
if (0 === $count) { if (0 === $count) {
// add to journal: // add to journal:
\DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]); \DB::table('tag_transaction_journal')->insert(['tag_id' => $tag->id, 'transaction_journal_id' => $journal['transaction_journal_id']]);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -27,29 +26,26 @@ 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\Expressions\ActionExpression;
/** /**
* Class AppendDescription. * Class AppendDescription.
*/ */
class AppendDescription implements ActionInterface class AppendDescription implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$actionValue = $this->expr->evaluate($journal); $append = $this->action->getValue($journal);
$description = sprintf('%s %s', $journal['description'], $actionValue); $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]);
// event for audit log entry // event for audit log entry

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -28,46 +27,44 @@ 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\Expressions\ActionExpression;
/** /**
* Class AppendNotes. * Class AppendNotes.
*/ */
class AppendNotes implements ActionInterface class AppendNotes implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$actionValue = $this->expr->evaluate($journal);
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id']) $dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
->where('noteable_type', TransactionJournal::class) ->where('noteable_type', TransactionJournal::class)
->first(['notes.*']); ->first(['notes.*'])
;
if (null === $dbNote) { if (null === $dbNote) {
$dbNote = new Note(); $dbNote = new Note();
$dbNote->noteable_id = (int)$journal['transaction_journal_id']; $dbNote->noteable_id = (int)$journal['transaction_journal_id'];
$dbNote->noteable_type = TransactionJournal::class; $dbNote->noteable_type = TransactionJournal::class;
$dbNote->text = ''; $dbNote->text = '';
} }
app('log')->debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $actionValue, $dbNote->text));
$before = $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->text = $text;
$dbNote->save(); $dbNote->save();
/** @var TransactionJournal $object */ /** @var 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']);
app('log')->debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $append, $before));
event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $text)); event(new TriggeredAuditLog($this->action->rule, $object, 'update_notes', $before, $text));
return true; return true;

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* ConvertToDeposit.php * ConvertToDeposit.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2019 james@firefly-iii.org
@@ -35,39 +34,36 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/** /**
* Class ConvertToDeposit * Class ConvertToDeposit
*/ */
class ConvertToDeposit implements ActionInterface class ConvertToDeposit implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool 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). // make object from array (so the data is fresh).
/** @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) {
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to deposit.', $journal['transaction_journal_id'])); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
return false; 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) { if ($groupCount > 1) {
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to deposit.', $journal['transaction_group_id'])); 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'))); 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'])); 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) { if (TransactionType::DEPOSIT === $type) {
app('log')->error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_deposit')));
@@ -128,7 +124,7 @@ class ConvertToDeposit implements ActionInterface
* *
* @throws FireflyException * @throws FireflyException
*/ */
private function convertWithdrawalArray(TransactionJournal $journal, string $actionValue): bool private function convertWithdrawalArray(TransactionJournal $journal, string $actionValue = ''): bool
{ {
$user = $journal->user; $user = $journal->user;
@@ -159,20 +155,23 @@ class ConvertToDeposit implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0) ->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. // update the destination transaction and put in the original source account ID.
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0) ->where('amount', '>', 0)
->update(['account_id' => $sourceAccount->id]); ->update(['account_id' => $sourceAccount->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->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.'); app('log')->debug('Converted withdrawal to deposit.');
@@ -214,7 +213,7 @@ class ConvertToDeposit implements ActionInterface
* *
* @throws FireflyException * @throws FireflyException
*/ */
private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool private function convertTransferArray(TransactionJournal $journal, string $actionValue = ''): bool
{ {
$user = $journal->user; $user = $journal->user;
@@ -238,20 +237,22 @@ class ConvertToDeposit implements ActionInterface
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE); $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 // update source transaction(s) to be revenue account
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0) ->where('amount', '<', 0)
->update(['account_id' => $opposingAccount->id]); ->update(['account_id' => $opposingAccount->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first(); $newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->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.'); app('log')->debug('Converted transfer to deposit.');

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* ConvertToTransfer.php * ConvertToTransfer.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2019 james@firefly-iii.org
@@ -34,23 +33,20 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/** /**
* Class ConvertToTransfer * Class ConvertToTransfer
*/ */
class ConvertToTransfer implements ActionInterface class ConvertToTransfer implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
/** /**
@@ -59,7 +55,7 @@ class ConvertToTransfer implements ActionInterface
*/ */
public function actOnArray(array $journal): bool 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). // make object from array (so the data is fresh).
/** @var null|TransactionJournal $object */ /** @var null|TransactionJournal $object */
@@ -108,7 +104,7 @@ class ConvertToTransfer implements ActionInterface
$expectedType = $this->getDestinationType($journalId); $expectedType = $this->getDestinationType($journalId);
// Deposit? Replace source with account with same type as destination. // Deposit? Replace source with account with same type as destination.
} }
$opposing = $repository->findByName($actionValue, [$expectedType]); $opposing = $repository->findByName($accountName, [$expectedType]);
if (null === $opposing) { if (null === $opposing) {
app('log')->error( 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).', 'Journal #%d cannot be converted because no valid %s account with name "%s" exists (rule #%d).',
$expectedType, $expectedType,
$journalId, $journalId,
$actionValue, $accountName,
$this->action->rule_id $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; return false;
} }
@@ -214,14 +210,16 @@ class ConvertToTransfer implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0) ->where('amount', '>', 0)
->update(['account_id' => $opposing->id]); ->update(['account_id' => $opposing->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); $newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->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.'); app('log')->debug('Converted withdrawal to transfer.');
@@ -267,14 +265,16 @@ class ConvertToTransfer implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0) ->where('amount', '<', 0)
->update(['account_id' => $opposing->id]); ->update(['account_id' => $opposing->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first(); $newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->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.'); app('log')->debug('Converted deposit to transfer.');

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* ConvertToWithdrawal.php * ConvertToWithdrawal.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2019 james@firefly-iii.org
@@ -35,39 +34,36 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/** /**
* Class ConvertToWithdrawal * Class ConvertToWithdrawal
*/ */
class ConvertToWithdrawal implements ActionInterface class ConvertToWithdrawal implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool 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). // make object from array (so the data is fresh).
/** @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) {
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id'])); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
return false; 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) { if ($groupCount > 1) {
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id'])); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
@@ -75,7 +71,7 @@ class ConvertToWithdrawal implements ActionInterface
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
if (TransactionType::WITHDRAWAL === $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)); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_withdrawal')));
@@ -123,7 +119,7 @@ class ConvertToWithdrawal implements ActionInterface
/** /**
* @throws FireflyException * @throws FireflyException
*/ */
private function convertDepositArray(TransactionJournal $journal, string $actionValue): bool private function convertDepositArray(TransactionJournal $journal, string $actionValue = ''): bool
{ {
$user = $journal->user; $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: // 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. // 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: // we check all possible source account types if one exists:
$validTypes = config('firefly.expected_source_types.destination.Withdrawal'); $validTypes = config('firefly.expected_source_types.destination.Withdrawal');
$opposingAccount = $repository->findByName($opposingName, $validTypes); $opposingAccount = $repository->findByName($opposingName, $validTypes);
@@ -153,19 +149,22 @@ class ConvertToWithdrawal implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0) ->where('amount', '<', 0)
->update(['account_id' => $destAccount->id]); ->update(['account_id' => $destAccount->id])
;
// update destination transaction(s) to be new expense account. // update destination transaction(s) to be new expense account.
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0) ->where('amount', '>', 0)
->update(['account_id' => $opposingAccount->id]); ->update(['account_id' => $opposingAccount->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id]); ->update(['transaction_type_id' => $newType->id])
;
app('log')->debug('Converted deposit to withdrawal.'); app('log')->debug('Converted deposit to withdrawal.');
@@ -206,7 +205,7 @@ class ConvertToWithdrawal implements ActionInterface
* *
* @throws FireflyException * @throws FireflyException
*/ */
private function convertTransferArray(TransactionJournal $journal, string $actionValue): bool private function convertTransferArray(TransactionJournal $journal, string $actionValue = ''): bool
{ {
// find or create expense account. // find or create expense account.
$user = $journal->user; $user = $journal->user;
@@ -236,13 +235,15 @@ class ConvertToWithdrawal implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id) ->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0) ->where('amount', '>', 0)
->update(['account_id' => $opposingAccount->id]); ->update(['account_id' => $opposingAccount->id])
;
// change transaction type of journal: // change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); $newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal->id) ->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id]); ->update(['transaction_type_id' => $newType->id])
;
app('log')->debug('Converted transfer to withdrawal.'); app('log')->debug('Converted transfer to withdrawal.');

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -30,7 +29,6 @@ use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -38,16 +36,14 @@ use FireflyIII\User;
*/ */
class LinkToBill implements ActionInterface class LinkToBill implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
@@ -58,12 +54,13 @@ class LinkToBill implements ActionInterface
/** @var BillRepositoryInterface $repository */ /** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class); $repository = app(BillRepositoryInterface::class);
$repository->setUser($user); $repository->setUser($user);
$billName = $this->expr->evaluate($journal); $billName = $this->action->getValue($journal);
$bill = $repository->findByName($billName); $bill = $repository->findByName($billName);
if (null !== $bill && TransactionType::WITHDRAWAL === $journal['transaction_type_type']) { if (null !== $bill && TransactionType::WITHDRAWAL === $journal['transaction_type_type']) {
$count = \DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id']) $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) { if (0 !== $count) {
app('log')->error( app('log')->error(
sprintf( sprintf(
@@ -79,7 +76,8 @@ class LinkToBill implements ActionInterface
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal['transaction_journal_id']) ->where('id', '=', $journal['transaction_journal_id'])
->update(['bill_id' => $bill->id]); ->update(['bill_id' => $bill->id])
;
app('log')->debug( app('log')->debug(
sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal['transaction_journal_id'], $bill->id, $bill->name) sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal['transaction_journal_id'], $bill->id, $bill->name)
); );

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -27,31 +26,26 @@ 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\Expressions\ActionExpression;
/** /**
* Class PrependDescription. * Class PrependDescription.
*/ */
class PrependDescription implements ActionInterface class PrependDescription implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$actionValue = $this->expr->evaluate($journal);
$before = $journal['description']; $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]); \DB::table('transaction_journals')->where('id', $journal['transaction_journal_id'])->limit(1)->update(['description' => $after]);
// journal // journal

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -28,31 +27,28 @@ 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\Expressions\ActionExpression;
/** /**
* Class PrependNotes. * Class PrependNotes.
*/ */
class PrependNotes implements ActionInterface class PrependNotes implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$actionValue = $this->expr->evaluate($journal);
$dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id']) $dbNote = Note::where('noteable_id', (int)$journal['transaction_journal_id'])
->where('noteable_type', TransactionJournal::class) ->where('noteable_type', TransactionJournal::class)
->first(['notes.*']); ->first(['notes.*'])
;
if (null === $dbNote) { if (null === $dbNote) {
$dbNote = new Note(); $dbNote = new Note();
$dbNote->noteable_id = (int)$journal['transaction_journal_id']; $dbNote->noteable_id = (int)$journal['transaction_journal_id'];
@@ -60,8 +56,9 @@ class PrependNotes implements ActionInterface
$dbNote->text = ''; $dbNote->text = '';
} }
$before = $dbNote->text; $before = $dbNote->text;
app('log')->debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $actionValue, $dbNote->text)); $after = $this->action->getValue($journal);
$text = sprintf('%s%s', $actionValue, $dbNote->text); app('log')->debug(sprintf('RuleAction PrependNotes prepended "%s" to "%s".', $after, $dbNote->text));
$text = sprintf('%s%s', $after, $dbNote->text);
$dbNote->text = $text; $dbNote->text = $text;
$dbNote->save(); $dbNote->save();

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -28,7 +27,6 @@ use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
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\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -36,27 +34,25 @@ use FireflyIII\User;
*/ */
class RemoveTag implements ActionInterface class RemoveTag implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
// if tag does not exist, no need to continue: $name = $this->action->getValue($journal);
$name = $this->expr->evaluate($journal);
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
$tag = $user->tags()->where('tag', $name)->first(); $tag = $user->tags()->where('tag', $name)->first();
// if tag does not exist, no need to continue:
if (null === $tag) { if (null === $tag) {
app('log')->debug( app('log')->debug(
sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal['transaction_journal_id']) 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') \DB::table('tag_transaction_journal')
->where('transaction_journal_id', $journal['transaction_journal_id']) ->where('transaction_journal_id', $journal['transaction_journal_id'])
->where('tag_id', $tag->id) ->where('tag_id', $tag->id)
->delete(); ->delete()
;
/** @var TransactionJournal $object */ /** @var 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']);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -37,23 +35,21 @@ use FireflyIII\User;
*/ */
class SetBudget implements ActionInterface class SetBudget implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
$search = $this->expr->evaluate($journal); $search = $this->action->getValue($journal);
$budget = $user->budgets()->where('name', $search)->first(); $budget = $user->budgets()->where('name', $search)->first();
if (null === $budget) { if (null === $budget) {

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -29,7 +28,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Factory\CategoryFactory; use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -37,23 +35,21 @@ use FireflyIII\User;
*/ */
class SetCategory implements ActionInterface class SetCategory implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
/** @var null|User $user */ /** @var null|User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
$search = $this->expr->evaluate($journal); $search = $this->action->getValue($journal);
if (null === $user) { if (null === $user) {
app('log')->error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal); 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'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -27,41 +26,39 @@ 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\Expressions\ActionExpression;
/** /**
* Class SetDescription. * Class SetDescription.
*/ */
class SetDescription implements ActionInterface class SetDescription implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
/** @var TransactionJournal $object */ /** @var 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']);
$before = $journal['description']; $before = $object->description;
$after = $this->expr->evaluate($journal); $after = $this->action->getValue($journal);
\DB::table('transaction_journals') \DB::table('transaction_journals')
->where('id', '=', $journal['transaction_journal_id']) ->where('id', '=', $journal['transaction_journal_id'])
->update(['description' => $this->action->action_value]); ->update(['description' => $after])
;
app('log')->debug( app('log')->debug(
sprintf( sprintf(
'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".', 'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".',
$journal['transaction_journal_id'], $journal['transaction_journal_id'],
$before, $journal['description'],
$after $after
) )
); );

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -32,7 +31,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -41,21 +39,19 @@ use FireflyIII\User;
class SetDestinationAccount implements ActionInterface class SetDestinationAccount implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpressionEvaluator $evaluator;
private AccountRepositoryInterface $repository; private AccountRepositoryInterface $repository;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->evaluator = $evaluator;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$accountName = $this->evaluator->evaluate($journal); $accountName = $this->action->getValue($journal);
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
@@ -132,7 +128,8 @@ class SetDestinationAccount implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $object->id) ->where('transaction_journal_id', '=', $object->id)
->where('amount', '>', 0) ->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)); app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -28,29 +27,27 @@ 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\Expressions\ActionExpression;
/** /**
* Class SetNotes. * Class SetNotes.
*/ */
class SetNotes implements ActionInterface class SetNotes implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$dbNote = Note::where('noteable_id', $journal['transaction_journal_id']) $dbNote = Note::where('noteable_id', $journal['transaction_journal_id'])
->where('noteable_type', TransactionJournal::class)->first(); ->where('noteable_type', TransactionJournal::class)->first()
;
if (null === $dbNote) { if (null === $dbNote) {
$dbNote = new Note(); $dbNote = new Note();
$dbNote->noteable_id = $journal['transaction_journal_id']; $dbNote->noteable_id = $journal['transaction_journal_id'];
@@ -58,7 +55,7 @@ class SetNotes implements ActionInterface
$dbNote->text = ''; $dbNote->text = '';
} }
$oldNotes = $dbNote->text; $oldNotes = $dbNote->text;
$newNotes = $this->expr->evaluate($journal); $newNotes = $this->action->getValue($journal);
$dbNote->text = $newNotes; $dbNote->text = $newNotes;
$dbNote->save(); $dbNote->save();

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
@@ -32,7 +31,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -41,21 +39,19 @@ use FireflyIII\User;
class SetSourceAccount implements ActionInterface class SetSourceAccount implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpressionEvaluator $evaluator;
private AccountRepositoryInterface $repository; private AccountRepositoryInterface $repository;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->evaluator = $evaluator;
} }
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$accountName = $this->evaluator->evaluate($journal); $accountName = $this->action->getValue($journal);
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
@@ -124,7 +120,8 @@ class SetSourceAccount implements ActionInterface
\DB::table('transactions') \DB::table('transactions')
->where('transaction_journal_id', '=', $object->id) ->where('transaction_journal_id', '=', $object->id)
->where('amount', '<', 0) ->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)); event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $newAccount->name));
@@ -133,24 +130,24 @@ class SetSourceAccount implements ActionInterface
return true; return true;
} }
private function findAssetAccount(string $type, string $name): ?Account private function findAssetAccount(string $type, string $accountName): ?Account
{ {
// switch on type: // switch on type:
$allowed = config(sprintf('firefly.expected_source_types.source.%s', $type)); $allowed = config(sprintf('firefly.expected_source_types.source.%s', $type));
$allowed = is_array($allowed) ? $allowed : []; $allowed = is_array($allowed) ? $allowed : [];
app('log')->debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $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'); $allowed = config('firefly.expected_source_types.source.Deposit');
$account = $this->repository->findByName($name, $allowed); $account = $this->repository->findByName($accountName, $allowed);
if (null === $account) { if (null === $account) {
// create new revenue account with this name: // create new revenue account with this name:
$data = [ $data = [
'name' => $name, 'name' => $accountName,
'account_type_name' => 'revenue', 'account_type_name' => 'revenue',
'account_type_id' => null, 'account_type_id' => null,
'virtual_balance' => 0, 'virtual_balance' => 0,

View File

@@ -31,7 +31,6 @@ use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
use FireflyIII\User; use FireflyIII\User;
/** /**
@@ -39,23 +38,22 @@ use FireflyIII\User;
*/ */
class UpdatePiggybank implements ActionInterface class UpdatePiggybank implements ActionInterface
{ {
private RuleAction $action; private RuleAction $action;
private ActionExpression $expr;
/** /**
* TriggerInterface constructor. * TriggerInterface constructor.
*/ */
public function __construct(RuleAction $action, ActionExpression $expr) public function __construct(RuleAction $action)
{ {
$this->action = $action; $this->action = $action;
$this->expr = $expr;
} }
public function actOnArray(array $journal): bool 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'])); app('log')->debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
$piggyBankName = $this->expr->evaluate($journal);
// refresh the transaction type. // refresh the transaction type.
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
@@ -63,12 +61,12 @@ class UpdatePiggybank implements ActionInterface
/** @var TransactionJournal $journalObj */ /** @var TransactionJournal $journalObj */
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']); $journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
$piggyBank = $this->findPiggyBank($user, $piggyBankName); $piggyBank = $this->findPiggyBank($user, $actionValue);
if (null === $piggyBank) { if (null === $piggyBank) {
app('log')->info( 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; return false;
} }
@@ -130,7 +128,7 @@ class UpdatePiggybank implements ActionInterface
$destination->account_id $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; return false;
} }

View File

@@ -51,9 +51,7 @@ class ActionFactory
$class = self::getActionClass($action->action_type); $class = self::getActionClass($action->action_type);
app('log')->debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class)); app('log')->debug(sprintf('self::getActionClass("%s") = "%s"', $action->action_type, $class));
$expr = new ActionExpression($action->action_value); return new $class($action); // @phpstan-ignore-line
return new $class($action, $expr); // @phpstan-ignore-line
} }
/** /**