rok = (int) date('Y'); } public function renderDefault(): void { $firmaId = $this->getUser()->getIdentity()->firma_id ?? null; if (!$firmaId) { $this->flashMessage('K účtu není přiřazena žádná aktivní firma.', 'danger'); $this->template->faktury = []; $this->template->celkemSum = 0; $this->template->neuhrazenoCount = 0; return; } // Dynamické sestavení podmínek WHERE pro T-SQL $where = ['F.FIRMA_ID = ?']; $params = [$firmaId]; if ($this->rok) { $where[] = 'YEAR(F.DATUM_VYSTAVENI) = ?'; $params[] = $this->rok; } if ($this->mesic !== '') { $where[] = 'MONTH(F.DATUM_VYSTAVENI) = ?'; $params[] = (int) $this->mesic; } $whereSql = implode(' AND ', $where); // Čisté T-SQL zaručující 100% kompatibilitu s MS SQL Serverem bez magických Nette relací $sql = " SELECT F.*, ISNULL((SELECT SUM(P.CENA) FROM dbo.POLOZKA P WHERE P.FAKTURA = F.ID), 0) AS CASTKA_CELKEM FROM dbo.FAKTURA F WHERE {$whereSql} ORDER BY F.DATUM_VYSTAVENI DESC, F.ID DESC "; // Provedeme dotaz přímo na Explorer connection $faktury = $this->kufrikExplorer->query($sql, ...$params)->fetchAll(); // Součty pro karty $celkemSum = 0; $neuhrazenoCount = 0; foreach ($faktury as $f) { $suma = (float) ($f->CASTKA_CELKEM ?? 0); $celkemSum += $suma; if ($f->STAV !== 'PAID') { $neuhrazenoCount++; } } $this->template->faktury = $faktury; $this->template->celkemSum = $celkemSum; $this->template->neuhrazenoCount = $neuhrazenoCount; $this->template->vybranyRok = $this->rok; $this->template->vybranyMesic = $this->mesic; } public function handleUhradit(int $id): void { $firmaId = $this->getUser()->getIdentity()->firma_id; $faktura = $this->kufrikExplorer->table('FAKTURA') ->where('ID', $id) ->where('FIRMA_ID', $firmaId) ->fetch(); if ($faktura) { $novyStav = ($faktura->STAV === 'PAID') ? 'ISSUED' : 'PAID'; $faktura->update(['STAV' => $novyStav]); $this->flashMessage( $novyStav === 'PAID' ? 'Faktura byla označena jako zaplacená.' : 'Stav faktury byl změněn na neuhrazeno.', 'success' ); } $this->redirect('this'); } public function handleDelete(int $id): void { $firmaId = $this->getUser()->getIdentity()->firma_id; $deleted = $this->kufrikExplorer->table('FAKTURA') ->where('ID', $id) ->where('FIRMA_ID', $firmaId) ->delete(); if ($deleted) { $this->flashMessage('Faktura byla smazána.', 'info'); } $this->redirect('this'); } }