Skip to content

Firefly III: a personal finances manager

Migrate PiggyBankAmountIsChanged handler to listener #11544

James Cole968da83c6736
diff --git a/app/Handlers/Events/Model/PiggyBankEventHandler.php b/app/Handlers/Events/Model/PiggyBankEventHandler.php
index e4f0f3946b..600e8b4e93 100644
--- a/app/Handlers/Events/Model/PiggyBankEventHandler.php
+++ b/app/Handlers/Events/Model/PiggyBankEventHandler.php
@@ -56,35 +56,5 @@ class PiggyBankEventHandler
}
}
- public function changePiggyAmount(PiggyBankAmountIsChanged $event): void
- {
- // find journal if group is present.
- $journal = $event->transactionJournal;
- if ($event->transactionGroup instanceof TransactionGroup) {
- $journal = $event->transactionGroup->transactionJournals()->first();
- }
- $date = $journal->date ?? today(config('app.timezone'));
- // sanity check: event must not already exist for this journal and piggy bank.
- if (null !== $journal) {
- $exists = PiggyBankEvent::where('piggy_bank_id', $event->piggyBank->id)
- ->where('transaction_journal_id', $journal->id)
- ->exists()
- ;
- if ($exists) {
- Log::warning('Already have event for this journal and piggy, will not create another.');
-
- return;
- }
- }
- PiggyBankEvent::create(
- [
- 'piggy_bank_id' => $event->piggyBank->id,
- 'transaction_journal_id' => $journal?->id,
- 'date' => $date->format('Y-m-d'),
- 'date_tz' => $date->format('e'),
- 'amount' => $event->amount,
- ]
- );
- }
}
diff --git a/app/Listeners/Model/PiggyBank/CreatesPiggyBankEventForChangedAmount.php b/app/Listeners/Model/PiggyBank/CreatesPiggyBankEventForChangedAmount.php
new file mode 100644
index 0000000000..92195e1c35
--- /dev/null
+++ b/app/Listeners/Model/PiggyBank/CreatesPiggyBankEventForChangedAmount.php
@@ -0,0 +1,62 @@
+<?php
+/*
+ * CreatesPiggyBankEventForChangedAmount.php
+ * Copyright (c) 2026 [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/>.
+ */
+
+namespace FireflyIII\Listeners\Model\PiggyBank;
+
+use FireflyIII\Events\Model\PiggyBank\PiggyBankAmountIsChanged;
+use FireflyIII\Models\PiggyBankEvent;
+use FireflyIII\Models\TransactionGroup;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Support\Facades\Log;
+
+class CreatesPiggyBankEventForChangedAmount implements ShouldQueue
+{
+ public function handle(PiggyBankAmountIsChanged $event): void
+ {
+ // find journal if group is present.
+ $journal = $event->transactionJournal;
+ if ($event->transactionGroup instanceof TransactionGroup) {
+ $journal = $event->transactionGroup->transactionJournals()->first();
+ }
+ $date = $journal->date ?? today(config('app.timezone'));
+ // sanity check: event must not already exist for this journal and piggy bank.
+ if (null !== $journal) {
+ $exists = PiggyBankEvent::where('piggy_bank_id', $event->piggyBank->id)
+ ->where('transaction_journal_id', $journal->id)
+ ->exists();
+ if ($exists) {
+ Log::warning('Already have event for this journal and piggy, will not create another.');
+
+ return;
+ }
+ }
+
+ PiggyBankEvent::create(
+ [
+ 'piggy_bank_id' => $event->piggyBank->id,
+ 'transaction_journal_id' => $journal?->id,
+ 'date' => $date->format('Y-m-d'),
+ 'date_tz' => $date->format('e'),
+ 'amount' => $event->amount,
+ ]
+ );
+ }
+}
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 9721eeb33c..7187bea977 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -27,7 +27,6 @@ use FireflyIII\Events\ActuallyLoggedIn;
use FireflyIII\Events\Admin\InvitationCreated;
use FireflyIII\Events\DestroyedTransactionGroup;
use FireflyIII\Events\DetectedNewIPAddress;
-use FireflyIII\Events\Model\PiggyBank\PiggyBankAmountIsChanged;
use FireflyIII\Events\Model\PiggyBank\ChangedName;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
@@ -171,9 +170,9 @@ class EventServiceProvider extends ServiceProvider
'FireflyIII\Handlers\Events\AuditEventHandler@storeAuditEvent',
],
// piggy bank related events:
- PiggyBankAmountIsChanged::class => [
- 'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changePiggyAmount',
- ],
+ // PiggyBankAmountIsChanged::class => [
+ // 'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changePiggyAmount',
+ // ],
ChangedName::class => [
'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changedPiggyBankName',
],