Kufrik/app/UI/Nastaveni/NastaveniPresenter.php

83 lines
2.6 KiB
PHP

<?php
namespace App\UI\Nastaveni;
use App\Model\Faktura\FakturaCislovani;
use App\Model\Facade\FakturaFacade;
use App\UI\BasePresenter;
use Nette\Application\UI\Form;
class NastaveniPresenter extends BasePresenter
{
public function __construct(
private FakturaFacade $fakturaFacade
) {
parent::__construct();
}
public function actionDefault(): void
{
$firmaId = $this->getUser()->getIdentity()->firma_id ?? null;
if (!$firmaId) {
$this->flashMessage('K účtu není přiřazena žádná aktivní firma.', 'danger');
$this->redirect('Home:');
}
$nastaveni = $this->fakturaFacade->getNastaveni((int) $firmaId);
$this['nastaveniForm']->setDefaults([
'maska' => $nastaveni['maska'],
'obdobi' => $nastaveni['obdobi'],
'splatnost' => $nastaveni['splatnostDny'],
]);
}
public function renderDefault(): void
{
$firmaId = (int) $this->getUser()->getIdentity()->firma_id;
$dalsi = $this->fakturaFacade->getNextCislo($firmaId);
$this->template->prikladCisla = $dalsi['cislo'];
}
protected function createComponentNastaveniForm(): Form
{
$form = new Form;
$form->addText('maska', 'Číslování faktur (maska):')
->setRequired('Zadejte masku čísla faktury.')
->setMaxLength(40);
$form->addSelect('obdobi', 'Číslovat po:', FakturaCislovani::OBDOBI)
->setRequired('Vyberte období číslování.');
$form->addInteger('splatnost', 'Splatnost faktury (dny):')
->setRequired('Zadejte počet dní splatnosti.')
->setDefaultValue(FakturaCislovani::SPLATNOST_VYCHOZI)
->addRule(Form::Range, 'Splatnost musí být 0 až 365 dní.', [0, 365]);
$form->addSubmit('save', 'Uložit nastavení');
$form->onSuccess[] = [$this, 'nastaveniFormSucceeded'];
return $form;
}
public function nastaveniFormSucceeded(Form $form, \stdClass $data): void
{
$chyba = FakturaCislovani::validateMaska((string) $data->maska);
if ($chyba !== null) {
$form['maska']->addError($chyba);
return;
}
$firmaId = (int) $this->getUser()->getIdentity()->firma_id;
$this->fakturaFacade->saveNastaveni(
$firmaId,
(string) $data->maska,
(string) $data->obdobi,
(int) $data->splatnost
);
$this->flashMessage('Nastavení faktur bylo uloženo.', 'success');
$this->redirect('this');
}
}