<?php
namespace Modules\Partners\Corporates\AutoCompensate; class Controller extends \App\Mvc\Controller\Form\PopupAbstract { protected function initialize() { parent::initialize(); $this->actionsCfg['index'] = [ 'checkSection' => false, 'checkOption' => false ]; } protected function getDefaultFormData($formData) { if (!$formData['typeChanged']) { $formData['compensateType'] = \App\EntityPreference\Corporate::getInstance((Int)$formData['idCorporate'])->autoCompensateType; $formData['cashingDocType'] = \App\Config\ClientInvoicesCompensate::getInstance()->get('cashingDocType'); } return $formData; } protected function processFormData(Array $formData, \App\MessagesCollector $msgs) { if (!$formData['compensateType']) { $msgs->addWarning($this->lang['index']['messages']['noCompType']); } if (!$formData['cashingDocType']) { $msgs->addWarning($this->lang['index']['messages']['noCashDocType']); } if (!$formData['invoicesList']) { $msgs->addWarning($this->lang['index']['messages']['noInv']); } if (!$msgs->hasErrorOrWarning()) { try { \DB::startTransaction(); foreach ($formData['invoicesList'] as $suggestionInvoicesList) { if (!$suggestionInvoicesList or !is_array($suggestionInvoicesList)) { $msgs->addWarning($this->lang['index']['messages']['invalidInvSelect']); \DB::rollbackTransaction(); return false; } $compensationObj = new \App\Utils\Invoices\Compensate\Client\Compensate($suggestionInvoicesList); $compensationObj->setDocumentData($formData['cashingDocType'], TODAY_ISO, CURRENT_ID_USER, CURRENT_ID_OFFICE); $compensationObj->process(); } \DB::commitTransaction(); $msgs->addSuccess( sprintf($this->lang['index']['messages']['successMsg'], count($formData['invoicesList'])) ); return true; } catch (\App\MessagesException | \App\Entity\Exception $e) { \DB::rollbackTransaction(); $msgs->addError( sprintf($this->lang['index']['messages']['errorMsg'], $e->getMessagesCollector()->getMessageBulk()) ); return false; } } return false; } protected function preRenderOperations(\App\Mvc\ViewAbstract $view, array $formData) { if ($formData['compensateType'] and !$view->getClosePopup()) { $invSelection = new \App\Utils\Invoices\Compensate\Client\SelectInvoices($formData['compensateType']); $invSelection->setIdCorporate($formData['idCorporate']); $invSelection->process(); $compensations = $invSelection->getCompensations(); if (!$compensations) { $view->getMessagesCollector()->addWarning($this->lang['index']['messages']['noCompSuggFound']); } $formData['compensationsSuggestions'] = $compensations; if (!key_exists('invoicesList', $formData) or $formData['typeChanged']) { $formData['invoicesList'] = []; foreach($compensations as $suggestionKey=>$invoicesList) { $formData['invoicesList'][$suggestionKey] = []; if ($formData['compensateType']=='bulk') { $model = new \App\Utils\Invoices\Compensate\Client\Compensate($invoicesList); $compensatedValues = $model->calculateCompensatedValues(false); $formData['invoicesList'][$suggestionKey] = array_keys($compensatedValues); } elseif ($formData['compensateType']=='onlyExactCreditNotes') { $formData['invoicesList'][$suggestionKey] = $invoicesList; } } } $view->addInput('formData', $formData); } } } 