Files
firefly-iii/app/Api/V1/Controllers/Data/DestroyController.php

208 lines
8.9 KiB
PHP
Raw Normal View History

2020-07-11 15:13:15 +02:00
<?php
2021-03-07 08:16:33 +01:00
/*
2020-07-11 15:13:15 +02:00
* DestroyController.php
2021-03-07 08:16:33 +01:00
* Copyright (c) 2021 james@firefly-iii.org
2020-07-11 15:13:15 +02:00
*
* 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\Api\V1\Controllers\Data;
use FireflyIII\Api\V1\Controllers\Controller;
2021-03-07 08:16:33 +01:00
use FireflyIII\Api\V1\Requests\Data\DestroyRequest;
2020-07-11 15:13:15 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\ObjectGroup\ObjectGroupRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\AccountDestroyService;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
use Illuminate\Http\JsonResponse;
2024-01-05 10:55:46 +01:00
use Illuminate\Support\Facades\Log;
2020-07-11 15:13:15 +02:00
/**
* Class DestroyController
*/
class DestroyController extends Controller
{
2022-10-19 19:57:22 +02:00
private bool $unused;
2020-07-11 15:13:15 +02:00
/**
2021-09-18 05:58:22 +02:00
* This endpoint is documented at:
2023-02-12 06:53:36 +01:00
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/data/destroyData
2021-09-18 05:58:22 +02:00
*
2021-03-07 08:16:33 +01:00
* @throws FireflyException
2020-07-11 15:13:15 +02:00
*/
2021-03-07 08:16:33 +01:00
public function destroy(DestroyRequest $request): JsonResponse
2020-07-11 15:13:15 +02:00
{
$objects = $request->getObjects();
$this->unused = $request->boolean('unused', false);
2023-12-02 07:05:34 +01:00
2023-12-20 19:35:52 +01:00
$allExceptAssets = [AccountType::BENEFICIARY, AccountType::CASH, AccountType::CREDITCARD, AccountType::DEFAULT, AccountType::EXPENSE, AccountType::IMPORT, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT, AccountType::RECONCILIATION, AccountType::REVENUE];
$all = [AccountType::ASSET, AccountType::BENEFICIARY, AccountType::CASH, AccountType::CREDITCARD, AccountType::DEBT, AccountType::DEFAULT, AccountType::EXPENSE, AccountType::IMPORT, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::RECONCILIATION];
2023-12-02 07:05:34 +01:00
$liabilities = [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
2023-12-20 19:35:52 +01:00
$transactions = [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER, TransactionType::RECONCILIATION, TransactionType::OPENING_BALANCE];
2023-12-02 07:05:34 +01:00
match ($objects) {
'budgets' => $this->destroyBudgets(),
'bills' => $this->destroyBills(),
'piggy_banks' => $this->destroyPiggyBanks(),
'rules' => $this->destroyRules(),
'recurring' => $this->destroyRecurringTransactions(),
'categories' => $this->destroyCategories(),
'tags' => $this->destroyTags(),
'object_groups' => $this->destroyObjectGroups(),
'not_assets_liabilities' => $this->destroyAccounts($allExceptAssets),
'accounts' => $this->destroyAccounts($all),
'asset_accounts' => $this->destroyAccounts([AccountType::ASSET, AccountType::DEFAULT]),
'expense_accounts' => $this->destroyAccounts([AccountType::BENEFICIARY, AccountType::EXPENSE]),
'revenue_accounts' => $this->destroyAccounts([AccountType::REVENUE]),
'liabilities' => $this->destroyAccounts($liabilities),
'transactions' => $this->destroyTransactions($transactions),
'withdrawals' => $this->destroyTransactions([TransactionType::WITHDRAWAL]),
'deposits' => $this->destroyTransactions([TransactionType::DEPOSIT]),
'transfers' => $this->destroyTransactions([TransactionType::TRANSFER]),
default => throw new FireflyException(sprintf('200033: This endpoint can\'t handle object "%s"', $objects)),
};
2022-12-24 08:44:22 +01:00
app('preferences')->mark();
2020-07-11 15:13:15 +02:00
return response()->json([], 204);
}
private function destroyBudgets(): void
{
/** @var AvailableBudgetRepositoryInterface $abRepository */
$abRepository = app(AvailableBudgetRepositoryInterface::class);
2020-07-11 15:13:15 +02:00
$abRepository->destroyAll();
/** @var BudgetLimitRepositoryInterface $blRepository */
$blRepository = app(BudgetLimitRepositoryInterface::class);
2020-07-11 15:13:15 +02:00
$blRepository->destroyAll();
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
$budgetRepository->destroyAll();
}
2023-06-21 12:34:58 +02:00
private function destroyBills(): void
2020-07-11 15:13:15 +02:00
{
2023-06-21 12:34:58 +02:00
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->destroyAll();
2020-07-11 15:13:15 +02:00
}
2023-05-29 13:56:55 +02:00
private function destroyPiggyBanks(): void
2020-10-18 08:00:49 +02:00
{
2023-05-29 13:56:55 +02:00
/** @var PiggyBankRepositoryInterface $repository */
$repository = app(PiggyBankRepositoryInterface::class);
2020-10-18 08:00:49 +02:00
$repository->destroyAll();
}
2023-06-21 12:34:58 +02:00
private function destroyRules(): void
{
/** @var RuleGroupRepositoryInterface $repository */
$repository = app(RuleGroupRepositoryInterface::class);
$repository->destroyAll();
}
2020-07-11 15:13:15 +02:00
private function destroyRecurringTransactions(): void
{
/** @var RecurringRepositoryInterface $repository */
$repository = app(RecurringRepositoryInterface::class);
$repository->destroyAll();
}
2023-06-21 12:34:58 +02:00
private function destroyCategories(): void
2020-07-11 15:13:15 +02:00
{
2023-06-21 12:34:58 +02:00
/** @var CategoryRepositoryInterface $categoryRepos */
$categoryRepos = app(CategoryRepositoryInterface::class);
$categoryRepos->destroyAll();
2020-07-11 15:13:15 +02:00
}
private function destroyTags(): void
{
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = app(TagRepositoryInterface::class);
$tagRepository->destroyAll();
}
2023-06-21 12:34:58 +02:00
private function destroyObjectGroups(): void
{
/** @var ObjectGroupRepositoryInterface $repository */
$repository = app(ObjectGroupRepositoryInterface::class);
$repository->deleteAll();
}
/**
2023-12-23 06:22:47 +01:00
* @param array<int, string> $types
2023-06-21 12:34:58 +02:00
*/
private function destroyAccounts(array $types): void
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$collection = $repository->getAccountsByType($types);
$service = app(AccountDestroyService::class);
/** @var Account $account */
foreach ($collection as $account) {
$count = $account->transactions()->count();
if (true === $this->unused && 0 === $count) {
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('Deleted unused account #%d "%s"', $account->id, $account->name));
2024-01-05 10:55:46 +01:00
Log::channel('audit')->info(sprintf('Deleted unused account #%d "%s"', $account->id, $account->name));
2023-06-21 12:34:58 +02:00
$service->destroy($account, null);
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
continue;
}
if (false === $this->unused) {
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('Deleting account #%d "%s"', $account->id, $account->name));
2024-01-05 10:55:46 +01:00
Log::channel('audit')->info(sprintf('Deleted account #%d "%s"', $account->id, $account->name));
2023-06-21 12:34:58 +02:00
$service->destroy($account, null);
}
}
}
2020-07-11 15:13:15 +02:00
/**
2023-12-23 06:22:47 +01:00
* @param array<int, string> $types
2020-07-11 15:13:15 +02:00
*/
private function destroyTransactions(array $types): void
{
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
2020-10-18 08:00:49 +02:00
$journals = $repository->findByType($types);
$service = app(JournalDestroyService::class);
2023-12-20 19:35:52 +01:00
2020-07-11 15:13:15 +02:00
/** @var TransactionJournal $journal */
2020-10-18 08:00:49 +02:00
foreach ($journals as $journal) {
2020-07-11 15:13:15 +02:00
$service->destroy($journal);
}
}
}