<?php
namespace Modules\Invoices\CashBook\Management\CashbookView; class Controller extends \App\Mvc\Controller\Popup\PopupAbstract { protected function initialize() : void { $this->actionsCfg['index'] = ['checkSection' => false, 'checkOption' => false]; $this->actionsCfg['block'] = ['checkSection' => false, 'checkOption' => false]; $this->actionsCfg['unblock'] = ['checkSection' => false, 'checkOption' => false]; $this->actionsCfg['export'] = ['checkSection' => false, 'checkOption' => false]; } protected function actionIndex($R) { $msgs = \App\MessagesCollector::create(); return $this->renderIndex($R, $msgs); } protected function actionBlock($R) { $msgs = \App\MessagesCollector::create(); try { \DB::startTransaction(); $viewData = (new Model())->getViewData($R); $blockDate = $R['blockDate'] ?? null; if (!$blockDate || !array_key_exists($blockDate, $viewData['processedDocs'])) { throw new \RuntimeException($this->lang['index']['messages']['blockDateNotFound']); } $totalsObj = new \App\Cashbook\Totals($R['office']['id']); $totalsObj->block($blockDate); \DB::commitTransaction(); $msgs->addSuccess(sprintf($this->lang['index']['messages']['blockSuccess'], \Engine\DateTime::isoToStd($blockDate))); } catch (\RuntimeException $e) { \DB::rollbackTransaction(); $msgs->addError($e->getMessage()); } return $this->renderIndex($R, $msgs); } protected function actionUnblock($R) { $msgs = \App\MessagesCollector::create(); try { \DB::startTransaction(); $totals = new \App\Cashbook\Totals($R['office']['id']); $lastClosedDate = $totals->getLastClosedDate(); if ($R['blockDate'] !== $lastClosedDate) { throw new \RuntimeException($this->lang['index']['messages']['unblockDateMismatch']); } $totals->unblock(); $msgs->addSuccess(sprintf($this->lang['index']['messages']['unblockSuccess'], \Engine\DateTime::isoToStd($lastClosedDate))); \DB::commitTransaction(); } catch (\RuntimeException $e) { \DB::rollbackTransaction(); $msgs->addError($e->getMessage()); } return $this->renderIndex($R, $msgs); } protected function actionExport($R) { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $format = $R['exportFormat'] ?? 'xls'; $exportCfg = $CFG['Invoices']['Cashbook']['Management']['cashBookExport'][$format] ?? []; $templatePath = $exportCfg['defaultTemplate'] ?? ''; if (!$templatePath) { throw new \Exception("missing template path."); } $view = $this->buildViewObjectFromCurrentModuleForAction('export'); $view->addInput('formData', array_merge($R, (new Model())->getViewData($R))); $view->addInput('templatePath', $templatePath); $content = $view->render(); if ($format === 'html') { return new \Engine\Mvc\Response\Html($content); } $filename = ($this->lang['index']['exportFilename'] ?? 'Cashbook') . '.' . ($exportCfg['fileExtension'] ?? 'xls'); $response = new \Engine\Mvc\Response\FileDownload($content); $response->setFilename($filename); return $response; } private function renderIndex(array $R, \App\MessagesCollector $msgs) : \Engine\Mvc\Response\Html { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); $view = $this->buildViewObjectFromCurrentModuleForAction('index'); $viewData = (new Model())->getViewData($R); $view->addInput('viewData', $viewData); $view->setOutputVar('processedDocs', $viewData['processedDocs'] ?? []); $view->setOutputVar('lastClosedDate', $viewData['lastClosedDate'] ?? null); $view->setOutputVar('selectedCurrencies', $R['currency'] ?? []); $view->setOutputVar('showEquivDefCurr', !empty($R['equivDefCurr'])); $view->setOutputVar('showEquivWorkCurr', !empty($R['equivWorkCurr'])); $view->setOutputVar('showCurrExch', !empty($R['currExchDisplay'])); $view->setOutputVar('idOffice', $R['office']['id'] ?? null); $view->setOutputVar('period', $R['period'] ?? []); $view->setOutputVar('cashBookExport', $CFG['Invoices']['Cashbook']['Management']['cashBookExport'] ?? []); $view->getMessagesCollector()->appendMessages($msgs); return new \Engine\Mvc\Response\Html($view->render()); } }