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

150 lines
5.5 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* 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;
2019-05-30 06:23:25 +02:00
use Exception;
2017-12-28 19:03:15 +01:00
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2018-04-06 21:38:43 +02:00
use FireflyIII\Http\Middleware\Installer;
2017-12-15 12:59:21 +01:00
use FireflyIII\Models\AccountType;
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;
use Illuminate\Contracts\View\Factory;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2016-06-11 07:38:30 +02:00
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
2016-05-13 15:53:39 +02:00
use Illuminate\Support\Collection;
use Illuminate\View\View;
2016-06-11 07:38:30 +02:00
use Log;
2021-04-06 08:51:27 +02:00
use DB;
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.
*
2019-07-21 17:15:06 +02:00
* @codeCoverageIgnore
*/
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.
*
2016-06-11 07:38:30 +02:00
* @param Request $request
*
* @return JsonResponse
2020-08-05 18:51:17 +02: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
{
2016-06-11 07:38:30 +02:00
$start = new Carbon($request->get('start'));
$end = new Carbon($request->get('end'));
$label = $request->get('label');
2016-04-29 08:56:56 +02:00
$isCustomRange = false;
2016-04-25 21:37:08 +02:00
2016-06-11 07:38:30 +02:00
Log::debug('Received dateRange', ['start' => $request->get('start'), 'end' => $request->get('end'), '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.
2021-03-23 06:23:30 +01:00
if ($label === (string)trans('firefly.everything') || $label === (string)trans('firefly.customRange')) {
2016-04-29 08:56:56 +02:00
$isCustomRange = true;
2016-06-11 07:38:30 +02:00
Log::debug('Range is now marked as "custom".');
2016-04-25 21:37:08 +02:00
}
2015-03-02 13:19:13 +01:00
2020-12-30 19:25:53 +01:00
$diff = $start->diffInDays($end) + 1;
2015-03-02 13:19:13 +01:00
if ($diff > 50) {
2021-03-23 06:23:30 +01:00
$request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => $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);
Log::debug(sprintf('Set is_custom_range to %s', var_export($isCustomRange, true)));
$request->session()->put('start', $start);
Log::debug(sprintf('Set start to %s', $start->format('Y-m-d H:i:s')));
$request->session()->put('end', $end);
Log::debug(sprintf('Set end to %s', $end->format('Y-m-d H:i:s')));
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.
*
2017-03-05 13:21:36 +01:00
* @param AccountRepositoryInterface $repository
*
* @return Factory|RedirectResponse|Redirector|View
2020-08-05 18:51:17 +02:00
* @throws Exception
2015-02-06 07:23:26 +01:00
*/
2017-03-05 13:21:36 +01:00
public function index(AccountRepositoryInterface $repository)
2015-02-06 07:23:26 +01:00
{
2021-03-23 06:23:30 +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
}
2021-03-23 06:23:30 +01:00
$subTitle = (string)trans('firefly.welcome_back');
2016-10-29 07:44:46 +02:00
$transactions = [];
2020-10-24 07:18:37 +02:00
$frontPage = app('preferences')->getFresh('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray());
2016-02-05 15:41:40 +01:00
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
2016-02-05 15:41:40 +01:00
/** @var Carbon $end */
2018-07-13 15:50:42 +02:00
$end = session('end', Carbon::now()->endOfMonth());
2018-07-09 19:24:08 +02:00
/** @noinspection NullPointerExceptionInspection */
2017-09-16 09:24:48 +02:00
$accounts = $repository->getAccountsById($frontPage->data);
2020-09-11 07:12:33 +02:00
$today = today(config('app.timezone'));
2020-10-24 07:18:37 +02:00
Log::debug('Frontpage accounts are ', $frontPage->data);
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();
event(new RequestedVersionCheckStatus($user));
2017-12-28 19:03:15 +01:00
return prefixView('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today'));
2015-02-06 07:23:26 +01:00
}
2015-02-06 04:39:52 +01:00
}