Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
fix rename of rules when bill name changes.
James Cole057b82b471c1
diff --git a/app/Events/Model/Bill/UpdatedExistingBill.php b/app/Events/Model/Bill/UpdatedExistingBill.phpnew file mode 100644index 0000000000..0a11e81603--- /dev/null+++ b/app/Events/Model/Bill/UpdatedExistingBill.php@@ -0,0 +1,37 @@+<?php+/*+ * UpdatedExistingBill.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\Bill;++use FireflyIII\Events\Event;+use FireflyIII\Models\Bill;+use Illuminate\Queue\SerializesModels;++class UpdatedExistingBill extends Event+{+ use SerializesModels;++ /**+ * Create a new event instance.+ */+ public function __construct(public Bill $bill, public array $oldData) {}+}+diff --git a/app/Listeners/Model/Bill/UpdatesRulesForChangedBill.php b/app/Listeners/Model/Bill/UpdatesRulesForChangedBill.phpnew file mode 100644index 0000000000..deab79f556--- /dev/null+++ b/app/Listeners/Model/Bill/UpdatesRulesForChangedBill.php@@ -0,0 +1,79 @@+<?php+/*+ * UpdatesRulesForChangedBill.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\Bill;++use FireflyIII\Events\Model\Bill\UpdatedExistingBill;+use FireflyIII\Models\Bill;+use FireflyIII\Models\Rule;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\RuleTrigger;+use FireflyIII\Repositories\Rule\RuleRepositoryInterface;+use Illuminate\Contracts\Queue\ShouldQueue;+use Illuminate\Support\Facades\Log;++class UpdatesRulesForChangedBill implements ShouldQueue+{+ public function handle(UpdatedExistingBill $event): void+ {++ // update rule actions.+ if ($event->bill->name !== $event->oldData['name']) {+ $this->updateBillTriggersAndActions($event->bill, $event->oldData);+ }+ }+++ private function updateBillTriggersAndActions(Bill $bill, array $oldData): void+ {+ Log::debug(sprintf('Now in updateBillTriggersAndActions(#%d)', $bill->id));+ $repository = app(RuleRepositoryInterface::class);+ $repository->setUser($bill->user);+ $rules = $repository->getAll();++ /** @var Rule $rule */+ foreach ($rules as $rule) {+ $this->updateRule($bill, $rule, $oldData);+ }+ }++ private function updateRule(Bill $bill, Rule $rule, array $oldData): void+ {+ $triggers = ['bill_is', 'bill_ends', 'bill_starts', 'bill_contains'];+ /** @var RuleTrigger $trigger */+ foreach ($rule->ruleTriggers as $trigger) {+ if (in_array($trigger->trigger_type, $triggers, true) && $trigger->trigger_value === $oldData['name']) {+ Log::debug(sprintf('Updated trigger #%d in rule #%d to new subscription name', $trigger->id, $rule->id));+ $trigger->trigger_value = $bill->name;+ $trigger->save();+ }+ }+ /** @var RuleAction $action */+ foreach ($rule->ruleActions as $action) {+ if ($action->action_type === 'link_to_bill' && $action->action_value === $oldData['name']) {+ Log::debug(sprintf('Updated action #%d in rule #%d to new subscription name', $action->id, $rule->id));+ $action->action_value = $bill->name;+ $action->save();+ }+ }+ }++}diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.phpindex dc8ef58de1..05e2683b14 100644--- a/app/Repositories/Bill/BillRepository.php+++ b/app/Repositories/Bill/BillRepository.php@@ -96,7 +96,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterfaceforeach ($set as $bill) {if ($bill->order !== $current) {$bill->order = $current;- $bill->save();+ $bill->saveQuietly();}++$current;}@@ -613,7 +613,7 @@ class BillRepository implements BillRepositoryInterface, UserGroupInterfacepublic function setOrder(Bill $bill, int $order): void{$bill->order = $order;- $bill->save();+ $bill->saveQuietly();}/**diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.phpindex d60846fbae..35be12d667 100644--- a/app/Repositories/Rule/RuleRepository.php+++ b/app/Repositories/Rule/RuleRepository.php@@ -102,7 +102,7 @@ class RuleRepository implements RuleRepositoryInterface, UserGroupInterface{return $this->user->rules()- ->with(['ruleGroup'])+ ->with(['ruleGroup', 'ruleTriggers', 'ruleActions'])->get();}diff --git a/app/Services/Internal/Update/BillUpdateService.php b/app/Services/Internal/Update/BillUpdateService.phpindex 1052470112..1303195def 100644--- a/app/Services/Internal/Update/BillUpdateService.php+++ b/app/Services/Internal/Update/BillUpdateService.php@@ -24,6 +24,7 @@ declare(strict_types=1);namespace FireflyIII\Services\Internal\Update;+use FireflyIII\Events\Model\Bill\UpdatedExistingBill;use FireflyIII\Factory\TransactionCurrencyFactory;use FireflyIII\Models\Bill;use FireflyIII\Models\ObjectGroup;@@ -50,6 +51,7 @@ class BillUpdateServicepublic function update(Bill $bill, array $data): Bill{$this->user = $bill->user;+ $oldData = $bill->toArray();if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) {$factory = app(TransactionCurrencyFactory::class);@@ -68,13 +70,6 @@ class BillUpdateService$bill = $this->updateBillProperties($bill, $data);$bill->save();$bill->refresh();- // old values- $oldData = [- 'name' => $bill->name,- 'amount_min' => $bill->amount_min,- 'amount_max' => $bill->amount_max,- 'transaction_currency_name' => $bill->transactionCurrency->name,- ];// update note:if (array_key_exists('notes', $data)) {$this->updateNote($bill, (string) $data['notes']);@@ -90,12 +85,6 @@ class BillUpdateService}}- // update rule actions.- if (array_key_exists('name', $data)) {- $this->updateBillActions($bill, $oldData['name'], $data['name']);- $this->updateBillTriggers($bill, $oldData, $data);- }-// update using name:if (array_key_exists('object_group_title', $data)) {$objectGroupTitle = $data['object_group_title'] ?? '';@@ -127,6 +116,7 @@ class BillUpdateService$bill->objectGroups()->sync([]);$bill->save();}+ event(new UpdatedExistingBill($bill, $oldData));return $bill;}@@ -181,39 +171,6 @@ class BillUpdateServicereturn $bill;}- private function updateBillTriggers(Bill $bill, array $oldData, array $newData): void- {- Log::debug(sprintf('Now in updateBillTriggers(%d, "%s")', $bill->id, $bill->name));-- /** @var BillRepositoryInterface $repository */- $repository = app(BillRepositoryInterface::class);- $repository->setUser($bill->user);- $rules = $repository->getRulesForBill($bill);- if (0 === $rules->count()) {- Log::debug('Found no rules.');-- return;- }- Log::debug(sprintf('Found %d rules', $rules->count()));- $fields = [- 'name' => 'description_contains',- 'amount_min' => 'amount_more',- 'amount_max' => 'amount_less',- 'transaction_currency_name' => 'currency_is',- ];- foreach ($fields as $field => $ruleTriggerKey) {- if (!array_key_exists($field, $newData)) {- continue;- }- if ($oldData[$field] === $newData[$field]) {- Log::debug(sprintf('Field %s is unchanged ("%s"), continue.', $field, $oldData[$field]));-- continue;- }- $this->updateRules($rules, $ruleTriggerKey, $oldData[$field], $newData[$field]);- }- }-private function updateOrder(Bill $bill, int $oldOrder, int $newOrder): void{if ($newOrder > $oldOrder) {