getHttpRequest()->getQuery(); $fromUrl = array_key_exists('rok', $query) || array_key_exists('mesic', $query); if ($fromUrl) { $this->rok = $this->rok ?? (int) date('Y'); $this->mesic = $this->normalizeMesic($this->mesic); $this->saveFilter($this->rok, $this->mesic); return; } $saved = $this->loadFilter(); if ($saved === null) { return; } $this->rok = $saved['rok']; $this->mesic = $saved['mesic']; // Do URL doplníme uložený filtr, ale ne při signálu (úhrada / smazání). if ($this->getSignal() === null) { $this->redirect('this'); } } public function renderDefault(): void { $rok = $this->rok ?? (int) date('Y'); $mesic = $this->normalizeMesic($this->mesic); $faktury = $this->fakturaFacade->getFiltered($rok, $mesic); $celkemSum = 0; $neuhrazenoCount = 0; foreach ($faktury as $f) { $celkemSum += (float) ($f->CASTKA_CELKEM ?? 0); if ($f->STAV !== 'PAID') { $neuhrazenoCount++; } } $this->template->faktury = $faktury; $this->template->celkemSum = $celkemSum; $this->template->neuhrazenoCount = $neuhrazenoCount; $this->template->selectedRok = $rok; $this->template->selectedMesic = $mesic; $this->template->roky = $this->fakturaFacade->getDostupneRoky($rok); } private function normalizeMesic(string $mesic): string { if ($mesic === '') { return ''; } $cislo = (int) $mesic; return ($cislo >= 1 && $cislo <= 12) ? (string) $cislo : ''; } /** * @return array{rok: int, mesic: string}|null */ private function loadFilter(): ?array { $uid = (int) $this->getUser()->getId(); $section = $this->getSession(self::FILTER_SESSION); if (isset($section->rok) && (int) ($section->userId ?? 0) === $uid) { return $this->validFilter((int) $section->rok, (string) ($section->mesic ?? '')); } $raw = $this->getHttpRequest()->getCookie(self::FILTER_COOKIE); if (!is_string($raw) || $raw === '') { return null; } $parts = explode(':', $raw, 3); if (count($parts) < 2 || (int) $parts[0] !== $uid) { return null; } return $this->validFilter((int) $parts[1], (string) ($parts[2] ?? '')); } private function saveFilter(int $rok, string $mesic): void { $uid = (int) $this->getUser()->getId(); $section = $this->getSession(self::FILTER_SESSION); $section->userId = $uid; $section->rok = $rok; $section->mesic = $mesic; $this->getHttpResponse()->setCookie( self::FILTER_COOKIE, $uid . ':' . $rok . ':' . $mesic, '1 year', null, null, null, true, 'Lax' ); } /** * @return array{rok: int, mesic: string}|null */ private function validFilter(int $rok, string $mesic): ?array { if ($rok < 2000 || $rok > 2100) { return null; } return [ 'rok' => $rok, 'mesic' => $this->normalizeMesic($mesic), ]; } /** * Přepnutí stavu zaplaceno / neuhrazeno */ public function handleUhradit(int $id): void { $novyStav = $this->fakturaFacade->toggleUhrada($id); if ($novyStav !== '') { $this->flashMessage( $novyStav === 'PAID' ? 'Faktura byla označena jako zaplacená.' : 'Stav faktury byl změněn na neuhrazeno.', 'success' ); } $this->redirect('this'); } /** * Smazání faktury */ public function handleDelete(int $id): void { $this->fakturaFacade->delete($id); $this->flashMessage('Faktura byla smazána.', 'info'); $this->redirect('this'); } }