Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
importonecpackage.php
1<?php
3
4
18
19IncludeModuleLangFile(__FILE__);
20
22{
23 use PackageTrait;
24 use LoggerTrait;
25
27
28 private static $instance = null;
29 private static $settings = null;
30
31 protected $order = null;
32
36 public static function getInstance()
37 {
38 if(self::$instance === null)
39 {
40 self::$instance = new static();
41 }
42 return self::$instance;
43 }
44
45 private function __clone() {}
46 private function __construct() {}
47
52 static public function checkSettings()
53 {
54 if(self::$settings === null)
55 {
56 $result = new Result();
57 $message = self::getMessage();
58
60 Option::get('catalog', 'enable_reservation', 'N')=='Y')
61 {
62 $result->addError(new Error($message["CC_BSC1_USE_STORE_SALE_1"]));
63 }
64
65 if(Option::get("main", "~sale_converted_15", 'N') <> 'Y')
66 {
67 $result->addError(new Error($message["CC_BSC1_CONVERT_SALE"]));
68 }
69
70 if(Option::get("sale", "allow_deduction_on_delivery", "N") == 'Y')
71 {
72 $result->addError(new Error($message["CC_BSC1_SALE_ALLOW_DEDUCTION_ON_DELIVERY_ERROR"]));
73 }
74
75 self::$settings = $result;
76 }
77
78 return self::$settings;
79 }
80
85 protected function checkDocuments(array $documents)
86 {
87 return new Result();
88 }
89
94 protected function getDeliveryServiceItem(array $list)
95 {
96 foreach ($list as $k=>$items)
97 {
98 if(array_key_exists(self::DELIVERY_SERVICE_XMLID, $items))
99 {
100 return $items;
101 }
102 }
103
104 return null;
105 }
106
113 protected function convert(array $documents)
114 {
115 $result = new Result();
116 $list = array();
117
118 $documentOrder = $this->getDocumentByTypeId(OneC\DocumentType::ORDER, $documents);
119
120 if($documentOrder instanceof OneC\DocumentBase)
121 {
122 $agentFieldValue = $documentOrder->getFieldValues();
123 if(is_array($agentFieldValue['AGENT']))
124 {
125 $documentProfile = new OneC\UserProfileDocument();
126 $documentProfile->setFields($agentFieldValue['AGENT']);
127 $documents[] = $documentProfile;
128 }
129 }
130
131 foreach($documents as $document)
132 {
133 $list[] = $this->convertDocument($document);
134 }
135
136 if($result->isSuccess())
137 {
138 $result = $this->checkFields($list);
139 if($result->isSuccess())
140 {
141 $result->setData($list);
142 }
143 }
144
145 return $result;
146 }
147
152 protected function resolveOwnerEntityTypeId($typeId)
153 {
154 $entityTypeId = EntityType::UNDEFINED;
155
156 switch ($typeId)
157 {
158 case OneC\DocumentType::ORDER:
159 $entityTypeId = static::getParentEntityTypeId();
160 break;
161 case OneC\DocumentType::PAYMENT_CARD_TRANSACTION:
162 $entityTypeId = static::getPaymentCardEntityTypeId();
163 break;
164 case OneC\DocumentType::PAYMENT_CASH:
165 $entityTypeId = static::getPaymentCashEntityTypeId();
166 break;
167 case OneC\DocumentType::PAYMENT_CASH_LESS:
168 $entityTypeId = static::getPaymentCashLessEntityTypeId();
169 break;
170 case OneC\DocumentType::SHIPMENT:
171 $entityTypeId = static::getShipmentEntityTypeId();
172 break;
173 case OneC\DocumentType::USER_PROFILE:
174 case OneC\DocumentType::PROFILE:
175 $entityTypeId = $typeId;
176 break;
177 }
178 return $entityTypeId;
179 }
180
185 protected function checkFields(array $items)
186 {
187 $result = new Result();
188
189 $parentEntityId = null;
190
191 $item = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
192 if($item instanceof Exchange\Entity\EntityImport)
193 {
194 $params = $item->getFieldValues();
195 $fields = $params['TRAITS'];
196
197 if($fields['ID']<>'')
198 {
199 $parentEntityId = $fields['ID'];
200 }
201 elseif($fields[$item::getFieldExternalId()]<>'')
202 {
203 $parentEntityId = $fields[$item::getFieldExternalId()];
204 }
205 }
206
207 if(empty($parentEntityId))
208 $result->addErrors(array(new Error('Order not found')));
209
210 foreach($items as $item)
211 {
212 if($item->getOwnerTypeId() <> static::getParentEntityTypeId())
213 {
214 $params = $item->getFieldValues();
215 $fields = $params['TRAITS'];
216
217 if(!empty($parentEntityId) && $fields['ORDER_ID']<>'')
218 {
219 if($parentEntityId <> $fields['ORDER_ID'])
220 {
221 $result->addErrors(array(new Error('Order not found')));
222 break 1;
223 }
224 }
225 }
226 }
227
228 if($result->isSuccess())
229 $result = parent::checkFields($items);
230
231 return $result;
232 }
233
234 public static function configuration()
235 {
236 parent::configuration();
237
238 static::setConfig(static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT);
239
240 ManagerImport::registerInstance(static::getParentEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionOrder(), new OneC\CriterionOrder());
241 ManagerImport::registerInstance(static::getShipmentEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionShipment(), new OneC\CriterionShipment());
242 ManagerImport::registerInstance(static::getPaymentCashEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionPayment(), new OneC\CriterionPayment());
243 ManagerImport::registerInstance(static::getPaymentCashLessEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionPayment(), new OneC\CriterionPayment());
244 ManagerImport::registerInstance(static::getPaymentCardEntityTypeId(), OneC\ImportSettings::getCurrent(), new OneC\CollisionPayment(), new OneC\CriterionPayment());
246 }
247
252 protected function sortItems(array $items)
253 {
254 $list = array();
255 $i = 0;
256
257 foreach ($items as $item)
258 {
259 if($item->getOwnerTypeId() == EntityType::USER_PROFILE)
260 {
261 $list[$i++] = $item;
262 }
263 }
264
265 foreach ($items as $item)
266 {
267 if($item->getOwnerTypeId() == static::getParentEntityTypeId())
268 {
269 $list[$i++] = $item;
270 }
271 }
272
273 foreach ($items as $item)
274 {
275 if($item->getOwnerTypeId() <> static::getParentEntityTypeId() && $item->getOwnerTypeId() <> EntityType::USER_PROFILE)
276 {
277 $list[$i++] = $item;
278 }
279 }
280
281 return $list;
282 }
283
289 protected function import(array $items)
290 {
291 $result = new Result();
292
293 $items = $this->sortItems($items);
295 $itemParent = $this->loadParent($items);
296
297 if($itemParent->getEntityId()>0)
298 {
299 $r = $this->UpdateCashBoxChecks($itemParent, $items);
300 if($r->isSuccess())
301 {
302 $this->save($itemParent, $items);
303 return $result;
304 }
305
306 $r = $this->onBeforeEntityModify($itemParent, $items);
307 if($r->hasWarnings())
308 $this->marker($itemParent, $r);
309 }
310
311 if(!$this->hasCollisionErrors($items))
312 {
314 foreach($items as $item)
315 {
316 if($item->getOwnerTypeId() == EntityType::USER_PROFILE)
317 {
319 $r = new Result();
320 if($itemParent->getEntityId() == null)
321 {
322 $params = $item->getFieldValues();
323 $fields = $params['TRAITS'];
324
325 $personalTypeId = $params['TRAITS']['PERSON_TYPE_ID'] = $item->resolvePersonTypeId($fields);
326
327 $property = $params['ORDER_PROPS'];
328 if(!empty($property))
329 {
330 $params['ORDER_PROP'] = $item->getPropertyOrdersByConfig($personalTypeId, array(), $property);
331 }
332
333 unset($params['ORDER_PROPS']);
334 $item->setFields($params);
335
336 $r = $item->load($fields);
337
338 if(intval($personalTypeId)<=0)
339 $r->addError(new Error(GetMessage("SALE_EXCHANGE_PACKAGE_ERROR_PERSONAL_TYPE_IS_EMPTY", array("#DOCUMENT_ID#"=>$fields['XML_ID'])), "PACKAGE_ERROR_PERSONAL_TYPE_IS_EPMTY"));
340
341 if($r->isSuccess())
342 {
343 if(!$this->importableItems($item))
344 {
345 return new Result();
346 }
347
348 $r = $this->modifyEntity($item);
349
350 if(intval($item->getId())<=0)
351 $r->addError(new Error(GetMessage("SALE_EXCHANGE_PACKAGE_ERROR_USER_IS_EMPTY", array("#DOCUMENT_ID#"=>$fields['XML_ID'])), "PACKAGE_ERROR_USER_IS_EPMTY"));
352
353 if($r->isSuccess())
354 {
356 $paramsOrder = $itemParent->getFieldValues();
357 $fieldsOrder = &$paramsOrder['TRAITS'];
358
359 if(!empty($property))
360 {
361 $fieldsOrder['ORDER_PROP'] = $params['ORDER_PROP'];
362 }
363
364 $fieldsOrder['USER_ID'] = $item->getId();
365 $fieldsOrder['PERSON_TYPE_ID'] = $personalTypeId;
366 $itemParent->setFields($paramsOrder);
367 }
368 }
369 }
370 }
371 elseif($item->getOwnerTypeId() == static::getParentEntityTypeId())
372 {
373 if(!$this->importableItems($itemParent))
374 {
375 return new Result();
376 }
377 $r = $this->modifyEntity($itemParent);
378 }
379 else
380 {
383 $order = $itemParent->getEntity();
384 $params = $item->getFieldValues();
385 $fields = $params['TRAITS'];
386
387 $r = $this->orderIsLoad($order, $itemParent);
388 if(!$r->hasWarnings())
389 {
390 static::load($item, $fields, $order);
391
392 $r = $this->checkParentById($fields['ID'], $item);
393 if(!$r->hasWarnings())
394 {
395 $isShipped = $order->isShipped();
396
397 $r = $this->modifyEntity($item);
398
399 if($r->isSuccess())
400 {
401 if($item->getOwnerTypeId() == static::getShipmentEntityTypeId())
402 {
403 if(!$isShipped && $order->isShipped())
404 $this->onAfterShipmentModifyChangeStatusOnDelivery($itemParent);
405 }
406 }
407 }
408 }
409 }
410
411 if(!$r->isSuccess())
412 {
413 $result->addErrors($r->getErrors());
414 break;
415 }
416 elseif($r->hasWarnings())
417 {
418 $result->addWarnings($r->getWarnings());
419 break;
420 }
421 }
422
423 if($result->isSuccess() && !$result->hasWarnings() && !$this->hasCollisionErrors($items))
424 {
425 $r = $this->onAfterEntitiesModify($itemParent, $items);
426 if(!$r->isSuccess())
427 $result->addErrors($r->getErrors());
428 if($r->hasWarnings())
429 $result->addWarnings($r->getWarnings());
430 }
431 }
432
433 if($result->isSuccess())
434 {
435 $r = $this->save($itemParent, $items);
436 if(!$r->isSuccess())
437 $result->addErrors($r->getErrors());
438 if($r->hasWarnings())
439 $result->addWarnings($r->getWarnings());
440 }
441
442 return $result;
443 }
444
449 private function importableItems($item)
450 {
451 if($item->getId() == null && !$item->isImportable())
452 {
453 switch ($item->getOwnerTypeId())
454 {
455 case static::getParentEntityTypeId():
456 case EntityType::USER_PROFILE:
457 return false;
458 break;
459 }
460 }
461
462 return true;
463 }
464
468 static protected function getParentEntityTypeId()
469 {
470 return EntityType::ORDER;
471 }
472
476 static protected function getShipmentEntityTypeId()
477 {
479 }
480
484 static protected function getPaymentCardEntityTypeId()
485 {
487 }
488
492 static protected function getPaymentCashEntityTypeId()
493 {
495 }
496
500 static protected function getPaymentCashLessEntityTypeId()
501 {
503 }
504
509 protected function loadParent(array $items)
510 {
511 $item = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
512 if($item instanceof Exchange\Entity\EntityImport)
513 {
514 $params = $item->getFieldValues();
515 $fields = $params['TRAITS'];
516
517 static::load($item, $fields);
518
519 return $item;
520 }
521
522 return null;
523 }
524
537 protected function onBeforeShipmentCollectionModify(Exchange\Entity\OrderImport $orderImport, $items)
538 {
539 $result = new Result();
540
541 $shipmentItems = array();
542
544 $order = $orderImport->getEntity();
545 if(empty($order))
546 throw new ArgumentNullException('Order');
547
548 foreach($items as $item)
549 {
550 switch($item->getOwnerTypeId())
551 {
552 case static::getShipmentEntityTypeId():
553 $params = $item->getFieldValues();
554 $fields = $params['TRAITS'];
555
556 if(!empty($fields['ID']))
557 $shipmentItems[] = $fields['ID'];
558 break;
559 }
560 }
561
563 $shipmentList = array();
564 $shipmentCollection = $order->getShipmentCollection();
565 foreach($shipmentCollection as $shipment)
566 {
567 if($shipment->isSystem())
568 continue;
569
570 if(!in_array($shipment->getId(), $shipmentItems))
571 $shipmentList[$shipment->getId()] = $shipment;
572 }
573
574 if(!empty($shipmentList))
575 {
576 foreach($shipmentList as $id=>$shipment)
577 {
578 $typeId = $this->resolveEntityTypeId($shipment);
579
582 $item = $this->entityFactoryCreate($typeId);
584 static::load($item, array('ID'=>$id), $order);
585 $collision = $item->getLoadedCollision();
586
587 $collision->resolve($item);
588 if(!$item->hasCollisionErrors())
589 {
590 $result = $item->delete();
591 }
592 else
593 {
594 $item->setCollisions(EntityCollisionType::BeforeUpdateShipmentDeletedError, $item->getParentEntity());
595 }
596
597 $collisions = $item->getCollisions();
598 $item->markedEntityCollisions($collisions);
599 }
600 }
601
602 return $result;
603 }
604
605 protected function resolveEntityTypeId(\Bitrix\Sale\Internals\Entity $entity)
606 {
607 $typeId = EntityType::UNDEFINED;
608
609 if($entity instanceof Order)
610 $typeId = Exchange\Entity\OrderImport::resolveEntityTypeId($entity);
611 elseif ($entity instanceof Payment)
612 $typeId = Exchange\Entity\PaymentImport::resolveEntityTypeId($entity);
613 elseif ($entity instanceof Shipment)
614 $typeId = Exchange\Entity\ShipmentImport::resolveEntityTypeId($entity);
615
616 return $typeId;
617 }
618
630 protected function onBeforePaymentCollectionModify(Exchange\Entity\OrderImport $orderImport, $items)
631 {
632 $result = new Result();
633
634 $paymentItems = array();
635
637 $order = $orderImport->getEntity();
638 if(empty($order))
639 throw new ArgumentNullException('Order');
640
641 foreach($items as $item)
642 {
643 switch($item->getOwnerTypeId())
644 {
645 case static::getPaymentCardEntityTypeId():
646 case static::getPaymentCashEntityTypeId():
647 case static::getPaymentCashLessEntityTypeId():
648 $params = $item->getFieldValues();
649 $fields = $params['TRAITS'];
650
651 if(!empty($fields['ID']))
652 $paymentItems[] = $fields['ID'];
653 break;
654 }
655 }
656
658 $paymentList = array();
659 $paymentCollection = $order->getPaymentCollection();
660 foreach($paymentCollection as $payment)
661 {
662 if(!in_array($payment->getId(), $paymentItems))
663 $paymentList[$payment->getId()] = $payment;
664 }
665
666 if(!empty($paymentList))
667 {
668 foreach($paymentList as $id=>$payment)
669 {
670 $result = $this->paymentDelete($payment);
671 if(!$result->isSuccess())
672 break;
673 }
674 }
675
676 return $result;
677 }
678
679 protected function paymentDelete(Payment $payment)
680 {
681 $result = new Result();
682
683 $typeId = $this->resolveEntityTypeId($payment);
685 $order = $payment->getCollection()->getOrder();
686
688 $item = $this->entityFactoryCreate($typeId);
690 static::load($item, array('ID'=>$payment->getId()), $order);
691 $collision = $item->getLoadedCollision();
692
693 $collision->resolve($item);
694 if(!$item->hasCollisionErrors())
695 {
696 $result = $item->delete();
697 }
698 else
699 {
700 $item->setCollisions(Exchange\EntityCollisionType::BeforeUpdatePaymentDeletedError, $item->getParentEntity());
701 }
702
703 $collisions = $item->getCollisions();
704 $item->markedEntityCollisions($collisions);
705
706 return $result;
707 }
708
718 protected function onBeforeBasketModify(Exchange\Entity\OrderImport $orderImport, $items)
719 {
720 $basketItems = array();
721
723 $order = $orderImport->getEntity();
724 if(empty($order))
725 throw new ArgumentNullException('Order');
726
727 $basket = $order->getBasket();
728
729 foreach($items as $item)
730 {
731 switch($item->getOwnerTypeId())
732 {
733 case static::getParentEntityTypeId():
734 $params = $item->getFieldValues();
735 $basketItems = $params['ITEMS'];
736 break;
737 }
738 }
739
740 $basketItemsIndex = $orderImport->calculateDeltaQuantity($basket, $basketItems);
741
742 $result = $orderImport->synchronizeQuantityBasketItems($basketItemsIndex);
743
744 return $result;
745 }
746
754 protected function onBeforeEntityModify(Exchange\Entity\OrderImport $orderImport, array $items)
755 {
756 $result = new Result();
757 $paymentResult = new Result();
758
764 $basketItemsResult = $this->onBeforeBasketModify($orderImport, $items);
765
766 if(static::$config & static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT)
767 {
768 $paymentResult = $this->onBeforePaymentCollectionModify($orderImport, $items);
769 if(!$paymentResult->isSuccess())
770 $result->addWarnings($paymentResult->getErrors());
771 }
772
773 $shipmentResult = $this->onBeforeShipmentCollectionModify($orderImport, $items);
774
775 if(!$shipmentResult->isSuccess())
776 $result->addWarnings($shipmentResult->getErrors());
777
778 if(!$basketItemsResult->isSuccess() || !$shipmentResult->isSuccess() || !$paymentResult->isSuccess())
779 $result->addWarning(new ResultWarning(GetMessage('SALE_EXCHANGE_PACKAGE_ERROR_ORDER_CANNOT_UPDATE'), "PACKAGE_ERROR_ORDER_CANNOT_UPDATE"));
780
781 return $result;
782 }
783
790 protected function onAfterShipmentModifyChangeStatusOnDelivery(Exchange\Entity\OrderImport $orderImport)
791 {
792 $result = new Result();
793
795 $order = $orderImport->getEntity();
796 if($order->isShipped())
797 {
799 $settings = $orderImport->getSettings();
800 $status = $settings->finalStatusOnDeliveryFor($orderImport->getOwnerTypeId());
801 if($status !== '')
802 $order->setField("STATUS_ID", $status);
803 }
804 return $result;
805 }
806
813 protected function onAfterEntitiesModify(Exchange\Entity\OrderImport $orderImport, $items)
814 {
815 $result = new Result();
816
817 foreach ($items as $item)
818 {
819 if($item->getOwnerTypeId() == static::getParentEntityTypeId())
820 {
822 $order = $orderImport->getEntity();
823 $params = $item->getFieldValues();
824 $fields = $params['TRAITS'];
825
826 if($fields['1C_PAYED_DATE'] instanceof DateTime)
827 {
828 if(!$order->isPaid())
829 {
832 }
833 }
834
835 if($fields['1C_DELIVERY_DATE'] instanceof DateTime)
836 {
837 if(!$order->isShipped())
838 {
841 }
842 }
843 }
844 }
845 return $result;
846 }
847
853 protected function checkParentById($id, Entity\EntityImport $item)
854 {
855 $result = new Result();
856
858 $entity = $item->getEntity();
859
860 if(!empty($id) && empty($entity))
861 {
862 switch($item->getOwnerTypeId())
863 {
864 case static::getPaymentCashEntityTypeId():
865 case static::getPaymentCardEntityTypeId():
866 case static::getPaymentCashLessEntityTypeId():
867 $result->addWarning(new ResultWarning(GetMessage("SALE_EXCHANGE_PACKAGE_ERROR_PAYMENT_IS_NOT_RELATED_TO_ORDER_OR_DELETED", array("#DOCUMENT_ID#"=>$id)), "PACKAGE_ERROR_PAYMENT_IS_NOT_RELATED_TO_ORDER_OR_DELETED"));
868 break;
869 case static::getShipmentEntityTypeId():
870 $result->addWarning(new ResultWarning(GetMessage("SALE_EXCHANGE_PACKAGE_ERROR_SHIPMENT_IS_NOT_RELATED_TO_ORDER_OR_DELETED", array("#DOCUMENT_ID#"=>$id)), "PACKAGE_ERROR_SHIPMENT_IS_NOT_RELATED_TO_ORDER_OR_DELETED"));
871 break;
872 }
873 }
874 return $result;
875 }
876
882 protected function orderIsLoad($order, $itemOrder)
883 {
884 $result = new Result();
885
886 if(!($order instanceof Order))
887 {
888 $params = $itemOrder->getFieldValues();
889 $fields = $params['TRAITS'];
890
891 $result->addWarning(new ResultWarning(GetMessage("SALE_EXCHANGE_PACKAGE_ERROR_ORDER_IS_NOT_LOADED", array("#DOCUMENT_ID#"=>$fields['ID_1C'])), "PACKAGE_ERROR_ORDER_IS_NOT_LOADED"));
892 }
893
894 return $result;
895 }
896
901 protected function marker(Exchange\Entity\OrderImport $orderImport, Result $result)
902 {
904 $order = $orderImport->getEntity();
905
906 $orderImport->setField('MARKED', 'Y');
907 EntityMarker::addMarker($order, $order, $result);
908 }
909
915 protected function save(Exchange\Entity\OrderImport $orderImport, $items)
916 {
917 foreach ($items as $item)
918 {
919 if($item instanceof Exchange\Entity\EntityImport)
920 {
921 $collisions = $item->getCollisions();
922 if (!empty($collisions))
923 {
925 $item->markedEntityCollisions($collisions);
926 }
927 }
928 }
929 return $orderImport->save();
930 }
931
936 protected function hasCollisionErrors($items)
937 {
938 foreach($items as $item)
939 {
940 if($item->hasCollisionErrors())
941 return true;
942 }
943 return false;
944 }
945
952 protected function UpdateCashBoxChecks(Exchange\Entity\OrderImport $orderImport, array $items)
953 {
954 $result = new Result();
955 $result->addError(new Error('', 'CASH_BOX_CHECK_IGNORE'));
956
957 return $result;
958 }
959
964 protected function logger(array $items)
965 {
967 $itemParent = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
968 return $this->loggerEntitiesPackage($items, $itemParent);
969 }
970}
convertDocument(DocumentBase $document)
resolveEntityTypeId(\Bitrix\Sale\Internals\Entity $entity)
UpdateCashBoxChecks(Exchange\Entity\OrderImport $orderImport, array $items)
static configure(ImportBase $entity)
static registerInstance($typeId, ISettingsImport $settings, ICollision $collision=null, ICriterion $criterion=null)
loggerEntitiesPackage(array $items, OrderImport $orderItem)
addWarning(Main\Error $error)
Definition warning.php:22