<?php
 namespace Modules\Orders\ImportedOrders\ImportOffer; class Model extends \App\Mvc\ModelAbstract { protected $idService; protected $idImportedService; protected $offerStatus = 'insert'; public function __construct($idService, $idImportedService) { parent::__construct(); $this->idService = $idService; $this->idImportedService = $idImportedService; } public function setOfferStatus($offerStatus) { if ( !in_array($offerStatus, ['insert', 'insertChoose', 'insertChooseConfirm']) ) { throw new \Exception(sprintf('The offer status `%s` is not valid.', $offerStatus)); } $this->offerStatus = $offerStatus; } protected function check() { $msgs = new \App\MessagesCollector(); if (!\DB::isInsideTransaction()) { throw new \Exception('No DB transaction was started. You cannot execute `'.__CLASS__.'` unless you start a transaction before.'); } $serviceEnt = \Entity\Service::selectForUpdate($this->idService); if (!$serviceEnt->recordExists()) { $msgs->addError($this->lang['index']['msgs']['invalidServiceId']); } elseif ($serviceEnt->statusOp=='XX') { $msgs->addWarning(); } $importedServiceEnt = \Entity\ImportedService::selectForUpdate($this->idImportedService); if (!$importedServiceEnt->recordExists()) { $msgs->addError($this->lang['index']['msgs']['invalidImpServId']); } elseif ($importedServiceEnt->processStatus!='new') { $msgs->addWarning($this->lang['index']['msgs']['impServProcessed']); } if ($msgs->hasErrorOrWarning()) { throw new \App\MessagesException($msgs); } } public function execute() { $this->check(); $importedServiceEnt = \Entity\ImportedService::selectForUpdate($this->idImportedService); $importedServiceObj = $importedServiceEnt->object; $importedOfferObj = $importedServiceObj->chosenOffer; $serviceSelEnt = \Entity\Service::selectForUpdate($this->idService); $serviceUpdateEnt = \Entity\Service::update($this->idService); $serviceUpdateEnt->importedServicesRefsInsert = [$this->idImportedService]; $serviceUpdateEnt->save(); $offerEnt = \Entity\Offer::insert(); $offerItemEnt = \Entity\OfferItem::insert(); $populateOfferObj = new \App\ImportedOrder\PopulateEntity\Offer($importedOfferObj, $offerEnt, $importedServiceEnt->idSystem); $populateOfferObj->setOfferItem($offerItemEnt); $populateOfferObj->setIdCorporateForCardMatch($serviceSelEnt->order->idCorporateOwner); $populateOfferObj->populate(); $offerEnt->offerItems = [$offerItemEnt]; $offerEnt->idService = $this->idService; $offerEnt->idImportedService = $this->idImportedService; $offerEnt->save(); $idOffer = $offerEnt->getId(); if ($this->offerStatus=='insertChoose' or $this->offerStatus=='insertChooseConfirm') { $serviceUpdateEnt = \Entity\Service::update($this->idService); $serviceUpdateEnt->chosenOfferId = $idOffer; $serviceUpdateEnt->confirmedOffer = ($this->offerStatus=='insertChooseConfirm'); $serviceUpdateEnt->save(); } } } 