text changes to registration mail content
[namibia] / module / Valuation / src / Valuation / Service / XmlRpcCallback.php
1 <?php
2 namespace Valuation\Service;
3
4 class XmlRpcCallback
5 {
6
7
8         /**
9          * @var \Doctrine\ORM\EntityManager
10          */
11         protected $em           = null;
12         /**
13          * @var \Valuation\Entity\XmlRpc
14          */
15         protected $client       = null;
16         /**
17          * @var \Valuation\Entity\Valuation
18          */
19         protected $valuation = null;
20
21
22
23         /**
24          * Utility to log api callbacks.
25          * @param string $methodName
26          * @param array $packet
27          * @param string $status
28          */
29         protected function logCallback($methodName, array $packet, array $response, $status = 'OK')
30         {
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),
39                                 'status'                => $status
40                 ));
41                 $this->em->persist($log);
42                 $this->em->flush($log);
43         }
44
45         /**
46          * Put together a client-friendly valuation packet.
47          * @param \Valuation\Entity\Valuation $valuation
48          * @return struct
49          */
50         protected function prepareCallbackData($methodName, $valuation)
51         {
52                 $damages = array();
53                 $items = $valuation->stock->damages->getIterator();
54                 foreach ($items as $item)
55                 {
56                         $damages[] = array(
57                                         'VehicleComponentId'    => $item->damage->id,
58                                         'Amount'                                => $item->amount
59                         );
60                 }
61                 $accessories = array();
62                 $items = $valuation->stock->accessories->getIterator();
63                 foreach ($items as $item)
64                 {
65                         $accessories[] = array(
66                                         'VehicleAccessoryId'    => $item->accessory->id
67                         );
68                 }
69                 $data = array(
70                                 // Valuation data.
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
88                                                                                                         : null,
89                                 'VehicleExteriorColourId'       => !is_null($valuation->stock->exteriorColour)
90                                                                                                         ? $valuation->stock->exteriorColour->id
91                                                                                                         : null,
92                                 'VehicleInteriorColourId'       => !is_null($valuation->stock->interiorColour)
93                                                                                                         ? $valuation->stock->interiorColour->id
94                                                                                                         : null,
95                                 'VehicleUpholsteryId'           =>  !is_null($valuation->stock->upholstery)
96                                                                                                         ? $valuation->stock->upholstery->id
97                                                                                                         : null,
98                                 'VehiclePapersId'                       =>  !is_null($valuation->stock->papers)
99                                                                                                         ? $valuation->stock->papers->id
100                                                                                                         : null,
101                                 'VehicleNatisId'                        =>  !is_null($valuation->stock->natis)
102                                                                                                         ? $valuation->stock->natis->id
103                                                                                                         : null,
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
112                 );
113                 return $data;
114         }
115
116         /**
117          * Do SentToSales client system callback.
118          * @param integer $valuationId
119          */
120         public function triggerSentToSales($valuationId)
121         {
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
127                 );
128
129                 #-> Prepare data.
130                 $data = $this->prepareCallbackData('SentToSales', $this->valuation);
131                 $packet = array(
132                                 'Email'         => $this->valuation->createdBy->email,
133                                 'ItemId'        => $this->valuation->sfItemId,
134                                 'Valuation' => $data
135                 );
136                 try
137                 {
138                         #-> Do xml-rpc call to client system.
139                         $response = $client->call(
140                                         'SentToSales',
141                                         array(
142                                                         $this->valuation->createdBy->email,
143                                                         $this->valuation->sfItemId,
144                                                         $data
145                                         )
146                         );
147                         $this->logCallback('SentToSales', $packet, is_array($response) ? $response : array($response));
148                 }
149                 catch (\Exception $e)
150                 {
151                         $this->logCallback('SentToSales', $packet, array($e->getMessage()), 'Exception');
152                         \Utility\Debug::errorLog('triggerSentToSales EXCEPTION', "$e");
153                 }
154         }
155
156         /**
157          * Make another attempt at client callback.
158          * @param string $methodName
159          * @param \Valuation\Entity\XmlRpcCallbackLog $callback
160          */
161         protected function retryCallback($methodName, $callback)
162         {
163                 $this->valuation = $callback->valuation;
164                 $this->client = $this->valuation->xmlRpcClient;
165                 $client = new \Zend\XmlRpc\Client(
166                                 $this->client->callbackUrl
167                 );
168
169                 #-> Prepare data.
170                 $data = $this->prepareCallbackData($methodName, $this->valuation);
171                 $packet = array(
172                                 'Email'         => $this->valuation->createdBy->email,
173                                 'ItemId'        => $this->valuation->sfItemId,
174                                 'Valuation' => $data
175                 );
176                 try
177                 {
178                         #-> Do xml-rpc call to client system.
179                         $response = $client->call(
180                                         $methodName,
181                                         array(
182                                                         $this->valuation->createdBy->email,
183                                                         $this->valuation->sfItemId,
184                                                         $data
185                                         )
186                         );
187                         $callback->response = is_array($response)
188                                 ? serialize($response)
189                                 : serialize(array($response));
190                         $callback->status       = 'OK';
191                         $callback->attempts = $callback->attempts + 1;
192                         $this->em->flush();
193                 }
194                 catch (\Exception $e)
195                 {
196                         #-> Oops, log the exception.
197                         $callback->response = serialize(array($e->getMessage()));
198                         $callback->attempts = $callback->attempts + 1;
199                         $this->em->flush();
200                         \Utility\Debug::errorLog('retryCallback EXCEPTION', "$e");
201                 }
202         }
203
204         /**
205          * Retry failed callback up to max of 3 attempts.
206          */
207         public function retryCallbacks()
208         {
209                 $this->em = \Utility\Registry::getEntityManager();
210                 $methods = array(
211                                 'SentToSales'
212                 );
213                 foreach ($methods as $methodName)
214                 {
215                         for ($i = 5; $i > 0; $i--)
216                         {
217                                 $callbacks = $this->em->getRepository('Valuation\Entity\XmlRpcCallbackLog')
218                                         ->findBy(array(
219                                                         'status'                => 'Exception',
220                                                         'attempts'              => $i,
221                                                         'methodName'    => $methodName
222                                         ));
223                                 foreach ($callbacks as $callback)
224                                 {
225                                         $this->retryCallback($methodName, $callback);
226                                 }
227                         }
228                 }
229         }
230
231
232         /* ------------------------------------------ TEST UTILITY ------------------------------------------ */
233         /**
234          * @param string $Email
235          * @param string $ItemId
236          * @param string $Valuation
237          * @return string
238          */
239         public function SentToSales($Email, $ItemId, $Valuation)
240         {
241                 \Utility\Debug::errorLog('SentToSales email', $Email);
242                 \Utility\Debug::errorLog('SentToSales itemId', $ItemId);
243                 \Utility\Debug::errorLog('SentToSales data', $Valuation);
244                 return 'Moo';
245         }
246
247
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262