Skip to content

Firefly III: a personal finances manager

Fix stored account event.

James Coleff646751225c
diff --git a/app/Events/StoredAccount.php b/app/Events/Model/Account/CreatedNewAccount.php
similarity index 77%
rename from app/Events/StoredAccount.php
rename to app/Events/Model/Account/CreatedNewAccount.php
index 981a326883..0923dade31 100644
--- a/app/Events/StoredAccount.php
+++ b/app/Events/Model/Account/CreatedNewAccount.php
@@ -1,8 +1,7 @@
<?php
-
/*
- * StoredAccount.php
- * Copyright (c) 2021 [email protected]
+ * CreatedNewAccount.php
+ * Copyright (c) 2026 [email protected]
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@@ -20,24 +19,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-declare(strict_types=1);
-
-namespace FireflyIII\Events;
+namespace FireflyIII\Events\Model\Account;
+use FireflyIII\Events\Event;
use FireflyIII\Models\Account;
use Illuminate\Queue\SerializesModels;
-/**
- * Class StoredAccount
- */
-class StoredAccount extends Event
+class CreatedNewAccount extends Event
{
use SerializesModels;
/**
* Create a new event instance.
*/
- public function __construct(
- public Account $account
- ) {}
+ public function __construct(public Account $account) {}
}
diff --git a/app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php b/app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php
deleted file mode 100644
index 6c26df3760..0000000000
--- a/app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/*
- * CreatedTransactionGroupBatch.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\TransactionGroup;
-
-use FireflyIII\Events\Event;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Collection;
-
-class CreatedTransactionGroupInBatch extends Event
-{
- use SerializesModels;
-
- /**
- * Create a new event instance.
- */
- public function __construct(
- public Collection $collection,
- public array $flags
- ) {}
-}
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 5d06a29c61..9d09318f99 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -25,7 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Enums\AccountTypeEnum;
-use FireflyIII\Events\StoredAccount;
+use FireflyIII\Events\Model\Account\CreatedNewAccount;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
@@ -47,12 +47,12 @@ class AccountFactory
use LocationServiceTrait;
protected AccountRepositoryInterface $accountRepository;
- protected array $validAssetFields;
- protected array $validCCFields;
- protected array $validFields;
- private array $canHaveOpeningBalance;
- private array $canHaveVirtual;
- private User $user;
+ protected array $validAssetFields;
+ protected array $validCCFields;
+ protected array $validFields;
+ private array $canHaveOpeningBalance;
+ private array $canHaveVirtual;
+ private User $user;
/**
* AccountFactory constructor.
@@ -74,7 +74,7 @@ class AccountFactory
{
Log::debug(sprintf('findOrCreate("%s", "%s")', $accountName, $accountType));
- $type = $this->accountRepository->getAccountTypeByType($accountType);
+ $type = $this->accountRepository->getAccountTypeByType($accountType);
if (!$type instanceof AccountType) {
throw new FireflyException(sprintf('Cannot find account type "%s"', $accountType));
}
@@ -84,21 +84,20 @@ class AccountFactory
->accounts
->where('account_type_id', $type->id)
->where('name', $accountName)
- ->first()
- ;
+ ->first();
if (null === $return) {
Log::debug('Found nothing. Will create a new one.');
$return = $this->create([
- 'user_id' => $this->user->id,
- 'user_group_id' => $this->user->user_group_id,
- 'name' => $accountName,
- 'account_type_id' => $type->id,
- 'account_type_name' => null,
- 'virtual_balance' => '0',
- 'iban' => null,
- 'active' => true,
- ]);
+ 'user_id' => $this->user->id,
+ 'user_group_id' => $this->user->user_group_id,
+ 'name' => $accountName,
+ 'account_type_id' => $type->id,
+ 'account_type_name' => null,
+ 'virtual_balance' => '0',
+ 'iban' => null,
+ 'active' => true,
+ ]);
}
return $return;
@@ -114,15 +113,15 @@ class AccountFactory
$data['iban'] = $this->filterIban($data['iban'] ?? null);
// account may exist already:
- $return = $this->find($data['name'], $type->type);
+ $return = $this->find($data['name'], $type->type);
if ($return instanceof Account) {
return $return;
}
- $return = $this->createAccount($type, $data);
+ $return = $this->createAccount($type, $data);
- event(new StoredAccount($return));
+ event(new CreatedNewAccount($return));
return $return;
}
@@ -132,7 +131,7 @@ class AccountFactory
*/
protected function getAccountType(array $data): ?AccountType
{
- $accountTypeId = array_key_exists('account_type_id', $data) ? (int) $data['account_type_id'] : 0;
+ $accountTypeId = array_key_exists('account_type_id', $data) ? (int)$data['account_type_id'] : 0;
$accountTypeName = $data['account_type_name'] ?? null;
$result = null;
// find by name or ID
@@ -170,8 +169,7 @@ class AccountFactory
->accounts()
->where('account_type_id', $type->id)
->where('name', $accountName)
- ->first()
- ;
+ ->first();
}
/**
@@ -195,7 +193,7 @@ class AccountFactory
'iban' => $data['iban'],
];
// fix virtual balance when it's empty
- if ('' === (string) $databaseData['virtual_balance']) {
+ if ('' === (string)$databaseData['virtual_balance']) {
$databaseData['virtual_balance'] = null;
}
// remove virtual balance when not an asset account
@@ -203,11 +201,11 @@ class AccountFactory
$databaseData['virtual_balance'] = null;
}
// create account!
- $account = Account::create($databaseData);
+ $account = Account::create($databaseData);
Log::channel('audit')->info(sprintf('Account #%d ("%s") has been created.', $account->id, $account->name));
// update meta data:
- $data = $this->cleanMetaDataArray($account, $data);
+ $data = $this->cleanMetaDataArray($account, $data);
$this->storeMetaData($account, $data);
// create opening balance (only asset accounts)
@@ -227,7 +225,7 @@ class AccountFactory
}
// create notes
- $notes = array_key_exists('notes', $data) ? $data['notes'] : '';
+ $notes = array_key_exists('notes', $data) ? $data['notes'] : '';
$this->updateNote($account, $notes);
// create location
@@ -247,10 +245,10 @@ class AccountFactory
*/
private function cleanMetaDataArray(Account $account, array $data): array
{
- $currencyId = array_key_exists('currency_id', $data) ? (int) $data['currency_id'] : 0;
- $currencyCode = array_key_exists('currency_code', $data) ? (string) $data['currency_code'] : '';
- $accountRole = array_key_exists('account_role', $data) ? (string) $data['account_role'] : null;
- $currency = $this->getCurrency($currencyId, $currencyCode);
+ $currencyId = array_key_exists('currency_id', $data) ? (int)$data['currency_id'] : 0;
+ $currencyCode = array_key_exists('currency_code', $data) ? (string)$data['currency_code'] : '';
+ $accountRole = array_key_exists('account_role', $data) ? (string)$data['account_role'] : null;
+ $currency = $this->getCurrency($currencyId, $currencyCode);
// only asset account may have a role:
if (AccountTypeEnum::ASSET->value !== $account->accountType->type) {
@@ -268,7 +266,7 @@ class AccountFactory
private function storeMetaData(Account $account, array $data): void
{
- $fields = $this->validFields;
+ $fields = $this->validFields;
if (AccountTypeEnum::ASSET->value === $account->accountType->type) {
$fields = $this->validAssetFields;
}
@@ -277,8 +275,8 @@ class AccountFactory
}
// remove currency_id if necessary.
- $type = $account->accountType->type;
- $list = config('firefly.valid_currency_account_types');
+ $type = $account->accountType->type;
+ $list = config('firefly.valid_currency_account_types');
if (!in_array($type, $list, true)) {
$pos = array_search('currency_id', $fields, true);
if (false !== $pos) {
@@ -300,7 +298,7 @@ class AccountFactory
$data[$field] = 1;
}
- $factory->crud($account, $field, (string) $data[$field]);
+ $factory->crud($account, $field, (string)$data[$field]);
}
}
}
@@ -355,14 +353,14 @@ class AccountFactory
*/
private function storeOrder(Account $account, array $data): void
{
- $accountType = $account->accountType->type;
- $maxOrder = $this->accountRepository->maxOrder($accountType);
- $order = null;
+ $accountType = $account->accountType->type;
+ $maxOrder = $this->accountRepository->maxOrder($accountType);
+ $order = null;
if (!array_key_exists('order', $data)) {
$order = $maxOrder + 1;
}
if (array_key_exists('order', $data)) {
- $order = (int) ($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']);
+ $order = (int)($data['order'] > $maxOrder ? $maxOrder + 1 : $data['order']);
$order = 0 === $order ? $maxOrder + 1 : $order;
}
diff --git a/app/Handlers/Events/StoredAccountEventHandler.php b/app/Listeners/Model/Account/TriggersCreditRecalculation.php
similarity index 68%
rename from app/Handlers/Events/StoredAccountEventHandler.php
rename to app/Listeners/Model/Account/TriggersCreditRecalculation.php
index 574148def2..d8b5eb1d85 100644
--- a/app/Handlers/Events/StoredAccountEventHandler.php
+++ b/app/Listeners/Model/Account/TriggersCreditRecalculation.php
@@ -1,8 +1,7 @@
<?php
-
/*
- * StoredAccountEventHandler.php
- * Copyright (c) 2021 [email protected]
+ * TriggersCreditRecalculation.php
+ * Copyright (c) 2026 [email protected]
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
@@ -20,25 +19,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-declare(strict_types=1);
-
-namespace FireflyIII\Handlers\Events;
+namespace FireflyIII\Listeners\Model\Account;
-use FireflyIII\Events\StoredAccount;
+use FireflyIII\Events\Model\Account\CreatedNewAccount;
use FireflyIII\Services\Internal\Support\CreditRecalculateService;
+use Illuminate\Support\Facades\Log;
-/**
- * Class StoredAccountEventHandler
- */
-class StoredAccountEventHandler
+class TriggersCreditRecalculation
{
- public function recalculateCredit(StoredAccount $event): void
+ 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 = app(CreditRecalculateService::class);
$object->setAccount($account);
$object->recalculate();
}
+
+
}
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 687db5c952..f0bd4d360c 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -23,14 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Providers;
-use FireflyIII\Events\DestroyedTransactionGroup;
-use FireflyIII\Events\Model\TransactionGroup\TriggeredStoredTransactionGroup;
-use FireflyIII\Events\Preferences\UserGroupChangedPrimaryCurrency;
-use FireflyIII\Events\StoredAccount;
-use FireflyIII\Events\UpdatedAccount;
-use FireflyIII\Events\UpdatedTransactionGroup;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
-use Laravel\Passport\Events\AccessTokenCreated;
use Override;
/**
@@ -40,21 +33,22 @@ use Override;
*/
class EventServiceProvider extends ServiceProvider
{
- protected $listen = [
- // is a Transaction Journal related event.
- // StoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@runAllHandlers'],
- // TriggeredStoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@triggerRulesManually'],
- // is a Transaction Journal related event.
- // UpdatedTransactionGroup::class => ['FireflyIII\Handlers\Events\UpdatedGroupEventHandler@runAllHandlers'],
- // DestroyedTransactionGroup::class => ['FireflyIII\Handlers\Events\DestroyedGroupEventHandler@runAllHandlers'],
- // API related events:
- // AccessTokenCreated::class => ['FireflyIII\Handlers\Events\APIEventHandler@accessTokenCreated'],
- // account related events:
- // StoredAccount::class => ['FireflyIII\Handlers\Events\StoredAccountEventHandler@recalculateCredit'],
- // UpdatedAccount::class => ['FireflyIII\Handlers\Events\UpdatedAccountEventHandler@recalculateCredit'],
- // preferences
- // UserGroupChangedPrimaryCurrency::class => ['FireflyIII\Handlers\Events\PreferencesEventHandler@resetPrimaryCurrencyAmounts'],
- ];
+ protected $listen
+ = [
+ // is a Transaction Journal related event.
+ // StoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@runAllHandlers'],
+ // TriggeredStoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@triggerRulesManually'],
+ // is a Transaction Journal related event.
+ // UpdatedTransactionGroup::class => ['FireflyIII\Handlers\Events\UpdatedGroupEventHandler@runAllHandlers'],
+ // DestroyedTransactionGroup::class => ['FireflyIII\Handlers\Events\DestroyedGroupEventHandler@runAllHandlers'],
+ // API related events:
+ // AccessTokenCreated::class => ['FireflyIII\Handlers\Events\APIEventHandler@accessTokenCreated'],
+ // account related events:
+ // StoredAccount::class => ['FireflyIII\Handlers\Events\StoredAccountEventHandler@recalculateCredit'],
+ // UpdatedAccount::class => ['FireflyIII\Handlers\Events\UpdatedAccountEventHandler@recalculateCredit'],
+ // preferences
+ // UserGroupChangedPrimaryCurrency::class => ['FireflyIII\Handlers\Events\PreferencesEventHandler@resetPrimaryCurrencyAmounts'],
+ ];
/**
* Register any events for your application.