<?php
 namespace Modules\Invoices\InvoicesList\Cancel; class Controller extends \App\Mvc\Controller\Form\PopupAbstract { protected function getDefaultFormData($R) { return $R; } protected function processFormData(array $R, \App\MessagesCollector $msgs) { if (!is_array($R['id']) or !$R['id']) { $msgs->addError($this->lang['index']['msgs']['noInvIds']); return false; } $invoiceCashingEntCfg = \Entity\InvoiceCashing::loadCFG(); if ($invoiceCashingEntCfg['_special']['voidReasonMandatory'] and !$R['invoiceVoidReason']) { foreach ($R['id'] as $idInvoice) { if (\Entity\Invoice::select($idInvoice)->issueTime) { $msgs->addWarning($this->lang['index']['msgs']['noVoidReason']); return false; } } } $invoicesListingSelection = new \App\Mvc\View\Listing\Members\Selection('Modules/Invoices/InvoicesList'); $OK = true; foreach ($R['id'] as $idInvoice) { try { \DB::startTransaction(); $invoiceEnt = \Entity\Invoice::selectForUpdate($idInvoice); if ($R['voidCashingDocs']) { foreach ($invoiceEnt->cashingDocumentsList as $idDocumentCashing => $documentCashingDetails) { if (!$documentCashingDetails['voidTime']) { $invoiceCashingEntity = \Entity\InvoiceCashing::cancel($idDocumentCashing); $invoiceCashingEntity->voidReason = $R['cashingVoidReason']; $invoiceCashingEntity->save(); } } } $invoiceVoidingEnt = \Entity\Invoice::cancel($idInvoice); $invoiceVoidingEnt->voidReason = $R['invoiceVoidReason']; $invoiceVoidingEnt->save(); \DB::commitTransaction(); $msgs->addSuccess(sprintf($this->lang['index']['msgs']['success'], $idInvoice)); $invoicesListingSelection->delete([$idInvoice]); if ($R['selectTempInvoices']) { if ($invoiceEnt->cumulatedInvoicesIdsList) { $invoicesListingSelection->add($invoiceEnt->cumulatedInvoicesIdsList); } } } catch (\App\Entity\Exception $e) { \DB::rollbackTransaction(); $msgs->appendMessages($e->getMessagesCollector(), sprintf($this->lang['index']['msgs']['error'], $idInvoice).' %s'); $OK = false; break ; } } return $OK; } }