Kufrik/app/UI/FakturaDetail/FakturaDetailPresenter.php

61 lines
1.8 KiB
PHP

<?php
namespace App\UI\FakturaDetail;
use App\Model\Faktura\FakturaPdf;
use App\UI\BasePresenter;
use App\Model\Facade\FakturaFacade;
use Nette\Application\Responses\CallbackResponse;
class FakturaDetailPresenter extends BasePresenter
{
public function __construct(
private FakturaFacade $fakturaFacade,
private FakturaPdf $fakturaPdf,
) {
parent::__construct();
}
public function actionPdf(int $id, bool $tisk = false): void
{
$faktura = $this->fakturaFacade->getById($id);
if (!$faktura) {
$this->error('Faktura nebyla nalezena.', 404);
}
$pdf = $this->fakturaPdf->render($faktura, $this->fakturaFacade->getPolozky($id), $tisk);
$fileName = FakturaPdf::fileName((string) $faktura->CISLO);
$disposition = $tisk ? 'inline' : 'attachment';
$this->sendResponse(new CallbackResponse(function ($httpRequest, $httpResponse) use ($pdf, $fileName, $disposition): void {
$httpResponse->setContentType('application/pdf');
$httpResponse->setHeader(
'Content-Disposition',
$disposition . '; filename="' . $fileName . '"'
);
echo $pdf;
}));
}
public function renderDefault(int $id): void
{
$faktura = $this->fakturaFacade->getById($id);
if (!$faktura) {
$this->flashMessage('Faktura nebyla nalezena.', 'danger');
$this->redirect('Faktura:default');
}
$polozky = $this->fakturaFacade->getPolozky($id);
// Výpočet celkové částky
$celkem = 0;
foreach ($polozky as $p) {
$celkem += (float) $p->CENA;
}
$this->template->faktura = $faktura;
$this->template->polozky = $polozky;
$this->template->celkem = $celkem;
}
}