Skip to content

Firefly III: a personal finances manager

Remove piggy bank event handler and replace name event. #11544

James Cole5de88a70f8f2
diff --git a/app/Events/Model/PiggyBank/PiggyBankNameIsChanged.php b/app/Events/Model/PiggyBank/PiggyBankNameIsChanged.php
new file mode 100644
index 0000000000..269352005d
--- /dev/null
+++ b/app/Events/Model/PiggyBank/PiggyBankNameIsChanged.php
@@ -0,0 +1,36 @@
+<?php
+/*
+ * PiggyBankNameIsChanged.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\Events\Model\PiggyBank;
+
+use FireflyIII\Events\Event;
+use FireflyIII\Models\PiggyBank;
+use Illuminate\Queue\SerializesModels;
+
+/**
+ * Needs to be an event because system needs old value as well as the new value.
+ */
+class PiggyBankNameIsChanged extends Event
+{
+ use SerializesModels;
+
+ public function __construct(public PiggyBank $piggyBank, public string $oldName, public string $newName) {}
+}
diff --git a/app/Handlers/Events/Model/PiggyBankEventHandler.php b/app/Listeners/Model/PiggyBank/UpdatesRulesForChangedPiggyBankName.php
similarity index 75%
rename from app/Handlers/Events/Model/PiggyBankEventHandler.php
rename to app/Listeners/Model/PiggyBank/UpdatesRulesForChangedPiggyBankName.php
index 600e8b4e93..a4d458b9c9 100644
--- a/app/Handlers/Events/Model/PiggyBankEventHandler.php
+++ b/app/Listeners/Model/PiggyBank/UpdatesRulesForChangedPiggyBankName.php
@@ -1,8 +1,7 @@
<?php
-
/*
- * PiggyBankEventHandler.php
- * Copyright (c) 2023 [email protected]
+ * UpdatesRulesForChangedPiggyBankName.php
+ * Copyright (c) 2026 [email protected]
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@@ -20,25 +19,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-declare(strict_types=1);
-
-namespace FireflyIII\Handlers\Events\Model;
+namespace FireflyIII\Listeners\Model\PiggyBank;
-use FireflyIII\Events\Model\PiggyBank\PiggyBankAmountIsChanged;
-use FireflyIII\Events\Model\PiggyBank\ChangedName;
+use FireflyIII\Events\Model\PiggyBank\PiggyBankNameIsChanged;
use FireflyIII\Models\Account;
-use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleAction;
-use FireflyIII\Models\TransactionGroup;
-use Illuminate\Support\Facades\Log;
+use Illuminate\Contracts\Queue\ShouldQueue;
-/**
- * Class PiggyBankEventHandler
- */
-class PiggyBankEventHandler
+class UpdatesRulesForChangedPiggyBankName implements ShouldQueue
{
- public function changedPiggyBankName(ChangedName $event): void
+ public function handle(PiggyBankNameIsChanged $event): void
{
// loop all accounts, collect all user's rules.
/** @var Account $account */
@@ -56,5 +47,4 @@ class PiggyBankEventHandler
}
}
-
}
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 7187bea977..71fcfd9357 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -173,9 +173,9 @@ class EventServiceProvider extends ServiceProvider
// PiggyBankAmountIsChanged::class => [
// 'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changePiggyAmount',
// ],
- ChangedName::class => [
- 'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changedPiggyBankName',
- ],
+// ChangedName::class => [
+// 'FireflyIII\Handlers\Events\Model\PiggyBankEventHandler@changedPiggyBankName',
+// ],
// rule actions
RuleActionFailedOnArray::class => [
diff --git a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
index 463404cb25..73ec9c33e1 100644
--- a/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
+++ b/app/Repositories/PiggyBank/ModifiesPiggyBanks.php
@@ -26,7 +26,7 @@ namespace FireflyIII\Repositories\PiggyBank;
use Exception;
use FireflyIII\Events\Model\PiggyBank\PiggyBankAmountIsChanged;
-use FireflyIII\Events\Model\PiggyBank\ChangedName;
+use FireflyIII\Events\Model\PiggyBank\PiggyBankNameIsChanged;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\PiggyBankFactory;
use FireflyIII\Models\Account;
@@ -68,13 +68,13 @@ trait ModifiesPiggyBanks
{
$currentAmount = $this->getCurrentAmount($piggyBank, $account);
$pivot = $piggyBank->accounts()->where('accounts.id', $account->id)->first()->pivot;
- $pivot->current_amount = bcsub((string) $currentAmount, $amount);
+ $pivot->current_amount = bcsub((string)$currentAmount, $amount);
$pivot->native_current_amount = null;
// also update native_current_amount.
- $userCurrency = Amount::getPrimaryCurrencyByUserGroup($this->user->userGroup);
+ $userCurrency = Amount::getPrimaryCurrencyByUserGroup($this->user->userGroup);
if ($userCurrency->id !== $piggyBank->transaction_currency_id) {
- $converter = new ExchangeRateConverter();
+ $converter = new ExchangeRateConverter();
$converter->setIgnoreSettings(true);
$pivot->native_current_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $pivot->current_amount);
}
@@ -91,13 +91,13 @@ trait ModifiesPiggyBanks
{
$currentAmount = $this->getCurrentAmount($piggyBank, $account);
$pivot = $piggyBank->accounts()->where('accounts.id', $account->id)->first()->pivot;
- $pivot->current_amount = bcadd((string) $currentAmount, $amount);
+ $pivot->current_amount = bcadd((string)$currentAmount, $amount);
$pivot->native_current_amount = null;
// also update native_current_amount.
- $userCurrency = Amount::getPrimaryCurrencyByUserGroup($this->user->userGroup);
+ $userCurrency = Amount::getPrimaryCurrencyByUserGroup($this->user->userGroup);
if ($userCurrency->id !== $piggyBank->transaction_currency_id) {
- $converter = new ExchangeRateConverter();
+ $converter = new ExchangeRateConverter();
$converter->setIgnoreSettings(true);
$pivot->native_current_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $pivot->current_amount);
}
@@ -123,14 +123,14 @@ trait ModifiesPiggyBanks
if (0 !== bccomp($piggyBank->target_amount, '0')) {
- $leftToSave = bcsub($piggyBank->target_amount, (string) $savedSoFar);
- $maxAmount = 1 === bccomp((string) $leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
+ $leftToSave = bcsub($piggyBank->target_amount, (string)$savedSoFar);
+ $maxAmount = 1 === bccomp((string)$leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
Log::debug(sprintf('Left to save: %s', $leftToSave));
Log::debug(sprintf('Maximum amount: %s', $maxAmount));
}
- $compare = bccomp($amount, (string) $maxAmount);
- $result = $compare <= 0;
+ $compare = bccomp($amount, (string)$maxAmount);
+ $result = $compare <= 0;
Log::debug(sprintf('Compare <= 0? %d, so canAddAmount is %s', $compare, var_export($result, true)));
@@ -141,7 +141,7 @@ trait ModifiesPiggyBanks
{
$savedSoFar = $this->getCurrentAmount($piggyBank, $account);
- return bccomp($amount, (string) $savedSoFar) <= 0;
+ return bccomp($amount, (string)$savedSoFar) <= 0;
}
/**
@@ -164,11 +164,11 @@ trait ModifiesPiggyBanks
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank
{
- $repetition = $this->getRepetition($piggyBank);
+ $repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return $piggyBank;
}
- $max = $piggyBank->target_amount;
+ $max = $piggyBank->target_amount;
if (1 === bccomp($amount, $max) && 0 !== bccomp($piggyBank->target_amount, '0')) {
$amount = $max;
}
@@ -211,14 +211,14 @@ trait ModifiesPiggyBanks
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
- $piggyBank = $this->updateProperties($piggyBank, $data);
+ $piggyBank = $this->updateProperties($piggyBank, $data);
if (array_key_exists('notes', $data)) {
$this->updateNote($piggyBank, (string)$data['notes']);
}
// update the order of the piggy bank:
- $oldOrder = $piggyBank->order;
- $newOrder = (int)($data['order'] ?? $oldOrder);
+ $oldOrder = $piggyBank->order;
+ $newOrder = (int)($data['order'] ?? $oldOrder);
if ($oldOrder !== $newOrder) {
$this->setOrder($piggyBank, $newOrder);
}
@@ -234,9 +234,9 @@ trait ModifiesPiggyBanks
// if the piggy bank is now smaller than the sum of the money saved,
// remove money from all accounts until the piggy bank is the right amount.
$currentAmount = $this->getCurrentAmount($piggyBank);
- if (1 === bccomp((string) $currentAmount, (string)$piggyBank->target_amount) && 0 !== bccomp((string)$piggyBank->target_amount, '0')) {
+ if (1 === bccomp((string)$currentAmount, (string)$piggyBank->target_amount) && 0 !== bccomp((string)$piggyBank->target_amount, '0')) {
Log::debug(sprintf('Current amount is %s, target amount is %s', $currentAmount, $piggyBank->target_amount));
- $difference = bcsub((string)$piggyBank->target_amount, (string) $currentAmount);
+ $difference = bcsub((string)$piggyBank->target_amount, (string)$currentAmount);
// an amount will be removed, create "negative" event:
// Log::debug(sprintf('ChangedAmount: is triggered with difference "%s"', $difference));
@@ -283,7 +283,7 @@ trait ModifiesPiggyBanks
private function updateProperties(PiggyBank $piggyBank, array $data): PiggyBank
{
if (array_key_exists('name', $data) && '' !== $data['name']) {
- event(new ChangedName($piggyBank, $piggyBank->name, $data['name']));
+ event(new PiggyBankNameIsChanged($piggyBank, $piggyBank->name, $data['name']));
$piggyBank->name = $data['name'];
}
if (array_key_exists('transaction_currency_id', $data) && is_int($data['transaction_currency_id'])) {
@@ -318,7 +318,7 @@ trait ModifiesPiggyBanks
return;
}
- $dbNote = $piggyBank->notes()->first();
+ $dbNote = $piggyBank->notes()->first();
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($piggyBank);
@@ -329,16 +329,15 @@ trait ModifiesPiggyBanks
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
{
- $oldOrder = $piggyBank->order;
+ $oldOrder = $piggyBank->order;
// Log::debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
if ($newOrder > $oldOrder) {
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
- ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
- ->where('accounts.user_id', $this->user->id)
- ->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
- ->where('piggy_banks.id', '!=', $piggyBank->id)
- ->distinct()->decrement('piggy_banks.order')
- ;
+ ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
+ ->where('accounts.user_id', $this->user->id)
+ ->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
+ ->where('piggy_banks.id', '!=', $piggyBank->id)
+ ->distinct()->decrement('piggy_banks.order');
$piggyBank->order = $newOrder;
Log::debug(sprintf('[1] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
@@ -347,12 +346,11 @@ trait ModifiesPiggyBanks
return true;
}
PiggyBank::leftJoin('account_piggy_bank', 'account_piggy_bank.piggy_bank_id', '=', 'piggy_banks.id')
- ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
- ->where('accounts.user_id', $this->user->id)
- ->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
- ->where('piggy_banks.id', '!=', $piggyBank->id)
- ->distinct()->increment('piggy_banks.order')
- ;
+ ->leftJoin('accounts', 'accounts.id', '=', 'account_piggy_bank.account_id')
+ ->where('accounts.user_id', $this->user->id)
+ ->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
+ ->where('piggy_banks.id', '!=', $piggyBank->id)
+ ->distinct()->increment('piggy_banks.order');
$piggyBank->order = $newOrder;
Log::debug(sprintf('[2] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
@@ -373,7 +371,7 @@ trait ModifiesPiggyBanks
}
// if this account contains less than the amount, remove the current amount, update the amount and continue.
$this->removeAmount($piggyBank, $account, $current);
- $amount = bcsub($amount, (string)$current);
+ $amount = bcsub($amount, (string)$current);
}
}
}