Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Clean up events for budgets.
James Cole229b45c7ada5
diff --git a/app/Events/Model/Budget/CreatedBudget.php b/app/Events/Model/Budget/CreatedBudget.phpnew file mode 100644index 0000000000..af7d187af6--- /dev/null+++ b/app/Events/Model/Budget/CreatedBudget.php@@ -0,0 +1,34 @@+<?php+/*+ * CreatedBudget.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\Budget;++use FireflyIII\Events\Event;+use FireflyIII\Models\Budget;+use Illuminate\Queue\SerializesModels;++class CreatedBudget extends Event+{+ use SerializesModels;++ public function __construct(public Budget $budget, public bool $createWebhookMessages) {}++}diff --git a/app/Events/Model/Budget/DestroyedBudget.php b/app/Events/Model/Budget/DestroyedBudget.phpnew file mode 100644index 0000000000..e581bb3462--- /dev/null+++ b/app/Events/Model/Budget/DestroyedBudget.php@@ -0,0 +1,36 @@+<?php+/*+ * DestroyedBudget.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\Budget;++use FireflyIII\Events\Event;+use FireflyIII\Models\Budget;+use Illuminate\Queue\SerializesModels;+use Illuminate\Support\Facades\Log;++class DestroyedBudget extends Event+{+ public function __construct() {+ Log::debug('Created event DestroyedBudget');+ }+ use SerializesModels;++}diff --git a/app/Events/Model/Budget/DestroyingBudget.php b/app/Events/Model/Budget/DestroyingBudget.phpnew file mode 100644index 0000000000..494619b5f2--- /dev/null+++ b/app/Events/Model/Budget/DestroyingBudget.php@@ -0,0 +1,37 @@+<?php+/*+ * DestroyingBudget.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\Budget;++use FireflyIII\Events\Event;+use FireflyIII\Models\Budget;+use Illuminate\Queue\SerializesModels;+use Illuminate\Support\Facades\Log;++class DestroyingBudget extends Event+{+ public function __construct(public Budget $budget)+ {+ Log::debug(sprintf('Created event DestroyingBudget(#%d)', $budget->id));+ }++ use SerializesModels;+}diff --git a/app/Events/Model/Budget/UpdatedBudget.php b/app/Events/Model/Budget/UpdatedBudget.phpnew file mode 100644index 0000000000..bb2ee6d785--- /dev/null+++ b/app/Events/Model/Budget/UpdatedBudget.php@@ -0,0 +1,34 @@+<?php+/*+ * UpdatedBudget.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\Budget;++use FireflyIII\Events\Event;+use FireflyIII\Models\Budget;+use Illuminate\Queue\SerializesModels;++class UpdatedBudget extends Event+{+ use SerializesModels;++ public function __construct(public Budget $budget, public bool $createWebhookMessages) {}++}diff --git a/app/Handlers/Observer/BudgetObserver.php b/app/Handlers/Observer/BudgetObserver.phpdeleted file mode 100644index 8d15495d23..0000000000--- a/app/Handlers/Observer/BudgetObserver.php+++ /dev/null@@ -1,126 +0,0 @@-<?php--/*- * BudgetObserver.php- * Copyright (c) 2023 [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/>.- */-declare(strict_types=1);--namespace FireflyIII\Handlers\Observer;--use FireflyIII\Enums\WebhookTrigger;-use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending;-use FireflyIII\Generator\Webhook\MessageGeneratorInterface;-use FireflyIII\Models\Attachment;-use FireflyIII\Models\Budget;-use FireflyIII\Models\BudgetLimit;-use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;-use FireflyIII\Support\Observers\RecalculatesAvailableBudgetsTrait;-use FireflyIII\Support\Singleton\PreferencesSingleton;-use Illuminate\Support\Collection;-use Illuminate\Support\Facades\Log;--/**- * Class BudgetObserver- */-class BudgetObserver-{- use RecalculatesAvailableBudgetsTrait;-- public function created(Budget $budget): void- {- Log::debug(sprintf('Observe "created" of budget #%d ("%s").', $budget->id, $budget->name));-- // this is a lame trick to communicate with the observer.- $singleton = PreferencesSingleton::getInstance();-- if (true === $singleton->getPreference('fire_webhooks_budget_create')) {- // fire event.- $user = $budget->user;-- /** @var MessageGeneratorInterface $engine */- $engine = app(MessageGeneratorInterface::class);- $engine->setUser($user);- $engine->setObjects(new Collection()->push($budget));- $engine->setTrigger(WebhookTrigger::STORE_BUDGET);- $engine->generateMessages();- Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));- event(new WebhookMessagesRequestSending());- }- }-- public function deleting(Budget $budget): void- {- Log::debug('Observe "deleting" of a budget.');-- $user = $budget->user;-- /** @var MessageGeneratorInterface $engine */- $engine = app(MessageGeneratorInterface::class);- $engine->setUser($user);- $engine->setObjects(new Collection()->push($budget));- $engine->setTrigger(WebhookTrigger::DESTROY_BUDGET);- $engine->generateMessages();- Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));- event(new WebhookMessagesRequestSending());-- $repository = app(AttachmentRepositoryInterface::class);- $repository->setUser($budget->user);-- /** @var Attachment $attachment */- foreach ($budget->attachments()->get() as $attachment) {- $repository->destroy($attachment);- }- $budgetLimits = $budget->budgetlimits()->get();-- /** @var BudgetLimit $budgetLimit */- foreach ($budgetLimits as $budgetLimit) {- // this loop exists so several events are fired.- $copy = clone $budgetLimit;- $copy->id = 0;- $this->updateAvailableBudget($copy);- $budgetLimit->deleteQuietly(); // delete is quietly when in a loop.- }-- $budget->notes()->delete();- $budget->autoBudgets()->delete();-- // recalculate available budgets.- }-- public function updated(Budget $budget): void- {- Log::debug(sprintf('Observe "updated" of budget #%d ("%s").', $budget->id, $budget->name));-- // this is a lame trick to communicate with the observer.- $singleton = PreferencesSingleton::getInstance();-- if (true === $singleton->getPreference('fire_webhooks_budget_update')) {- $user = $budget->user;-- /** @var MessageGeneratorInterface $engine */- $engine = app(MessageGeneratorInterface::class);- $engine->setUser($user);- $engine->setObjects(new Collection()->push($budget));- $engine->setTrigger(WebhookTrigger::UPDATE_BUDGET);- $engine->generateMessages();- Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));- event(new WebhookMessagesRequestSending());- }- }-}diff --git a/app/Listeners/Model/Budget/ProcessesBudgets.php b/app/Listeners/Model/Budget/ProcessesBudgets.phpnew file mode 100644index 0000000000..8497485bd1--- /dev/null+++ b/app/Listeners/Model/Budget/ProcessesBudgets.php@@ -0,0 +1,55 @@+<?php+/*+ * ProcessesBudgets.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\Budget;++use FireflyIII\Enums\WebhookTrigger;+use FireflyIII\Events\Model\Budget\CreatedBudget;+use FireflyIII\Events\Model\Budget\DestroyingBudget;+use FireflyIII\Events\Model\Budget\UpdatedBudget;+use FireflyIII\Generator\Webhook\MessageGeneratorInterface;+use Illuminate\Contracts\Queue\ShouldQueue;+use Illuminate\Support\Collection;+use Illuminate\Support\Facades\Log;++class ProcessesBudgets implements ShouldQueue+{+ public function handle(DestroyingBudget | CreatedBudget | UpdatedBudget $event): void+ {+ Log::debug(sprintf('Will now handle %s', get_class($event)));+ $trigger = WebhookTrigger::STORE_BUDGET;+ if ($event instanceof DestroyingBudget) {+ $trigger = WebhookTrigger::DESTROY_BUDGET;+ }+ if ($event instanceof UpdatedBudget) {+ $trigger = WebhookTrigger::UPDATE_BUDGET;+ }+++ /** @var MessageGeneratorInterface $engine */+ $engine = app(MessageGeneratorInterface::class);+ $engine->setUser($event->budget->user);+ $engine->setObjects(new Collection()->push($event->budget));+ $engine->setTrigger($trigger);+ $engine->generateMessages();+ }++}diff --git a/app/Models/Budget.php b/app/Models/Budget.phpindex 32e5566885..98ead22ba0 100644--- a/app/Models/Budget.php+++ b/app/Models/Budget.php@@ -23,11 +23,9 @@ declare(strict_types=1);namespace FireflyIII\Models;-use FireflyIII\Handlers\Observer\BudgetObserver;use FireflyIII\Support\Models\ReturnsIntegerIdTrait;use FireflyIII\Support\Models\ReturnsIntegerUserIdTrait;use FireflyIII\User;-use Illuminate\Database\Eloquent\Attributes\ObservedBy;use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;@@ -37,7 +35,6 @@ use Illuminate\Database\Eloquent\Relations\MorphMany;use Illuminate\Database\Eloquent\SoftDeletes;use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;-#[ObservedBy([BudgetObserver::class])]class Budget extends Model{use ReturnsIntegerIdTrait;diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.phpindex 2870d51aa0..b324de1ce7 100644--- a/app/Repositories/Budget/BudgetRepository.php+++ b/app/Repositories/Budget/BudgetRepository.php@@ -26,6 +26,9 @@ namespace FireflyIII\Repositories\Budget;use Carbon\Carbon;use FireflyIII\Enums\AutoBudgetType;use FireflyIII\Enums\TransactionTypeEnum;+use FireflyIII\Events\Model\Budget\CreatedBudget;+use FireflyIII\Events\Model\Budget\UpdatedBudget;+use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending;use FireflyIII\Exceptions\FireflyException;use FireflyIII\Helpers\Collector\GroupCollectorInterface;use FireflyIII\Models\Account;@@ -303,6 +306,10 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface}$budget->save();+ $createWebhookMessages = $data['fire_webhooks'] ?? true;+ event(new UpdatedBudget($budget, $createWebhookMessages));+ event(new WebhookMessagesRequestSending());+// update or create auto-budget:$autoBudget = $this->getAutoBudget($budget);@@ -592,7 +599,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterfacepublic function setBudgetOrder(Budget $budget, int $order): void{$budget->order = $order;- $budget->save();+ $budget->saveQuietly();}public function spentInPeriod(Carbon $start, Carbon $end): array@@ -754,6 +761,10 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface$this->setNoteText($newBudget, (string) $data['notes']);}+ $createWebhookMessages = $data['fire_webhooks'] ?? true;+ event(new CreatedBudget($newBudget, $createWebhookMessages));+ event(new WebhookMessagesRequestSending());+if (!array_key_exists('auto_budget_type', $data) || !array_key_exists('auto_budget_amount', $data) || !array_key_exists('auto_budget_period', $data)) {return $newBudget;}@@ -809,6 +820,7 @@ class BudgetRepository implements BudgetRepositoryInterface, UserGroupInterface'start_date' => $start,'end_date' => $end,'amount' => $autoBudget->amount,+ 'fire_webhooks' => false,]);return $newBudget;diff --git a/app/Services/Internal/Destroy/BudgetDestroyService.php b/app/Services/Internal/Destroy/BudgetDestroyService.phpindex 0391ce924a..998bfa9707 100644--- a/app/Services/Internal/Destroy/BudgetDestroyService.php+++ b/app/Services/Internal/Destroy/BudgetDestroyService.php@@ -24,8 +24,19 @@ declare(strict_types=1);namespace FireflyIII\Services\Internal\Destroy;+use FireflyIII\Enums\WebhookTrigger;+use FireflyIII\Events\Model\Budget\DestroyedBudget;+use FireflyIII\Events\Model\Budget\DestroyingBudget;+use FireflyIII\Events\Model\Webhook\WebhookMessagesRequestSending;+use FireflyIII\Generator\Webhook\MessageGeneratorInterface;+use FireflyIII\Models\Attachment;use FireflyIII\Models\Budget;+use FireflyIII\Models\BudgetLimit;+use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;+use FireflyIII\Support\Models\AvailableBudgetCalculator;+use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Log;/*** Class BudgetDestroyService@@ -34,12 +45,38 @@ class BudgetDestroyService{public function destroy(Budget $budget): void{- $budget->delete();+ // first trigger delete event so the webhook messages can get generated.+ event(new DestroyingBudget($budget));++ // remove attachments+ $repository = app(AttachmentRepositoryInterface::class);+ $repository->setUser($budget->user);++ /** @var Attachment $attachment */+ foreach ($budget->attachments()->get() as $attachment) {+ $repository->destroy($attachment);+ }- // also delete auto budget:- foreach ($budget->autoBudgets()->get() as $autoBudget) {- $autoBudget->delete();+ // get budget limits, recalculate for period each time, then delete .+ $budgetLimits = $budget->budgetlimits()->get();++ /** @var BudgetLimit $budgetLimit */+ foreach ($budgetLimits as $budgetLimit) {+ // need to recalculate all available budgets for this user.+ $calculator = new AvailableBudgetCalculator();+ $calculator->setUser($budget->user);+ $calculator->setStart($budgetLimit->start_date);+ $calculator->setEnd($budgetLimit->end_date);+ $calculator->setCreate(false);+ $calculator->setCurrency($budgetLimit->transactionCurrency);++ // delete budget limit so the recalculation ignores it.+ $budgetLimit->delete();+ $calculator->recalculateByRange();}+ // delete notes and other objects.+ $budget->notes()->delete();+ $budget->autoBudgets()->delete();// also delete all relations between categories and transaction journals:DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();@@ -47,9 +84,7 @@ class BudgetDestroyService// also delete all relations between categories and transactions:DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();- // also delete all budget limits- foreach ($budget->budgetlimits()->get() as $limit) {- $limit->delete();- }+ $budget->delete();+ event(new DestroyedBudget());}}