Skip to content

Commit

Permalink
Fix #1996
Browse files Browse the repository at this point in the history
  • Loading branch information
JC5 committed Jan 11, 2019
1 parent ba778d4 commit 7f91ff4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/Repositories/Budget/BudgetRepository.php
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -185,11 +186,9 @@ public function collectBudgetInformation(Collection $budgets, Carbon $start, Car
*/
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;
}
Expand Down
63 changes: 63 additions & 0 deletions app/Services/Internal/Destroy/BudgetDestroyService.php
@@ -0,0 +1,63 @@
<?php
/**
* BudgetDestroyService.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* 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();
}
}
7 changes: 7 additions & 0 deletions app/Services/Internal/Destroy/CategoryDestroyService.php
Expand Up @@ -23,6 +23,7 @@

namespace FireflyIII\Services\Internal\Destroy;

use DB;
use Exception;
use FireflyIII\Models\Category;
use Log;
Expand Down Expand Up @@ -52,5 +53,11 @@ public function destroy(Category $category): void
} 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();
}
}

0 comments on commit 7f91ff4

Please sign in to comment.