Files
firefly-iii/app/Http/Controllers/HomeController.php

199 lines
7.5 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
2022-12-29 19:41:57 +01:00
/**
* HomeController.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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* 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.
2017-10-21 08:40:00 +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/>.
*/
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Http\Controllers;
2015-02-06 04:39:52 +01:00
2015-02-07 06:49:24 +01:00
use Carbon\Carbon;
2023-04-05 20:22:17 +02:00
use Carbon\Exceptions\InvalidFormatException;
2025-01-03 09:15:52 +01:00
use FireflyIII\Enums\AccountTypeEnum;
2017-12-28 19:03:15 +01:00
use FireflyIII\Events\RequestedVersionCheckStatus;
2022-12-29 19:41:57 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-04-06 21:38:43 +02:00
use FireflyIII\Http\Middleware\Installer;
2017-03-05 13:21:36 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-10-16 19:01:26 +02:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2018-07-09 19:24:08 +02:00
use FireflyIII\User;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2016-06-11 07:38:30 +02:00
use Illuminate\Http\Request;
2016-05-13 15:53:39 +02:00
use Illuminate\Support\Collection;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2019-05-30 06:23:25 +02:00
2015-02-06 21:23:14 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class HomeController.
2015-02-06 21:23:14 +01:00
*/
2015-02-06 07:23:26 +01:00
class HomeController extends Controller
{
/**
* HomeController constructor.
*/
2016-01-08 18:29:47 +01:00
public function __construct()
{
2016-01-08 19:13:51 +01:00
parent::__construct();
2017-12-16 19:46:36 +01:00
app('view')->share('title', 'Firefly III');
app('view')->share('mainTitleIcon', 'fa-fire');
2018-04-06 21:38:43 +02:00
$this->middleware(Installer::class);
2016-01-08 18:29:47 +01:00
}
2015-02-06 04:39:52 +01:00
2016-06-11 07:38:30 +02:00
/**
2018-07-22 08:10:16 +02:00
* Change index date range.
*
2023-12-20 19:35:52 +01:00
* @throws \Exception
2016-06-11 07:38:30 +02:00
*/
2018-07-08 12:28:42 +02:00
public function dateRange(Request $request): JsonResponse
2015-03-02 13:19:13 +01:00
{
$stringStart = '';
$stringEnd = '';
2023-12-20 19:35:52 +01:00
2023-04-05 20:22:17 +02:00
try {
2024-05-21 17:48:54 +02:00
$stringStart = e((string) $request->get('start'));
2023-05-29 13:56:55 +02:00
$start = Carbon::createFromFormat('Y-m-d', $stringStart);
2023-04-05 20:22:17 +02:00
} catch (InvalidFormatException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Start: could not parse date string "%s" so ignore it.', $stringStart));
2023-04-05 20:22:17 +02:00
$start = Carbon::now()->startOfMonth();
}
2023-12-20 19:35:52 +01:00
2023-04-05 20:22:17 +02:00
try {
2024-05-21 17:48:54 +02:00
$stringEnd = e((string) $request->get('end'));
2023-05-29 13:56:55 +02:00
$end = Carbon::createFromFormat('Y-m-d', $stringEnd);
2023-04-05 20:22:17 +02:00
} catch (InvalidFormatException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('End could not parse date string "%s" so ignore it.', $stringEnd));
2023-04-05 20:22:17 +02:00
$end = Carbon::now()->endOfMonth();
}
if (null === $start) {
2023-11-28 04:45:07 +01:00
$start = Carbon::now()->startOfMonth();
}
if (null === $end) {
2023-11-28 04:45:07 +01:00
$end = Carbon::now()->endOfMonth();
}
2016-06-11 07:38:30 +02:00
$label = $request->get('label');
2016-04-29 08:56:56 +02:00
$isCustomRange = false;
2016-04-25 21:37:08 +02:00
2024-03-23 15:20:49 +01:00
app('log')->debug('dateRange: Received dateRange', ['start' => $stringStart, 'end' => $stringEnd, 'label' => $request->get('label')]);
2016-04-25 21:37:08 +02:00
// check if the label is "everything" or "Custom range" which will betray
// a possible problem with the budgets.
2024-05-21 17:48:54 +02:00
if ($label === (string) trans('firefly.everything') || $label === (string) trans('firefly.customRange')) {
2016-04-29 08:56:56 +02:00
$isCustomRange = true;
2023-10-29 06:33:43 +01:00
app('log')->debug('Range is now marked as "custom".');
2016-04-25 21:37:08 +02:00
}
2015-03-02 13:19:13 +01:00
$diff = $start->diffInDays($end, true) + 1;
2015-03-02 13:19:13 +01:00
2024-03-23 15:20:49 +01:00
if ($diff > 366) {
2024-05-21 17:48:54 +02:00
$request->session()->flash('warning', (string) trans('firefly.warning_much_data', ['days' => (int) $diff]));
2015-03-02 13:19:13 +01:00
}
2015-03-02 12:35:14 +01:00
2017-12-15 12:59:21 +01:00
$request->session()->put('is_custom_range', $isCustomRange);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set is_custom_range to %s', var_export($isCustomRange, true)));
2017-12-15 12:59:21 +01:00
$request->session()->put('start', $start);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set start to %s', $start->format('Y-m-d H:i:s')));
2017-12-15 12:59:21 +01:00
$request->session()->put('end', $end);
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set end to %s', $end->format('Y-m-d H:i:s')));
2017-12-15 12:59:21 +01:00
2018-03-10 20:30:09 +01:00
return response()->json(['ok' => 'ok']);
2015-03-02 12:35:14 +01:00
}
2021-03-28 11:46:23 +02:00
2015-02-06 07:23:26 +01:00
/**
2018-07-22 08:10:16 +02:00
* Show index.
*
2022-12-29 19:41:57 +01:00
* @throws FireflyException
2015-02-06 07:23:26 +01:00
*/
2021-05-23 20:59:18 +02:00
public function index(AccountRepositoryInterface $repository): mixed
2015-02-06 07:23:26 +01:00
{
$types = config('firefly.accountTypesByIdentifier.asset');
$count = $repository->count($types);
2019-02-08 07:13:59 +01:00
Log::channel('audit')->info('User visits homepage.');
2017-11-15 12:25:49 +01:00
if (0 === $count) {
2015-07-06 16:27:21 +02:00
return redirect(route('new-user.index'));
2015-06-01 18:13:54 +02:00
}
2024-04-07 06:06:40 +02:00
2024-05-21 17:48:54 +02:00
if ('v1' === (string) config('view.layout')) {
2024-04-07 06:06:40 +02:00
return $this->indexV1($repository);
}
2024-05-21 17:48:54 +02:00
if ('v2' === (string) config('view.layout')) {
2024-04-07 06:06:40 +02:00
return $this->indexV2();
}
2024-04-07 06:06:40 +02:00
throw new FireflyException('Invalid layout configuration');
}
private function indexV1(AccountRepositoryInterface $repository): mixed
{
$types = config('firefly.accountTypesByIdentifier.asset');
2024-05-21 17:48:54 +02:00
$pageTitle = (string) trans('firefly.main_dashboard_page_title');
$count = $repository->count($types);
2024-05-21 17:48:54 +02:00
$subTitle = (string) trans('firefly.welcome_back');
2023-11-28 04:45:07 +01:00
$transactions = [];
2025-01-03 09:15:52 +01:00
$frontpage = app('preferences')->getFresh('frontpageAccounts', $repository->getAccountsByType([AccountTypeEnum::ASSET->value])->pluck('id')->toArray());
2024-04-01 20:26:02 +02:00
$frontpageArray = $frontpage->data;
if (!is_array($frontpageArray)) {
$frontpageArray = [];
2023-11-28 04:45:07 +01:00
}
2016-02-05 15:41:40 +01:00
/** @var Carbon $start */
$start = session('start', today(config('app.timezone'))->startOfMonth());
2024-04-23 19:40:48 +02:00
/** @var Carbon $end */
$end = session('end', today(config('app.timezone'))->endOfMonth());
$accounts = $repository->getAccountsById($frontpageArray);
$today = today(config('app.timezone'));
$accounts = $accounts->sortBy('order'); // sort frontpage accounts by order
2023-08-29 18:28:29 +02:00
2024-04-01 20:26:02 +02:00
app('log')->debug('Frontpage accounts are ', $frontpageArray);
2020-10-24 07:18:37 +02:00
2017-10-16 19:01:26 +02:00
/** @var BillRepositoryInterface $billRepository */
$billRepository = app(BillRepositoryInterface::class);
$billCount = $billRepository->getBills()->count();
2020-08-05 18:51:17 +02:00
// collect groups for each transaction.
2015-02-07 06:49:24 +01:00
foreach ($accounts as $account) {
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
2020-08-05 18:51:17 +02:00
$collector->setAccounts(new Collection([$account]))->withAccountInformation()->setRange($start, $end)->setLimit(10)->setPage(1);
$set = $collector->getExtractedJournals();
$transactions[] = ['transactions' => $set, 'account' => $account];
2015-02-07 06:49:24 +01:00
}
2015-06-03 21:25:11 +02:00
2018-07-09 19:24:08 +02:00
/** @var User $user */
$user = auth()->user();
2018-07-09 19:24:08 +02:00
event(new RequestedVersionCheckStatus($user));
2017-12-28 19:03:15 +01:00
2024-05-21 17:48:54 +02:00
return view('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today', 'pageTitle'));
2015-02-06 07:23:26 +01:00
}
2024-04-07 06:06:40 +02:00
private function indexV2(): mixed
{
2024-05-21 17:48:54 +02:00
$subTitle = (string) trans('firefly.welcome_back');
$pageTitle = (string) trans('firefly.main_dashboard_page_title');
2024-04-07 06:06:40 +02:00
$start = session('start', today(config('app.timezone'))->startOfMonth());
$end = session('end', today(config('app.timezone'))->endOfMonth());
2024-04-07 06:06:40 +02:00
/** @var User $user */
$user = auth()->user();
2024-04-07 06:06:40 +02:00
event(new RequestedVersionCheckStatus($user));
2024-05-21 17:48:54 +02:00
return view('index', compact('subTitle', 'start', 'end', 'pageTitle'));
2024-04-07 06:06:40 +02:00
}
2015-02-06 04:39:52 +01:00
}