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 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);
}
}

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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;
/**
@@ -38,15 +36,13 @@ use FireflyIII\User;
class AddTag implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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']]);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -27,7 +26,6 @@ namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class AppendDescription.
@@ -35,21 +33,19 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class AppendDescription implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -28,7 +27,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class AppendNotes.
@@ -36,38 +34,37 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class AppendNotes implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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;

View File

@@ -1,5 +1,4 @@
<?php
/**
* ConvertToDeposit.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -35,7 +34,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class ConvertToDeposit
@@ -43,20 +41,18 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class ConvertToDeposit implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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);
$actionValue = $this->action->getValue($journal);
// make object from array (so the data is fresh).
/** @var null|TransactionJournal $object */
@@ -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.');

View File

@@ -1,5 +1,4 @@
<?php
/**
* ConvertToTransfer.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -34,7 +33,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class ConvertToTransfer
@@ -42,15 +40,13 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class ConvertToTransfer implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* TriggerInterface constructor.
*/
public function __construct(RuleAction $action, ActionExpression $expr)
public function __construct(RuleAction $action)
{
$this->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.');

View File

@@ -1,5 +1,4 @@
<?php
/**
* ConvertToWithdrawal.php
* Copyright (c) 2019 james@firefly-iii.org
@@ -35,7 +34,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class ConvertToWithdrawal
@@ -43,20 +41,18 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class ConvertToWithdrawal implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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);
$actionValue = $this->action->getValue($journal);
// make object from array (so the data is fresh).
/** @var null|TransactionJournal $object */
@@ -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;
@@ -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.');

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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;
/**
@@ -39,15 +37,13 @@ use FireflyIII\User;
class LinkToBill implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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)
);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -27,7 +26,6 @@ namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class PrependDescription.
@@ -35,23 +33,19 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class PrependDescription implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -28,7 +27,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class PrependNotes.
@@ -36,23 +34,21 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class PrependNotes implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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();

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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;
/**
@@ -37,26 +35,24 @@ use FireflyIII\User;
class RemoveTag implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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']);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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;
/**
@@ -38,22 +36,20 @@ use FireflyIII\User;
class SetBudget implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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) {

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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;
/**
@@ -38,22 +36,20 @@ use FireflyIII\User;
class SetCategory implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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')));

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -27,7 +26,6 @@ namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class SetDescription.
@@ -35,33 +33,32 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class SetDescription implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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
)
);

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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));

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
@@ -28,7 +27,6 @@ use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Expressions\ActionExpression;
/**
* Class SetNotes.
@@ -36,21 +34,20 @@ use FireflyIII\TransactionRules\Expressions\ActionExpression;
class SetNotes implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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();

View File

@@ -19,7 +19,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\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,

View File

@@ -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;
/**
@@ -40,22 +39,21 @@ use FireflyIII\User;
class UpdatePiggybank implements ActionInterface
{
private RuleAction $action;
private ActionExpression $expr;
/**
* 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;
}

View File

@@ -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
}
/**