2018-08-09 16:07:33 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-08-09 16:07:33 +02:00
|
|
|
/**
|
|
|
|
* UserNavigation.php
|
2020-02-16 13:56:52 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-08-09 16:07:33 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-08-09 16:07:33 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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.
|
2018-08-09 16:07:33 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-08-09 16:07:33 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-08-09 16:07:33 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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/>.
|
2018-08-09 16:07:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Http\Controllers;
|
|
|
|
|
2025-01-03 09:15:52 +01:00
|
|
|
use FireflyIII\Enums\AccountTypeEnum;
|
2025-01-03 09:05:19 +01:00
|
|
|
use FireflyIII\Enums\TransactionTypeEnum;
|
2018-08-09 17:46:14 +02:00
|
|
|
use FireflyIII\Models\Account;
|
2018-08-09 16:07:33 +02:00
|
|
|
use FireflyIII\Models\Transaction;
|
2019-08-17 08:29:35 +02:00
|
|
|
use FireflyIII\Models\TransactionGroup;
|
2018-08-09 16:07:33 +02:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2021-03-21 09:15:40 +01:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Routing\Redirector;
|
2018-08-09 16:07:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait UserNavigation
|
|
|
|
*/
|
|
|
|
trait UserNavigation
|
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
/**
|
|
|
|
* Functionality:.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* - If the $identifier contains the word "delete" then a remembered url with the text "/show/" in it will not be
|
|
|
|
* returned but instead the index (/) will be returned.
|
|
|
|
* - If the remembered url contains "jscript/" the remembered url will not be returned but instead the index (/)
|
|
|
|
* will be returned.
|
2021-03-21 09:15:40 +01:00
|
|
|
*/
|
2022-04-12 18:19:30 +02:00
|
|
|
final protected function getPreviousUrl(string $identifier): string
|
2021-03-21 09:15:40 +01:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
|
2024-12-22 08:43:12 +01:00
|
|
|
$url = (string) session($identifier);
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('The URL is %s', $url));
|
2021-03-21 09:15:40 +01:00
|
|
|
|
2021-10-03 06:05:47 +02:00
|
|
|
return app('steam')->getSafeUrl($url, route('index'));
|
2021-03-21 09:15:40 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:07:33 +02:00
|
|
|
/**
|
2019-08-17 08:29:35 +02:00
|
|
|
* Will return false if you cant edit this account type.
|
2018-08-09 16:07:33 +02:00
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function isEditableAccount(Account $account): bool
|
2018-08-09 16:07:33 +02:00
|
|
|
{
|
2025-01-03 09:15:52 +01:00
|
|
|
$editable = [AccountTypeEnum::EXPENSE->value, AccountTypeEnum::REVENUE->value, AccountTypeEnum::ASSET->value, AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
|
2019-08-17 08:29:35 +02:00
|
|
|
$type = $account->accountType->type;
|
2019-08-01 06:22:07 +02:00
|
|
|
|
2019-08-17 08:29:35 +02:00
|
|
|
return in_array($type, $editable, true);
|
|
|
|
}
|
|
|
|
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function isEditableGroup(TransactionGroup $group): bool
|
2019-08-17 08:29:35 +02:00
|
|
|
{
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|TransactionJournal $journal */
|
2024-12-22 20:37:54 +01:00
|
|
|
$journal = $group->transactionJournals()->first();
|
2019-08-17 08:29:35 +02:00
|
|
|
if (null === $journal) {
|
|
|
|
return false;
|
2018-08-09 16:07:33 +02:00
|
|
|
}
|
2019-08-17 08:29:35 +02:00
|
|
|
$type = $journal->transactionType->type;
|
2025-01-03 09:11:20 +01:00
|
|
|
$editable = [TransactionTypeEnum::WITHDRAWAL->value, TransactionTypeEnum::TRANSFER->value, TransactionTypeEnum::DEPOSIT->value, TransactionTypeEnum::RECONCILIATION->value];
|
2018-08-09 16:07:33 +02:00
|
|
|
|
2019-08-17 08:29:35 +02:00
|
|
|
return in_array($type, $editable, true);
|
2018-08-09 16:07:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:46:14 +02:00
|
|
|
/**
|
2023-12-20 19:35:52 +01:00
|
|
|
* @return Redirector|RedirectResponse
|
2018-08-09 17:46:14 +02:00
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function redirectAccountToAccount(Account $account)
|
2018-08-09 17:46:14 +02:00
|
|
|
{
|
2019-08-17 08:29:35 +02:00
|
|
|
$type = $account->accountType->type;
|
2025-01-03 09:15:52 +01:00
|
|
|
if (AccountTypeEnum::RECONCILIATION->value === $type || AccountTypeEnum::INITIAL_BALANCE->value === $type || AccountTypeEnum::LIABILITY_CREDIT->value === $type) {
|
2019-08-17 08:29:35 +02:00
|
|
|
// reconciliation must be stored somewhere in this account's transactions.
|
2018-08-09 17:46:14 +02:00
|
|
|
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|Transaction $transaction */
|
2019-08-17 08:29:35 +02:00
|
|
|
$transaction = $account->transactions()->first();
|
|
|
|
if (null === $transaction) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Account #%d has no transactions. Dont know where it belongs.', $account->id));
|
2019-08-17 08:29:35 +02:00
|
|
|
session()->flash('error', trans('firefly.cant_find_redirect_account'));
|
|
|
|
|
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
2024-12-22 20:37:54 +01:00
|
|
|
$journal = $transaction->transactionJournal;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
/** @var null|Transaction $other */
|
2024-12-22 20:37:54 +01:00
|
|
|
$other = $journal->transactions()->where('id', '!=', $transaction->id)->first();
|
2019-08-17 08:29:35 +02:00
|
|
|
if (null === $other) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Account #%d has no valid journals. Dont know where it belongs.', $account->id));
|
2019-08-17 08:29:35 +02:00
|
|
|
session()->flash('error', trans('firefly.cant_find_redirect_account'));
|
|
|
|
|
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(route('accounts.show', [$other->account_id]));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
|
2019-08-17 08:29:35 +02:00
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 19:35:52 +01:00
|
|
|
* @return Redirector|RedirectResponse
|
2019-08-17 08:29:35 +02:00
|
|
|
*/
|
2021-04-06 17:00:16 +02:00
|
|
|
final protected function redirectGroupToAccount(TransactionGroup $group)
|
2019-08-17 08:29:35 +02:00
|
|
|
{
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|TransactionJournal $journal */
|
2024-12-22 20:37:54 +01:00
|
|
|
$journal = $group->transactionJournals()->first();
|
2021-03-21 09:15:40 +01:00
|
|
|
if (null === $journal) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('No journals in group #%d', $group->id));
|
2018-08-09 17:46:14 +02:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
|
|
|
// prefer redirect to everything but expense and revenue:
|
|
|
|
$transactions = $journal->transactions;
|
2025-01-03 09:15:52 +01:00
|
|
|
$ignore = [AccountTypeEnum::REVENUE->value, AccountTypeEnum::EXPENSE->value, AccountTypeEnum::RECONCILIATION->value, AccountTypeEnum::INITIAL_BALANCE->value];
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$type = $transaction->account->accountType->type;
|
|
|
|
if (!in_array($type, $ignore, true)) {
|
|
|
|
return redirect(route('accounts.edit', [$transaction->account_id]));
|
|
|
|
}
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
return redirect(route('index'));
|
2018-08-09 17:46:14 +02:00
|
|
|
}
|
2018-08-10 17:05:37 +02:00
|
|
|
|
2022-04-12 18:19:30 +02:00
|
|
|
final protected function rememberPreviousUrl(string $identifier): ?string
|
2018-08-10 17:05:37 +02:00
|
|
|
{
|
2021-10-03 06:05:47 +02:00
|
|
|
$return = app('steam')->getSafePreviousUrl();
|
|
|
|
session()->put($identifier, $return);
|
2021-03-21 09:15:40 +01:00
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('rememberPreviousUrl: %s: "%s"', $identifier, $return));
|
2021-10-02 12:50:21 +02:00
|
|
|
|
2019-08-05 19:45:20 +02:00
|
|
|
return $return;
|
2018-08-10 17:05:37 +02:00
|
|
|
}
|
2018-12-31 07:48:23 +01:00
|
|
|
}
|