<?php
namespace App\ImportedDocument\EntityMatch; class Corporate extends \App\EntityMatch\Corporate { use \App\ImportedDocument\EntityMatch\ExtSysCleanMatchesTrait; public function setIdInExtSys($idEntityExtSys, $idSystem) { $this->setSearchCriterion('documentExtSys', $idEntityExtSys, false, ['idSystem'=>$idSystem]); } protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options = []) { if ($field=='documentExtSys') { $matchedId = \App\Config\ImportedDocuments\Matches::getInstance()->getCorporate($options['idSystem'], $value); if ($matchedId) { return $this->searchRecord_primaryKey($matchedId); } else { return null; } } elseif ($field=='invoicesList') { return $this->searchRecord_invoicesList($options['invoicesList']); } elseif ($field=='importedServicesList') { return $this->searchRecord_importedServicesList($options['importedServicesList']); } elseif ($field=='servicesList') { return $this->searchRecord_servicesList($options['servicesList']); } else { return parent::dispatchSearchRecords($field, $value, $useWildcard, $options); } } public function setInvoicesList(Array $invoicesList) { foreach ($invoicesList as $invoice) { if (!($invoice instanceof \App\ImportedDocument\ClientInvoice) ) { throw new \Exception("The invoices must be instance of \App\ImportedDocument\ClientInvoice."); } } $this->setSearchCriterion('invoicesList', true, false, ['invoicesList'=>$invoicesList]); } public function setImportedServices(Array $importedServicesList) { foreach ($importedServicesList as $service) { if (!($service instanceof \App\ImportedDocument\ImportedService) ) { throw new \Exception("The imported services must be instance of \App\ImportedDocument\ImportedService."); } } $this->setSearchCriterion('importedServicesList', true, false, ['importedServicesList'=>$importedServicesList]); } public function setServices(Array $servicesList) { foreach ($servicesList as $service) { if (!($service instanceof \App\ImportedDocument\Service) ) { throw new \Exception("The services must be instance of \App\ImportedDocument\Service."); } } $this->setSearchCriterion('servicesList', true, false, ['servicesList'=>$servicesList]); } private function searchRecord_invoicesList(Array $invoicesList) { $return = null; $corporatesList = []; foreach ($invoicesList as $invoice) { $results = $invoice->getMatchedIds(); foreach ($results as $idInv) { $corporatesList[] = \Entity\Invoice::select($idInv)->idCorporate; } } $corporatesList = array_unique($corporatesList); if ($corporatesList) { $return = []; foreach ($corporatesList as $idCorp) { $corpEnt =\Entity\Corporate::select($idCorp); $return[$idCorp] = []; foreach ($this->exportFields as $field) { $return[$idCorp][$field] = $corpEnt->$field; } } } return $return; } private function searchRecord_importedServicesList(Array $importedServicesList) { $return = null; $corporatesList = []; foreach ($importedServicesList as $impServ) { $idImportedService = $impServ->getMatchedId(); if ($idImportedService) { $importedServiceEnt = \Entity\ImportedService::select($idImportedService); $idCorp = $importedServiceEnt->serviceTina->relevantInvoice->idCorporate; if ($idCorp) { $corporatesList[$idCorp] = $idCorp; } } } if ($corporatesList) { $return = []; foreach ($corporatesList as $idCorp) { $corpEnt =\Entity\Corporate::select($idCorp); $return[$idCorp] = []; foreach ($this->exportFields as $field) { $return[$idCorp][$field] = $corpEnt->$field; } } } return $return; } private function searchRecord_servicesList(Array $servicesList) { $return = null; $corporatesList = []; foreach ($servicesList as $serv) { $idServ = $serv->getMatchedId(); if ($idServ) { $serviceEnt = \Entity\Service::select($idServ); $idCorp = $serviceEnt->relevantInvoice->idCorporate; if ($idCorp) { $corporatesList[$idCorp] = $idCorp; } } } if ($corporatesList) { $return = []; foreach ($corporatesList as $idCorp) { $corpEnt =\Entity\Corporate::select($idCorp); $return[$idCorp] = []; foreach ($this->exportFields as $field) { $return[$idCorp][$field] = $corpEnt->$field; } } } return $return; } public function setFromImportedDocumentObj(\App\ImportedDocument\Cashing $impDocObj) { if ($impDocObj->getInvoicesList()) { $this->setInvoicesList($impDocObj->getInvoicesList()); } if ($impDocObj->getImportedServicesList()) { $this->setImportedServices($impDocObj->getImportedServicesList()); } if ($impDocObj->getTinaServicesList()) { $this->setServices($impDocObj->getTinaServicesList()); } } }