<?php
 namespace App\EntityMatch; class Customer extends \App\EntityMatch\BaseAbstract { protected $exportFields = ['lastName', 'firstName', 'idCorporate', 'PNC', 'email', 'address']; protected $matchingFilteringSQL = 'AND ent.deleted=0 AND ent.idRealCustomer=0'; protected $entityClassName = '\Entity\Customer'; private $idCorporateFilter; protected function setTableName() { global $DBTCustomers; $this->tableName = $DBTCustomers; } public function setPNC($value) { \Entity\Customer::loadCFG(); $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); if ($value!=$CFG['globalEntity']['::Customer']['PNC']['notUniqueValue']) { $this->setSearchCriterion('PNC', $value, false); } } public function setName($lastName, $firstName, $useWildcard=true, $searchMixed=true) { $this->setSearchCriterion('name', ['lastName'=>$lastName, 'firstName'=>$firstName], $useWildcard, ['searchMixed'=>$searchMixed]); } public function setEmail($email) { $this->setSearchCriterion('email', $email, false); } public function setTelephone($tel) { $this->setSearchCriterion('telephone', $tel, false); $this->setSearchCriterion('telephoneMobile', $tel, false); } public function filterByIdCorporate($idCorporateFilter) { $this->idCorporateFilter = $idCorporateFilter; } protected function dispatchSearchRecords($field, $value, $useWildcard, Array $options=[]) { switch ($field) { case 'name': return $this->searchRecords_name($value, $useWildcard, $options); break; default: return parent::dispatchSearchRecords($field, $value, $useWildcard, $options); } } private function searchRecords_name($names, $useWildcard, Array $options=[]) { global $CFG; $colsSQL=$this->getSelectingFieldsSQL(); $lastNameString = $names['lastName']; $firstNameString = $names['firstName']; if ($useWildcard) $firstNameString .= '%'; $searchSQL = "SELECT {$colsSQL} FROM `{$this->tableName}` AS ent WHERE 1 {$this->matchingFilteringSQL}"; if ($CFG['useAlternativeNames']['customers']) { $searchSQL .= " AND (ent.lastName LIKE ? OR ent.lastName2 LIKE ?) AND (ent.firstName LIKE ? OR ent.firstName2 LIKE ?)"; $sqlVals= [$lastNameString, $lastNameString, $firstNameString, $firstNameString]; $sqlValsMixed= [$firstNameString, $firstNameString, $lastNameString, $lastNameString]; } else { $searchSQL .= " AND ent.lastName LIKE ? AND ent.firstName LIKE ?"; $sqlVals= [$lastNameString, $firstNameString]; $sqlValsMixed= [$firstNameString, $lastNameString]; } $searchSQL .= ' LIMIT '.$this->limitResultsPerCriterion; $list1 = $this->searchRecordsRunSQL($searchSQL, $sqlVals); if ($options['searchMixed']) { $list2 = $this->searchRecordsRunSQL($searchSQL, $sqlValsMixed); } else { $list2 = []; } return $list1 + $list2; } protected function cleanMatches() { if ($this->idCorporateFilter) { foreach($this->bulkMatches as $idx=>$matchesList) { foreach($matchesList as $entCode=>$entDet) { if ($entDet['idCorporate']!=$this->idCorporateFilter) { unset($this->bulkMatches[$idx][$entCode]); } } } } $lastName = $firstName = ''; foreach($this->criteriaList as $criterion) { if ($criterion['field']=='name') { $lastName = mb_strtoupper((string)$criterion['value']['lastName']); $firstName = mb_strtoupper((string)$criterion['value']['firstName']); break; } } if ($lastName and $firstName) { foreach($this->bulkMatches as $idx=>$matchesList) { foreach($matchesList as $entCode=>$entDet) { if ( ( $entDet['_matchedField']=='email' or $entDet['_matchedField']=='telephone' or $entDet['_matchedField']=='telephoneMobile' ) and ( $lastName!=mb_strtoupper((string)$entDet['lastName']) or $firstName!=mb_strtoupper((string)$entDet['firstName']) ) ) { unset($this->bulkMatches[$idx][$entCode]); } } } } if ($this->bulkMatches) { $PNCs = []; foreach($this->criteriaList as $criterion) { if ($criterion['field']=='PNC' and $criterion['value']) { $PNCs[] = $criterion['value']; } } if ($PNCs) { foreach($this->bulkMatches as $idx=>$matchesList) { foreach($matchesList as $entCode=>$entDet) { if ( $entDet['_matchedField']=='name' and $entDet['PNC'] and !in_array($entDet['PNC'], $PNCs)) { unset($this->bulkMatches[$idx][$entCode]); } } } } } if ($this->bulkMatches) { $emails = []; foreach($this->criteriaList as $criterion) { if ($criterion['field']=='email' and $criterion['value']) { $emails[] = mb_strtoupper($criterion['value']); } } if ($emails) { foreach($this->bulkMatches as $idx=>$matchesList) { foreach($matchesList as $entCode=>$entDet) { if ( $entDet['_matchedField']=='name' and $entDet['email'] and !in_array(mb_strtoupper($entDet['email']), $emails)) { unset($this->bulkMatches[$idx][$entCode]); } } } } } $bulkMatches = $this->bulkMatches; \Engine\Plugins\Manager::getInstance()->executeEvent('App/EntityMatch/Customer-cleanMatches', ['object' => $this, 'bulkMatches'=>&$bulkMatches]); $this->bulkMatches = array_filter($bulkMatches); } } 