2023-09-21 11:29:09 +02:00
|
|
|
<?php
|
2023-09-21 11:32:56 +02:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
/*
|
|
|
|
|
* BudgetObserver.php
|
|
|
|
|
* Copyright (c) 2023 james@firefly-iii.org
|
|
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
2023-09-21 11:32:56 +02:00
|
|
|
declare(strict_types=1);
|
2023-09-21 11:29:09 +02:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Handlers\Observer;
|
|
|
|
|
|
2025-02-09 07:02:12 +01:00
|
|
|
use FireflyIII\Models\Attachment;
|
2023-09-21 11:29:09 +02:00
|
|
|
use FireflyIII\Models\Budget;
|
2023-12-16 19:11:27 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2025-02-05 06:36:51 +01:00
|
|
|
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
|
2023-09-21 11:29:09 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BudgetObserver
|
|
|
|
|
*/
|
|
|
|
|
class BudgetObserver
|
|
|
|
|
{
|
|
|
|
|
public function deleting(Budget $budget): void
|
|
|
|
|
{
|
|
|
|
|
app('log')->debug('Observe "deleting" of a budget.');
|
2025-02-05 06:36:51 +01:00
|
|
|
|
2025-02-05 06:41:08 +01:00
|
|
|
$repository = app(AttachmentRepositoryInterface::class);
|
2025-02-05 06:36:51 +01:00
|
|
|
$repository->setUser($budget->user);
|
|
|
|
|
|
2025-02-09 07:02:12 +01:00
|
|
|
/** @var Attachment $attachment */
|
2023-09-21 11:29:09 +02:00
|
|
|
foreach ($budget->attachments()->get() as $attachment) {
|
2025-02-05 06:36:51 +01:00
|
|
|
$repository->destroy($attachment);
|
2023-09-21 11:29:09 +02:00
|
|
|
}
|
2023-12-16 19:11:27 +01:00
|
|
|
$budgetLimits = $budget->budgetlimits()->get();
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-12-16 19:11:27 +01:00
|
|
|
/** @var BudgetLimit $budgetLimit */
|
2024-02-22 01:29:01 +01:00
|
|
|
foreach ($budgetLimits as $budgetLimit) {
|
2023-12-16 19:11:27 +01:00
|
|
|
// this loop exists so several events are fired.
|
|
|
|
|
$budgetLimit->delete();
|
|
|
|
|
}
|
2023-09-21 11:29:09 +02:00
|
|
|
|
|
|
|
|
$budget->notes()->delete();
|
|
|
|
|
$budget->autoBudgets()->delete();
|
2023-12-16 19:11:27 +01:00
|
|
|
|
|
|
|
|
// recalculate available budgets.
|
2023-09-21 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
}
|