From 3257006a0203b2bbfc4b79e509f457dcd48458e8 Mon Sep 17 00:00:00 2001 From: Kowalski Date: Sat, 31 Oct 2026 11:09:23 +0100 Subject: [PATCH] =?UTF-8?q?Kop=C3=ADrov=C3=A1n=C3=AD=20faktur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Model/Facade/FakturaFacade.php | 47 +++++++++++++++++++++++++++++ app/UI/Faktura/FakturaPresenter.php | 18 +++++++++++ app/UI/Faktura/default.latte | 7 +++++ 3 files changed, 72 insertions(+) diff --git a/app/Model/Facade/FakturaFacade.php b/app/Model/Facade/FakturaFacade.php index 72155e4..8cd994f 100644 --- a/app/Model/Facade/FakturaFacade.php +++ b/app/Model/Facade/FakturaFacade.php @@ -538,4 +538,51 @@ class FakturaFacade { $this->kufrikExplorer->table('FAKTURA')->where('ID', $id)->delete(); } + + /** + * Kopie faktury: nové číslo, dnešní data, stejný odběratel a položky, bez plateb. + * + * @return int ID nové faktury + */ + public function duplicate(int $id, int $firmaId): int + { + $zdroj = $this->getById($id); + if (!$zdroj) { + throw new \InvalidArgumentException('Faktura nebyla nalezena.'); + } + + $dnes = new \DateTimeImmutable('today'); + $dalsi = $this->getNextCislo($firmaId, $dnes); + $datumVystaveni = $dnes->format('Y-m-d'); + $datumSplatnosti = $dnes + ->modify('+' . $dalsi['splatnostDny'] . ' days') + ->format('Y-m-d'); + + $novaId = $this->saveFaktura([ + 'ZAKAZNIK_ID' => (int) $zdroj->ZAKAZNIK, + 'CISLO' => $dalsi['cislo'], + 'CISLO_ABS' => $dalsi['cisloAbs'], + 'FORMA_UHRADY' => (int) ($zdroj->FORMA_UHRADY ?? 1), + 'DATUM_VYSTAVENI' => $datumVystaveni, + 'DATUM_SPLATNOSTI' => $datumSplatnosti, + 'DATUM_DUZP' => $datumVystaveni, + 'STAV' => self::STAV_VYSTAVENO, + 'CINNOSTI' => $zdroj->CINNOSTI, + ], null, $firmaId); + + $polozky = []; + foreach ($this->getPolozky($id) as $polozka) { + $polozky[] = [ + 'NAZEV' => $polozka->NAZEV, + 'POCET' => $polozka->POCET, + 'JEDNOTKA' => $polozka->JEDNOTKA, + 'CENA_JEDNOTKA' => $polozka->CENA_JEDNOTKA, + 'SLEVA' => $polozka->SLEVA, + ]; + } + $this->savePolozky($novaId, $polozky, $firmaId); + $this->syncStav($novaId); + + return $novaId; + } } \ No newline at end of file diff --git a/app/UI/Faktura/FakturaPresenter.php b/app/UI/Faktura/FakturaPresenter.php index 407f74d..5f2b32c 100644 --- a/app/UI/Faktura/FakturaPresenter.php +++ b/app/UI/Faktura/FakturaPresenter.php @@ -218,4 +218,22 @@ class FakturaPresenter extends BasePresenter $this->redirect('this'); } + + /** + * Kopie faktury — nové číslo, dnešní data, stejné položky; otevře editaci. + */ + public function handleDuplicate(int $id): void + { + $firmaId = (int) $this->getUser()->getIdentity()->firma_id; + + try { + $novaId = $this->fakturaFacade->duplicate($id, $firmaId); + } catch (\InvalidArgumentException $e) { + $this->flashMessage($e->getMessage(), 'danger'); + $this->redirect('this'); + } + + $this->flashMessage('Kopie faktury byla vytvořena.', 'success'); + $this->redirect('FakturaEdit:edit', $novaId); + } } \ No newline at end of file diff --git a/app/UI/Faktura/default.latte b/app/UI/Faktura/default.latte index 739818d..7cb7ed9 100644 --- a/app/UI/Faktura/default.latte +++ b/app/UI/Faktura/default.latte @@ -137,6 +137,13 @@ + + + + +