<?php
namespace Modules\Orders\Sabre\Collector; class Model extends \ModulesAbstract\ImportedOrdersFileManager\Model { public function createConvertInputToFilters() { return new \Modules\Orders\Sabre\Collector\Model\ConvertInputToFilters(); } public function getFileParsedData($fileFullPath) { $fileParser = $this->buildFileParser($fileFullPath); return [ 'extractedData' => $fileParser->getExtractedData(), 'fileId' => $fileParser->getFileId(), 'messages' => $fileParser->getMessagesCollector(), ]; } private function buildFileParser($fileFullPath) { $fileParser = new \App\Sabre\FileParser\IUR(); $fileParser->setMessagesCollector(new \App\MessagesCollector()); $fileParser->setRawData(\Engine\File::getContents($fileFullPath)); $fileParser->setStoredData('fileDate', min($fileParser->collectFileDatesFromPath($fileFullPath))); $fileParser->extract(); return $fileParser; } public function getStructureParserData($fileFullPath) { $processor = $this->buildStructureParser($fileFullPath); return [ 'transactions' => $processor->getSortedTransactions(), 'messages' => $processor->getMessagesCollector(), 'fileId' => $processor->getFileParser()->getFileId(), 'fileParserMessages' => $processor->getFileParser()->getMessagesCollector(), 'generalAssociations'=> $processor->getInternalAssociations(), ]; } private function buildStructureParser($fileFullPath) { $fileParser = $this->buildFileParser($fileFullPath); $processor = new \App\Sabre\StructureParser\Processor($fileParser); $processor->initialize(); $processor->setFile($fileFullPath); return $processor; } public function parseFile($fileFullPath) { $processor = $this->buildStructureParser($fileFullPath); $storage = new \App\Sabre\Storage\StorageHandler(); $storage->setProcessor($processor); $storage->setOption('source', "manual"); $storage->setOption('IP', $_SERVER['REMOTE_ADDR']); $ret = $storage->execute(); $entities = []; if ($ret['inserted']) { foreach ($ret['inserted'] as $entityId) { $entities[] = \Entity\SabreService::select($entityId); } \Engine\Plugins\Manager::getInstance()->executeEvent('Sabre-beforeConvertingImportedOrder-inCollector', [ 'this' => $this, 'entities' => $entities, 'structureParser'=> $processor, '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(); $applyMode = $convertor->getConvertedApplyMode(); $orderInsert = \Entity\ImportedOrder::insert(); $orderInsert->applyMode = $applyMode; $orderInsert->order = $order; $orderInsert->originalFilesWithPath = [$this->getFilePathRelativeToFilesRoot($fileFullPath)]; $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(); } $processor->getFileParser()->destroyCCInformationAndWriteInto($fileFullPath); } } } }