Skip to content

Firefly III: a personal finances manager

Fix #1996

James Cole7f91ff4b0cc1
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index a4c3b4bb31..452ee2cfd9 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -37,6 +37,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
+use FireflyIII\Services\Internal\Destroy\BudgetDestroyService;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
@@ -185,11 +186,9 @@ class BudgetRepository implements BudgetRepositoryInterface
*/
public function destroy(Budget $budget): bool
{
- try {
- $budget->delete();
- } catch (Exception $e) {
- Log::error(sprintf('Could not delete budget: %s', $e->getMessage()));
- }
+ /** @var BudgetDestroyService $service */
+ $service = app(BudgetDestroyService::class);
+ $service->destroy($budget);
return true;
}
diff --git a/app/Services/Internal/Destroy/BudgetDestroyService.php b/app/Services/Internal/Destroy/BudgetDestroyService.php
new file mode 100644
index 0000000000..4f1f1b06cf
--- /dev/null
+++ b/app/Services/Internal/Destroy/BudgetDestroyService.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ * BudgetDestroyService.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Services\Internal\Destroy;
+
+use DB;
+use Exception;
+use FireflyIII\Models\Budget;
+use Log;
+
+/**
+ * Class BudgetDestroyService
+ */
+class BudgetDestroyService
+{
+ /**
+ * Constructor.
+ */
+ public function __construct()
+ {
+ if ('testing' === config('app.env')) {
+ Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
+ }
+ }
+
+ /**
+ * @param Budget $budget
+ */
+ public function destroy(Budget $budget): void
+ {
+ try {
+ $budget->delete();
+ } catch (Exception $e) { // @codeCoverageIgnore
+ Log::error(sprintf('Could not delete budget: %s', $e->getMessage())); // @codeCoverageIgnore
+ }
+
+ // also delete all relations between categories and transaction journals:
+ DB::table('budget_transaction_journal')->where('budget_id', (int)$budget->id)->delete();
+
+ // also delete all relations between categories and transactions:
+ DB::table('budget_transaction')->where('budget_id', (int)$budget->id)->delete();
+ }
+}
diff --git a/app/Services/Internal/Destroy/CategoryDestroyService.php b/app/Services/Internal/Destroy/CategoryDestroyService.php
index 7a00e06942..d8d34ab00b 100644
--- a/app/Services/Internal/Destroy/CategoryDestroyService.php
+++ b/app/Services/Internal/Destroy/CategoryDestroyService.php
@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Services\Internal\Destroy;
+use DB;
use Exception;
use FireflyIII\Models\Category;
use Log;
@@ -52,5 +53,11 @@ class CategoryDestroyService
} catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete category: %s', $e->getMessage())); // @codeCoverageIgnore
}
+
+ // also delete all relations between categories and transaction journals:
+ DB::table('category_transaction_journal')->where('category_id', (int)$category->id)->delete();
+
+ // also delete all relations between categories and transactions:
+ DB::table('category_transaction')->where('category_id', (int)$category->id)->delete();
}
}