1 <?php
2
3 namespace RetailCrm;
4
5 use RetailCrm\Http\Client;
6 use RetailCrm\Response\ApiResponse;
7
8 9 10
11 class ApiClient
12 {
13 const VERSION = 'v3';
14
15 protected $client;
16
17 18 19 20 21 22 23
24 public function __construct($url, $apiKey)
25 {
26 if ('/' != substr($url, strlen($url) - 1, 1)) {
27 $url .= '/';
28 }
29
30 $url = $url . 'api/' . self::VERSION;
31
32 $this->client = new Client($url, array('apiKey' => $apiKey));
33 }
34
35 36 37 38 39 40
41 public function ordersCreate(array $order)
42 {
43 if (!sizeof($order)) {
44 throw new \InvalidArgumentException('Parameter `order` must contains a data');
45 }
46
47 return $this->client->makeRequest("/orders/create", Client::METHOD_POST, array(
48 'order' => json_encode($order)
49 ));
50 }
51
52 53 54 55 56 57
58 public function ordersEdit(array $order, $by = 'externalId')
59 {
60 if (!sizeof($order)) {
61 throw new \InvalidArgumentException('Parameter `order` must contains a data');
62 }
63
64 $this->checkIdParameter($by);
65
66 if (!isset($order[$by])) {
67 throw new \InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by));
68 }
69
70 return $this->client->makeRequest("/orders/" . $order[$by] . "/edit", Client::METHOD_POST, array(
71 'order' => json_encode($order),
72 'by' => $by,
73 ));
74 }
75
76 77 78 79 80 81
82 public function ordersUpload(array $orders)
83 {
84 if (!sizeof($orders)) {
85 throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders');
86 }
87
88 return $this->client->makeRequest("/orders/upload", Client::METHOD_POST, array(
89 'orders' => json_encode($orders),
90 ));
91 }
92
93 94 95 96 97 98 99
100 public function ordersGet($id, $by = 'externalId')
101 {
102 $this->checkIdParameter($by);
103
104 return $this->client->makeRequest("/orders/$id", Client::METHOD_GET, array('by' => $by));
105 }
106
107 108 109 110 111 112 113 114 115 116
117 public function ordersHistory(
118 \DateTime $startDate = null,
119 \DateTime $endDate = null,
120 $limit = 100,
121 $offset = 0,
122 $skipMyChanges = true
123 ) {
124 $parameters = array();
125
126 if ($startDate) {
127 $parameters['startDate'] = $startDate->format('Y-m-d H:i:s');
128 }
129 if ($endDate) {
130 $parameters['endDate'] = $endDate->format('Y-m-d H:i:s');
131 }
132 if ($limit) {
133 $parameters['limit'] = (int) $limit;
134 }
135 if ($offset) {
136 $parameters['offset'] = (int) $offset;
137 }
138 if ($skipMyChanges) {
139 $parameters['skipMyChanges'] = (bool) $skipMyChanges;
140 }
141
142 return $this->client->makeRequest('/orders/history', Client::METHOD_GET, $parameters);
143 }
144
145 146 147 148 149 150 151 152
153 public function ordersList(array $filter = array(), $page = null, $limit = null)
154 {
155 $parameters = array();
156
157 if (sizeof($filter)) {
158 $parameters['filter'] = $filter;
159 }
160 if (null !== $page) {
161 $parameters['page'] = (int) $page;
162 }
163 if (null !== $limit) {
164 $parameters['limit'] = (int) $limit;
165 }
166
167 return $this->client->makeRequest('/orders', Client::METHOD_GET, $parameters);
168 }
169
170 171 172 173 174 175
176 public function ordersFixExternalIds(array $ids)
177 {
178 if (!sizeof($ids)) {
179 throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
180 }
181
182 return $this->client->makeRequest("/orders/fix-external-ids", Client::METHOD_POST, array(
183 'orders' => json_encode($ids),
184 ));
185 }
186
187 188 189 190 191 192
193 public function customersCreate(array $customer)
194 {
195 if (!sizeof($customer)) {
196 throw new \InvalidArgumentException('Parameter `customer` must contains a data');
197 }
198
199 return $this->client->makeRequest("/customers/create", Client::METHOD_POST, array(
200 'customer' => json_encode($customer)
201 ));
202 }
203
204 205 206 207 208 209
210 public function customersEdit(array $customer, $by = 'externalId')
211 {
212 if (!sizeof($customer)) {
213 throw new \InvalidArgumentException('Parameter `customer` must contains a data');
214 }
215
216 $this->checkIdParameter($by);
217
218 if (!isset($customer[$by])) {
219 throw new \InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by));
220 }
221
222 return $this->client->makeRequest("/customers/" . $customer[$by] . "/edit", Client::METHOD_POST, array(
223 'customer' => json_encode($customer),
224 'by' => $by,
225 ));
226 }
227
228 229 230 231 232 233
234 public function customersUpload(array $customers)
235 {
236 if (!sizeof($customers)) {
237 throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers');
238 }
239
240 return $this->client->makeRequest("/customers/upload", Client::METHOD_POST, array(
241 'customers' => json_encode($customers),
242 ));
243 }
244
245 246 247 248 249 250 251
252 public function customersGet($id, $by = 'externalId')
253 {
254 $this->checkIdParameter($by);
255
256 return $this->client->makeRequest("/customers/$id", Client::METHOD_GET, array('by' => $by));
257 }
258
259 260 261 262 263 264 265 266
267 public function customersList(array $filter = array(), $page = null, $limit = null)
268 {
269 $parameters = array();
270
271 if (sizeof($filter)) {
272 $parameters['filter'] = $filter;
273 }
274 if (null !== $page) {
275 $parameters['page'] = (int) $page;
276 }
277 if (null !== $limit) {
278 $parameters['limit'] = (int) $limit;
279 }
280
281 return $this->client->makeRequest('/customers', Client::METHOD_GET, $parameters);
282 }
283
284 285 286 287 288 289
290 public function customersFixExternalIds(array $ids)
291 {
292 if (!sizeof($ids)) {
293 throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
294 }
295
296 return $this->client->makeRequest("/customers/fix-external-ids", Client::METHOD_POST, array(
297 'customers' => json_encode($ids),
298 ));
299 }
300
301 302 303 304 305
306 public function deliveryServicesList()
307 {
308 return $this->client->makeRequest('/reference/delivery-services', Client::METHOD_GET);
309 }
310
311 312 313 314 315
316 public function deliveryTypesList()
317 {
318 return $this->client->makeRequest('/reference/delivery-types', Client::METHOD_GET);
319 }
320
321 322 323 324 325
326 public function orderMethodsList()
327 {
328 return $this->client->makeRequest('/reference/order-methods', Client::METHOD_GET);
329 }
330
331 332 333 334 335
336 public function orderTypesList()
337 {
338 return $this->client->makeRequest('/reference/order-types', Client::METHOD_GET);
339 }
340
341 342 343 344 345
346 public function paymentStatusesList()
347 {
348 return $this->client->makeRequest('/reference/payment-statuses', Client::METHOD_GET);
349 }
350
351 352 353 354 355
356 public function paymentTypesList()
357 {
358 return $this->client->makeRequest('/reference/payment-types', Client::METHOD_GET);
359 }
360
361 362 363 364 365
366 public function productStatusesList()
367 {
368 return $this->client->makeRequest('/reference/product-statuses', Client::METHOD_GET);
369 }
370
371 372 373 374 375
376 public function statusGroupsList()
377 {
378 return $this->client->makeRequest('/reference/status-groups', Client::METHOD_GET);
379 }
380
381 382 383 384 385
386 public function statusesList()
387 {
388 return $this->client->makeRequest('/reference/statuses', Client::METHOD_GET);
389 }
390
391 392 393 394 395 396
397 public function deliveryServicesEdit(array $data)
398 {
399 if (!isset($data['code'])) {
400 throw new \InvalidArgumentException('Data must contain "code" parameter.');
401 }
402
403 return $this->client->makeRequest(
404 '/reference/delivery-services/' . $data['code'] . '/edit',
405 Client::METHOD_POST,
406 array(
407 'deliveryService' => json_encode($data)
408 )
409 );
410 }
411
412 413 414 415 416 417
418 public function deliveryTypesEdit(array $data)
419 {
420 if (!isset($data['code'])) {
421 throw new \InvalidArgumentException('Data must contain "code" parameter.');
422 }
423
424 return $this->client->makeRequest(
425 '/reference/delivery-types/' . $data['code'] . '/edit',
426 Client::METHOD_POST,
427 array(
428 'deliveryType' => json_encode($data)
429 )
430 );
431 }
432
433 434 435 436 437 438
439 public function orderMethodsEdit(array $data)
440 {
441 if (!isset($data['code'])) {
442 throw new \InvalidArgumentException('Data must contain "code" parameter.');
443 }
444
445 return $this->client->makeRequest(
446 '/reference/order-methods/' . $data['code'] . '/edit',
447 Client::METHOD_POST,
448 array(
449 'orderMethod' => json_encode($data)
450 )
451 );
452 }
453
454 455 456 457 458 459
460 public function orderTypesEdit(array $data)
461 {
462 if (!isset($data['code'])) {
463 throw new \InvalidArgumentException('Data must contain "code" parameter.');
464 }
465
466 return $this->client->makeRequest(
467 '/reference/order-types/' . $data['code'] . '/edit',
468 Client::METHOD_POST,
469 array(
470 'orderType' => json_encode($data)
471 )
472 );
473 }
474
475 476 477 478 479 480
481 public function paymentStatusesEdit(array $data)
482 {
483 if (!isset($data['code'])) {
484 throw new \InvalidArgumentException('Data must contain "code" parameter.');
485 }
486
487 return $this->client->makeRequest(
488 '/reference/payment-statuses/' . $data['code'] . '/edit',
489 Client::METHOD_POST,
490 array(
491 'paymentStatus' => json_encode($data)
492 )
493 );
494 }
495
496 497 498 499 500 501
502 public function paymentTypesEdit(array $data)
503 {
504 if (!isset($data['code'])) {
505 throw new \InvalidArgumentException('Data must contain "code" parameter.');
506 }
507
508 return $this->client->makeRequest(
509 '/reference/payment-types/' . $data['code'] . '/edit',
510 Client::METHOD_POST,
511 array(
512 'paymentType' => json_encode($data)
513 )
514 );
515 }
516
517 518 519 520 521 522
523 public function productStatusesEdit(array $data)
524 {
525 if (!isset($data['code'])) {
526 throw new \InvalidArgumentException('Data must contain "code" parameter.');
527 }
528
529 return $this->client->makeRequest(
530 '/reference/product-statuses/' . $data['code'] . '/edit',
531 Client::METHOD_POST,
532 array(
533 'productStatus' => json_encode($data)
534 )
535 );
536 }
537
538 539 540 541 542 543
544 public function statusesEdit(array $data)
545 {
546 if (!isset($data['code'])) {
547 throw new \InvalidArgumentException('Data must contain "code" parameter.');
548 }
549
550 return $this->client->makeRequest(
551 '/reference/statuses/' . $data['code'] . '/edit',
552 Client::METHOD_POST,
553 array(
554 'status' => json_encode($data)
555 )
556 );
557 }
558
559 560 561 562 563
564 public function statisticUpdate()
565 {
566 return $this->client->makeRequest('/statistic/update', Client::METHOD_GET);
567 }
568
569 570 571 572 573 574
575 protected function checkIdParameter($by)
576 {
577 $allowedForBy = array('externalId', 'id');
578 if (!in_array($by, $allowedForBy)) {
579 throw new \InvalidArgumentException(sprintf(
580 'Value "%s" for parameter "by" is not valid. Allowed values are %s.',
581 $by,
582 implode(', ', $allowedForBy)
583 ));
584 }
585
586 return true;
587 }
588 }