Files
firefly-iii/app/Services/Internal/Destroy/BudgetDestroyService.php

86 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2019-01-11 16:57:40 +01:00
<?php
2019-01-11 16:57:40 +01:00
/**
* BudgetDestroyService.php
2020-02-16 13:56:35 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-01-11 16:57:40 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-01-11 16:57:40 +01:00
*
* 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.
2019-01-11 16:57:40 +01:00
*
* This program is distributed in the hope that it will be useful,
2019-01-11 16:57:40 +01:00
* 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.
2019-01-11 16:57:40 +01:00
*
* 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/>.
2019-01-11 16:57:40 +01:00
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Destroy;
2026-02-06 13:47:18 +01:00
use FireflyIII\Events\Model\Budget\DestroyedBudget;
use FireflyIII\Events\Model\Budget\DestroyingBudget;
use FireflyIII\Models\Attachment;
2019-01-11 16:57:40 +01:00
use FireflyIII\Models\Budget;
2026-02-06 13:47:18 +01:00
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
use FireflyIII\Support\Models\AvailableBudgetCalculator;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Facades\DB;
2019-01-11 16:57:40 +01:00
/**
* Class BudgetDestroyService
*/
class BudgetDestroyService
{
public function destroy(Budget $budget): void
{
2026-02-06 13:47:18 +01:00
// first trigger delete event so the webhook messages can get generated.
event(new DestroyingBudget($budget));
// remove attachments
$repository = app(AttachmentRepositoryInterface::class);
2026-02-06 13:47:18 +01:00
$repository->setUser($budget->user);
/** @var Attachment $attachment */
foreach ($budget->attachments()->get() as $attachment) {
$repository->destroy($attachment);
}
2019-01-11 16:57:40 +01:00
2026-02-06 13:47:18 +01:00
// 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();
}
2026-02-06 13:47:18 +01:00
// delete notes and other objects.
$budget->notes()->delete();
$budget->autoBudgets()->delete();
2019-01-11 16:57:40 +01:00
// also delete all relations between categories and transaction journals:
2025-02-23 12:47:04 +01:00
DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->delete();
2019-01-11 16:57:40 +01:00
// also delete all relations between categories and transactions:
2025-02-23 12:47:04 +01:00
DB::table('budget_transaction')->where('budget_id', $budget->id)->delete();
2020-02-11 05:34:36 +01:00
2026-02-06 13:47:18 +01:00
$budget->delete();
event(new DestroyedBudget());
2019-01-11 16:57:40 +01:00
}
}