<?php
namespace Modules\Orders\Sabre\FluX; class Controller extends \ModulesAbstract\FluxRest\ControllerUploaderAutoSaveFile { protected function getBasePath() { global $CFG; return $CFG['paths']['sabre']; } protected function validateFluXSystem(\Entity\FluxInstance $fluxInstance) { return ($fluxInstance->assignedSystem=='sabre'); } protected function handleSystemHealth($request, \ModulesAbstract\FluxRest\Response\Main $mainResponse, \Entity\FluxInstance $fluxInstance) { parent::handleSystemHealth($request, $mainResponse, $fluxInstance); if (!$mainResponse->hasOperation('Wait')) { $behaviourOnSystemLoad = []; foreach (explode(",", $fluxInstance->behaviourOnSystemLoad) as $behaviour) { list($k, $v) = explode(":", $behaviour); $behaviourOnSystemLoad[$k] = $v; } global $DBTImportedOrders_services, $DBTImportedOrders_tickets, $DBTImportedOrders_orders, $DBTSabreParsedFiles, $DBTSabreServices; $openTables = \DB::getOpenTables(); $busyTables = array_intersect([$DBTImportedOrders_services, $DBTImportedOrders_tickets, $DBTImportedOrders_orders, $DBTSabreParsedFiles, $DBTSabreServices, ], $openTables); if ($busyTables) { $op = new \ModulesAbstract\FluxRest\Response\Operation\NotifyUserLog(); $op->text = sprintf('Server busy, some database tables are in use (%s)', implode(", ", $busyTables)); $mainResponse->appendOperation($op); $op = new \ModulesAbstract\FluxRest\Response\Operation\Wait(); $op->timeout = count($busyTables)*2.5; $mainResponse->appendOperation($op); if ($behaviourOnSystemLoad['onTablesBusy']=='wait') { throw new \Exception\IO\Base("System busy"); } } } } protected function parseFileOnDisk($path, $originalFilename, \Entity\FluxInstance $fluxInstance, $rawRequest) { \DB::startTransaction(); $transactionTimings = [ 'start' => null, 'FileParser' => null, 'Storage' => null, 'CompatibilityWithImportedOrders' => null, 'commit' => null, ]; try { $transactionTimings['start'] = microtime(true); $parser = new \App\Sabre\FileParser\IUR(); $parser->setRawData(file_get_contents($path)); $parser->setStoredData('fileDate', min($parser->collectFileDatesFromPath($path))); $parser->extract(); $parser->getStructure(); $transactionTimings['FileParser'] = microtime(true); $structureParser = new \App\Sabre\StructureParser\Processor($parser); $structureParser->initialize(); $structureParser->setFile($path); $storage = new \App\Sabre\Storage\StorageHandler(); $storage->setOption('source', 'FluX'); $storage->setOption('IP', ($_SERVER['REMOTE_ADDR']?:'')); $storage->setProcessor($structureParser); $ret = $storage->execute(); $transactionTimings['Storage'] = microtime(true); if ($ret) { $entities = []; if ($ret['inserted']) { foreach($ret['inserted'] as $entityId) { $entities[] = \Entity\SabreService::select($entityId); } } \Engine\Plugins\Manager::getInstance()->executeEvent('Sabre-beforeConvertingImportedOrder-inFluX', [ 'this' => $this, 'entities' => $entities, 'structureParser'=> $structureParser, 'storage' => $storage, 'storageOutput' => $ret, ]); $entities = array_filter($entities, function($service) { return !(in_array($service->serviceCode, ['documentIssue', ]) && !$service->transactionData->documents->docSubType); }); if ($entities) { $convertor = new \App\Sabre\CompatibilityWithImportedOrders\Processor($entities); $order = $convertor->getConvertedOrder(); $mode = $convertor->getConvertedApplyMode(); $orderInsert = \Entity\ImportedOrder::insert(); $orderInsert->applyMode = $mode; $orderInsert->order = $order; $orderInsert->originalFilesWithPath = [$this->getFilePathRelativeToFilesRoot($path)]; $orderInsert->save(); $insertedOrderId = $orderInsert->getId(); $affectedFiles = []; foreach ($entities as $ent) { $affectedFiles[$ent->idFile] = $ent->file; } foreach ($affectedFiles as $file) { $fileUpd = \Entity\SabreFile::update($file->getId()); $idImportedOrders = $file->idImportedOrders; $idImportedOrders[] = $insertedOrderId; $fileUpd->idImportedOrders = $idImportedOrders; $fileUpd->save(); } $transactionTimings['CompatibilityWithImportedOrders'] = microtime(true); } } $parser->destroyCCInformationAndWriteInto($path); \DB::commitTransaction(); $transactionTimings['commit'] = microtime(true); $this->queueSessionData[__FUNCTION__] = $transactionTimings; } catch(\Exception $ex) { \DB::rollbackTransaction(); \App\Logger\FluxInstance::getInstance()->warning(sprintf("[%s] %s: %s", __METHOD__, get_class($ex), $ex->getMessage()), [ 'file' => new \Engine\Logger\Context\File([ 'name'=> $originalFilename, 'content' => \Engine\File::getContents($path), ]), 'fluxInstance' => $fluxInstance, ]); if (is_a($ex, "App\Utils\BaseParser\GeneralParserException")) { throw new \ModulesAbstract\FluxRest\ExceptionInvalidFile(sprintf("Invalid file %s. Error: %s", $originalFilename, $ex->getMessage()), null, $ex); } else { throw $ex; } } } } 