Add rule error reporting to all rules.

This commit is contained in:
James Cole
2023-08-13 15:01:12 +02:00
parent 965acd6d45
commit b0ab06b7eb
19 changed files with 199 additions and 66 deletions

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* RuleActionFailedOnArray.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*/
namespace FireflyIII\Events\Model\Rule;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Queue\SerializesModels;
/**
* Class RuleActionFailedOnObject
*/
class RuleActionFailedOnObject
{
use SerializesModels;
public string $error;
public TransactionJournal $journal;
public RuleAction $ruleAction;
/**
* @param RuleAction $ruleAction
* @param TransactionJournal $journal
* @param string $error
*/
public function __construct(RuleAction $ruleAction, TransactionJournal $journal, string $error)
{
app('log')->debug('Created new RuleActionFailedOnObject');
$this->ruleAction = $ruleAction;
$this->journal = $journal;
$this->error = $error;
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Handlers\Events\Model; namespace FireflyIII\Handlers\Events\Model;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
use FireflyIII\Notifications\User\RuleActionFailed; use FireflyIII\Notifications\User\RuleActionFailed;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
@@ -37,7 +38,7 @@ class RuleHandler
* *
* @return void * @return void
*/ */
public function ruleActionFailed(RuleActionFailedOnArray $event): void public function ruleActionFailedOnArray(RuleActionFailedOnArray $event): void
{ {
app('log')->debug('Now in ruleActionFailed'); app('log')->debug('Now in ruleActionFailed');
$ruleAction = $event->ruleAction; $ruleAction = $event->ruleAction;
@@ -54,6 +55,31 @@ class RuleHandler
$params = [$mainMessage, $groupTitle, $groupLink, $ruleTitle, $ruleLink]; $params = [$mainMessage, $groupTitle, $groupLink, $ruleTitle, $ruleLink];
Notification::send($user, new RuleActionFailed($params));
}
/**
* @param RuleActionFailedOnObject $event
*
* @return void
*/
public function ruleActionFailedOnObject(RuleActionFailedOnObject $event): void
{
app('log')->debug('Now in ruleActionFailed');
$ruleAction = $event->ruleAction;
$rule = $ruleAction->rule;
$journal = $event->journal;
$error = $event->error;
$user = $ruleAction->rule->user;
$mainMessage = trans('rules.main_message', ['rule' => $rule->title, 'action' => $ruleAction->action_type, 'group' => $journal->transaction_group_id, 'error' => $error]);
$groupTitle = $journal->description ?? '';
$groupLink = route('transactions.show', [$journal->transaction_group_id]);
$ruleTitle = $rule->title;
$ruleLink = route('rules.edit', [$rule->id]);
$params = [$mainMessage, $groupTitle, $groupLink, $ruleTitle, $ruleLink];
Notification::send($user, new RuleActionFailed($params)); Notification::send($user, new RuleActionFailed($params));
} }

View File

@@ -33,6 +33,7 @@ use FireflyIII\Events\Model\BudgetLimit\Created;
use FireflyIII\Events\Model\BudgetLimit\Deleted; use FireflyIII\Events\Model\BudgetLimit\Deleted;
use FireflyIII\Events\Model\BudgetLimit\Updated; use FireflyIII\Events\Model\BudgetLimit\Updated;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
use FireflyIII\Events\NewVersionAvailable; use FireflyIII\Events\NewVersionAvailable;
use FireflyIII\Events\RegisteredUser; use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword; use FireflyIII\Events\RequestedNewPassword;
@@ -173,7 +174,10 @@ class EventServiceProvider extends ServiceProvider
// rule actions // rule actions
RuleActionFailedOnArray::class => [ RuleActionFailedOnArray::class => [
'FireflyIII\Handlers\Events\Model\RuleHandler@ruleActionFailed', 'FireflyIII\Handlers\Events\Model\RuleHandler@ruleActionFailedOnArray',
],
RuleActionFailedOnObject::class => [
'FireflyIII\Handlers\Events\Model\RuleHandler@ruleActionFailedOnObject',
], ],
]; ];

View File

@@ -82,7 +82,7 @@ class AppendNotesToDescription implements ActionInterface
return true; return true;
} }
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.new_notes_empty')));
return false; return false;
} }

View File

@@ -115,7 +115,7 @@ class ConvertToDeposit implements ActionInterface
return $res; return $res;
} }
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_deposit', ['type' => $type])));
return false; return false;
} }

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
@@ -84,12 +85,15 @@ class ConvertToTransfer implements ActionInterface
return false; return false;
} }
if (TransactionType::DEPOSIT !== $type && TransactionType::WITHDRAWAL !== $type) {
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
return false;
}
// find the asset account in the action value. // find the asset account in the action value.
/** @var AccountRepositoryInterface $repository */ /** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class); $repository = app(AccountRepositoryInterface::class);
$repository->setUser($user); $repository->setUser($user);
$opposing = null;
$expectedType = null; $expectedType = null;
if (TransactionType::WITHDRAWAL === $type) { if (TransactionType::WITHDRAWAL === $type) {
$expectedType = $this->getSourceType($journalId); $expectedType = $this->getSourceType($journalId);
@@ -126,7 +130,9 @@ class ConvertToTransfer implements ActionInterface
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
return false; return false;
} }
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER)); if (false !== $res) {
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER));
}
return $res; return $res;
} }
if (TransactionType::DEPOSIT === $type) { if (TransactionType::DEPOSIT === $type) {
@@ -139,10 +145,12 @@ class ConvertToTransfer implements ActionInterface
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
return false; return false;
} }
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER)); if (false !== $res) {
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER));
}
return $res; return $res;
} }
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
return false; return false;
} }
@@ -173,7 +181,6 @@ class ConvertToTransfer implements ActionInterface
$journal = TransactionJournal::find($journalId); $journal = TransactionJournal::find($journalId);
if (null === $journal) { if (null === $journal) {
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId)); Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
// TODO introduce error
return ''; return '';
} }
return (string)$journal->transactions()->where('amount', '>', 0)->first()?->account?->accountType?->type; return (string)$journal->transactions()->where('amount', '>', 0)->first()?->account?->accountType?->type;
@@ -200,7 +207,7 @@ class ConvertToTransfer implements ActionInterface
[$journal->id, $opposing->name, $this->action->rule_id] [$journal->id, $opposing->name, $this->action->rule_id]
) )
); );
// TODO introduce error event(new RuleActionFailedOnObject($this->action, $journal, trans('rules.already_has_source_asset', ['name' => $opposing->name])));
return false; return false;
} }
@@ -259,7 +266,7 @@ class ConvertToTransfer implements ActionInterface
[$journal->id, $opposing->name, $this->action->rule_id] [$journal->id, $opposing->name, $this->action->rule_id]
) )
); );
// TODO introduce error event(new RuleActionFailedOnObject($this->action, $journal, trans('rules.already_has_destination_asset', ['name' => $opposing->name])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory; use FireflyIII\Factory\AccountFactory;
@@ -65,23 +66,26 @@ class ConvertToWithdrawal implements ActionInterface
$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) {
Log::error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id'])); Log::error(sprintf('Cannot find journal #%d, cannot convert to withdrawal.', $journal['transaction_journal_id']));
// TODO introduce error 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) {
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id'])); Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to withdrawal.', $journal['transaction_group_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
if (TransactionType::WITHDRAWAL === $type) { if (TransactionType::WITHDRAWAL === $type) {
Log::error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)); Log::error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_withdrawal')));
return false;
}
if (TransactionType::DEPOSIT !== $type && TransactionType::TRANSFER !== $type) {
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_withdrawal', ['type' => $type])));
return false; return false;
} }
if (TransactionType::DEPOSIT === $type) { if (TransactionType::DEPOSIT === $type) {
Log::debug('Going to transform a deposit to a withdrawal.'); Log::debug('Going to transform a deposit to a withdrawal.');
try { try {
@@ -89,7 +93,7 @@ class ConvertToWithdrawal implements ActionInterface
} catch (JsonException | FireflyException $e) { } catch (JsonException | FireflyException $e) {
Log::debug('Could not convert transfer to deposit.'); Log::debug('Could not convert transfer to deposit.');
Log::error($e->getMessage()); Log::error($e->getMessage());
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
return false; return false;
} }
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::WITHDRAWAL)); event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::WITHDRAWAL));
@@ -104,14 +108,14 @@ class ConvertToWithdrawal implements ActionInterface
} catch (JsonException | FireflyException $e) { } catch (JsonException | FireflyException $e) {
Log::debug('Could not convert transfer to deposit.'); Log::debug('Could not convert transfer to deposit.');
Log::error($e->getMessage()); Log::error($e->getMessage());
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
return false; return false;
} }
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::WITHDRAWAL)); event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::TRANSFER, TransactionType::WITHDRAWAL));
return $res; return $res;
} }
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_withdrawal', ['type' => $type])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
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;
@@ -73,7 +74,7 @@ class LinkToBill implements ActionInterface
$billName $billName
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_subscription', ['name' => $billName])));
return false; return false;
} }
@@ -98,7 +99,7 @@ class LinkToBill implements ActionInterface
$billName $billName
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_subscription', ['name' => $billName])));
return false; return false;
} }
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Note; use FireflyIII\Models\Note;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
@@ -57,7 +58,7 @@ class MoveDescriptionToNotes implements ActionInterface
$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) {
Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user')));
return false; return false;
} }
$note = $object->notes()->first(); $note = $object->notes()->first();

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
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;
@@ -63,19 +64,19 @@ class MoveNotesToDescription implements ActionInterface
$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) {
Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id'])); Log::error(sprintf('No journal #%d belongs to user #%d.', $journal['transaction_journal_id'], $journal['user_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_other_user')));
return false; return false;
} }
$note = $object->notes()->first(); $note = $object->notes()->first();
if (null === $note) { if (null === $note) {
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_notes_to_move')));
// nothing to move, return null // nothing to move, return null
// TODO introduce error
return false; return false;
} }
if ('' === $note->text) { if ('' === $note->text) {
// nothing to move, return null // nothing to move, return null
$note->delete(); $note->delete();
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_notes_to_move')));
return false; return false;
} }
$before = $object->description; $before = $object->description;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
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;
@@ -55,7 +56,7 @@ class RemoveAllTags implements ActionInterface
$count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->count(); $count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->count();
if (0 === $count) { if (0 === $count) {
Log::debug(sprintf('RuleAction RemoveAllTags, journal #%d has no tags.', $journal['transaction_journal_id'])); Log::debug(sprintf('RuleAction RemoveAllTags, journal #%d has no tags.', $journal['transaction_journal_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_tags_to_remove')));
return false; return false;
} }
Log::debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id'])); Log::debug(sprintf('RuleAction RemoveAllTags removed all tags from journal %d.', $journal['transaction_journal_id']));

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
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;
@@ -61,7 +62,7 @@ class RemoveTag implements ActionInterface
Log::debug( 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'])
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_tag', ['tag' => $name])));
return false; return false;
} }
$count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->where('tag_id', $tag->id)->count(); $count = DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal['transaction_journal_id'])->where('tag_id', $tag->id)->count();
@@ -69,7 +70,7 @@ class RemoveTag implements ActionInterface
Log::debug( Log::debug(
sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag is linked.', $name, $journal['transaction_journal_id']) sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag is linked.', $name, $journal['transaction_journal_id'])
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_unlink_tag', ['tag' => $name])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
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;
@@ -65,7 +66,7 @@ class SetBudget implements ActionInterface
$search $search
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_budget', ['name' => $search])));
return false; return false;
} }
@@ -78,7 +79,7 @@ class SetBudget implements ActionInterface
$journal['transaction_type_type'] $journal['transaction_type_type']
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_set_budget', ['type' => $journal['transaction_type_type'], 'name' => $search])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Factory\CategoryFactory; use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
@@ -57,7 +58,7 @@ class SetCategory implements ActionInterface
$search = $this->action->action_value; $search = $this->action->action_value;
if (null === $user) { if (null === $user) {
Log::error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal); Log::error(sprintf('Journal has no valid user ID so action SetCategory("%s") cannot be applied', $search), $journal);
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
return false; return false;
} }
@@ -73,7 +74,7 @@ class SetCategory implements ActionInterface
$search $search
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_category', ['name' => $search])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
@@ -65,7 +66,7 @@ class SetDestinationAccount implements ActionInterface
if (null === $object) { if (null === $object) {
Log::error('Could not find journal.'); Log::error('Could not find journal.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
@@ -81,7 +82,7 @@ class SetDestinationAccount implements ActionInterface
$this->action->action_value $this->action->action_value
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $this->action->action_value])));
return false; return false;
} }
@@ -90,13 +91,13 @@ class SetDestinationAccount implements ActionInterface
$source = $object->transactions()->where('amount', '<', 0)->first(); $source = $object->transactions()->where('amount', '<', 0)->first();
if (null === $source) { if (null === $source) {
Log::error('Could not find source transaction.'); Log::error('Could not find source transaction.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction')));
return false; return false;
} }
// account must not be deleted (in the meantime): // account must not be deleted (in the meantime):
if (null === $source->account) { if (null === $source->account) {
Log::error('Could not find source transaction account.'); Log::error('Could not find source transaction account.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction_account')));
return false; return false;
} }
if (null !== $newAccount && (int)$newAccount->id === (int)$source->account_id) { if (null !== $newAccount && (int)$newAccount->id === (int)$source->account_id) {
@@ -107,7 +108,8 @@ class SetDestinationAccount implements ActionInterface
$source->account_id $source->account_id
) )
); );
// TODO introduce error
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_destination', ['name' => $newAccount->name])));
return false; return false;
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
@@ -64,7 +65,7 @@ class SetSourceAccount implements ActionInterface
$this->repository = app(AccountRepositoryInterface::class); $this->repository = app(AccountRepositoryInterface::class);
if (null === $object) { if (null === $object) {
Log::error('Could not find journal.'); Log::error('Could not find journal.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
@@ -76,7 +77,7 @@ class SetSourceAccount implements ActionInterface
Log::error( Log::error(
sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value) sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value)
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $this->action->action_value])));
return false; return false;
} }
@@ -85,13 +86,13 @@ class SetSourceAccount implements ActionInterface
$destination = $object->transactions()->where('amount', '>', 0)->first(); $destination = $object->transactions()->where('amount', '>', 0)->first();
if (null === $destination) { if (null === $destination) {
Log::error('Could not find destination transaction.'); Log::error('Could not find destination transaction.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction')));
return false; return false;
} }
// account must not be deleted (in the meantime): // account must not be deleted (in the meantime):
if (null === $destination->account) { if (null === $destination->account) {
Log::error('Could not find destination transaction account.'); Log::error('Could not find destination transaction account.');
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction_account')));
return false; return false;
} }
if (null !== $newAccount && (int)$newAccount->id === (int)$destination->account_id) { if (null !== $newAccount && (int)$newAccount->id === (int)$destination->account_id) {
@@ -102,12 +103,12 @@ class SetSourceAccount implements ActionInterface
$destination->account_id $destination->account_id
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $newAccount->name])));
return false; return false;
} }
// if this is a deposit, the new source account must be a revenue account and may be created: // if this is a deposit, the new source account must be a revenue account and may be created:
// or its a liability // or it's a liability
if (TransactionType::DEPOSIT === $type) { if (TransactionType::DEPOSIT === $type) {
$newAccount = $this->findDepositSourceAccount(); $newAccount = $this->findDepositSourceAccount();
} }

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use DB; use DB;
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\Transaction; use FireflyIII\Models\Transaction;
@@ -59,20 +60,20 @@ class SwitchAccounts implements ActionInterface
$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) {
Log::error(sprintf('Cannot find journal #%d, cannot switch accounts.', $journal['transaction_journal_id'])); Log::error(sprintf('Cannot find journal #%d, cannot switch accounts.', $journal['transaction_journal_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
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) {
Log::error(sprintf('Group #%d has more than one transaction in it, cannot switch accounts.', $journal['transaction_group_id'])); Log::error(sprintf('Group #%d has more than one transaction in it, cannot switch accounts.', $journal['transaction_group_id']));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
if (TransactionType::TRANSFER !== $type) { if (TransactionType::TRANSFER !== $type) {
Log::error(sprintf('Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id)); Log::error(sprintf('Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_not_transfer')));
return false; return false;
} }
@@ -82,7 +83,7 @@ class SwitchAccounts implements ActionInterface
$destTransaction = $object->transactions()->where('amount', '>', 0)->first(); $destTransaction = $object->transactions()->where('amount', '>', 0)->first();
if (null === $sourceTransaction || null === $destTransaction) { if (null === $sourceTransaction || null === $destTransaction) {
Log::error(sprintf('Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id)); Log::error(sprintf('Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.', $journal['transaction_journal_id'], $this->action->rule_id));
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_accounts')));
return false; return false;
} }
$sourceAccountId = (int)$sourceTransaction->account_id; $sourceAccountId = (int)$sourceTransaction->account_id;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions; namespace FireflyIII\TransactionRules\Actions;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\TriggeredAuditLog; use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleAction;
@@ -61,16 +62,15 @@ class UpdatePiggybank implements ActionInterface
// refresh the transaction type. // refresh the transaction type.
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
/** @var TransactionJournal $journalObj */ /** @var TransactionJournal $journalObj */
$journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']); $journalObj = $user->transactionJournals()->find($journal['transaction_journal_id']);
$type = TransactionType::find((int)$journalObj->transaction_type_id); $type = TransactionType::find((int)$journalObj->transaction_type_id);
$journal['transaction_type_type'] = $type->type;
$piggyBank = $this->findPiggyBank($user); $piggyBank = $this->findPiggyBank($user);
if (null === $piggyBank) { if (null === $piggyBank) {
Log::info( Log::info(
sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id) sprintf('No piggy bank named "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id)
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_piggy', ['name' => $this->action->action_value])));
return false; return false;
} }
@@ -130,7 +130,7 @@ class UpdatePiggybank implements ActionInterface
$destination->account_id $destination->account_id
) )
); );
// TODO introduce error event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_link_piggy', ['name' => $this->action->action_value])));
return false; return false;
} }

View File

@@ -23,19 +23,47 @@ declare(strict_types=1);
return [ return [
'main_message' => 'Action ":action", present in rule ":rule", could not be applied to transaction #:group: :error', 'main_message' => 'Action ":action", present in rule ":rule", could not be applied to transaction #:group: :error',
'find_or_create_tag_failed' => 'Could not find or create tag ":tag"', 'find_or_create_tag_failed' => 'Could not find or create tag ":tag"',
'tag_already_added' => 'Tag ":tag" is already linked to this transaction.', 'tag_already_added' => 'Tag ":tag" is already linked to this transaction',
'inspect_transaction' => 'Inspect transaction ":title" @ Firefly III', 'inspect_transaction' => 'Inspect transaction ":title" @ Firefly III',
'inspect_rule' => 'Inspect rule ":title" @ Firefly III', 'inspect_rule' => 'Inspect rule ":title" @ Firefly III',
'journal_other_user' => 'This transaction doesn\'t belong to the user', 'journal_other_user' => 'This transaction doesn\'t belong to the user',
'journal_already_no_budget' => 'This transaction has no budget, so it cannot be removed.', 'no_such_journal' => 'This transaction doesn\'t exist',
'journal_already_no_category' => 'This transaction had no category, so it cannot be removed', 'journal_already_no_budget' => 'This transaction has no budget, so it cannot be removed',
'journal_already_no_notes' => 'This transaction had no notes, so they cannot be removed', 'journal_already_no_category' => 'This transaction had no category, so it cannot be removed',
'journal_not_found' => 'Firefly III can\'t find the requested transaction.', 'journal_already_no_notes' => 'This transaction had no notes, so they cannot be removed',
'split_group' => 'Firefly III cannot execute this action on a transaction with multiple splits.', 'journal_not_found' => 'Firefly III can\'t find the requested transaction',
'is_already_deposit' => 'This transaction is already a deposit.', 'split_group' => 'Firefly III cannot execute this action on a transaction with multiple splits',
'is_already_transfer' => 'This transaction is already a transfer.', 'is_already_withdrawal' => 'This transaction is already a withdrawal',
'complex_error' => 'Something complicated went wrong. Please inspect the logs of Firefly III.', 'is_already_deposit' => 'This transaction is already a deposit',
'no_valid_opposing' => 'Conversion failed because there is no valid account named ":account".', 'is_already_transfer' => 'This transaction is already a transfer',
'is_not_transfer' => 'This transaction is not a transfer',
'complex_error' => 'Something complicated went wrong. Sorry about that. Please inspect the logs of Firefly III',
'no_valid_opposing' => 'Conversion failed because there is no valid account named ":account"',
'new_notes_empty' => 'The notes to be set are empty',
'unsupported_transaction_type_withdrawal' => 'Firefly III cannot convert a ":type" to a withdrawal',
'unsupported_transaction_type_deposit' => 'Firefly III cannot convert a ":type" to a deposit',
'unsupported_transaction_type_transfer' => 'Firefly III cannot convert a ":type" to a transfer',
'already_has_source_asset' => 'This transaction already has ":name" as the source asset account',
'already_has_destination_asset' => 'This transaction already has ":name" as the destination asset account',
'already_has_destination' => 'This transaction already has ":name" as the destination account',
'already_has_source' => 'This transaction already has ":name" as the source account',
'already_linked_to_subscription' => 'The transaction is already linked to subscription ":name"',
'cannot_find_subscription' => 'Firefly III can\'t find subscription ":name"',
'no_notes_to_move' => 'The transaction has no notes to move to the description field',
'no_tags_to_remove' => 'The transaction has no tags to remove',
'cannot_find_tag' => 'Firefly III can\'t find tag ":tag"',
'cannot_find_asset' => 'Firefly III can\'t find asset account ":name"',
'cannot_find_accounts' => 'Firefly III can\'t find the source or destination account',
'cannot_find_source_transaction' => 'Firefly III can\'t find the source transaction',
'cannot_find_destination_transaction' => 'Firefly III can\'t find the destination transaction',
'cannot_find_source_transaction_account' => 'Firefly III can\'t find the source transaction account',
'cannot_find_destination_transaction_account' => 'Firefly III can\'t find the destination transaction account',
'cannot_find_piggy' => 'Firefly III can\'t find a piggy bank named ":name"',
'no_link_piggy' => 'This transaction\'s accounts are not linked to the piggy bank, so no action will be taken',
'cannot_unlink_tag' => 'Tag ":tag" isn\'t linked to this transaction',
'cannot_find_budget' => 'Firefly III can\'t find budget ":name"',
'cannot_find_category' => 'Firefly III can\'t find category ":name"',
'cannot_set_budget' => 'Firefly III can\'t set budget ":name" to a transaction of type ":type"',
]; ];