2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2022-12-29 12:41:57 -06:00
|
|
|
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* HomeController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-20 01:57:45 -05:00
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-05 21:39:52 -06:00
|
|
|
|
2015-02-06 23:49:24 -06:00
|
|
|
use Carbon\Carbon;
|
2023-04-05 13:22:17 -05:00
|
|
|
use Carbon\Exceptions\InvalidFormatException;
|
2019-05-29 23:23:25 -05:00
|
|
|
use Exception;
|
2017-12-28 12:03:15 -06:00
|
|
|
use FireflyIII\Events\RequestedVersionCheckStatus;
|
2022-12-29 12:41:57 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-03-24 03:23:36 -05:00
|
|
|
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
2018-04-06 14:38:43 -05:00
|
|
|
use FireflyIII\Http\Middleware\Installer;
|
2022-04-13 09:23:02 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2017-03-05 06:21:36 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-10-16 12:01:26 -05:00
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
2018-07-09 12:24:08 -05:00
|
|
|
use FireflyIII\User;
|
2018-07-08 05:28:42 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-06-11 00:38:30 -05:00
|
|
|
use Illuminate\Http\Request;
|
2016-05-13 08:53:39 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2023-04-01 00:04:42 -05:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-05-29 23:23:25 -05:00
|
|
|
|
2015-02-06 14:23:14 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class HomeController.
|
2015-02-06 14:23:14 -06:00
|
|
|
*/
|
2015-02-06 00:23:26 -06:00
|
|
|
class HomeController extends Controller
|
|
|
|
{
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
|
|
|
* HomeController constructor.
|
2020-01-31 00:24:41 -06:00
|
|
|
*
|
2023-02-12 00:15:06 -06:00
|
|
|
|
2016-01-09 01:20:55 -06:00
|
|
|
*/
|
2016-01-08 11:29:47 -06:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-01-08 12:13:51 -06:00
|
|
|
parent::__construct();
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('title', 'Firefly III');
|
|
|
|
app('view')->share('mainTitleIcon', 'fa-fire');
|
2018-04-06 14:38:43 -05:00
|
|
|
$this->middleware(Installer::class);
|
2016-01-08 11:29:47 -06:00
|
|
|
}
|
2015-02-05 21:39:52 -06:00
|
|
|
|
2016-06-11 00:38:30 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Change index date range.
|
|
|
|
*
|
2022-12-29 12:41:57 -06:00
|
|
|
* @param Request $request
|
2020-01-31 00:24:41 -06:00
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return JsonResponse
|
2020-08-05 11:51:17 -05:00
|
|
|
* @throws Exception
|
2016-06-11 00:38:30 -05:00
|
|
|
*/
|
2018-07-08 05:28:42 -05:00
|
|
|
public function dateRange(Request $request): JsonResponse
|
2015-03-02 06:19:13 -06:00
|
|
|
{
|
2023-04-05 13:22:17 -05:00
|
|
|
try {
|
|
|
|
$stringStart = e((string)$request->get('start'));
|
|
|
|
$start = Carbon::createFromFormat('Y-m-d', $stringStart);
|
|
|
|
} catch (InvalidFormatException $e) {
|
|
|
|
Log::error(sprintf('Start: could not parse date string "%s" so ignore it.', $stringStart));
|
|
|
|
$start = Carbon::now()->startOfMonth();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$stringEnd = e((string)$request->get('end'));
|
|
|
|
$end = Carbon::createFromFormat('Y-m-d', $stringEnd);
|
|
|
|
} catch (InvalidFormatException $e) {
|
|
|
|
Log::error(sprintf('End could not parse date string "%s" so ignore it.', $stringEnd));
|
|
|
|
$end = Carbon::now()->endOfMonth();
|
|
|
|
}
|
2016-06-11 00:38:30 -05:00
|
|
|
$label = $request->get('label');
|
2016-04-29 01:56:56 -05:00
|
|
|
$isCustomRange = false;
|
2016-04-25 14:37:08 -05:00
|
|
|
|
2023-04-05 13:22:17 -05:00
|
|
|
Log::debug('Received dateRange', ['start' => $stringStart, 'end' => $stringEnd, 'label' => $request->get('label')]);
|
2016-04-25 14:37:08 -05:00
|
|
|
// check if the label is "everything" or "Custom range" which will betray
|
|
|
|
// a possible problem with the budgets.
|
2022-12-29 12:41:57 -06:00
|
|
|
if ($label === (string)trans('firefly.everything') || $label === (string)trans('firefly.customRange')) {
|
2016-04-29 01:56:56 -05:00
|
|
|
$isCustomRange = true;
|
2016-06-11 00:38:30 -05:00
|
|
|
Log::debug('Range is now marked as "custom".');
|
2016-04-25 14:37:08 -05:00
|
|
|
}
|
2015-03-02 06:19:13 -06:00
|
|
|
|
2020-12-30 12:25:53 -06:00
|
|
|
$diff = $start->diffInDays($end) + 1;
|
2015-03-02 06:19:13 -06:00
|
|
|
|
|
|
|
if ($diff > 50) {
|
2022-12-29 12:41:57 -06:00
|
|
|
$request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => $diff]));
|
2015-03-02 06:19:13 -06:00
|
|
|
}
|
2015-03-02 05:35:14 -06:00
|
|
|
|
2017-12-15 05:59:21 -06: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 13:30:09 -06:00
|
|
|
return response()->json(['ok' => 'ok']);
|
2015-03-02 05:35:14 -06:00
|
|
|
}
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2015-02-06 00:23:26 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Show index.
|
|
|
|
*
|
2022-12-29 12:41:57 -06:00
|
|
|
* @param AccountRepositoryInterface $repository
|
2020-01-31 00:24:41 -06:00
|
|
|
*
|
2021-05-24 01:54:58 -05:00
|
|
|
* @return mixed
|
2022-12-29 12:41:57 -06:00
|
|
|
* @throws FireflyException
|
2015-02-06 00:23:26 -06:00
|
|
|
*/
|
2021-05-23 13:59:18 -05:00
|
|
|
public function index(AccountRepositoryInterface $repository): mixed
|
2015-02-06 00:23:26 -06:00
|
|
|
{
|
2022-01-02 00:22:04 -06:00
|
|
|
if ('v3' === config('firefly.layout')) {
|
2022-02-27 03:04:08 -06:00
|
|
|
return view('pwa');
|
2022-01-02 00:22:04 -06:00
|
|
|
}
|
2021-04-07 00:28:43 -05:00
|
|
|
$types = config('firefly.accountTypesByIdentifier.asset');
|
|
|
|
$count = $repository->count($types);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('User visits homepage.');
|
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $count) {
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(route('new-user.index'));
|
2015-06-01 11:13:54 -05:00
|
|
|
}
|
2022-12-29 12:41:57 -06:00
|
|
|
$subTitle = (string)trans('firefly.welcome_back');
|
2016-10-29 00:44:46 -05:00
|
|
|
$transactions = [];
|
2022-04-13 09:23:02 -05:00
|
|
|
$frontPage = app('preferences')->getFresh('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray());
|
2016-02-05 08:41:40 -06:00
|
|
|
/** @var Carbon $start */
|
2023-02-11 00:36:45 -06:00
|
|
|
$start = session('start', today(config('app.timezone'))->startOfMonth());
|
2016-02-05 08:41:40 -06:00
|
|
|
/** @var Carbon $end */
|
2023-02-11 00:36:45 -06:00
|
|
|
$end = session('end', today(config('app.timezone'))->endOfMonth());
|
2017-09-16 02:24:48 -05:00
|
|
|
$accounts = $repository->getAccountsById($frontPage->data);
|
2020-09-11 00:12:33 -05:00
|
|
|
$today = today(config('app.timezone'));
|
2015-04-09 04:52:35 -05:00
|
|
|
|
2020-10-24 00:18:37 -05:00
|
|
|
Log::debug('Frontpage accounts are ', $frontPage->data);
|
|
|
|
|
2017-10-16 12:01:26 -05:00
|
|
|
/** @var BillRepositoryInterface $billRepository */
|
|
|
|
$billRepository = app(BillRepositoryInterface::class);
|
|
|
|
$billCount = $billRepository->getBills()->count();
|
2020-08-05 11:51:17 -05:00
|
|
|
// collect groups for each transaction.
|
2015-02-06 23:49:24 -06:00
|
|
|
foreach ($accounts as $account) {
|
2019-03-24 03:23:36 -05:00
|
|
|
/** @var GroupCollectorInterface $collector */
|
|
|
|
$collector = app(GroupCollectorInterface::class);
|
2020-08-05 11:51:17 -05:00
|
|
|
$collector->setAccounts(new Collection([$account]))->withAccountInformation()->setRange($start, $end)->setLimit(10)->setPage(1);
|
|
|
|
$set = $collector->getExtractedJournals();
|
|
|
|
$transactions[] = ['transactions' => $set, 'account' => $account];
|
2015-02-06 23:49:24 -06:00
|
|
|
}
|
2015-06-03 14:25:11 -05:00
|
|
|
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
event(new RequestedVersionCheckStatus($user));
|
2017-12-28 12:03:15 -06:00
|
|
|
|
2022-01-29 07:11:12 -06:00
|
|
|
return view('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today'));
|
2015-02-06 00:23:26 -06:00
|
|
|
}
|
2015-02-05 21:39:52 -06:00
|
|
|
}
|