1
|
|
|
<?php |
2
|
|
|
namespace Route4Me; |
3
|
|
|
|
4
|
|
|
use Route4Me\Common; |
5
|
|
|
|
6
|
|
|
class Order extends Common |
7
|
|
|
{ |
8
|
|
|
static public $apiUrl = '/api.v4/order.php'; |
9
|
|
|
static public $apiUrlRoute = '/api.v4/route.php'; |
10
|
|
|
static public $apiUrlOpt = '/api.v4/optimization_problem.php'; |
11
|
|
|
|
12
|
|
|
public $address_1; |
13
|
|
|
public $address_2; |
14
|
|
|
public $cached_lat; |
15
|
|
|
public $cached_lng; |
16
|
|
|
public $curbside_lat; |
17
|
|
|
public $curbside_lng; |
18
|
|
|
public $address_alias; |
19
|
|
|
public $address_city; |
20
|
|
|
public $EXT_FIELD_first_name; |
21
|
|
|
public $EXT_FIELD_last_name; |
22
|
|
|
public $EXT_FIELD_email; |
23
|
|
|
public $EXT_FIELD_phone; |
24
|
|
|
public $EXT_FIELD_custom_data; |
25
|
|
|
|
26
|
|
|
public $color; |
27
|
|
|
public $order_icon; |
28
|
|
|
public $local_time_window_start; |
29
|
|
|
public $local_time_window_end; |
30
|
|
|
public $local_time_window_start_2; |
31
|
|
|
public $local_time_window_end_2; |
32
|
|
|
public $service_time; |
33
|
|
|
|
34
|
|
|
public $day_scheduled_for_YYMMDD; |
35
|
|
|
|
36
|
|
|
public $route_id; |
37
|
|
|
public $redirect; |
38
|
|
|
public $optimization_problem_id; |
39
|
|
|
public $order_id; |
40
|
|
|
public $order_ids; |
41
|
|
|
|
42
|
|
|
public $day_added_YYMMDD; |
43
|
|
|
public $scheduled_for_YYMMDD; |
44
|
|
|
public $fields; |
45
|
|
|
public $offset; |
46
|
|
|
public $limit; |
47
|
|
|
public $query; |
48
|
|
|
|
49
|
|
|
public function __construct () { } |
50
|
|
|
|
51
|
|
|
public static function fromArray(array $params) { |
52
|
|
|
$order= new Order(); |
53
|
|
|
foreach($params as $key => $value) { |
54
|
|
|
if (property_exists($order, $key)) { |
55
|
|
|
$order->{$key} = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $order; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function addOrder($params) |
63
|
|
|
{ |
64
|
|
|
$response = Route4Me::makeRequst(array( |
65
|
|
|
'url' => self::$apiUrl, |
66
|
|
|
'method' => 'POST', |
67
|
|
|
'body' => array( |
68
|
|
|
'address_1' => isset($params->address_1) ? $params->address_1: null, |
69
|
|
|
'address_2' => isset($params->address_2) ? $params->address_2: null, |
70
|
|
|
'cached_lat' => isset($params->cached_lat) ? $params->cached_lat : null, |
71
|
|
|
'cached_lng' => isset($params->cached_lng) ? $params->cached_lng : null, |
72
|
|
|
'curbside_lat' => isset($params->curbside_lat) ? $params->curbside_lat : null, |
73
|
|
|
'curbside_lng' => isset($params->curbside_lng) ? $params->curbside_lng : null, |
74
|
|
|
'color' => isset($params->color) ? $params->color : null, |
75
|
|
|
'order_icon' => isset($params->order_icon) ? $params->order_icon : null, |
76
|
|
|
'day_scheduled_for_YYMMDD' => isset($params->day_scheduled_for_YYMMDD) ? $params->day_scheduled_for_YYMMDD : null, |
77
|
|
|
'address_alias' => isset($params->address_alias) ? $params->address_alias : null, |
78
|
|
|
'address_city' => isset($params->address_city) ? $params->address_city: null, |
79
|
|
|
'local_time_window_start' => isset($params->local_time_window_start) ? $params->local_time_window_start: null, |
80
|
|
|
'local_time_window_end' => isset($params->local_time_window_end) ? $params->local_time_window_end: null, |
81
|
|
|
'local_time_window_start_2' => isset($params->local_time_window_start_2) ? $params->local_time_window_start_2: null, |
82
|
|
|
'local_time_window_end_2' => isset($params->local_time_window_end_2) ? $params->local_time_window_end_2: null, |
83
|
|
|
'service_time' => isset($params->service_time) ? $params->service_time: null, |
84
|
|
|
'EXT_FIELD_first_name' => isset($params->EXT_FIELD_first_name) ? $params->EXT_FIELD_first_name: null, |
85
|
|
|
'EXT_FIELD_last_name' => isset($params->EXT_FIELD_last_name) ? $params->EXT_FIELD_last_name: null, |
86
|
|
|
'EXT_FIELD_email' => isset($params->EXT_FIELD_email) ? $params->EXT_FIELD_email: null, |
87
|
|
|
'EXT_FIELD_phone' => isset($params->EXT_FIELD_phone) ? $params->EXT_FIELD_phone: null, |
88
|
|
|
'EXT_FIELD_custom_data' => isset($params->EXT_FIELD_custom_data) ? $params->EXT_FIELD_custom_data: null, |
89
|
|
|
) |
90
|
|
|
)); |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public static function addOrder2Route($params,$body) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$response = Route4Me::makeRequst(array( |
98
|
|
|
'url' => self::$apiUrlRoute, |
99
|
|
|
'method' => 'PUT', |
100
|
|
|
'query' => array( |
101
|
|
|
'route_id' => isset($params->route_id) ? $params->route_id: null, |
102
|
|
|
'redirect' => isset($params->redirect) ? $params->redirect : null |
103
|
|
|
), |
104
|
|
|
'body' => (array)$body |
105
|
|
|
)); |
106
|
|
|
|
107
|
|
|
return $response; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public static function addOrder2Destination($params,$body) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$response = Route4Me::makeRequst(array( |
113
|
|
|
'url' => self::$apiUrlOpt, |
114
|
|
|
'method' => 'PUT', |
115
|
|
|
'query' => array( |
116
|
|
|
'optimization_problem_id' => isset($params->optimization_problem_id) ? $params->optimization_problem_id: null, |
117
|
|
|
'redirect' => isset($params->redirect) ? $params->redirect : null |
118
|
|
|
), |
119
|
|
|
'body' => (array)$body |
120
|
|
|
)); |
121
|
|
|
|
122
|
|
|
return $response; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public static function getOrder($params) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$response = Route4Me::makeRequst(array( |
128
|
|
|
'url' => self::$apiUrl, |
129
|
|
|
'method' => 'GET', |
130
|
|
|
'query' => array( |
131
|
|
|
'order_id' => isset($params->order_id) ? $params->order_id: null, |
132
|
|
|
) |
133
|
|
|
)); |
134
|
|
|
|
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function getOrders() |
139
|
|
|
{ |
140
|
|
|
$response = Route4Me::makeRequst(array( |
141
|
|
|
'url' => self::$apiUrl, |
142
|
|
|
'method' => 'GET' |
143
|
|
|
)); |
144
|
|
|
|
145
|
|
|
return $response; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public static function removeOrder($params) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$response = Route4Me::makeRequst(array( |
151
|
|
|
'url' => self::$apiUrl, |
152
|
|
|
'method' => 'DELETE', |
153
|
|
|
'body' => array( |
154
|
|
|
'order_ids' => isset($params->order_ids) ? $params->order_ids: null |
155
|
|
|
) |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
return $response; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public static function updateOrder($body) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$response = Route4Me::makeRequst(array( |
164
|
|
|
'url' => self::$apiUrl, |
165
|
|
|
'method' => 'PUT', |
166
|
|
|
'body' => (array)$body |
167
|
|
|
)); |
168
|
|
|
|
169
|
|
|
return $response; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public static function searchOrder($params) |
173
|
|
|
{ |
174
|
|
|
$response = Route4Me::makeRequst(array( |
175
|
|
|
'url' => self::$apiUrl, |
176
|
|
|
'method' => 'GET', |
177
|
|
|
'query' => array( |
178
|
|
|
'day_added_YYMMDD' => isset($params->day_added_YYMMDD) ? $params->day_added_YYMMDD: null, |
179
|
|
|
'scheduled_for_YYMMDD' => isset($params->scheduled_for_YYMMDD) ? $params->scheduled_for_YYMMDD: null, |
180
|
|
|
'fields' => isset($params->fields) ? $params->fields: null, |
181
|
|
|
'offset' => isset($params->offset) ? $params->offset: null, |
182
|
|
|
'limit' => isset($params->limit) ? $params->limit: null, |
183
|
|
|
'query' => isset($params->query) ? $params->query: null, |
184
|
|
|
) |
185
|
|
|
)); |
186
|
|
|
|
187
|
|
|
return $response; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
View Code Duplication |
public static function validateLatitude($lat) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
if (!is_numeric($lat)) return false; |
193
|
|
|
|
194
|
|
|
if ($lat>90 || $lat<-90) return false; |
195
|
|
|
|
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public static function validateLongitude($lng) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if (!is_numeric($lng)) return false; |
202
|
|
|
|
203
|
|
|
if ($lng>180 || $lng<-180) return false; |
204
|
|
|
|
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function addOrdersFromCsvFile($csvFileHandle, $ordersFieldsMapping) |
209
|
|
|
{ |
210
|
|
|
$max_line_length = 512; |
211
|
|
|
$delemietr=','; |
212
|
|
|
|
213
|
|
|
$results['fail']=array(); |
|
|
|
|
214
|
|
|
$results['success']=array(); |
215
|
|
|
|
216
|
|
|
$columns = fgetcsv($csvFileHandle, $max_line_length, $delemietr); |
217
|
|
|
|
218
|
|
|
if (!$columns) { |
|
|
|
|
219
|
|
|
array_push($results['fail'],'Empty CSV table'); |
220
|
|
|
return ($results); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$iRow=1; |
224
|
|
|
|
225
|
|
|
while (($rows = fgetcsv($csvFileHandle, $max_line_length, $delemietr)) !== false) { |
226
|
|
|
if ($rows[$ordersFieldsMapping['cached_lat']] && $rows[$ordersFieldsMapping['cached_lng']] && $rows[$ordersFieldsMapping['address_1']] && array(null) !== $rows) { |
227
|
|
|
|
228
|
|
|
$cached_lat=0.000; |
|
|
|
|
229
|
|
|
|
230
|
|
View Code Duplication |
if (!$this->validateLatitude($rows[$ordersFieldsMapping['cached_lat']])) { |
|
|
|
|
231
|
|
|
array_push($results['fail'],"$iRow --> Wrong cached_lat"); |
232
|
|
|
$iRow++; |
233
|
|
|
continue; |
234
|
|
|
} |
235
|
|
|
else $cached_lat=doubleval($rows[$ordersFieldsMapping['cached_lat']]); |
236
|
|
|
|
237
|
|
|
$cached_lng=0.000; |
|
|
|
|
238
|
|
|
|
239
|
|
View Code Duplication |
if (!$this->validateLongitude($rows[$ordersFieldsMapping['cached_lng']])) { |
|
|
|
|
240
|
|
|
array_push($results['fail'],"$iRow --> Wrong cached_lng"); |
241
|
|
|
$iRow++; |
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
else $cached_lng=doubleval($rows[$ordersFieldsMapping['cached_lng']]); |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
|
248
|
|
|
if (isset($ordersFieldsMapping['curbside_lat'])) { |
249
|
|
|
if (!$this->validateLatitude($rows[$ordersFieldsMapping['curbside_lat']])) { |
250
|
|
|
array_push($results['fail'],"$iRow --> Wrong curbside_lat"); |
251
|
|
|
$iRow++; |
252
|
|
|
continue; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if (isset($ordersFieldsMapping['curbside_lng'])) { |
257
|
|
|
if (!$this->validateLongitude($rows[$ordersFieldsMapping['curbside_lng']])) { |
258
|
|
|
array_push($results['fail'],"$iRow --> Wrong curbside_lng"); |
259
|
|
|
$iRow++; |
260
|
|
|
continue; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$address=$rows[$ordersFieldsMapping['address_1']]; |
265
|
|
|
|
266
|
|
|
if (isset($ordersFieldsMapping['order_city'])) $address.=', '.$rows[$ordersFieldsMapping['order_city']]; |
267
|
|
|
if (isset($ordersFieldsMapping['order_state_id'])) $address.=', '.$rows[$ordersFieldsMapping['order_state_id']]; |
268
|
|
|
if (isset($ordersFieldsMapping['order_zip_code'])) $address.=', '.$rows[$ordersFieldsMapping['order_zip_code']]; |
269
|
|
|
if (isset($ordersFieldsMapping['order_country_id'])) $address.=', '.$rows[$ordersFieldsMapping['order_country_id']]; |
270
|
|
|
|
271
|
|
|
echo "$iRow --> ".$ordersFieldsMapping['day_scheduled_for_YYMMDD'].", ".$rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']]."<br>"; |
272
|
|
|
|
273
|
|
|
$orderParameters = Order::fromArray(array( |
274
|
|
|
"cached_lat" => $cached_lat, |
275
|
|
|
"cached_lng" => $cached_lng, |
276
|
|
|
"curbside_lat" => isset($ordersFieldsMapping['curbside_lat']) ? $rows[$ordersFieldsMapping['curbside_lat']] : null, |
277
|
|
|
"curbside_lng" => isset($ordersFieldsMapping['curbside_lng']) ? $rows[$ordersFieldsMapping['curbside_lng']] : null, |
278
|
|
|
"color" => isset($ordersFieldsMapping['color']) ? $rows[$ordersFieldsMapping['color']] : null, |
279
|
|
|
"day_scheduled_for_YYMMDD" => isset($ordersFieldsMapping['day_scheduled_for_YYMMDD']) ? $rows[$ordersFieldsMapping['day_scheduled_for_YYMMDD']] : null, |
280
|
|
|
"address_alias" => isset($ordersFieldsMapping['address_alias']) ? $rows[$ordersFieldsMapping['address_alias']] : null, |
281
|
|
|
"address_1" => $address, |
282
|
|
|
"address_2" => isset($ordersFieldsMapping['address_2']) ? $rows[$ordersFieldsMapping['address_2']] : null, |
283
|
|
|
"local_time_window_start" => isset($ordersFieldsMapping['local_time_window_start']) ? $rows[$ordersFieldsMapping['local_time_window_start']] : null, |
284
|
|
|
"local_time_window_end" => isset($ordersFieldsMapping['local_time_window_end']) ? $rows[$ordersFieldsMapping['local_time_window_end']] : null, |
285
|
|
|
"local_time_window_start_2" => isset($ordersFieldsMapping['local_time_window_start_2']) ? $rows[$ordersFieldsMapping['local_time_window_start_2']] : null, |
286
|
|
|
"local_time_window_end_2" => isset($ordersFieldsMapping['local_time_window_end_2']) ? $rows[$ordersFieldsMapping['local_time_window_end_2']] : null, |
287
|
|
|
"service_time" => isset($ordersFieldsMapping['service_time']) ? $rows[$ordersFieldsMapping['service_time']] : null, |
288
|
|
|
"EXT_FIELD_first_name" => isset($ordersFieldsMapping['EXT_FIELD_first_name']) ? $rows[$ordersFieldsMapping['EXT_FIELD_first_name']] : null, |
289
|
|
|
"EXT_FIELD_last_name" => isset($ordersFieldsMapping['EXT_FIELD_last_name']) ? $rows[$ordersFieldsMapping['EXT_FIELD_last_name']] : null, |
290
|
|
|
"EXT_FIELD_email" => isset($ordersFieldsMapping['EXT_FIELD_email']) ? $rows[$ordersFieldsMapping['EXT_FIELD_email']] : null, |
291
|
|
|
"EXT_FIELD_phone" => isset($ordersFieldsMapping['EXT_FIELD_phone']) ? $rows[$ordersFieldsMapping['EXT_FIELD_phone']] : null, |
292
|
|
|
"EXT_FIELD_custom_data" => isset($ordersFieldsMapping['EXT_FIELD_custom_data']) ? $rows[$ordersFieldsMapping['EXT_FIELD_custom_data']] : null, |
293
|
|
|
"order_icon" => isset($ordersFieldsMapping['order_icon']) ? $rows[$ordersFieldsMapping['order_icon']] : null, |
294
|
|
|
)); |
295
|
|
|
|
296
|
|
|
$order = new Order(); |
297
|
|
|
|
298
|
|
|
$orderResults = $order->addOrder($orderParameters); |
299
|
|
|
|
300
|
|
|
array_push($results['success'],"The order with order_id = ".strval($orderResults["order_id"])." added successfuly."); |
301
|
|
|
} |
302
|
|
|
else { |
303
|
|
|
array_push($results['fail'],"$iRow --> one of the parameters cached_lat, cached_lng, address_1 is not set"); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$iRow++; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
?> |
|
|
|
|
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.