Files
firefly-iii/app/Http/Controllers/Transaction/DeleteController.php

145 lines
5.0 KiB
PHP
Raw Normal View History

2019-08-01 06:22:07 +02:00
<?php
/**
* DeleteController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 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/>.
*/
2019-08-17 12:09:03 +02:00
declare(strict_types=1);
2019-08-01 06:22:07 +02:00
namespace FireflyIII\Http\Controllers\Transaction;
2021-03-28 11:46:23 +02:00
use FireflyIII\Events\UpdatedAccount;
2019-08-01 06:22:07 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
2019-08-01 06:22:07 +02:00
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
2019-08-01 06:22:07 +02:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
2021-09-18 10:20:19 +02:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
2019-08-01 06:22:07 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2019-08-01 06:22:07 +02:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class DeleteController
*/
class DeleteController extends Controller
{
2021-04-07 07:53:05 +02:00
private TransactionGroupRepositoryInterface $repository;
2019-08-01 06:22:07 +02:00
/**
* IndexController constructor.
*
2019-08-01 06:22:07 +02:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
2022-12-29 19:42:26 +01:00
app('view')->share('title', (string)trans('firefly.transactions'));
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2019-08-01 06:22:07 +02:00
$this->repository = app(TransactionGroupRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Shows the form that allows a user to delete a transaction journal.
*
2023-06-21 12:34:58 +02:00
* @param TransactionGroup $group
2019-08-01 06:22:07 +02:00
*
2021-09-18 10:20:19 +02:00
* @return Factory|View|Redirector|RedirectResponse
2019-08-01 06:22:07 +02:00
*/
public function delete(TransactionGroup $group)
{
2019-08-17 08:29:35 +02:00
if (!$this->isEditableGroup($group)) {
return $this->redirectGroupToAccount($group);
2019-08-17 08:29:35 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Start of delete view for group #%d', $group->id));
2019-08-01 06:22:07 +02:00
$journal = $group->transactionJournals->first();
if (null === $journal) {
2022-10-30 14:24:28 +01:00
throw new NotFoundHttpException();
2019-08-01 06:22:07 +02:00
}
$objectType = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
2023-06-21 12:34:58 +02:00
$subTitle = (string)trans('firefly.delete_' . $objectType, ['description' => $group->title ?? $journal->description]);
2022-12-31 06:57:05 +01:00
$previous = app('steam')->getSafePreviousUrl();
2019-08-01 06:22:07 +02:00
// put previous url in session
2023-10-29 06:33:43 +01:00
app('log')->debug('Will try to remember previous URL');
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('transactions.delete.url');
2019-08-01 06:22:07 +02:00
return view('transactions.delete', compact('group', 'journal', 'subTitle', 'objectType', 'previous'));
2019-08-01 06:22:07 +02:00
}
/**
* Actually destroys the journal.
*
2023-06-21 12:34:58 +02:00
* @param TransactionGroup $group
2019-08-01 06:22:07 +02:00
*
2023-11-28 04:45:07 +01:00
* @return RedirectResponse|Redirector
2019-08-01 06:22:07 +02:00
*/
2023-12-10 06:51:59 +01:00
public function destroy(TransactionGroup $group): RedirectResponse | Redirector
2019-08-01 06:22:07 +02:00
{
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now in %s(#%d).', __METHOD__, $group->id));
2019-08-17 08:29:35 +02:00
if (!$this->isEditableGroup($group)) {
return $this->redirectGroupToAccount($group);
2019-08-17 08:29:35 +02:00
}
2019-08-01 06:22:07 +02:00
$journal = $group->transactionJournals->first();
if (null === $journal) {
2022-10-30 14:24:28 +01:00
throw new NotFoundHttpException();
2019-08-01 06:22:07 +02:00
}
$objectType = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
2023-06-21 12:34:58 +02:00
session()->flash('success', (string)trans('firefly.deleted_' . strtolower($objectType), ['description' => $group->title ?? $journal->description]));
2019-08-01 06:22:07 +02:00
// grab asset account(s) from group:
$accounts = [];
2023-11-05 08:15:17 +01:00
/** @var TransactionJournal $currentJournal */
foreach ($group->transactionJournals as $currentJournal) {
/** @var Transaction $transaction */
2023-11-05 08:15:17 +01:00
foreach ($currentJournal->transactions as $transaction) {
$type = $transaction->account->accountType->type;
// if is valid liability, trigger event!
2022-10-30 14:24:28 +01:00
if (in_array($type, config('firefly.valid_liabilities'), true)) {
$accounts[] = $transaction->account;
}
}
}
2019-08-01 06:22:07 +02:00
$this->repository->destroy($group);
/** @var Account $account */
2022-10-30 14:24:28 +01:00
foreach ($accounts as $account) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Now going to trigger updated account event for account #%d', $account->id));
event(new UpdatedAccount($account));
}
app('preferences')->mark();
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('transactions.delete.url'));
2019-08-01 06:22:07 +02:00
}
2019-08-17 12:09:03 +02:00
}