. */ declare(strict_types=1); namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use Carbon\Exceptions\InvalidFormatException; use Exception; use FireflyIII\Events\RequestedVersionCheckStatus; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Http\Middleware\Installer; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; /** * Class HomeController. */ class HomeController extends Controller { /** * HomeController constructor. * */ public function __construct() { parent::__construct(); app('view')->share('title', 'Firefly III'); app('view')->share('mainTitleIcon', 'fa-fire'); $this->middleware(Installer::class); } /** * Change index date range. * * @param Request $request * * @return JsonResponse * @throws Exception */ public function dateRange(Request $request): JsonResponse { 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(); } $label = $request->get('label'); $isCustomRange = false; Log::debug('Received dateRange', ['start' => $stringStart, 'end' => $stringEnd, 'label' => $request->get('label')]); // check if the label is "everything" or "Custom range" which will betray // a possible problem with the budgets. if ($label === (string)trans('firefly.everything') || $label === (string)trans('firefly.customRange')) { $isCustomRange = true; Log::debug('Range is now marked as "custom".'); } $diff = $start->diffInDays($end) + 1; if ($diff > 50) { $request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => $diff])); } $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'))); return response()->json(['ok' => 'ok']); } /** * Show index. * * @param AccountRepositoryInterface $repository * * @return mixed * @throws FireflyException */ public function index(AccountRepositoryInterface $repository): mixed { if ('v3' === config('firefly.layout')) { return view('pwa'); } $types = config('firefly.accountTypesByIdentifier.asset'); $count = $repository->count($types); Log::channel('audit')->info('User visits homepage.'); if (0 === $count) { return redirect(route('new-user.index')); } $subTitle = (string)trans('firefly.welcome_back'); $transactions = []; $frontPage = app('preferences')->getFresh('frontPageAccounts', $repository->getAccountsByType([AccountType::ASSET])->pluck('id')->toArray()); /** @var Carbon $start */ $start = session('start', today(config('app.timezone'))->startOfMonth()); /** @var Carbon $end */ $end = session('end', today(config('app.timezone'))->endOfMonth()); $accounts = $repository->getAccountsById($frontPage->data); $today = today(config('app.timezone')); Log::debug('Frontpage accounts are ', $frontPage->data); /** @var BillRepositoryInterface $billRepository */ $billRepository = app(BillRepositoryInterface::class); $billCount = $billRepository->getBills()->count(); // collect groups for each transaction. foreach ($accounts as $account) { /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setAccounts(new Collection([$account]))->withAccountInformation()->setRange($start, $end)->setLimit(10)->setPage(1); $set = $collector->getExtractedJournals(); $transactions[] = ['transactions' => $set, 'account' => $account]; } /** @var User $user */ $user = auth()->user(); event(new RequestedVersionCheckStatus($user)); return view('index', compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today')); } }