|
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 $addresses = array(); |
|
64
|
|
|
|
|
65
|
|
|
public static function fromArray(array $params) { |
|
66
|
|
|
$order = new Order(); |
|
67
|
|
|
foreach ($params as $key => $value) { |
|
68
|
|
|
if (property_exists($order, $key)) { |
|
69
|
|
|
$order->{$key} = $value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $order; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Order $params |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function addOrder($params) |
|
80
|
|
|
{ |
|
81
|
|
|
$excludeFields = array('route_id', 'redirect', 'optimization_problem_id', 'order_id', |
|
82
|
|
|
'order_ids', 'fields', 'offset', 'limit', 'query', 'created_timestamp'); |
|
83
|
|
|
|
|
84
|
|
|
$allBodyFields = Route4Me::getObjectProperties(new Order(), $excludeFields); |
|
85
|
|
|
|
|
86
|
|
|
$response = Route4Me::makeRequst(array( |
|
87
|
|
|
'url' => Endpoint::ORDER_V4, |
|
88
|
|
|
'method' => 'POST', |
|
89
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
|
90
|
|
|
)); |
|
91
|
|
|
|
|
92
|
|
|
return $response; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public static function addOrder2Route($params) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$allQueryFields = array('route_id', 'redirect'); |
|
98
|
|
|
$allBodyFields = array('addresses'); |
|
99
|
|
|
|
|
100
|
|
|
$response = Route4Me::makeRequst(array( |
|
101
|
|
|
'url' => Endpoint::ROUTE_V4, |
|
102
|
|
|
'method' => 'PUT', |
|
103
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
104
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
|
105
|
|
|
)); |
|
106
|
|
|
|
|
107
|
|
|
return $response; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
public static function addOrder2Optimization($params) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$allQueryFields = array('optimization_problem_id', 'redirect', 'device_type'); |
|
113
|
|
|
$allBodyFields = array('addresses'); |
|
114
|
|
|
|
|
115
|
|
|
$response = Route4Me::makeRequst(array( |
|
116
|
|
|
'url' => Endpoint::OPTIMIZATION_PROBLEM, |
|
117
|
|
|
'method' => 'PUT', |
|
118
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params), |
|
119
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
|
120
|
|
|
)); |
|
121
|
|
|
|
|
122
|
|
|
return $response; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
View Code Duplication |
public static function getOrder($params) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$allQueryFields = array('order_id', 'fields', 'day_added_YYMMDD', 'scheduled_for_YYMMDD', 'query', 'offset', 'limit'); |
|
128
|
|
|
|
|
129
|
|
|
$response = Route4Me::makeRequst(array( |
|
130
|
|
|
'url' => Endpoint::ORDER_V4, |
|
131
|
|
|
'method' => 'GET', |
|
132
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params) |
|
133
|
|
|
)); |
|
134
|
|
|
|
|
135
|
|
|
return $response; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public static function getOrders($params) |
|
139
|
|
|
{ |
|
140
|
|
|
$allQueryFields = array('offset', 'limit'); |
|
141
|
|
|
|
|
142
|
|
|
$response = Route4Me::makeRequst(array( |
|
143
|
|
|
'url' => Endpoint::ORDER_V4, |
|
144
|
|
|
'method' => 'GET', |
|
145
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params) |
|
146
|
|
|
)); |
|
147
|
|
|
|
|
148
|
|
|
return $response; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getRandomOrderId($offset, $limit) |
|
152
|
|
|
{ |
|
153
|
|
|
$randomOrder = $this->getRandomOrder($offset, $limit); |
|
154
|
|
|
|
|
155
|
|
|
if (is_null($randomOrder)) { |
|
156
|
|
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (!isset($randomOrder)) { |
|
160
|
|
|
return null; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $randomOrder['order_id']; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function getRandomOrder($offset, $limit) |
|
167
|
|
|
{ |
|
168
|
|
|
$params = array('offset' => $offset, 'limit' => $limit); |
|
169
|
|
|
|
|
170
|
|
|
$orders = self::getOrders($params); |
|
171
|
|
|
|
|
172
|
|
|
if (is_null($orders)) { |
|
173
|
|
|
return null; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (!isset($orders['results'])) { |
|
177
|
|
|
return null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$randomIndex = rand(0, sizeof($orders['results']) - 1); |
|
181
|
|
|
|
|
182
|
|
|
$order = $orders['results'][$randomIndex]; |
|
183
|
|
|
|
|
184
|
|
|
return $order; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public static function removeOrder($params) |
|
188
|
|
|
{ |
|
189
|
|
|
$allBodyFields = array('order_ids'); |
|
190
|
|
|
|
|
191
|
|
|
$response = Route4Me::makeRequst(array( |
|
192
|
|
|
'url' => Endpoint::ORDER_V4, |
|
193
|
|
|
'method' => 'DELETE', |
|
194
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
|
195
|
|
|
)); |
|
196
|
|
|
|
|
197
|
|
|
return $response; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
View Code Duplication |
public static function updateOrder($params) |
|
|
|
|
|
|
201
|
|
|
{ |
|
202
|
|
|
$excludeFields = array('route_id', 'redirect', 'optimization_problem_id', |
|
203
|
|
|
'order_ids', 'fields', 'offset', 'limit', 'query', 'created_timestamp'); |
|
204
|
|
|
|
|
205
|
|
|
$allBodyFields = Route4Me::getObjectProperties(new Order(), $excludeFields); |
|
206
|
|
|
|
|
207
|
|
|
$response = Route4Me::makeRequst(array( |
|
208
|
|
|
'url' => Endpoint::ORDER_V4, |
|
209
|
|
|
'method' => 'PUT', |
|
210
|
|
|
'body' => Route4Me::generateRequestParameters($allBodyFields, $params) |
|
211
|
|
|
)); |
|
212
|
|
|
|
|
213
|
|
|
return $response; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
View Code Duplication |
public static function searchOrder($params) |
|
|
|
|
|
|
217
|
|
|
{ |
|
218
|
|
|
$allQueryFields = array('fields', 'day_added_YYMMDD', 'scheduled_for_YYMMDD', 'query', 'offset', 'limit' ); |
|
219
|
|
|
|
|
220
|
|
|
$response = Route4Me::makeRequst(array( |
|
221
|
|
|
'url' => Endpoint::ORDER_V4, |
|
222
|
|
|
'method' => 'GET', |
|
223
|
|
|
'query' => Route4Me::generateRequestParameters($allQueryFields, $params) |
|
224
|
|
|
)); |
|
225
|
|
|
|
|
226
|
|
|
return $response; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public static function validateCoordinate($coord) |
|
230
|
|
|
{ |
|
231
|
|
|
$key = key($coord); |
|
232
|
|
|
|
|
233
|
|
|
if (!is_numeric($coord[$key])) { |
|
234
|
|
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
switch (variable) { |
|
238
|
|
|
case 'cached_lat': |
|
239
|
|
|
case 'curbside_lat': |
|
240
|
|
|
if ($lat>90 || $lat<-90) { |
|
|
|
|
|
|
241
|
|
|
return false; |
|
242
|
|
|
} |
|
243
|
|
|
break; |
|
244
|
|
|
case 'cached_lng': |
|
245
|
|
|
case 'curbside_lng': |
|
246
|
|
|
if ($lng>180 || $lng<-180) { |
|
|
|
|
|
|
247
|
|
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
break; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function addOrdersFromCsvFile($csvFileHandle, $ordersFieldsMapping) |
|
256
|
|
|
{ |
|
257
|
|
|
$max_line_length = 512; |
|
258
|
|
|
$delemietr = ','; |
|
259
|
|
|
|
|
260
|
|
|
$results = array(); |
|
261
|
|
|
$results['fail'] = array(); |
|
262
|
|
|
$results['success'] = array(); |
|
263
|
|
|
|
|
264
|
|
|
$columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
|
265
|
|
|
|
|
266
|
|
|
$excludeFields = array('route_id', 'redirect', 'optimization_problem_id', 'order_id', |
|
267
|
|
|
'order_ids', 'fields', 'offset', 'limit', 'query', 'created_timestamp'); |
|
268
|
|
|
|
|
269
|
|
|
$allOrderFields = Route4Me::getObjectProperties(new Order(), $excludeFields); |
|
270
|
|
|
|
|
271
|
|
|
if (!empty($columns)) { |
|
272
|
|
|
array_push($results['fail'],'Empty CSV table'); |
|
273
|
|
|
return ($results); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$iRow=1; |
|
277
|
|
|
|
|
278
|
|
|
while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr))!==false) { |
|
279
|
|
|
if ($rows[$ordersFieldsMapping['cached_lat']] && $rows[$ordersFieldsMapping['cached_lng']] && $rows[$ordersFieldsMapping['address_1']] && array(null)!==$rows) { |
|
280
|
|
|
|
|
281
|
|
|
$cached_lat = 0.000; |
|
282
|
|
|
$cached_lng = 0.000; |
|
283
|
|
|
|
|
284
|
|
|
foreach (array('cached_lat', 'cached_lng', 'curbside_lat', 'curbside_lng') as $coord) { |
|
285
|
|
|
if (!$this->validateCoordinate(array($coord => $rows[$ordersFieldsMapping[$coord]]))) { |
|
286
|
|
|
array_push($results['fail'], "$iRow --> Wrong "+$coord); |
|
287
|
|
|
$iRow++; |
|
288
|
|
|
continue; |
|
289
|
|
|
} else { |
|
290
|
|
|
switch ($coord) { |
|
291
|
|
|
case 'cached_lat': |
|
292
|
|
|
$cached_lat = doubleval($rows[$ordersFieldsMapping[$coord]]); |
|
293
|
|
|
break; |
|
294
|
|
|
case 'cached_lng': |
|
295
|
|
|
$cached_lng = doubleval($rows[$ordersFieldsMapping[$coord]]); |
|
296
|
|
|
break; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$address = $rows[$ordersFieldsMapping['address_1']]; |
|
302
|
|
|
|
|
303
|
|
|
foreach (array('order_city', 'order_state_id', 'order_zip_code', 'order_country_id') as $addressPart) { |
|
304
|
|
|
if (isset($ordersFieldsMapping[$addressPart])) { |
|
305
|
|
|
$address .= ', '.$rows[$ordersFieldsMapping[$addressPart]]; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
echo "$iRow --> ".$ordersFieldsMapping['day_scheduled_for_YYMMDD'].", ".$rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']]."<br>"; |
|
310
|
|
|
|
|
311
|
|
|
$parametersArray = array(); |
|
312
|
|
|
|
|
313
|
|
|
$parametersArray["cached_lat"] = $cached_lat; |
|
314
|
|
|
$parametersArray["cached_lng"] = $cached_lng; |
|
315
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
foreach ($allOrderFields as $orderField) { |
|
318
|
|
|
if (isset($ordersFieldsMapping[$orderField])) { |
|
319
|
|
|
$parametersArray[$orderField] = $rows[$ordersFieldsMapping[$orderField]]; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
$orderParameters = Order::fromArray($parametersArray); |
|
324
|
|
|
|
|
325
|
|
|
$order = new Order(); |
|
326
|
|
|
|
|
327
|
|
|
$orderResults = $order->addOrder($orderParameters); |
|
328
|
|
|
|
|
329
|
|
|
array_push($results['success'], "The order with order_id = ".strval($orderResults["order_id"])." added successfuly."); |
|
330
|
|
|
} |
|
331
|
|
|
else { |
|
332
|
|
|
array_push($results['fail'], "$iRow --> one of the parameters cached_lat, cached_lng, address_1 is not set"); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$iRow++; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
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.