fromArray(array( 'xmlRpcClient' => $this->client, 'clientApiId' => isset($packet['apiId']) ? $packet['apiId'] : null, 'ipAddress' => $_SERVER['REMOTE_ADDR'], 'valuation' => $this->valuation, 'methodName' => $methodName, 'packet' => serialize($packet), 'status' => $status )); $this->em->persist($log); $this->em->flush($log); } /** * @param string $apiId * @param string $password * @return string */ public function Authenticate($ApiId, $Password) { $this->em = \Utility\Registry::getEntityManager(); $this->client = $this->em->getRepository('Valuation\\Entity\\XmlRpc') ->findOneBy(array( 'clientApiId' => $ApiId )); if (is_null($this->client) || md5($Password) != $this->client->passwordHash || $this->client->archived) { $this->logRequest(__METHOD__, array('apiId' => $ApiId), 'System Authentication Failure'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('System Authentication Failure.', 001); } $this->client->ipAddress = $_SERVER['REMOTE_ADDR']; $this->em->flush($this->client); $this->logRequest(__METHOD__, array('apiId' => $ApiId)); return $this->client->authToken; } protected function checkAuth($AuthToken) { $this->em = \Utility\Registry::getEntityManager(); $this->client = $this->em->getRepository('Valuation\\Entity\\XmlRpc') ->findOneBy(array( 'authToken' => $AuthToken, 'ipAddress' => $_SERVER['REMOTE_ADDR'] )); if (is_null($this->client) || $this->client->archived) { $this->logRequest(__METHOD__, array('authToken' => $AuthToken), 'System Authentication Failure.'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('System Authentication Failure.', 001); } } protected function tempAuth($email) { \Utility\Registry::clearAuthData(); \Utility\Registry::clearSessionData(); $user = $this->em->getRepository('\User\Entity\Profile') ->findOneBy(array('email' => $email)); if (is_null($user) || $user->archived) { $this->logRequest(__METHOD__, array('email' => $email), 'User Authentication Failure.'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('User Authentication Failure.', 001); } $authData = $user->toArray(array( 'company', 'tradeCenter', 'group', 'groupDivision', 'permissions', 'city', 'region', 'contact', 'manager' )); if ($authData['company']['jobState'] != 'Active') { $this->logRequest(__METHOD__, array('email' => $email), 'Dealership Authentication Failure due to suspension.'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Dealership Authentication Failure due to suspension.', 002); } if ($authData['jobState'] != 'Active') { $this->logRequest(__METHOD__, array('email' => $email), 'User Authentication Failure due to suspension.'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('User Authentication Failure due to suspension.', 003); } \Utility\Registry::setAuthData($authData); } protected function getDataList($AuthToken, $entity, $fields = false, $extraFields = '', array $simplify = array()) { list($module, $mid, $short) = explode('\\', $entity); if (false === $fields) { $selection = $short; } else { $selection = 'partial ' . $short . '.{' . $fields . '}' . $extraFields; } $data = $this->em->createQuery( 'SELECT ' . $selection . ' FROM ' . $entity . ' ' . $short ) ->getArrayResult(); $response = array(); if (!empty($simplify)) { foreach ($data as $i => $row) { $response[$i] = array(); foreach ($row[0] as $field => $value) { $response[$i][ucfirst($field)] = $value; } foreach ($simplify as $field) { $response[$i][ucfirst($field)] = $row[$field]; } } } else { foreach ($data as $i => $row) { $response[$i] = array(); foreach ($data[$i] as $field => $value) { $response[$i][ucfirst($field)] = $value; } } } return array('List' => $response); } /** * @param string $AuthToken * @return struct */ public function GetVehicleYearData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Year'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleCategoryData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Category'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleMakeData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList( $AuthToken, 'Stock\\Entity\\Make', 'id,name,created,updated,archived' ); } /** * @param string $AuthToken * @return struct */ public function GetVehicleModelData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList( $AuthToken, 'Stock\\Entity\\Model', 'id,name,created,updated,archived', ', IDENTITY(Model.make) as makeId', array('makeId') ); } /** * @param string $AuthToken * @return struct */ public function GetVehicleTypeData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList( $AuthToken, 'Stock\\Entity\\Type', 'id,name,mmCode,introMonth,discMonth,created,updated,archived', ', IDENTITY(Type.model) as modelId , IDENTITY(Type.introYear) as introYearId , IDENTITY(Type.discYear) as discYearId', array('modelId', 'introYearId', 'discYearId') ); } /** * @param string $AuthToken * @return struct */ public function GetVehicleFuelData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\FuelType'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleTransmissionData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\TransmissionType'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleConditionData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Condition'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleExteriorColourData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\ExteriorColour'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleInteriorColourData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\InteriorColour'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleUpholsteryData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Upholstery'); } /** * @param string $AuthToken * @return struct */ public function GetVehiclePapersData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Paper'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleNatisData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Natis'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleAccessoryData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Accessory'); } /** * @param string $AuthToken * @return struct */ public function GetVehicleComponentData($AuthToken) { $this->checkAuth($AuthToken); $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Damage'); } /* * @param string $AuthToken * @return struct */ /*public function GetStockList($AuthToken) { $this->checkAuth($AuthToken); if ('GEMS' != $this->client->clientName) { throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Access Denied.', 099); } $this->logRequest(__METHOD__, array('authToken' => $AuthToken)); return $this->getDataList($AuthToken, 'Stock\\Entity\\Damage'); }*/ /** * @param string $AuthToken * @param string $email * @param struct $valuation * @return struct */ public function CreateValuation($AuthToken, $Email, array $Valuation) { #-> Authentication. $this->checkAuth($AuthToken); $this->tempAuth($Email); #-> Mandatory field checks. $packet = array( 'authToken' => $AuthToken, 'email' => $Email, 'valuation' => $Valuation ); $requiredString = array( 'CustomerName', 'CustomerSurname', 'CustomerMobile', 'RegistrationNumber' ); $requiredNumeric = array( 'Km' ); $requiredReference = array( 'VehicleYearId' => 'Stock\\Entity\\Year', 'VehicleTypeId' => 'Stock\\Entity\\Type', 'VehicleFuelTypeId' => 'Stock\\Entity\\FuelType', 'VehicleTransmissionTypeId' => 'Stock\\Entity\\TransmissionType' ); foreach ($requiredString as $field) { if (!isset($Valuation[$field]) || 0 == strlen($Valuation[$field])) { $this->logRequest(__METHOD__, $packet, 'Required field not provided or empty: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Required field not provided or empty: ' . $field, 010); } } foreach ($requiredNumeric as $field) { if (!isset($Valuation[$field]) || 0 == strlen($Valuation[$field]) || !is_numeric($Valuation[$field])) { $this->logRequest(__METHOD__, $packet, 'Required field not provided or not numeric: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Required field not provided or not numeric: ' . $field, 011); } } foreach ($requiredReference as $field => $entityName) { if (!isset($Valuation[$field]) || 0 == strlen($Valuation[$field]) || !is_numeric($Valuation[$field])) { $this->logRequest(__METHOD__, $packet, 'Required field not provided or not numeric: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Required field not provided or not numeric: ' . $field, 013); } $record = $this->em->find($entityName, $Valuation[$field]); if (is_null($record)) { $this->logRequest(__METHOD__, $packet, 'Required field not a valid reference: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Required field not a valid reference: ' . $field, 012); } $Valuation[$field] = $record; } #-> Verify remaining fields. if (isset($Valuation['id'])) { unset($Valuation['id']); } $remainingNumeric = array(); foreach ($remainingNumeric as $field) { if (isset($Valuation[$field]) && null != $Valuation[$field] && !is_numeric($Valuation[$field])) { $this->logRequest(__METHOD__, $packet, 'Optional field not null or numeric: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Optional field not null or numeric: ' . $field, 014); } } $remainingReference = array( 'VehicleConditionId' => 'Stock\\Entity\\Condition', 'VehicleExteriorColourId' => 'Stock\\Entity\\ExteriorColour', 'VehicleInteriorColourId' => 'Stock\\Entity\\InteriorColour', 'VehicleUpholsteryId' => 'Stock\\Entity\\Upholstery', 'VehiclePapersId' => 'Stock\\Entity\\Paper', 'VehicleNatisId' => 'Stock\\Entity\\Natis' ); foreach ($remainingReference as $field => $entityName) { if (!isset($Valuation[$field]) || 0 == strlen($Valuation[$field]) || !is_numeric($Valuation[$field])) { if (isset($Valuation[$field])) { unset($Valuation[$field]); } continue; } $record = $this->em->find($entityName, $Valuation[$field]); if (is_null($record)) { $this->logRequest(__METHOD__, $packet, 'Optional field not null or a valid reference: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Optional field not null or a valid reference: ' . $field, 015); } $Valuation[$field] = $record; } #-> Verify valid urls on images. $urlValidator = new \Zend\Validator\Uri(); for ($i = 1; $i <= 8; $i++) { $field = 'VehiclePhoto' . $i . 'URL'; if (isset($Valuation[$field]) && !is_null($Valuation[$field])) { if (!$urlValidator->isValid($Valuation[$field])) { $this->logRequest(__METHOD__, $packet, 'Optional field not null or valid url: ' . $field); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Optional field not null or a valid url: ' . $field, 016); } } } #-> Verify damages collection. if (isset($Valuation['Damages'])) { foreach ($Valuation['Damages'] as $i => $damageEntry) { if (!isset($damageEntry['VehicleComponentId']) || 0 == strlen($damageEntry['VehicleComponentId']) || !is_numeric($damageEntry['VehicleComponentId'])) { $this->logRequest(__METHOD__, $packet, 'Damages section required field not provided or not numeric: VehicleComponentId'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Damages section required field not provided or not numeric: VehicleComponentId', 017); } $record = $this->em->find('Stock\\Entity\\Damage', $damageEntry['VehicleComponentId']); if (is_null($record)) { $this->logRequest(__METHOD__, $packet, 'Damages section required field not a valid reference: VehicleComponentId'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Damages section required field not a valid reference: VehicleComponentId', 018); } $Valuation['Damages'][$i]['VehicleComponentId'] = $record; if (isset($damageEntry['Amount']) && !is_numeric($damageEntry['Amount'])) { $this->logRequest(__METHOD__, $packet, 'Damages section required field not numeric: Amount'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Damages section required field not numeric: Amount', 019); } } } #-> Verify accessories collection. if (isset($Valuation['Accessories'])) { foreach ($Valuation['Accessories'] as $i => $accessoryEntry) { if (!isset($accessoryEntry['VehicleAccessoryId']) || 0 == strlen($accessoryEntry['VehicleAccessoryId']) || !is_numeric($accessoryEntry['VehicleAccessoryId'])) { $this->logRequest(__METHOD__, $packet, 'Accessories section required field not provided or not numeric: VehicleAccessoryId'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Accessories section required field not provided or not numeric: VehicleAccessoryId', 020); } $record = $this->em->find('Stock\\Entity\\Accessory', $accessoryEntry['VehicleAccessoryId']); if (is_null($record)) { $this->logRequest(__METHOD__, $packet, 'Accessories section required field not a valid reference: VehicleAccessoryId'); throw new \Zend\XmlRpc\Server\Exception\RuntimeException('Accessories section required field not a valid reference: VehicleAccessoryId', 021); } $Valuation['Accessories'][$i]['VehicleAccessoryId'] = $record; } } #-> Collect trade and retail values. $result = \Utility\Comms\TransUnion::searchByMmCode( $Valuation['VehicleTypeId']->mmCode, $Valuation['VehicleYearId']->name ); if (is_array($result) && isset($result['VehicleDetails']) && isset($result['VehicleDetails'][0]) && isset($result['VehicleDetails'][0]['Value'])) { $Valuation['tradePrice'] = $result['VehicleDetails'][0]['Value']['TradePrice']; $Valuation['retailPrice'] = $result['VehicleDetails'][0]['Value']['RetailPrice']; $Valuation['listPrice'] = $result['VehicleDetails'][0]['Value']['ListPrice']; } else { $Valuation['tradePrice'] = 0.0; $Valuation['retailPrice'] = 0.0; $Valuation['listPrice'] = 0.0; } # ------------------------------------------------------------------------------ # #-> Validation complete, create stock, damages, accessories and valuation entries. #-> Create stock entry. $stock = new \Stock\Entity\Stock(); $stock->tradePrice = $Valuation['tradePrice']; $stock->retailPrice = $Valuation['retailPrice']; $stock->listPrice = $Valuation['listPrice']; isset($Valuation['VehicleYearId']) && $stock->vehicleYear = $Valuation['VehicleYearId']; isset($Valuation['VehicleTypeId']) && $stock->type = $Valuation['VehicleTypeId']; isset($Valuation['RegistrationNumber']) && $stock->registrationNumber = $Valuation['RegistrationNumber']; isset($Valuation['VehicleFuelTypeId']) && $stock->fuelType = $Valuation['VehicleFuelTypeId']; isset($Valuation['VehicleTransmissionTypeId']) && $stock->transmissionType = $Valuation['VehicleTransmissionTypeId']; isset($Valuation['VinNumber']) && $stock->vinNumber = $Valuation['VinNumber']; isset($Valuation['EngineNumber']) && $stock->engineNumber = $Valuation['EngineNumber']; isset($Valuation['Km']) && $stock->km = $Valuation['Km']; isset($Valuation['VehicleConditionId']) && $stock->condition = $Valuation['VehicleConditionId']; isset($Valuation['VehicleExteriorColourId']) && $stock->exteriorColour = $Valuation['VehicleExteriorColourId']; isset($Valuation['VehicleInteriorColourId']) && $stock->interiorColour = $Valuation['VehicleInteriorColourId']; isset($Valuation['VehicleUpholsteryId']) && $stock->upholstery = $Valuation['VehicleUpholsteryId']; isset($Valuation['VehiclePapersId']) && $stock->papers = $Valuation['VehiclePapersId']; isset($Valuation['VehicleNatisId']) && $stock->natisc = $Valuation['VehicleNatisId']; isset($Valuation['natis']) && $stock->spareKeys = $Valuation['natis']; isset($Valuation['FullServiceHistory']) && $stock->fullServiceHistory = $Valuation['FullServiceHistory']; isset($Valuation['FullServiceHistoryNotes']) && $stock->fshNotes = $Valuation['FullServiceHistoryNotes']; isset($Valuation['PreviousRepairs']) && $stock->previousRepairsNoted = $Valuation['PreviousRepairs']; isset($Valuation['PreviousRepairsNotes']) && $stock->previousRepairsNotes = $Valuation['PreviousRepairsNotes']; isset($Valuation['AccessoryNotes']) && $stock->accessoryNotes = $Valuation['AccessoryNotes']; isset($Valuation['DamageNotes']) && $stock->damageNotes = $Valuation['DamageNotes']; $this->em->persist($stock); $this->em->flush($stock); $stock->postInsert(); $this->em->flush($stock); #-> Log images so that we can download them later. $images = array( 1 => 'main', 2 => 'front', 3 => 'right', 4 => 'left', 5 => 'back', 6 => 'interior', 7 => 'engine', 8 => 'natis' ); $stockImages = null; for ($i = 1; $i <= 8; $i++) { $field = 'VehiclePhoto' . $i . 'URL'; $urlField = $images[$i] . 'ImageUrl'; if (isset($Valuation[$field]) && !is_null($Valuation[$field])) { if (is_null($stockImages)) { $stockImages = new \Stock\Entity\StockImages(); $stockImages->stock = $stock; } $stockImages->$urlField = $Valuation[$field]; } } if (!is_null($stockImages)) { $this->em->persist($stockImages); $this->em->flush($stockImages); } #-> Log vehicle accessories. foreach ($Valuation['Accessories'] as $item) { $accessory = new \Stock\Entity\StockAccessory(); $accessory->stock = $stock; $accessory->accessory = $item['VehicleAccessoryId']; $this->em->persist($accessory); } $this->em->flush(); #-> Log vehicle damages. $total = 0.0; foreach ($Valuation['Damages'] as $item) { $damage = new \Stock\Entity\StockDamage(); $damage->stock = $stock; $damage->damage = $item['VehicleComponentId']; $damage->amount = $item['Amount']; $this->em->persist($damage); $total += $damage->amount; } $stock->damageTotal = $total; $this->em->flush(); #-> Create valuation entry. $this->valuation = new \Valuation\Entity\Valuation(); $this->valuation->xmlRpcClient = $this->client; $this->valuation->stock = $stock; $this->valuation->tradeRetailRequested = true; $this->valuation->jobState = 'New Valuation'; isset($Valuation['ItemId']) && $this->valuation->sfItemId = $Valuation['ItemId']; isset($Valuation['CustomerName']) && $this->valuation->firstName = $Valuation['CustomerName']; isset($Valuation['CustomerSurname']) && $this->valuation->familyName = $Valuation['CustomerSurname']; isset($Valuation['CustomerIdNumber']) && $this->valuation->idNumber = $Valuation['CustomerIdNumber']; isset($Valuation['CustomerMobile']) && $this->valuation->mobile = $Valuation['CustomerMobile']; isset($Valuation['CustomerEmail']) && $this->valuation->email = $Valuation['CustomerEmail']; isset($Valuation['Department']) && $this->valuation->department = $Valuation['Department']; $this->em->persist($this->valuation); $this->em->flush($this->valuation); $this->valuation->postInsert(); $this->em->flush($this->valuation); $stock->valuation = $this->valuation; $this->em->flush($stock); #-> Collect list of valuators to send to. $userBin = new \User\DataBin\Profile(); $filters = $userBin->getValuationFilters(); $valuators = $this->em->getRepository('User\\Entity\\Profile') ->findBy($filters); if (!empty($valuators)) { $serviceInput = new \Workspace\Utility\ServiceInput(); $valuationService = new \Valuation\Service\Valuation(); $valuationService->setWorkflow(\Utility\Registry::getServiceManager()->get('Valuation')); $valuationService->generateHistoryList(array(), $this->valuation, $this->valuation, new \Workspace\Utility\ServiceInputParams()); $this->valuation->queueStatus = 1; $this->em->flush($this->valuation); $authData = \Utility\Registry::getAuthData(); $fromCompanyId = isset($authData['company']['id']) ? $authData['company']['id'] : null; $fromProfileId = isset($authData['id']) ? $authData['id'] : null; $params = array(); $params['firstName'] = $authData['firstName']; $params['familyName'] = $authData['familyName']; $params['customer_name'] = $this->valuation->firstName; $params['customer_family_name'] = $this->valuation->familyName; $params['vehicle_reg'] = is_null($stock->registrationNumber) ? '' : $stock->registrationNumber; $oNotify = new \Utility\Comms\Notification(); $templateName = 'valuation-mobile-new'; $subject = null; $toCompanyId = $fromCompanyId; $email = null; foreach ($valuators as $valuator) { $valuator = $valuator->toArray(); $toProfileId = $valuator['id']; $mobile = $valuator['mobile']; $oNotify->sendFromTemplate( $fromCompanyId, $fromProfileId, $toCompanyId, $toProfileId, $email, $mobile, $subject, $templateName, $params ); } } #-> Return. $this->logRequest(__METHOD__, $packet); return array( 'TradePrice' => $Valuation['tradePrice'], 'RetailPrice' => $Valuation['retailPrice'], 'ListPrice' => $Valuation['listPrice'] ); } }