Skip to content

Firefly III: a personal finances manager

Fix #11667

James Coleb85200e1f65b
diff --git a/app/Events/Model/Account/UpdatedExistingAccount.php b/app/Events/Model/Account/UpdatedExistingAccount.php
index 5b8d97a338..07b04707a9 100644
--- a/app/Events/Model/Account/UpdatedExistingAccount.php
+++ b/app/Events/Model/Account/UpdatedExistingAccount.php
@@ -36,6 +36,6 @@ class UpdatedExistingAccount extends Event
* Create a new event instance.
*/
public function __construct(
- public Account $account
+ public Account $account,public array $oldData
) {}
}
diff --git a/app/Listeners/Model/Account/UpdatesAccountInformation.php b/app/Listeners/Model/Account/UpdatesAccountInformation.php
index e4efe8baf3..114282e1c0 100644
--- a/app/Listeners/Model/Account/UpdatesAccountInformation.php
+++ b/app/Listeners/Model/Account/UpdatesAccountInformation.php
@@ -29,17 +29,79 @@ use FireflyIII\Events\Model\Account\UpdatedExistingAccount;
use FireflyIII\Handlers\ExchangeRate\ConversionParameters;
use FireflyIII\Handlers\ExchangeRate\ConvertsAmountToPrimaryAmount;
use FireflyIII\Models\Account;
+use FireflyIII\Models\Rule;
+use FireflyIII\Models\RuleAction;
+use FireflyIII\Models\RuleTrigger;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Services\Internal\Support\CreditRecalculateService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class UpdatesAccountInformation implements ShouldQueue
{
- public function handle(CreatedNewAccount|UpdatedExistingAccount $event): void
+ public function handle(CreatedNewAccount | UpdatedExistingAccount $event): void
{
$this->recalculateCredit($event->account);
$this->updateVirtualBalance($event->account);
+ if ($event instanceof UpdatedExistingAccount) {
+ $this->renameRules($event->account, $event->oldData);
+ }
+
+ }
+
+ private function correctRuleTriggers(Account $account, array $oldData, Rule $rule): void
+ {
+ $nameFields = ['source_account_is', 'source_account_contains', 'source_account_ends', 'source_account_starts', 'destination_account_is', 'destination_account_contains', 'destination_account_ends', 'destination_account_starts', 'account_is', 'account_contains', 'account_ends', 'account_starts',];
+ $numberFields = ['source_account_nr_is', 'source_account_nr_contains', 'source_account_nr_ends', 'source_account_nr_starts', 'destination_account_nr_is', 'destination_account_nr_contains', 'destination_account_nr_starts', 'account_nr_is', 'account_nr_contains', 'account_nr_ends', 'account_nr_starts',];
+
+ Log::debug(sprintf('Check if rule #%d triggers reference account #%d "%s"', $rule->id, $account->id, $account->name));
+ $fixed = 0;
+ /** @var RuleTrigger $trigger */
+ foreach ($rule->ruleTriggers as $trigger) {
+ // fix name:
+ if ($oldData['name'] === $trigger->trigger_value && in_array($trigger->trigger_type, $nameFields, true)) {
+ Log::debug(sprintf('Rule trigger #%d "%s" has old account name, replace with new.', $trigger->id, $trigger->trigger_type));
+ $trigger->trigger_value = $account->name;
+ $trigger->save();
+ $fixed++;
+ }
+ // fix IBAN:
+ if ($oldData['iban'] === $trigger->trigger_value && in_array($trigger->trigger_type, $numberFields, true)) {
+ Log::debug(sprintf('Rule trigger #%d "%s" has old account IBAN, replace with new.', $trigger->id, $trigger->trigger_type));
+ $trigger->trigger_value = $account->iban;
+ $trigger->save();
+ $fixed++;
+ }
+ // fix account number: // account_number
+ if ($oldData['account_number'] === $trigger->trigger_value && in_array($trigger->trigger_type, $numberFields, true)) {
+ Log::debug(sprintf('Rule trigger #%d "%s" has old account account_number, replace with new.', $trigger->id, $trigger->trigger_type));
+ $trigger->trigger_value = $account->iban;
+ $trigger->save();
+ $fixed++;
+ }
+ }
+ Log::debug(sprintf('Corrected %d trigger(s) for rule #%d', $fixed, $rule->id));
+ }
+
+ private function correctRuleActions(Account $account, array $oldData, Rule $rule): void
+ {
+ $fields = ['set_source_account', 'set_destination_account'];
+
+ Log::debug(sprintf('Check if rule #%d actions reference account #%d "%s"', $rule->id, $account->id, $account->name));
+ $fixed = 0;
+ /** @var RuleAction $action */
+ foreach ($rule->ruleActions as $action) {
+
+ // fix name:
+ if ($oldData['name'] === $action->action_value && in_array($action->action_type, $fields, true)) {
+ Log::debug(sprintf('Rule action #%d "%s" has old account name, replace with new.', $action->id, $action->action_type));
+ $action->action_value = $account->name;
+ $action->save();
+ $fixed++;
+ }
+ }
+ Log::debug(sprintf('Corrected %d action(s) for rule #%d', $fixed, $rule->id));
}
private function recalculateCredit(Account $account): void
@@ -52,6 +114,20 @@ class UpdatesAccountInformation implements ShouldQueue
$object->recalculate();
}
+ private function renameRules(Account $account, array $oldData): void
+ {
+ Log::debug('Updated account, will now correct rules.');
+ $repository = app(RuleRepositoryInterface::class);
+ $repository->setUser($account->user);
+ $rules = $repository->getAll();
+
+ /** @var Rule $rule */
+ foreach ($rules as $rule) {
+ $this->correctRuleTriggers($account, $oldData, $rule);
+ $this->correctRuleActions($account, $oldData, $rule);
+ }
+ }
+
private function updateVirtualBalance(Account $account): void
{
Log::debug('Will updateVirtualBalance');
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index 7ef55d69d9..3965f1e69b 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -79,6 +79,8 @@ class AccountUpdateService
Log::debug(sprintf('Now in %s', __METHOD__));
$this->accountRepository->setUser($account->user);
$this->user = $account->user;
+ $oldData = $account->toArray();
+ $oldData['account_number'] = $this->accountRepository->getMetaValue($account, 'account_number');
$account = $this->updateAccount($account, $data);
$account = $this->updateAccountOrder($account, $data);
@@ -109,7 +111,7 @@ class AccountUpdateService
// update preferences if inactive:
$this->updatePreferences($account);
- event(new UpdatedExistingAccount($account));
+ event(new UpdatedExistingAccount($account, $oldData));
return $account;
}
diff --git a/changelog.md b/changelog.md
index 7eae878f6d..e76ebf982f 100644
--- a/changelog.md
+++ b/changelog.md
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- #11671
+- #11667
## v6.4.17 - 2026-02-06