Skip to content

Firefly III: a personal finances manager

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

James Cole86f14885eb87
diff --git a/app/Api/V1/Controllers/TransactionController.php b/app/Api/V1/Controllers/TransactionController.php
index a14ea77e8b..becaa0af87 100644
--- a/app/Api/V1/Controllers/TransactionController.php
+++ b/app/Api/V1/Controllers/TransactionController.php
@@ -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);
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index ce558d2858..6ffeca74e2 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -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) {
diff --git a/app/Services/Internal/Destroy/JournalDestroyService.php b/app/Services/Internal/Destroy/JournalDestroyService.php
index 148eac82b4..219f8657e9 100644
--- a/app/Services/Internal/Destroy/JournalDestroyService.php
+++ b/app/Services/Internal/Destroy/JournalDestroyService.php
@@ -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
diff --git a/app/TransactionRules/Actions/DeleteTransaction.php b/app/TransactionRules/Actions/DeleteTransaction.php
new file mode 100644
index 0000000000..394537c59b
--- /dev/null
+++ b/app/TransactionRules/Actions/DeleteTransaction.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * DeleteTransaction.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * 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;
+ }
+}
diff --git a/config/firefly.php b/config/firefly.php
index cc2360cf56..7b563c511d 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -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',
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 7bb6ca162b..d7079a1f00 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -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"',