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

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