Added a rule action that will delete the transactions it matches.

This commit is contained in:
James Cole
2020-05-16 11:21:26 +02:00
parent 173e196bc8
commit 86f14885eb
6 changed files with 88 additions and 4 deletions

View File

@@ -269,6 +269,7 @@ class TransactionController extends Controller
*
* @param TransactionStoreRequest $request
*
* @throws FireflyException
* @return JsonResponse
*/
public function store(TransactionStoreRequest $request): JsonResponse
@@ -326,7 +327,7 @@ class TransactionController extends Controller
$selectedGroup = $collector->getGroups()->first();
if (null === $selectedGroup) {
throw new NotFoundHttpException(); // @codeCoverageIgnore
throw new FireflyException('Cannot find transaction. Possibly, a rule deleted this transaction after its creation.');
}
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);

View File

@@ -87,7 +87,7 @@ class Handler extends ExceptionHandler
);
}
return response()->json(['message' => 'Internal Firefly III Exception. See log files.', 'exception' => get_class($exception)], 500);
return response()->json(['message' => sprintf('Internal Firefly III Exception: %s', $exception->getMessage()), 'exception' => get_class($exception)], 500);
}
if ($exception instanceof NotFoundHttpException) {

View File

@@ -95,8 +95,6 @@ class JournalDestroyService
// update events
$journal->piggyBankEvents()->update(['transaction_journal_id' => null]);
$journal->delete();
} catch (Exception $e) {
Log::error(sprintf('Could not delete bill: %s', $e->getMessage())); // @codeCoverageIgnore

View File

@@ -0,0 +1,81 @@
<?php
/**
* DeleteTransaction.php
* Copyright (c) 2019 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use Exception;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
use Log;
/**
* Class DeleteTransaction.
*/
class DeleteTransaction implements ActionInterface
{
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
}
/**
* Will delete transaction journal. Also the group if no other journals are in the group.
*
* @param TransactionJournal $journal
*
* @throws Exception
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$count = $journal->transactionGroup->transactionJournals()->count();
// destroy entire group.
if (1 === $count) {
Log::debug(
sprintf(
'RuleAction DeleteTransaction DELETED the entire transaction group of journal #%d ("%s").',
$journal->id, $journal->description
)
);
$service = app(TransactionGroupDestroyService::class);
$service->destroy($journal->transactionGroup);
return true;
}
Log::debug(sprintf('RuleAction DeleteTransaction DELETED transaction journal #%d ("%s").', $journal->id, $journal->description));
// trigger delete factory:
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
$service->destroy($journal);
return true;
}
}

View File

@@ -66,6 +66,7 @@ use FireflyIII\TransactionRules\Actions\ClearNotes;
use FireflyIII\TransactionRules\Actions\ConvertToDeposit;
use FireflyIII\TransactionRules\Actions\ConvertToTransfer;
use FireflyIII\TransactionRules\Actions\ConvertToWithdrawal;
use FireflyIII\TransactionRules\Actions\DeleteTransaction;
use FireflyIII\TransactionRules\Actions\LinkToBill;
use FireflyIII\TransactionRules\Actions\PrependDescription;
use FireflyIII\TransactionRules\Actions\PrependNotes;
@@ -508,6 +509,7 @@ return [
'convert_deposit' => ConvertToDeposit::class,
'convert_transfer' => ConvertToTransfer::class,
'update_piggy' => UpdatePiggybank::class,
'delete_transaction' => DeleteTransaction::class,
],
'context-rule-actions' => [
'set_category',

View File

@@ -455,6 +455,8 @@ return [
'rule_trigger_notes_start' => 'Notes start with ":trigger_value"',
'rule_trigger_notes_end_choice' => 'Notes end with..',
'rule_trigger_notes_end' => 'Notes end with ":trigger_value"',
'rule_action_delete_transaction_choice' => 'DELETE transaction (!)',
'rule_action_delete_transaction' => 'DELETE transaction (!)',
'rule_action_set_category' => 'Set category to ":action_value"',
'rule_action_clear_category' => 'Clear category',
'rule_action_set_budget' => 'Set budget to ":action_value"',