Skip to content

Firefly III: a personal finances manager

Fix more events #11544

James Cole1b1ce3e04efd
diff --git a/app/Listeners/Model/Account/TriggersCreditRecalculation.php b/app/Events/Model/Account/UpdatedExistingAccount.php
similarity index 55%
rename from app/Listeners/Model/Account/TriggersCreditRecalculation.php
rename to app/Events/Model/Account/UpdatedExistingAccount.php
index d8b5eb1d85..485229daf9 100644
--- a/app/Listeners/Model/Account/TriggersCreditRecalculation.php
+++ b/app/Events/Model/Account/UpdatedExistingAccount.php
@@ -1,6 +1,6 @@
<?php
/*
- * TriggersCreditRecalculation.php
+ * UpdatedExistingAccount.php
* Copyright (c) 2026 [email protected]
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -19,23 +19,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-namespace FireflyIII\Listeners\Model\Account;
+namespace FireflyIII\Events\Model\Account;
-use FireflyIII\Events\Model\Account\CreatedNewAccount;
-use FireflyIII\Services\Internal\Support\CreditRecalculateService;
-use Illuminate\Support\Facades\Log;
+use FireflyIII\Events\Event;
+use FireflyIII\Models\Account;
+use Illuminate\Queue\SerializesModels;
-class TriggersCreditRecalculation
+class UpdatedExistingAccount extends Event
{
- public function handle(CreatedNewAccount $event): void
- {
- L¬og::debug('Will call CreditRecalculateService because a new account was created.');
- $account = $event->account;
- /** @var CreditRecalculateService $object */
- $object = app(CreditRecalculateService::class);
- $object->setAccount($account);
- $object->recalculate();
- }
-
+ use SerializesModels;
+ /**
+ * Create a new event instance.
+ */
+ public function __construct(public Account $account) {}
}
diff --git a/app/Handlers/Observer/AccountObserver.php b/app/Handlers/Observer/AccountObserver.php
index 25f5492ba4..b811289251 100644
--- a/app/Handlers/Observer/AccountObserver.php
+++ b/app/Handlers/Observer/AccountObserver.php
@@ -41,40 +41,6 @@ use Illuminate\Support\Facades\Log;
*/
class AccountObserver
{
- public function created(Account $account): void
- {
- // Log::debug('Observe "created" of an account.');
- $this->updatePrimaryCurrencyAmount($account);
- }
-
- private function updatePrimaryCurrencyAmount(Account $account): void
- {
- if (!Amount::convertToPrimary($account->user)) {
- return;
- }
- $userCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
- $repository = app(AccountRepositoryInterface::class);
- $currency = $repository->getAccountCurrency($account);
- if (
- null !== $currency
- && $currency->id !== $userCurrency->id
- && '' !== (string) $account->virtual_balance
- && 0 !== bccomp($account->virtual_balance, '0')
- ) {
- $converter = new ExchangeRateConverter();
- $converter->setUserGroup($account->user->userGroup);
- $converter->setIgnoreSettings(true);
- $account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
- }
- if ('' === (string) $account->virtual_balance || 0 === bccomp($account->virtual_balance, '0')) {
- $account->virtual_balance = null;
- $account->native_virtual_balance = null;
- }
- $account->saveQuietly();
-
- // Log::debug('Account primary currency virtual balance is updated.');
- }
-
/**
* Also delete related objects.
*/
@@ -117,10 +83,4 @@ class AccountObserver
$account->notes()->delete();
$account->locations()->delete();
}
-
- public function updated(Account $account): void
- {
- // Log::debug('Observe "updated" of an account.');
- $this->updatePrimaryCurrencyAmount($account);
- }
}
diff --git a/app/Listeners/Model/Account/UpdatesAccountInformation.php b/app/Listeners/Model/Account/UpdatesAccountInformation.php
new file mode 100644
index 0000000000..4ba85a5410
--- /dev/null
+++ b/app/Listeners/Model/Account/UpdatesAccountInformation.php
@@ -0,0 +1,81 @@
+<?php
+/*
+ * TriggersCreditRecalculation.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\Account;
+
+use FireflyIII\Events\Model\Account\CreatedNewAccount;
+use FireflyIII\Events\Model\Account\UpdatedExistingAccount;
+use FireflyIII\Models\Account;
+use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Services\Internal\Support\CreditRecalculateService;
+use FireflyIII\Support\Facades\Amount;
+use FireflyIII\Support\Http\Api\ExchangeRateConverter;
+use Illuminate\Support\Facades\Log;
+
+class UpdatesAccountInformation
+{
+ public function handle(CreatedNewAccount | UpdatedExistingAccount $event): void
+ {
+ $this->recalculateCredit($event->account);
+ $this->updateVirtualBalance($event->account);
+ }
+
+ private function recalculateCredit(Account $account): void
+ {
+ Log::debug('Will call CreditRecalculateService because a new account was created.');
+ /** @var CreditRecalculateService $object */
+ $object = app(CreditRecalculateService::class);
+ $object->setAccount($account);
+ $object->recalculate();
+ }
+
+ private function updateVirtualBalance(Account $account): void
+ {
+ if (!Amount::convertToPrimary($account->user)) {
+ Log::debug('After account creation, no need to convert virtual balance.');
+ return;
+ }
+ Log::debug('After account creation, convert virtual balance.');
+ $userCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
+ $repository = app(AccountRepositoryInterface::class);
+ $currency = $repository->getAccountCurrency($account);
+ if (
+ null !== $currency
+ && $currency->id !== $userCurrency->id
+ && '' !== (string)$account->virtual_balance
+ && 0 !== bccomp($account->virtual_balance, '0')
+ ) {
+ $converter = new ExchangeRateConverter();
+ $converter->setUserGroup($account->user->userGroup);
+ $converter->setIgnoreSettings(true);
+ $account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
+ }
+ if ('' === (string)$account->virtual_balance || 0 === bccomp($account->virtual_balance, '0')) {
+ $account->virtual_balance = null;
+ $account->native_virtual_balance = null;
+ }
+ $account->saveQuietly();
+
+ // Log::debug('Account primary currency virtual balance is updated.');
+ }
+
+
+}
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index b8bed0cfaf..bef63353a7 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -25,6 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use FireflyIII\Enums\AccountTypeEnum;
+use FireflyIII\Events\Model\Account\UpdatedExistingAccount;
use FireflyIII\Events\UpdatedAccount;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
@@ -150,6 +151,8 @@ class AccountUpdateService
$account->save();
+ event(new UpdatedExistingAccount($account));
+
return $account;
}