Move more events #11544

This commit is contained in:
James Cole
2026-01-24 13:52:35 +01:00
parent 92e985a9b8
commit 844470bf08
35 changed files with 418 additions and 394 deletions

View File

@@ -27,6 +27,7 @@ namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Enums\AccountTypeEnum;
use FireflyIII\Enums\TransactionTypeEnum;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\TransactionGroup\TransactionGroupRequestsAuditLogEntry;
use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
@@ -57,14 +58,14 @@ class ConvertToWithdrawal implements ActionInterface
// 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) {
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) {
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')));
@@ -72,7 +73,7 @@ class ConvertToWithdrawal implements ActionInterface
return false;
}
$type = $object->transactionType->type;
$type = $object->transactionType->type;
if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
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')));
@@ -97,7 +98,7 @@ class ConvertToWithdrawal implements ActionInterface
return false;
}
event(
new TriggeredAuditLog(
new TransactionGroupRequestsAuditLogEntry(
$this->action->rule,
$object,
'update_transaction_type',
@@ -121,7 +122,7 @@ class ConvertToWithdrawal implements ActionInterface
return false;
}
event(
new TriggeredAuditLog(
new TransactionGroupRequestsAuditLogEntry(
$this->action->rule,
$object,
'update_transaction_type',
@@ -138,21 +139,21 @@ class ConvertToWithdrawal implements ActionInterface
*/
private function convertDepositArray(TransactionJournal $journal, string $actionValue = ''): bool
{
$user = $journal->user;
$user = $journal->user;
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory = app(AccountFactory::class);
$factory->setUser($user);
$repository = app(AccountRepositoryInterface::class);
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
$sourceAccount = $this->getSourceAccount($journal);
$destAccount = $this->getDestinationAccount($journal);
$sourceAccount = $this->getSourceAccount($journal);
$destAccount = $this->getDestinationAccount($journal);
// 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);
@@ -164,20 +165,18 @@ class ConvertToWithdrawal implements ActionInterface
// update source transaction(s) to be the original destination account
DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->update(['account_id' => $destAccount->id])
;
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->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])
;
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0)
->update(['account_id' => $opposingAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionTypeEnum::WITHDRAWAL->value)->first();
$newType = TransactionType::whereType(TransactionTypeEnum::WITHDRAWAL->value)->first();
DB::table('transaction_journals')->where('id', '=', $journal->id)->update(['transaction_type_id' => $newType->id]);
Log::debug('Converted deposit to withdrawal.');
@@ -222,20 +221,20 @@ class ConvertToWithdrawal implements ActionInterface
private function convertTransferArray(TransactionJournal $journal, string $actionValue = ''): bool
{
// find or create expense account.
$user = $journal->user;
$user = $journal->user;
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory = app(AccountFactory::class);
$factory->setUser($user);
$repository = app(AccountRepositoryInterface::class);
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
$destAccount = $this->getDestinationAccount($journal);
$destAccount = $this->getDestinationAccount($journal);
// 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 ? $destAccount->name : $actionValue;
$opposingName = '' === $actionValue ? $destAccount->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);
@@ -247,13 +246,12 @@ class ConvertToWithdrawal implements ActionInterface
// 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])
;
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '>', 0)
->update(['account_id' => $opposingAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionTypeEnum::WITHDRAWAL->value)->first();
$newType = TransactionType::whereType(TransactionTypeEnum::WITHDRAWAL->value)->first();
DB::table('transaction_journals')->where('id', '=', $journal->id)->update(['transaction_type_id' => $newType->id]);
Log::debug('Converted transfer to withdrawal.');