Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
order.php
1<?php
2
3
5
6
15use Bitrix\Sale;
19
20class Order extends Controller
21{
22 public function getPrimaryAutoWiredParameter()
23 {
24 return new ExactParameter(
25 Sale\Order::class,
26 'order',
27 function($className, $id) {
28 $id = (int)$id;
29 if ($id > 0)
30 {
31 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
32
34 $orderClass = $registry->getOrderClassName();
35
37 $order = $orderClass::load($id);
38 if ($order instanceof Sale\OrderBase)
39 {
40 return $order;
41 }
42 }
43
44 $this->addError(new Error('order is not exists', 200540400001));
45 return null;
46 }
47 );
48 }
49
50 //region Actions
51 public function getFieldsAction()
52 {
53 $entity = new \Bitrix\Sale\Rest\Entity\Order();
54 return ['ORDER'=>$entity->prepareFieldInfos(
55 $entity->getFields()
56 )];
57 }
58
59 public function getAction(\Bitrix\Sale\Order $order)
60 {
61 //TODO: return $order->toArray();
62 return $this->toArray($order);
63 }
64
65 public function tryModifyAction(array $fields)
66 {
67 $r = $this->modify($fields);
68
69 if($r->isSuccess())
70 {
72 $order = $r->getData()['ORDER'];
73
74 $result = $this->toArray($order);
75
76 if(is_array($result['ORDER']['PAYMENTS']))
77 {
78 foreach ($result['ORDER']['PAYMENTS'] as $ix=>&$fields)
79 {
80 $paySystems = Manager::getListWithRestrictions(
81 $order
82 ->getPaymentCollection()
83 ->getItemByIndex($ix)
84 );
85
86 foreach ($paySystems as $paySystem)
87 {
88 if((int)$paySystem['PAY_SYSTEM_ID']>0) //Without Inner
89 {
90 $fields['LIST_PAY_SYSTEM_WITH_RESTRICTIONS'][]=[
91 'ID'=>$paySystem['PAY_SYSTEM_ID'],
92 ];
93 }
94 }
95 }
96 }
97
98 if(is_array($result['ORDER']['SHIPMENTS']))
99 {
100 foreach ($result['ORDER']['SHIPMENTS'] as $ix=>&$fields)
101 {
102 $services = \Bitrix\Sale\Delivery\Services\Manager::getRestrictedObjectsList(
103 $order
104 ->getShipmentCollection()
105 ->getItemByIndex($ix)
106 );
107
108 foreach ($services as $service)
109 {
110 $fields['LIST_DELIIVERY_SERVICES_RESTRICTIONS'][]=[
111 'ID'=>$service->getId(),
112 ];
113 }
114 }
115 }
116 return $result;
117 }
118 else
119 {
120 $this->addErrors($r->getErrors());
121 return null;
122 }
123 }
124
125 public function modifyAction(array $fields)
126 {
127 $r = $this->modify($fields);
128
129 if($r->isSuccess())
130 {
132 $order = $r->getData()['ORDER'];
133
134 $r = $order->save();
135 if(!$r->isSuccess())
136 {
137 $this->addErrors($r->getErrors());
138 return null;
139 }
140 elseif($r->hasWarnings())
141 {
142 $this->addErrors($r->getWarnings());
143 return null;
144 }
145
146 //TODO: return $order->toArray();
147 return $this->toArray($order);
148 }
149 elseif($r->hasWarnings())
150 {
151 $this->addErrors($r->getWarnings());
152 return null;
153 }
154 else
155 {
156 $this->addErrors($r->getErrors());
157 return null;
158 }
159 }
160
161 public function tryAddAction(array $fields)
162 {
163 $r = $this->add($fields);
164
165 if($r->isSuccess())
166 {
167 $order = $r->getData()['ORDER'];
168 return $this->toArray($order);
169 }
170 else
171 {
172 $this->addErrors($r->getErrors());
173 return null;
174 }
175 }
176
177 public function addAction(array $fields)
178 {
179 $result = null;
180
181 $r = $this->add($fields);
182
183 if($r->isSuccess())
184 {
186 $order = $r->getData()['ORDER'];
187
188 $r = $order->save();
189 if(!$r->isSuccess())
190 {
191 $this->addErrors($r->getErrors());
192 return null;
193 }
194
195 //TODO: return $order->toArray();
196 return $this->toArray($order);
197 }
198 else
199 {
200 $this->addErrors($r->getErrors());
201 return null;
202 }
203 }
204
205 public function tryUpdateAction(\Bitrix\Sale\Order $order, array $fields)
206 {
207 $r = $this->update($order, $fields);
208
209 if($r->isSuccess())
210 {
211 $order = $r->getData()['ORDER'];
212 return $this->toArray($order);
213 }
214 else
215 {
216 $this->addErrors($r->getErrors());
217 return null;
218 }
219 }
220
221 public function updateAction(\Bitrix\Sale\Order $order, array $fields)
222 {
223 $result = null;
224 $r = $this->update($order, $fields);
225
226 if($r->isSuccess())
227 {
229 $order = $r->getData()['ORDER'];
230
231 $r = $order->save();
232 if(!$r->isSuccess())
233 {
234 $this->addErrors($r->getErrors());
235 return null;
236 }
237
238 //TODO: return $order->toArray();
239 return $this->toArray($order);
240 }
241 else
242 {
243 $this->addErrors($r->getErrors());
244 return null;
245 }
246 }
247
248 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
249 {
250 $select = empty($select) ? ['*'] : $select;
251 $order = empty($order) ? ['ID' => 'ASC'] : $order;
252 $runtime = [
253 new \Bitrix\Main\Entity\ReferenceField(
254 'PERSON_TYPE',
255 '\Bitrix\Sale\Internals\PersonType',
256 array('=this.PERSON_TYPE_ID' => 'ref.ID')
257 ),
258 new \Bitrix\Main\Entity\ReferenceField(
259 'STATUS_TABLE',
260 '\Bitrix\Sale\Internals\StatusTable',
261 array('=this.STATUS_ID' => 'ref.ID')
262 )
263 ];
264
266
268 $orderClass = $registry->getOrderClassName();
269
270 $orders = $orderClass::getList(
271 [
272 'select'=>$select,
273 'filter'=>$filter,
274 'order'=>$order,
275 'offset'=>$pageNavigation->getOffset(),
276 'limit'=>$pageNavigation->getLimit(),
277 'runtime'=>$runtime
278 ]
279 )->fetchAll();
280
281 return new Page('ORDERS', $orders, function() use ($select, $filter, $runtime)
282 {
284
286 $orderClass = $registry->getOrderClassName();
287
288 return count(
289 $orderClass::getList(['select'=>$select, 'filter'=>$filter, 'runtime'=>$runtime])->fetchAll()
290 );
291 });
292 }
293
294 public function deleteAction(\Bitrix\Sale\Order $order)
295 {
296 $r = $order->delete($order->getId());
297 if($r->isSuccess())
298 $r = $order->save();
299
300 if(!$r->isSuccess())
301 {
302 $this->addErrors($r->getErrors());
303 return null;
304 }
305
306 if ($r->hasWarnings())
307 {
308 $this->addErrors($r->getWarnings());
309 return null;
310 }
311
312 return true;
313 }
314
315 public function getDeliveryIdListAction(\Bitrix\Sale\Order $order)
316 {
317 return ['DELIVERY_ID_LIST'=>$order->getDeliveryIdList()];
318 }
319
320 public function getPaymentsAction(\Bitrix\Sale\Order $order)
321 {
322 return ['PAYMENTS'=>$this->toArray($order)['ORDER']['PAYMENTS']];
323 }
324
325 public function getPaySystemIdListAction(\Bitrix\Sale\Order $order)
326 {
327 return ['PAY_SYSTEM_ID_LIST'=>$order->getPaySystemIdList()];
328 }
329
330 public function getPrintedChecksAction(\Bitrix\Sale\Order $order)
331 {
332 return ['PRINTED_CHECKS'=>$order->getPrintedChecks()];
333 }
334
335 public function getShipmentsAction(\Bitrix\Sale\Order $order)
336 {
337 return ['SHIPMENTS'=>$this->toArray($order)['ORDER']['SHIPMENTS']];
338 }
339
340 public function getBasketAction(\Bitrix\Sale\Order $order)
341 {
342 return ['BASKET_ITEMS'=>$this->toArray($order)['ORDER']['BASKET_ITEMS']];
343 }
344
345 public function getCurrencyAction(\Bitrix\Sale\Order $order)
346 {
347 return $order->getField('CURRENCY');
348 }
349
350 public function getDateInsertAction(\Bitrix\Sale\Order $order)
351 {
352 return $order->getField('DATE_INSERT');
353 }
354
355 public function getApplyDiscountAction(\Bitrix\Sale\Order $order)
356 {
357 //TODO: return $order->getDiscount()->toArray();
358 return $this->toArray($order)['DISCOUNTS'];
359 }
360
361 public function getPersonTypeIdAction(\Bitrix\Sale\Order $order)
362 {
363 return $order->getPersonTypeId();
364 }
365
366 public function getPriceAction(\Bitrix\Sale\Order $order)
367 {
368 return $order->getPrice();
369 }
370
371 public function getPropertyValuesAction(\Bitrix\Sale\Order $order)
372 {
373 return ['PROPERTY_VALUES'=>$this->toArray($order)['ORDER']['PROPERTY_VALUES']];
374 }
375
376 public function getSiteIdAction(\Bitrix\Sale\Order $order)
377 {
378 return $order->getSiteId();
379 }
380
381 public function getSumPaidAction(\Bitrix\Sale\Order $order)
382 {
383 return $order->getSumPaid();
384 }
385/*
386 public function getTaxListAction(\Bitrix\Sale\Order $order)
387 {
388 //TODO: return $order->getTax()->toArray();
389 return $this->toArray($order)['TAX'];
390 }
391
392 public function getTaxLocationAction(\Bitrix\Sale\Order $order)
393 {
394 return $order->getTaxLocation();
395 }
396*/
397 public function getTaxPriceAction(\Bitrix\Sale\Order $order)
398 {
399 return $order->getTaxPrice();
400 }
401
402 public function getTaxValueAction(\Bitrix\Sale\Order $order)
403 {
404 return $order->getTaxValue();
405 }
406
407 public function getUserIdAction(\Bitrix\Sale\Order $order)
408 {
409 return $order->getUserId();
410 }
411
412 public function getVatRateAction(\Bitrix\Sale\Order $order)
413 {
414 return $order->getVatRate();
415 }
416
417 public function getVatSumAction(\Bitrix\Sale\Order $order)
418 {
419 return $order->getVatSum();
420 }
421
422 public function isCanceledAction(\Bitrix\Sale\Order $order)
423 {
424 return $order->isCanceled()?'Y':'N';
425 }
426
427 public function isExternalAction(\Bitrix\Sale\Order $order)
428 {
429 return $order->isExternal()?'Y':'N';
430 }
431
432 public function isMarkedAction(\Bitrix\Sale\Order $order)
433 {
434 return $order->isMarked()?'Y':'N';
435 }
436
437 public function isPaidAction(\Bitrix\Sale\Order $order)
438 {
439 return $order->isPaid()?'Y':'N';
440 }
441
442 public function isShippedAction(\Bitrix\Sale\Order $order)
443 {
444 return $order->isShipped()?'Y':'N';
445 }
446
447 public function isUsedVatAction(\Bitrix\Sale\Order $order)
448 {
449 return $order->isUsedVat()?'Y':'N';
450 }
451
452 //public function applyDiscountAction(\Bitrix\Sale\Order $order, array $data)
453 //public function refreshAction(\Bitrix\Sale\Order $order, array $data)
454
455 //endregion
456
457 //region admin Actions
458 //public function cancelOrderAction(\Bitrix\Sale\Order $order, array $data)
459 //public function saveCommentsAction(\Bitrix\Sale\Order $order, array $data)
460 //public function saveStatusAction(\Bitrix\Sale\Order $order, array $data)
461 //public function changeResponsibleUserAction(\Bitrix\Sale\Order $order, array $data)
462 //public function updatePaymentStatusAction()
463 //public function updateShipmentStatusAction()
464 //public function changeDeliveryServiceAction()
465 //public function checkProductBarcodeAction()
466 //public function deleteCouponAction(\Bitrix\Sale\Order $order, array $data)
467 //public function addCouponsAction(\Bitrix\Sale\Order $order, array $data)
468 //public function getProductIdByBarcodeAction()
469 //public function refreshOrderDataAction(\Bitrix\Sale\Order $order, array $data)
470 //endregion
471
472 protected function modify(array $fields)
473 {
474 $r = new Result();
475
476 $builder = $this->getBuilder();
477 try{
478 $builder->build($fields);
479 $errorsContainer = $builder->getErrorsContainer();
480 }
481 catch(BuildingException $e)
482 {
483 if($builder->getErrorsContainer()->getErrorCollection()->count()<=0)
484 {
485 $builder->getErrorsContainer()->addError(new Error('unknow error', 200550000001));
486 }
487 $errorsContainer = $builder->getErrorsContainer();
488 }
489
490 if($errorsContainer->getErrorCollection()->count()>0)
491 $r->addErrors($errorsContainer->getErrors());
492 else
493 $r->setData(['ORDER'=>$builder->getOrder()]);
494
495 return $r;
496 }
497
498 protected function add(array $fields)
499 {
500 $r = new Result();
501
502 $fields = ['ORDER'=>$fields];
503
504 if($fields['ORDER']['ID'])
505 unset($fields['ORDER']['ID']);
506
507 $orderBuilder = $this->getBuilder();
508 $order = $orderBuilder->buildEntityOrder($fields);
509
510 if($orderBuilder->getErrorsContainer()->getErrorCollection()->count()>0)
511 $r->addErrors($orderBuilder->getErrorsContainer()->getErrors());
512 else
513 $r->setData(['ORDER'=>$order]);
514
515 return $r;
516 }
517
518 protected function update(\Bitrix\Sale\Order $order, array $fields)
519 {
520 $r = new Result();
521 $data=[];
522
523 $data['ORDER'] = $fields;
524 $data['ORDER']['ID'] = $order->getId();
525 $data['ORDER']['PERSON_TYPE_ID'] = $order->getPersonTypeId();
526
527 $orderBuilder = $this->getBuilder();
528 $order = $orderBuilder->buildEntityOrder($data);
529
530 if($orderBuilder->getErrorsContainer()->getErrorCollection()->count()>0)
531 $r->addErrors($orderBuilder->getErrorsContainer()->getErrors());
532 else
533 $r->setData(['ORDER'=>$order]);
534
535 return $r;
536 }
537
538 protected function get(\Bitrix\Sale\Order $order, array $fields=[])
539 {
540 return $this->toArray($order, $fields);
541 }
542
543 static public function prepareFields(array $fields)
544 {
545 $fields = isset($fields['ORDER'])? $fields['ORDER']:[];
546
547 if(isset($fields['BASKET_ITEMS']))
548 unset($fields['BASKET_ITEMS']);
549 if(isset($fields['PROPERTY_VALUES']))
550 unset($fields['PROPERTY_VALUES']);
551 if(isset($fields['PAYMENTS']))
552 unset($fields['PAYMENTS']);
553 if(isset($fields['SHIPMENTS']))
554 unset($fields['SHIPMENTS']);
555 if(isset($fields['TRADE_BINDINGS']))
556 unset($fields['TRADE_BINDINGS']);
557 if(isset($fields['CLIENTS']))
558 unset($fields['CLIENTS']);
559 if(isset($fields['REQUISITE_LINKS']))
560 unset($fields['REQUISITE_LINKS']);
561
562 return $fields;
563 }
564
565 private static function setFlagActionImport()
566 {
567 //TODO: huck \Bitrix\Sale\Rest\RestManager::processEvent(). - import ..
568 $instance = \Bitrix\Sale\Rest\Synchronization\Manager::getInstance();
569 $instance->setAction(\Bitrix\Sale\Rest\Synchronization\Manager::ACTION_IMPORT);
570 }
571
572 public function importDeleteAction(\Bitrix\Sale\Order $order)
573 {
574 self::setFlagActionImport();
575
576 return $this->deleteAction($order);
577 }
578
579 public function resolveExternalIdToInternalId(array $fields)
580 {
581 LoggerDiag::addMessage('ORDER_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID_SOURCE_FIELDS', var_export($fields, true));
582
583 $result = new Result();
584
585 $instance = \Bitrix\Sale\Rest\Synchronization\Manager::getInstance();
586
587 $ixInternal = [];
588 $ixExternal = [];
589 $internalOrderId = -1;
590
591 $externalId = $fields['ORDER']['XML_ID'];
592 $ixExternal['ORDER']['MAP'][$externalId] = $fields['ORDER']['ID'];
593
594 unset($fields['ORDER']['ID']);
595 $internalId = $this->getInternalId($fields['ORDER']['XML_ID'], Registry::ENTITY_ORDER);
596 if(intval($internalId)>0)
597 {
598 $fields['ORDER']['ID'] = $internalId;
599 $ixInternal['ORDER']['MAP'][$externalId] = $internalId;
600 $internalOrderId = $fields['ORDER']['ID'];
601 }
602
603 $internalOrderStatusId = $this->getInternalId($fields['ORDER']['STATUS_XML_ID'], Registry::ENTITY_ORDER_STATUS);
604 $fields['ORDER']['STATUS_ID' ] = $internalOrderStatusId<>''? $internalOrderStatusId:$instance->getDefaultOrderStatusId();
605
606 // .
607 // - ,
608 if(intval($internalId)<=0)
609 {
610 //TODO:
611 $internalPersonTypeId = $this->getInternalId($fields['ORDER']['PERSON_TYPE_XML_ID'], 'PERSON_TYPE_TYPE');
612 $fields['ORDER']['PERSON_TYPE_ID'] = $internalPersonTypeId>0 ? $internalPersonTypeId:$instance->getDefaultPersonTypeId();
613 $fields['ORDER']['USER_ID'] = \CSaleUser::GetAnonymousUserID();
614 $fields['ORDER']['SITE_ID'] = $instance->getDefaultSiteId();
615 }
616 else
617 {
619
621 $orderClass = $registry->getOrderClassName();
622
623 $order = $orderClass::load($internalId);
624 $fields['ORDER']['PERSON_TYPE_ID'] = $order->getPersonTypeId();
625 $fields['ORDER']['USER_ID'] = $order->getUserId();
626 $fields['ORDER']['SITE_ID'] = $order->getSiteId();
627 }
628
629 if(is_array($fields['ORDER']['PROPERTY_VALUES']))
630 {
631 foreach($fields['ORDER']['PROPERTY_VALUES'] as $k=>&$item)
632 {
633 $internalIdExternalSystem = $item['ORDER_PROPS_ID'];
634 $externalId = $item['ORDER_PROPS_XML_ID'];
635
636 unset($item['ORDER_PROPS_ID']);
637 unset($item['ORDER_PROPS_XML_ID']);
638 unset($item['ID']);//id .. orderPropsId
639
640 if($externalId<>'')
641 {
642 $ixExternal['PROPERTIES'][$k]['MAP'][$externalId] = $internalIdExternalSystem;
643
644 $internalId = $this->getInternalId($externalId, Registry::ENTITY_PROPERTY);
645 if(intval($internalId)>0)
646 {
647 $item['ORDER_PROPS_ID'] = $internalId;
648 $ixInternal['PROPERTIES'][$k]['MAP'][$externalId] = $internalId;
649 }
650 }
651 else
652 {
653 unset($item);
654 }
655 }
656 }
657
658 if(is_array($fields['ORDER']['BASKET_ITEMS']))
659 {
660 $n = 1;
661 foreach($fields['ORDER']['BASKET_ITEMS'] as $k=>&$item)
662 {
663 $internalIdExternalSystem = $item['ID'];
664 $externalId = $item['XML_ID'];
665
666 $internalId = $this->getInternalId($externalId, Registry::ENTITY_BASKET, ['ORDER_ID'=>$internalOrderId]);
667 $internalBasketItemId = (intval($internalId)>0)? $internalId:-1;
668 $ixInternal['BASKET_ITEMS'][$k]['MAP'][$externalId] = (intval($internalId)>0)? $internalId:'n'.$n++;
669 $ixExternal['BASKET_ITEMS'][$k]['MAP'][$externalId] = $internalIdExternalSystem;
670
671 $properties = $item['PROPERTIES'];
672 if(count($properties)>0)
673 {
674 foreach ($properties as $kp=>&$property)
675 {
676 $property['BASKET_ID'] = $ixInternal['BASKET_ITEMS'][$k]['MAP'][$externalId];
677 $internalIdBasketProps = $this->getInternalId($property['XML_ID'], Registry::ENTITY_BASKET_PROPERTIES_COLLECTION, ['BASKET_ID'=>$internalBasketItemId]);
678 if(intval($internalIdBasketProps)>0)
679 {
680 $ixInternal['BASKET_ITEMS'][$k]['PROPERTIES'][$kp][$property['XML_ID']] = $internalIdBasketProps;
681 $property['ID'] = $internalIdBasketProps;
682 }
683 $ixExternal['BASKET_ITEMS'][$k]['PROPERTIES'][$kp]['MAP'][$property['XML_ID']] = $property['ID'];
684 }
685 }
686
687
688 $item = array_merge(
689 ['PROPERTIES'=>$properties],
690 $this->prepareFieldsBasketItem($item)
691 );
692
693 $item['ID'] = $ixInternal['BASKET_ITEMS'][$k]['MAP'][$externalId];
694 }
695 }
696
697 if(is_array($fields['ORDER']['PAYMENTS']))
698 {
699 foreach($fields['ORDER']['PAYMENTS'] as $k=>&$item)
700 {
701 $externalId = $item['XML_ID'];
702 $ixExternal['PAYMENTS'][$k]['MAP'][$externalId] = $item['ID'];
703
704 unset($item['ID']);
705 $internalId = $this->getInternalId($externalId, Registry::ENTITY_PAYMENT_COLLECTION, ['ORDER_ID'=>$internalOrderId]);
706 if(intval($internalId)>0)
707 {
708 $item['ID'] = $internalId;
709 $ixInternal['PAYMENTS'][$k]['MAP'][$externalId] = $internalId;
710 }
711
712 $externalPaySystemId = $item['PAY_SYSTEM_XML_ID'];
713 $ixExternal['PAYMENTS'][$k]['PAY_SYSTEMS']['MAP'][$externalPaySystemId] = $item['PAY_SYSTEM_ID'];
714
715 unset($item['PAY_SYSTEM_XML_ID']);
716 $internalPaySystemId = $this->getInternalId($externalPaySystemId, 'PAY_SYSTEM_TYPE');
717 $item['PAY_SYSTEM_ID'] = $internalPaySystemId>0 ? $internalPaySystemId:$instance->getDefaultPaySystemId();
718 $ixInternal['PAYMENTS'][$k]['PAY_SYSTEM']['MAP'][$externalPaySystemId] = $item['PAY_SYSTEM_ID'];
719 }
720 }
721
722 if(is_array($fields['ORDER']['SHIPMENTS']))
723 {
724 foreach($fields['ORDER']['SHIPMENTS'] as $k=>&$item)
725 {
726 $externalId = $item['XML_ID'];
727 $ixExternal['SHIPMENTS'][$k]['MAP'][$externalId] = $item['ID'];
728
729 unset($item['ID']);
730 $internalId = $this->getInternalId($item['XML_ID'], Registry::ENTITY_SHIPMENT_COLLECTION, ['ORDER_ID'=>$internalOrderId]);
731 $internalShipmentId = (intval($internalId)>0)? $internalId:-1;
732 if(intval($internalId)>0)
733 {
734 $item['ID'] = $internalId;
735 $ixInternal['SHIPMENTS'][$k][$externalId] = $internalId;
736 }
737
738 $externalDeliveryId = $item['DELIVERY_XML_ID'];
739 $ixExternal['SHIPMENTS'][$k]['DELIVERY_SYSTEM']['MAP'][$externalDeliveryId] = $item['DELIVERY_ID'];
740
741 unset($item['DELIVERY_XML_ID']);
742 $internalDeliveryId = $this->getInternalId($externalDeliveryId, 'DELIVERY_SYSTEM_TYPE');
743 $item['DELIVERY_ID'] = $internalDeliveryId>0 ? $internalDeliveryId:$instance->getDefaultDeliverySystemId();
744 $ixInternal['SHIPMENTS'][$k]['DELIVERY_SYSTEM']['MAP'][$externalDeliveryId] = $item['DELIVERY_ID'];
745
746 $externalDeliveryStatusId = $item['STATUS_XML_ID'];
747 $ixExternal['SHIPMENTS'][$k]['DELIVERY_STATUS']['MAP'][$externalDeliveryStatusId] = $item['STATUS_ID'];
748
749 unset($item['STATUS_XML_ID']);
750 $internalDeliveryStatusId = $this->getInternalId($externalDeliveryStatusId, Registry::ENTITY_DELIVERY_STATUS);
751 $item['STATUS_ID'] = $internalDeliveryStatusId<>''? $internalDeliveryStatusId:$instance->getDefaultDeliveryStatusId();
752 $ixInternal['SHIPMENTS'][$k]['DELIVERY_STATUS']['MAP'][$externalDeliveryStatusId] = $item['STATUS_ID'];
753
754 foreach($item['SHIPMENT_ITEMS'] as $kb=>&$shipmentItem)
755 {
756 unset($shipmentItem['ID']);
757 unset($shipmentItem['ORDER_DELIVERY_ID']);
758 $internalIdShipmentItem = $this->getInternalId($shipmentItem['XML_ID'], Registry::ENTITY_SHIPMENT_ITEM_COLLECTION, ['ORDER_DELIVERY_ID'=>$internalShipmentId]);
759
760 if(intval($internalIdShipmentItem)>0)
761 {
762 $shipmentItem['ID'] = $internalIdShipmentItem;
763 if(intval($internalId)>0)
764 $shipmentItem['ORDER_DELIVERY_ID'] = $internalId;
765
766 $ixInternal['SHIPMENTS'][$k]['SHIPMENT_ITEMS'][$kb]['MAP'][$externalId] = $internalIdShipmentItem;
767 }
768
769 // xmlId => id. , id
770 $external = '';
771 foreach ($ixExternal['BASKET_ITEMS'] as $map)
772 {
773 $internal = current($map['MAP']);
774
775 if($shipmentItem['BASKET_ID'] == $internal)
776 {
777 $external = key($map['MAP']);
778 break;
779 }
780 }
781
782 if($external=='')
783 $result->addError(new Error('Modify fields error. ShipmentItem xmlId is invalid',200550000002));
784
785 if($external<>'')
786 {
787 // id xmlId => id.
788 foreach ($ixInternal['BASKET_ITEMS'] as $map)
789 {
790 if(isset($map['MAP'][$external]))
791 {
792 $shipmentItem['BASKET_ID'] = $map['MAP'][$external];
793 break;
794 }
795 }
796 }
797 }
798
799 $item = $this->prepareFieldsShipment($item);
800 }
801 }
802
803 if($this->isCrmModuleInstalled())
804 {
805 if(is_array($fields['ORDER']['TRADE_BINDINGS']))
806 {
807 foreach($fields['ORDER']['TRADE_BINDINGS'] as $k=>&$item)
808 {
809 $externalId = $item['XML_ID'];
810 $ixExternal['TRADE_BINDINGS'][$k]['MAP'][$externalId] = $item['ID'];
811
812 unset($item['ID']);
813 if($externalId<>'') // . xmlId
814 {
815 $internalId = $this->getInternalId($externalId, Registry::ENTITY_TRADE_BINDING_COLLECTION, ['ORDER_ID'=>$internalOrderId]);
816 if(intval($internalId)>0)
817 {
818 $item['ID'] = $internalId;
819 $ixInternal['TRADE_BINDINGS'][$k]['MAP'][$externalId] = $internalId;
820 }
821 }
822
823 $externalTradePlatformId = $item['TRADING_PLATFORM_XML_ID'];
824 $ixExternal['TRADE_BINDINGS'][$k]['TRADING_PLATFORMS']['MAP'][$externalTradePlatformId] = $item['TRADING_PLATFORM_ID'];
825
826 unset($item['TRADING_PLATFORM_XML_ID']);
827 $internalTradePlatformId = $this->getInternalId($externalTradePlatformId, 'TRADING_PLATFORM_TYPE');
828 //TODO: need default value <> 0
829 $item['TRADING_PLATFORM_ID'] = $internalTradePlatformId>0 ? $internalTradePlatformId:0;
830 $ixInternal['TRADE_BINDINGS'][$k]['TRADING_PLATFORM']['MAP'][$externalTradePlatformId] = $item['TRADING_PLATFORM_ID'];
831 }
832 }
833
834 if(is_array($fields['ORDER']['CLIENTS']))
835 {
836 foreach($fields['ORDER']['CLIENTS'] as $k=>&$item)
837 {
838 $externalId = $item['XML_ID'];
839 $ixExternal['CLIENTS'][$k]['MAP'][$externalId] = $item['ID'];
840
841 unset($item['ID']);
842 $internalId = $this->getInternalId($externalId, ENTITY_CRM_CONTACT_COMPANY_COLLECTION);
843 if(intval($internalId)>0)
844 {
845 $item['ID'] = $internalId;
846 $ixInternal['CLIENTS'][$k]['MAP'][$externalId] = $internalId;
847 }
848 }
849 }
850 }
851 else
852 {
853 // TRADE_BINDINGS .
854 // . 24
855 $fields['ORDER']['TRADE_BINDINGS'] = [];
856 unset($fields['ORDER']['CLIENTS']);
857 }
858
859 if($result->isSuccess())
860 {
861 $result->setData(['DATA'=>$fields]);
862 LoggerDiag::addMessage('ORDER_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID_SUCCESS', var_export($fields, true));
863 }
864 else
865 {
866 LoggerDiag::addMessage('ORDER_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID_ERROR');
867 }
868
869 return $result;
870 }
871
872 protected function getInternalId($externalId, $typeName, $params=[])
873 {
874 $loader = Factory::create($typeName, $params);
875 return $loader->getFieldsByExternalId($externalId);
876 }
877
878 private function prepareFieldsBasketItem($fields)
879 {
880 $instance = \Bitrix\Sale\Rest\Synchronization\Manager::getInstance();
881 $loader = Factory::create('PRODUCT');
882
883 $code = $loader->getCodeAfterDelimiter($fields['PRODUCT_XML_ID']);
884 $product = $code<>'' ? $loader->getFieldsByExternalId($code):array();
885 if(empty($product))
886 $product = $loader->getFieldsByExternalId($fields['PRODUCT_XML_ID']);
887
888 if(!empty($product))
889 {
890 $result = array(
891 "PRODUCT_ID" => $product["ID"],
892 "NAME" => $product["NAME"],
893 "MODULE" => "catalog",
894 "PRODUCT_PROVIDER_CLASS" => "CCatalogProductProvider",
895 "CATALOG_XML_ID" => $product["IBLOCK_XML_ID"],
896 "DETAIL_PAGE_URL" => $product["DETAIL_PAGE_URL"],
897 "WEIGHT" => $product["WEIGHT"],
898 "NOTES" => $product["CATALOG_GROUP_NAME"]
899 );
900 }
901 else
902 {
903 $ri = new RandomSequence($fields['PRODUCT_XML_ID']);
904 $result = array(
905 "PRODUCT_ID" => $ri->rand(1000000, 9999999),
906 "NAME" => $fields["NAME"],
907 "MODULE" => null,
908 "PRODUCT_PROVIDER_CLASS" => null,
909 "CATALOG_XML_ID" => null,
910 "MEASURE_CODE" => $fields["MEASURE_CODE"],
911 "MEASURE_NAME" => $fields["MEASURE_NAME"],
912 //"DISCOUNT_PRICE" => $item['DISCOUNT']['PRICE'],
913 );
914 }
915
916 $result["LID"] = $instance->getDefaultSiteId();
917 $result["QUANTITY"] = $fields["QUANTITY"];
918 $result["CURRENCY"] = $fields["CURRENCY"];
919 $result["DELAY"] = "N";
920 $result["CAN_BUY"] = "Y";
921 $result["IGNORE_CALLBACK_FUNC"] = "Y";
922 $result["PRODUCT_XML_ID"] = $fields["PRODUCT_XML_ID"];
923 $result["XML_ID"] = $fields["XML_ID"];
924
925 $result["PRICE"] = $fields["PRICE"];
926
927 $result["VAT_RATE"] = $fields["VAT_RATE"];
928 $result["VAT_INCLUDED"] = $fields["VAT_INCLUDED"];
929
930 return $result;
931 }
932
933 private function prepareFieldsShipment($item)
934 {
935 // .. xml 24 ( )
936 //
937 $item['CUSTOM_PRICE_DELIVERY'] = 'Y';
938
939 return $item;
940 }
941
942 public function importAction(array $fields)
943 {
944 $result = new Result();
945
946 self::setFlagActionImport();
947
948 $fields = $this->prepareFieldsImport($fields);
949
950 LoggerDiag::addMessage('ORDER_IMPORT_ACTION_WITH_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID', var_export($fields, true));
951
952 $r = $this->resolveExternalIdToInternalId($fields);
953
954 if($r->isSuccess())
955 {
956 $result = $this->modifyAction($r->getData()['DATA']);
957 }
958 else
959 {
960 $this->addErrors($r->getErrors());
961 }
962
963 if(count($this->getErrors())>0)
964 {
965 LoggerDiag::addMessage('ORDER_IMPORT_ACTION_WITH_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID_ERROR', var_export($this->getErrors(), true));
966 return null;
967 }
968 else
969 {
970 LoggerDiag::addMessage('ORDER_IMPORT_ACTION_WITH_RESOLVE_EXTERNAL_ID_TO_INTERNAL_ID_SUCCESS');
971 return $result;
972 }
973 }
974
975 public function prepareFieldsImport($fields)
976 {
977 $orderFields = [
978 'USER_ID',
979 'CURRENCY',
980 'LID',
981 'PERSON_TYPE_XML_ID',
982 'STATUS_XML_ID',
983 'CANCELED',
984 'REASON_CANCELED',
985 'COMMENTS',
986 'XML_ID',
987 'ID',
988 ];
989
990 $orderPropertyValuesFields = [
991 'NAME',
992 'CODE',
993 'ORDER_PROPS_XML_ID',
994 'VALUE',
995 'ORDER_PROPS_ID'
996 ];
997
998 $basketItemFields = [
999 'PRODUCT_XML_ID',
1000 'NAME',
1001 'MEASURE_CODE',
1002 'MEASURE_NAME',
1003 'QUANTITY',
1004 'CURRENCY',
1005 'XML_ID',
1006 'ID',
1007 'PRICE',
1008 'VAT_RATE',
1009 'VAT_INCLUDED',
1010 ];
1011
1012 $basketItemPropertiesFields = [
1013 'NAME',
1014 'VALUE',
1015 'CODE',
1016 'XML_ID'
1017 ];
1018
1019 $paymentFields = [
1020 'PAY_SYSTEM_XML_ID',
1021 'PAY_SYSTEM_ID',
1022 'PAID',
1023 'PAY_VOUCHER_NUM',
1024 'PAY_VOUCHER_DATE',
1025 'XML_ID',
1026 'ID',
1027 'SUM',
1028 'IS_RETURN',
1029 'PAY_RETURN_NUM',
1030 'PAY_RETURN_DATE',
1031 'PAY_RETURN_COMMENT',
1032 'COMMENTS',
1033 ];
1034
1035 $shipmentFields = [
1036 'BASE_PRICE_DELIVERY',
1037 'PRICE_DELIVERY',
1038 'ALLOW_DELIVERY',
1039 'DEDUCTED',
1040 'REASON_UNDO_DEDUCTED',
1041 'DELIVERY_DOC_NUM',
1042 'DELIVERY_DOC_DATE',
1043 'TRACKING_NUMBER',
1044 'XML_ID',
1045 'ID',
1046 'CANCELED',
1047 'COMMENTS',
1048 'STATUS_XML_ID',
1049 'STATUS_ID',
1050 'DELIVERY_XML_ID',
1051 'DELIVERY_ID',
1052 ];
1053
1054 $shipmentItemsFields = [
1055 'BASKET_ID',
1056 'QUANTITY',
1057 'XML_ID',
1058 ];
1059
1060 $result['ORDER'] = array_intersect_key($fields['ORDER'], array_flip($orderFields));
1061
1062 if(isset($fields['ORDER']['PROPERTY_VALUES']))
1063 {
1064 foreach($fields['ORDER']['PROPERTY_VALUES'] as $k=>$v)
1065 $result['ORDER']['PROPERTY_VALUES'][$k] = array_intersect_key($v, array_flip($orderPropertyValuesFields));
1066 }
1067
1068 if(isset($fields['ORDER']['BASKET_ITEMS']))
1069 {
1070 foreach($fields['ORDER']['BASKET_ITEMS'] as $k=>$item)
1071 {
1072 $result['ORDER']['BASKET_ITEMS'][$k] = array_intersect_key($item, array_flip($basketItemFields));
1073
1074 if(isset($item['PROPERTIES']))
1075 {
1076 foreach($item['PROPERTIES'] as $kProps=>$pros)
1077 {
1078 $result['ORDER']['BASKET_ITEMS'][$k]['PROPERTIES'][$kProps] = array_intersect_key($pros, array_flip($basketItemPropertiesFields));
1079 }
1080 }
1081 }
1082 }
1083
1084 if(isset($fields['ORDER']['PAYMENTS']))
1085 {
1086 foreach($fields['ORDER']['PAYMENTS'] as $k=>$payment)
1087 {
1088 $result['ORDER']['PAYMENTS'][$k] = array_intersect_key($payment, array_flip($paymentFields));
1089 }
1090 }
1091
1092 if(isset($fields['ORDER']['SHIPMENTS']))
1093 {
1094 foreach($fields['ORDER']['SHIPMENTS'] as $k=>$shipment)
1095 {
1096 $result['ORDER']['SHIPMENTS'][$k] = array_intersect_key($shipment, array_flip($shipmentFields));
1097
1098 if(isset($shipment['SHIPMENT_ITEMS']))
1099 {
1100 foreach($shipment['SHIPMENT_ITEMS'] as $kShipmentItem=>$shipmentItem)
1101 {
1102 $result['ORDER']['SHIPMENTS'][$k]['SHIPMENT_ITEMS'][$kShipmentItem] = array_intersect_key($shipmentItem, array_flip($shipmentItemsFields));
1103 }
1104 }
1105 }
1106 }
1107
1108 return $result;
1109 }
1110
1111 protected function checkPermissionEntity($name)
1112 {
1113 if($name == 'getdeliveryidlist'
1114 || $name == 'getpayments'
1115 || $name == 'getpaysystemidlist'
1116 || $name == 'getprintedchecks'
1117 || $name == 'getshipments'
1118 || $name == 'getbasket'
1119 || $name == 'getcurrency'
1120 || $name == 'getdateinsert'
1121 || $name == 'getdeliverylocation'
1122 || $name == 'getapplydiscount'
1123 || $name == 'getpersontypeid'
1124 || $name == 'getprice'
1125 || $name == 'getpropertyvalues'
1126 || $name == 'getsiteid'
1127 || $name == 'getsumpaid'
1128 || $name == 'gettaxlist'
1129 || $name == 'gettaxlocation'
1130 || $name == 'gettaxprice'
1131 || $name == 'gettaxvalue'
1132 || $name == 'getuserid'
1133 || $name == 'getvatrate'
1134 || $name == 'getvatsum'
1135 || $name == 'iscanceled'
1136 || $name == 'isexternal'
1137 || $name == 'ismarked'
1138 || $name == 'ispaid'
1139 || $name == 'isshipped'
1140 || $name == 'isusedvat'
1141 )
1142 {
1143 $r = $this->checkReadPermissionEntity();
1144 }
1145 elseif($name == 'import'
1146 || $name == 'importdelete'
1147 )
1148 {
1149 $r = $this->checkModifyPermissionEntity();
1150 }
1151 else
1152 {
1153 $r = parent::checkPermissionEntity($name);
1154 }
1155 return $r;
1156 }
1157}
getCurrencyAction(\Bitrix\Sale\Order $order)
Definition order.php:345
getSumPaidAction(\Bitrix\Sale\Order $order)
Definition order.php:381
getPrintedChecksAction(\Bitrix\Sale\Order $order)
Definition order.php:330
getPaySystemIdListAction(\Bitrix\Sale\Order $order)
Definition order.php:325
isUsedVatAction(\Bitrix\Sale\Order $order)
Definition order.php:447
getTaxPriceAction(\Bitrix\Sale\Order $order)
Definition order.php:397
getAction(\Bitrix\Sale\Order $order)
Definition order.php:59
tryAddAction(array $fields)
Definition order.php:161
importDeleteAction(\Bitrix\Sale\Order $order)
Definition order.php:572
isMarkedAction(\Bitrix\Sale\Order $order)
Definition order.php:432
isCanceledAction(\Bitrix\Sale\Order $order)
Definition order.php:422
getApplyDiscountAction(\Bitrix\Sale\Order $order)
Definition order.php:355
getVatRateAction(\Bitrix\Sale\Order $order)
Definition order.php:412
update(\Bitrix\Sale\Order $order, array $fields)
Definition order.php:518
getPropertyValuesAction(\Bitrix\Sale\Order $order)
Definition order.php:371
getTaxValueAction(\Bitrix\Sale\Order $order)
Definition order.php:402
getPriceAction(\Bitrix\Sale\Order $order)
Definition order.php:366
getDateInsertAction(\Bitrix\Sale\Order $order)
Definition order.php:350
getPersonTypeIdAction(\Bitrix\Sale\Order $order)
Definition order.php:361
getSiteIdAction(\Bitrix\Sale\Order $order)
Definition order.php:376
isPaidAction(\Bitrix\Sale\Order $order)
Definition order.php:437
importAction(array $fields)
Definition order.php:942
static prepareFields(array $fields)
Definition order.php:543
isExternalAction(\Bitrix\Sale\Order $order)
Definition order.php:427
tryUpdateAction(\Bitrix\Sale\Order $order, array $fields)
Definition order.php:205
getPaymentsAction(\Bitrix\Sale\Order $order)
Definition order.php:320
getBasketAction(\Bitrix\Sale\Order $order)
Definition order.php:340
getShipmentsAction(\Bitrix\Sale\Order $order)
Definition order.php:335
isShippedAction(\Bitrix\Sale\Order $order)
Definition order.php:442
getDeliveryIdListAction(\Bitrix\Sale\Order $order)
Definition order.php:315
modify(array $fields)
Definition order.php:472
getUserIdAction(\Bitrix\Sale\Order $order)
Definition order.php:407
deleteAction(\Bitrix\Sale\Order $order)
Definition order.php:294
getInternalId($externalId, $typeName, $params=[])
Definition order.php:872
getVatSumAction(\Bitrix\Sale\Order $order)
Definition order.php:417
const ENTITY_BASKET_PROPERTIES_COLLECTION
Definition registry.php:33
const ENTITY_SHIPMENT_COLLECTION
Definition registry.php:26
const ENTITY_TRADE_BINDING_COLLECTION
Definition registry.php:55
const ENTITY_PAYMENT_COLLECTION
Definition registry.php:22
static getInstance($type)
Definition registry.php:183
const ENTITY_SHIPMENT_ITEM_COLLECTION
Definition registry.php:38
const ENTITY_DELIVERY_STATUS
Definition registry.php:49