1
|
|
|
<?php |
2
|
|
|
namespace Route4Me; |
3
|
|
|
|
4
|
|
|
use Route4Me\Common; |
5
|
|
|
use Route4Me\Enum\Endpoint; |
6
|
|
|
|
7
|
|
|
class Order extends Common |
8
|
|
|
{ |
9
|
|
|
public $address_1; |
10
|
|
|
public $address_2; |
11
|
|
|
public $cached_lat; |
12
|
|
|
public $cached_lng; |
13
|
|
|
public $curbside_lat; |
14
|
|
|
public $curbside_lng; |
15
|
|
|
public $address_alias; |
16
|
|
|
public $address_city; |
17
|
|
|
public $EXT_FIELD_first_name; |
18
|
|
|
public $EXT_FIELD_last_name; |
19
|
|
|
public $EXT_FIELD_email; |
20
|
|
|
public $EXT_FIELD_phone; |
21
|
|
|
public $EXT_FIELD_custom_data; |
22
|
|
|
|
23
|
|
|
public $color; |
24
|
|
|
public $order_icon; |
25
|
|
|
public $local_time_window_start; |
26
|
|
|
public $local_time_window_end; |
27
|
|
|
public $local_time_window_start_2; |
28
|
|
|
public $local_time_window_end_2; |
29
|
|
|
public $service_time; |
30
|
|
|
|
31
|
|
|
public $day_scheduled_for_YYMMDD; |
32
|
|
|
|
33
|
|
|
public $route_id; |
34
|
|
|
public $redirect; |
35
|
|
|
public $optimization_problem_id; |
36
|
|
|
public $order_id; |
37
|
|
|
public $order_ids; |
38
|
|
|
|
39
|
|
|
public $day_added_YYMMDD; |
40
|
|
|
public $scheduled_for_YYMMDD; |
41
|
|
|
public $fields; |
42
|
|
|
public $offset; |
43
|
|
|
public $limit; |
44
|
|
|
public $query; |
45
|
|
|
|
46
|
|
|
public $created_timestamp; |
47
|
|
|
public $order_status_id; |
48
|
|
|
public $member_id; |
49
|
|
|
public $address_state_id; |
50
|
|
|
public $address_country_id; |
51
|
|
|
public $address_zip; |
52
|
|
|
public $in_route_count; |
53
|
|
|
public $last_visited_timestamp; |
54
|
|
|
public $last_routed_timestamp; |
55
|
|
|
public $local_timezone_string; |
56
|
|
|
public $is_validated; |
57
|
|
|
public $is_pending; |
58
|
|
|
public $is_accepted; |
59
|
|
|
public $is_started; |
60
|
|
|
public $is_completed; |
61
|
|
|
public $custom_user_fields; |
62
|
|
|
|
63
|
|
|
public static function fromArray(array $params) { |
64
|
|
|
$order = new Order(); |
65
|
|
|
foreach ($params as $key => $value) { |
66
|
|
|
if (property_exists($order, $key)) { |
67
|
|
|
$order->{$key} = $value; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $order; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Order $params |
76
|
|
|
*/ |
77
|
|
|
public static function addOrder($params) |
78
|
|
|
{ |
79
|
|
|
$body = array(); |
80
|
|
|
|
81
|
|
|
$allAddOrderParameters = array('address_1', 'address_2', 'member_id', 'cached_lat', 'cached_lng', 'curbside_lat', |
82
|
|
|
'curbside_lng', 'color', 'order_icon', 'day_scheduled_for_YYMMDD', 'address_alias', 'address_city', 'address_state_id', |
83
|
|
|
'address_country_id', 'address_zip', 'local_time_window_start', 'local_time_window_end', 'local_time_window_start_2', |
84
|
|
|
'local_time_window_end_2', 'service_time', 'local_timezone_string', 'EXT_FIELD_first_name', 'EXT_FIELD_last_name', |
85
|
|
|
'EXT_FIELD_email', 'EXT_FIELD_phone', 'EXT_FIELD_custom_data', 'is_validated', 'is_pending', 'is_accepted', 'is_started', |
86
|
|
|
'is_completed', 'custom_user_fields'); |
87
|
|
|
|
88
|
|
|
foreach ($allAddOrderParameters as $addOrderParameter) { |
89
|
|
|
if (isset($params->{$addOrderParameter})) $body[$addOrderParameter] = $params->{$addOrderParameter}; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$response = Route4Me::makeRequst(array( |
93
|
|
|
'url' => Endpoint::ORDER_V4, |
94
|
|
|
'method' => 'POST', |
95
|
|
|
'body' => $body |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public static function addOrder2Route($params, $body) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$response = Route4Me::makeRequst(array( |
104
|
|
|
'url' => Endpoint::ROUTE_V4, |
105
|
|
|
'method' => 'PUT', |
106
|
|
|
'query' => array( |
107
|
|
|
'route_id' => isset($params->route_id) ? $params->route_id : null, |
108
|
|
|
'redirect' => isset($params->redirect) ? $params->redirect : null |
109
|
|
|
), |
110
|
|
|
'body' => (array)$body |
111
|
|
|
)); |
112
|
|
|
|
113
|
|
|
return $response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public static function addOrder2Optimization($params, $body) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$response = Route4Me::makeRequst(array( |
119
|
|
|
'url' => Endpoint::OPTIMIZATION_PROBLEM, |
120
|
|
|
'method' => 'PUT', |
121
|
|
|
'query' => array( |
122
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null, |
123
|
|
|
'redirect' => isset($params['redirect']) ? $params['redirect'] : null, |
124
|
|
|
'device_type' => isset($params['device_type']) ? $params['device_type'] : null |
125
|
|
|
), |
126
|
|
|
'body' => (array)$body |
127
|
|
|
)); |
128
|
|
|
|
129
|
|
|
return $response; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public static function getOrder($params) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$query = array(); |
135
|
|
|
$allGetParameters = array('order_id', 'fields', 'day_added_YYMMDD', 'scheduled_for_YYMMDD', 'query', 'offset', 'limit' ); |
136
|
|
|
|
137
|
|
|
foreach ($allGetParameters as $getParameter) { |
138
|
|
|
if (isset($params->{$getParameter})) $query[$getParameter] = $params->{$getParameter}; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$response = Route4Me::makeRequst(array( |
142
|
|
|
'url' => Endpoint::ORDER_V4, |
143
|
|
|
'method' => 'GET', |
144
|
|
|
'query' => $query |
145
|
|
|
)); |
146
|
|
|
|
147
|
|
|
return $response; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public static function getOrders($params) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$response = Route4Me::makeRequst(array( |
153
|
|
|
'url' => Endpoint::ORDER_V4, |
154
|
|
|
'method' => 'GET', |
155
|
|
|
'query' => array( |
156
|
|
|
'offset' => isset($params->offset) ? $params->offset : null, |
157
|
|
|
'limit' => isset($params->limit) ? $params->limit : null |
158
|
|
|
) |
159
|
|
|
)); |
160
|
|
|
|
161
|
|
|
return $response; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
public function getRandomOrderId($offset, $limit) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$params = array('offset' => $offset, 'limit' => $limit); |
167
|
|
|
|
168
|
|
|
$orders = self::getOrders($params); |
169
|
|
|
|
170
|
|
|
if (is_null($orders)) { |
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!isset($orders['results'])) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$randomIndex = rand(0, sizeof($orders['results']) - 1); |
179
|
|
|
|
180
|
|
|
$order = $orders['results'][$randomIndex]; |
181
|
|
|
|
182
|
|
|
return $order['order_id']; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
View Code Duplication |
public function getRandomOrder($offset, $limit) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$params = array('offset' => $offset, 'limit' => $limit); |
188
|
|
|
|
189
|
|
|
$orders = self::getOrders($params); |
190
|
|
|
|
191
|
|
|
if (is_null($orders)) { |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (!isset($orders['results'])) { |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$randomIndex = rand(0, sizeof($orders['results']) - 1); |
200
|
|
|
|
201
|
|
|
$order = $orders['results'][$randomIndex]; |
202
|
|
|
|
203
|
|
|
return $order; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public static function removeOrder($params) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$response = Route4Me::makeRequst(array( |
209
|
|
|
'url' => Endpoint::ORDER_V4, |
210
|
|
|
'method' => 'DELETE', |
211
|
|
|
'body' => array( |
212
|
|
|
'order_ids' => isset($params->order_ids) ? $params->order_ids : null |
213
|
|
|
) |
214
|
|
|
)); |
215
|
|
|
|
216
|
|
|
return $response; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public static function updateOrder($body) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$response = Route4Me::makeRequst(array( |
222
|
|
|
'url' => Endpoint::ORDER_V4, |
223
|
|
|
'method' => 'PUT', |
224
|
|
|
'body' => (array)$body |
225
|
|
|
)); |
226
|
|
|
|
227
|
|
|
return $response; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
View Code Duplication |
public static function searchOrder($params) |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$query = array(); |
233
|
|
|
$allSearchParameters = array('fields', 'day_added_YYMMDD', 'scheduled_for_YYMMDD', 'query', 'offset', 'limit' ); |
234
|
|
|
|
235
|
|
|
foreach ($allSearchParameters as $searchParameter) { |
236
|
|
|
if (isset($params->{$searchParameter})) $query[$searchParameter] = $params->{$searchParameter}; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$response = Route4Me::makeRequst(array( |
240
|
|
|
'url' => Endpoint::ORDER_V4, |
241
|
|
|
'method' => 'GET', |
242
|
|
|
'query' => $query |
243
|
|
|
)); |
244
|
|
|
|
245
|
|
|
return $response; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
View Code Duplication |
public static function validateLatitude($lat) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
if (!is_numeric($lat)) { |
251
|
|
|
return false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if ($lat>90 || $lat<-90) { |
255
|
|
|
return false; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
public static function validateLongitude($lng) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
if (!is_numeric($lng)) { |
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
if ($lng>180 || $lng<-180) { |
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return true; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function addOrdersFromCsvFile($csvFileHandle, $ordersFieldsMapping) |
275
|
|
|
{ |
276
|
|
|
$max_line_length = 512; |
277
|
|
|
$delemietr = ','; |
278
|
|
|
|
279
|
|
|
$results = array(); |
280
|
|
|
$results['fail'] = array(); |
281
|
|
|
$results['success'] = array(); |
282
|
|
|
|
283
|
|
|
$columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
284
|
|
|
|
285
|
|
|
$allOrderFields = array("curbside_lat","curbside_lng","color","day_scheduled_for_YYMMDD", |
286
|
|
|
"address_alias","address_1","address_2","local_time_window_start","local_time_window_end","local_time_window_start_2", |
287
|
|
|
"local_time_window_end_2","service_time","EXT_FIELD_first_name","EXT_FIELD_last_name","EXT_FIELD_email","EXT_FIELD_phone", |
288
|
|
|
"EXT_FIELD_custom_data","order_icon"); |
289
|
|
|
|
290
|
|
|
if (!empty($columns)) { |
291
|
|
|
array_push($results['fail'],'Empty CSV table'); |
292
|
|
|
return ($results); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$iRow=1; |
296
|
|
|
|
297
|
|
|
while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))!==false) { |
298
|
|
|
if ($rows[$ordersFieldsMapping['cached_lat']] && $rows[$ordersFieldsMapping['cached_lng']] && $rows[$ordersFieldsMapping['address_1']] && array(null)!==$rows) { |
299
|
|
|
|
300
|
|
|
$cached_lat = 0.000; |
|
|
|
|
301
|
|
|
|
302
|
|
View Code Duplication |
if (!$this->validateLatitude($rows[$ordersFieldsMapping['cached_lat']])) { |
|
|
|
|
303
|
|
|
array_push($results['fail'], "$iRow --> Wrong cached_lat"); |
304
|
|
|
$iRow++; |
305
|
|
|
continue; |
306
|
|
|
} else { |
307
|
|
|
$cached_lat = doubleval($rows[$ordersFieldsMapping['cached_lat']]); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$cached_lng = 0.000; |
|
|
|
|
311
|
|
|
|
312
|
|
View Code Duplication |
if (!$this->validateLongitude($rows[$ordersFieldsMapping['cached_lng']])) { |
|
|
|
|
313
|
|
|
array_push($results['fail'], "$iRow --> Wrong cached_lng"); |
314
|
|
|
$iRow++; |
315
|
|
|
continue; |
316
|
|
|
} else { |
317
|
|
|
$cached_lng = doubleval($rows[$ordersFieldsMapping['cached_lng']]); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if (isset($ordersFieldsMapping['curbside_lat'])) { |
321
|
|
|
if (!$this->validateLatitude($rows[$ordersFieldsMapping['curbside_lat']])) { |
322
|
|
|
array_push($results['fail'], "$iRow --> Wrong curbside_lat"); |
323
|
|
|
$iRow++; |
324
|
|
|
continue; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if (isset($ordersFieldsMapping['curbside_lng'])) { |
329
|
|
|
if (!$this->validateLongitude($rows[$ordersFieldsMapping['curbside_lng']])) { |
330
|
|
|
array_push($results['fail'], "$iRow --> Wrong curbside_lng"); |
331
|
|
|
$iRow++; |
332
|
|
|
continue; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$address = $rows[$ordersFieldsMapping['address_1']]; |
337
|
|
|
|
338
|
|
|
if (isset($ordersFieldsMapping['order_city'])) { |
339
|
|
|
$address.=', '.$rows[$ordersFieldsMapping['order_city']]; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (isset($ordersFieldsMapping['order_state_id'])) { |
343
|
|
|
$address.=', '.$rows[$ordersFieldsMapping['order_state_id']]; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if (isset($ordersFieldsMapping['order_zip_code'])) { |
347
|
|
|
$address.=', '.$rows[$ordersFieldsMapping['order_zip_code']]; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
if (isset($ordersFieldsMapping['order_country_id'])) { |
351
|
|
|
$address.=', '.$rows[$ordersFieldsMapping['order_country_id']]; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
echo "$iRow --> ".$ordersFieldsMapping['day_scheduled_for_YYMMDD'].", ".$rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']]."<br>"; |
355
|
|
|
|
356
|
|
|
$parametersArray = array(); |
357
|
|
|
|
358
|
|
|
$parametersArray["cached_lat"] = $cached_lat; |
359
|
|
|
$parametersArray["cached_lng"] = $cached_lng; |
360
|
|
|
|
361
|
|
|
|
362
|
|
|
foreach ($allOrderFields as $orderField) { |
363
|
|
|
if (isset($ordersFieldsMapping[$orderField])) { |
364
|
|
|
$parametersArray[$orderField] = $rows[$ordersFieldsMapping[$orderField]]; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$orderParameters = Order::fromArray($parametersArray); |
369
|
|
|
|
370
|
|
|
$order = new Order(); |
371
|
|
|
|
372
|
|
|
$orderResults = $order->addOrder($orderParameters); |
373
|
|
|
|
374
|
|
|
array_push($results['success'], "The order with order_id = ".strval($orderResults["order_id"])." added successfuly."); |
375
|
|
|
} |
376
|
|
|
else { |
377
|
|
|
array_push($results['fail'], "$iRow --> one of the parameters cached_lat, cached_lng, address_1 is not set"); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$iRow++; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.