2 namespace Valuation\Service;
9 * @var \Doctrine\ORM\EntityManager
13 * @var \Valuation\Entity\XmlRpc
15 protected $client = null;
17 * @var \Valuation\Entity\Valuation
19 protected $valuation = null;
24 * Utility to log api callbacks.
25 * @param string $methodName
26 * @param array $packet
27 * @param string $status
29 protected function logCallback($methodName, array $packet, array $response, $status = 'OK')
31 $log = new \Valuation\Entity\XmlRpcCallbackLog();
32 $log->fromArray(array(
33 'xmlRpcClient' => $this->client,
34 'valuation' => $this->valuation,
35 'callbackUrl' => $this->client->callbackUrl,
36 'methodName' => $methodName,
37 'packet' => serialize($packet),
38 'response' => serialize($response),
41 $this->em->persist($log);
42 $this->em->flush($log);
46 * Put together a client-friendly valuation packet.
47 * @param \Valuation\Entity\Valuation $valuation
50 protected function prepareCallbackData($methodName, $valuation)
53 $items = $valuation->stock->damages->getIterator();
54 foreach ($items as $item)
57 'VehicleComponentId' => $item->damage->id,
58 'Amount' => $item->amount
61 $accessories = array();
62 $items = $valuation->stock->accessories->getIterator();
63 foreach ($items as $item)
65 $accessories[] = array(
66 'VehicleAccessoryId' => $item->accessory->id
71 'CustomerName' => $valuation->firstName,
72 'CustomerSurname' => $valuation->familyName,
73 'CustomerID' => $valuation->idNumber,
74 'Mobile' => $valuation->mobile,
75 'Email' => $valuation->email,
76 'Department' => $valuation->department,
77 'AmountOffered' => $valuation->amountOffered,
78 'VehicleYearId' => $valuation->stock->vehicleYear->id,
79 'VehicleTypeId' => $valuation->stock->type->id,
80 'RegistrationNumber' => $valuation->stock->registrationNumber,
81 'VehicleFuelTypeId' => $valuation->stock->fuelType->id,
82 'VehicleTransmissionTypeId' => $valuation->stock->transmissionType->id,
83 'VinNumber' => $valuation->stock->vinNumber,
84 'EngineNumber' => $valuation->stock->engineNumber,
85 'Km' => $valuation->stock->km,
86 'VehicleConditionId' => !is_null($valuation->stock->condition)
87 ? $valuation->stock->condition->id
89 'VehicleExteriorColourId' => !is_null($valuation->stock->exteriorColour)
90 ? $valuation->stock->exteriorColour->id
92 'VehicleInteriorColourId' => !is_null($valuation->stock->interiorColour)
93 ? $valuation->stock->interiorColour->id
95 'VehicleUpholsteryId' => !is_null($valuation->stock->upholstery)
96 ? $valuation->stock->upholstery->id
98 'VehiclePapersId' => !is_null($valuation->stock->papers)
99 ? $valuation->stock->papers->id
101 'VehicleNatisId' => !is_null($valuation->stock->natis)
102 ? $valuation->stock->natis->id
104 'FullServiceHistory' => $valuation->stock->fullServiceHistory,
105 'FullServiceHistoryNotes' => $valuation->stock->fshNotes,
106 'PreviousRepairs' => $valuation->stock->previousRepairsNoted,
107 'PreviousRepairsNotes' => $valuation->stock->previousRepairsNotes,
108 'AccessoryNotes' => $valuation->stock->accessoryNotes,
109 'DamageNotes' => $valuation->stock->damageNotes,
110 'Damages' => $damages,
111 'Accessories' => $accessories
117 * Do SentToSales client system callback.
118 * @param integer $valuationId
120 public function triggerSentToSales($valuationId)
122 $this->em = \Utility\Registry::getEntityManager();
123 $this->valuation = $this->em->find('Valuation\\Entity\\Valuation', $valuationId);
124 $this->client = $this->valuation->xmlRpcClient;
125 $client = new \Zend\XmlRpc\Client(
126 $this->client->callbackUrl
130 $data = $this->prepareCallbackData('SentToSales', $this->valuation);
132 'Email' => $this->valuation->createdBy->email,
133 'ItemId' => $this->valuation->sfItemId,
138 #-> Do xml-rpc call to client system.
139 $response = $client->call(
142 $this->valuation->createdBy->email,
143 $this->valuation->sfItemId,
147 $this->logCallback('SentToSales', $packet, is_array($response) ? $response : array($response));
149 catch (\Exception $e)
151 $this->logCallback('SentToSales', $packet, array($e->getMessage()), 'Exception');
152 \Utility\Debug::errorLog('triggerSentToSales EXCEPTION', "$e");
157 * Make another attempt at client callback.
158 * @param string $methodName
159 * @param \Valuation\Entity\XmlRpcCallbackLog $callback
161 protected function retryCallback($methodName, $callback)
163 $this->valuation = $callback->valuation;
164 $this->client = $this->valuation->xmlRpcClient;
165 $client = new \Zend\XmlRpc\Client(
166 $this->client->callbackUrl
170 $data = $this->prepareCallbackData($methodName, $this->valuation);
172 'Email' => $this->valuation->createdBy->email,
173 'ItemId' => $this->valuation->sfItemId,
178 #-> Do xml-rpc call to client system.
179 $response = $client->call(
182 $this->valuation->createdBy->email,
183 $this->valuation->sfItemId,
187 $callback->response = is_array($response)
188 ? serialize($response)
189 : serialize(array($response));
190 $callback->status = 'OK';
191 $callback->attempts = $callback->attempts + 1;
194 catch (\Exception $e)
196 #-> Oops, log the exception.
197 $callback->response = serialize(array($e->getMessage()));
198 $callback->attempts = $callback->attempts + 1;
200 \Utility\Debug::errorLog('retryCallback EXCEPTION', "$e");
205 * Retry failed callback up to max of 3 attempts.
207 public function retryCallbacks()
209 $this->em = \Utility\Registry::getEntityManager();
213 foreach ($methods as $methodName)
215 for ($i = 5; $i > 0; $i--)
217 $callbacks = $this->em->getRepository('Valuation\Entity\XmlRpcCallbackLog')
219 'status' => 'Exception',
221 'methodName' => $methodName
223 foreach ($callbacks as $callback)
225 $this->retryCallback($methodName, $callback);
232 /* ------------------------------------------ TEST UTILITY ------------------------------------------ */
234 * @param string $Email
235 * @param string $ItemId
236 * @param string $Valuation
239 public function SentToSales($Email, $ItemId, $Valuation)
241 \Utility\Debug::errorLog('SentToSales email', $Email);
242 \Utility\Debug::errorLog('SentToSales itemId', $ItemId);
243 \Utility\Debug::errorLog('SentToSales data', $Valuation);