28 private static $instance =
null;
29 private static $settings =
null;
38 if(self::$instance ===
null)
40 self::$instance =
new static();
42 return self::$instance;
45 private function __clone() {}
46 private function __construct() {}
54 if(self::$settings ===
null)
60 Option::get(
'catalog',
'enable_reservation',
'N')==
'Y')
62 $result->addError(
new Error($message[
"CC_BSC1_USE_STORE_SALE_1"]));
65 if(Option::get(
"main",
"~sale_converted_15",
'N') <>
'Y')
67 $result->addError(
new Error($message[
"CC_BSC1_CONVERT_SALE"]));
70 if(Option::get(
"sale",
"allow_deduction_on_delivery",
"N") ==
'Y')
72 $result->addError(
new Error($message[
"CC_BSC1_SALE_ALLOW_DEDUCTION_ON_DELIVERY_ERROR"]));
75 self::$settings = $result;
78 return self::$settings;
96 foreach ($list as $k=>$items)
98 if(array_key_exists(self::DELIVERY_SERVICE_XMLID, $items))
118 $documentOrder = $this->getDocumentByTypeId(OneC\DocumentType::ORDER, $documents);
122 $agentFieldValue = $documentOrder->getFieldValues();
123 if(is_array($agentFieldValue[
'AGENT']))
126 $documentProfile->setFields($agentFieldValue[
'AGENT']);
127 $documents[] = $documentProfile;
131 foreach($documents as $document)
136 if($result->isSuccess())
139 if($result->isSuccess())
141 $result->setData($list);
158 case OneC\DocumentType::ORDER:
159 $entityTypeId = static::getParentEntityTypeId();
161 case OneC\DocumentType::PAYMENT_CARD_TRANSACTION:
162 $entityTypeId = static::getPaymentCardEntityTypeId();
164 case OneC\DocumentType::PAYMENT_CASH:
165 $entityTypeId = static::getPaymentCashEntityTypeId();
167 case OneC\DocumentType::PAYMENT_CASH_LESS:
168 $entityTypeId = static::getPaymentCashLessEntityTypeId();
170 case OneC\DocumentType::SHIPMENT:
171 $entityTypeId = static::getShipmentEntityTypeId();
173 case OneC\DocumentType::USER_PROFILE:
174 case OneC\DocumentType::PROFILE:
175 $entityTypeId = $typeId;
178 return $entityTypeId;
189 $parentEntityId =
null;
191 $item = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
194 $params = $item->getFieldValues();
199 $parentEntityId =
$fields[
'ID'];
201 elseif(
$fields[$item::getFieldExternalId()]<>
'')
203 $parentEntityId =
$fields[$item::getFieldExternalId()];
207 if(empty($parentEntityId))
208 $result->addErrors(array(
new Error(
'Order not found')));
210 foreach($items as $item)
212 if($item->getOwnerTypeId() <> static::getParentEntityTypeId())
214 $params = $item->getFieldValues();
217 if(!empty($parentEntityId) &&
$fields[
'ORDER_ID']<>
'')
219 if($parentEntityId <>
$fields[
'ORDER_ID'])
221 $result->addErrors(array(
new Error(
'Order not found')));
228 if($result->isSuccess())
229 $result = parent::checkFields($items);
236 parent::configuration();
238 static::setConfig(static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT);
257 foreach ($items as $item)
265 foreach ($items as $item)
267 if($item->getOwnerTypeId() == static::getParentEntityTypeId())
273 foreach ($items as $item)
275 if($item->getOwnerTypeId() <> static::getParentEntityTypeId() && $item->getOwnerTypeId() <>
EntityType::USER_PROFILE)
289 protected function import(array $items)
297 if($itemParent->getEntityId()>0)
302 $this->save($itemParent, $items);
306 $r = $this->onBeforeEntityModify($itemParent, $items);
307 if($r->hasWarnings())
308 $this->marker($itemParent, $r);
314 foreach($items as $item)
320 if($itemParent->getEntityId() ==
null)
322 $params = $item->getFieldValues();
325 $personalTypeId = $params[
'TRAITS'][
'PERSON_TYPE_ID'] = $item->resolvePersonTypeId(
$fields);
327 $property = $params[
'ORDER_PROPS'];
328 if(!empty($property))
330 $params[
'ORDER_PROP'] = $item->getPropertyOrdersByConfig($personalTypeId, array(), $property);
333 unset($params[
'ORDER_PROPS']);
334 $item->setFields($params);
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"));
343 if(!$this->importableItems($item))
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"));
356 $paramsOrder = $itemParent->getFieldValues();
357 $fieldsOrder = &$paramsOrder[
'TRAITS'];
359 if(!empty($property))
361 $fieldsOrder[
'ORDER_PROP'] = $params[
'ORDER_PROP'];
364 $fieldsOrder[
'USER_ID'] = $item->getId();
365 $fieldsOrder[
'PERSON_TYPE_ID'] = $personalTypeId;
366 $itemParent->setFields($paramsOrder);
371 elseif($item->getOwnerTypeId() == static::getParentEntityTypeId())
373 if(!$this->importableItems($itemParent))
383 $order = $itemParent->getEntity();
384 $params = $item->getFieldValues();
388 if(!$r->hasWarnings())
392 $r = $this->checkParentById(
$fields[
'ID'], $item);
393 if(!$r->hasWarnings())
395 $isShipped =
$order->isShipped();
401 if($item->getOwnerTypeId() == static::getShipmentEntityTypeId())
403 if(!$isShipped &&
$order->isShipped())
404 $this->onAfterShipmentModifyChangeStatusOnDelivery($itemParent);
413 $result->addErrors($r->getErrors());
416 elseif($r->hasWarnings())
418 $result->addWarnings($r->getWarnings());
423 if($result->isSuccess() && !$result->hasWarnings() && !$this->hasCollisionErrors($items))
425 $r = $this->onAfterEntitiesModify($itemParent, $items);
427 $result->addErrors($r->getErrors());
428 if($r->hasWarnings())
429 $result->addWarnings($r->getWarnings());
433 if($result->isSuccess())
435 $r = $this->
save($itemParent, $items);
437 $result->addErrors($r->getErrors());
438 if($r->hasWarnings())
439 $result->addWarnings($r->getWarnings());
449 private function importableItems($item)
451 if($item->getId() ==
null && !$item->isImportable())
453 switch ($item->getOwnerTypeId())
455 case static::getParentEntityTypeId():
456 case EntityType::USER_PROFILE:
511 $item = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);
514 $params = $item->getFieldValues();
541 $shipmentItems = array();
544 $order = $orderImport->getEntity();
548 foreach($items as $item)
550 switch($item->getOwnerTypeId())
552 case static::getShipmentEntityTypeId():
553 $params = $item->getFieldValues();
557 $shipmentItems[] =
$fields[
'ID'];
563 $shipmentList = array();
564 $shipmentCollection =
$order->getShipmentCollection();
565 foreach($shipmentCollection as $shipment)
567 if($shipment->isSystem())
570 if(!in_array($shipment->getId(), $shipmentItems))
571 $shipmentList[$shipment->getId()] = $shipment;
574 if(!empty($shipmentList))
576 foreach($shipmentList as $id=>$shipment)
582 $item = $this->entityFactoryCreate($typeId);
584 static::load($item, array(
'ID'=>$id),
$order);
585 $collision = $item->getLoadedCollision();
587 $collision->resolve($item);
588 if(!$item->hasCollisionErrors())
590 $result = $item->delete();
597 $collisions = $item->getCollisions();
598 $item->markedEntityCollisions($collisions);
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);
634 $paymentItems = array();
637 $order = $orderImport->getEntity();
641 foreach($items as $item)
643 switch($item->getOwnerTypeId())
645 case static::getPaymentCardEntityTypeId():
648 $params = $item->getFieldValues();
652 $paymentItems[] =
$fields[
'ID'];
658 $paymentList = array();
659 $paymentCollection =
$order->getPaymentCollection();
660 foreach($paymentCollection as $payment)
662 if(!in_array($payment->getId(), $paymentItems))
663 $paymentList[$payment->getId()] = $payment;
666 if(!empty($paymentList))
668 foreach($paymentList as $id=>$payment)
670 $result = $this->paymentDelete($payment);
671 if(!$result->isSuccess())
679 protected function paymentDelete(Payment $payment)
681 $result =
new Result();
685 $order = $payment->getCollection()->getOrder();
688 $item = $this->entityFactoryCreate($typeId);
690 static::load($item, array(
'ID'=>$payment->getId()),
$order);
691 $collision = $item->getLoadedCollision();
693 $collision->resolve($item);
694 if(!$item->hasCollisionErrors())
696 $result = $item->delete();
703 $collisions = $item->getCollisions();
704 $item->markedEntityCollisions($collisions);
718 protected function onBeforeBasketModify(Exchange\Entity\OrderImport $orderImport, $items)
720 $basketItems = array();
723 $order = $orderImport->getEntity();
725 throw new ArgumentNullException(
'Order');
727 $basket =
$order->getBasket();
729 foreach($items as $item)
731 switch($item->getOwnerTypeId())
733 case static::getParentEntityTypeId():
734 $params = $item->getFieldValues();
735 $basketItems = $params[
'ITEMS'];
740 $basketItemsIndex = $orderImport->calculateDeltaQuantity($basket, $basketItems);
742 $result = $orderImport->synchronizeQuantityBasketItems($basketItemsIndex);
754 protected function onBeforeEntityModify(Exchange\Entity\OrderImport $orderImport, array $items)
756 $result =
new Result();
757 $paymentResult =
new Result();
764 $basketItemsResult = $this->onBeforeBasketModify($orderImport, $items);
766 if(static::$config & static::DELETE_IF_NOT_FOUND_RELATED_PAYMENT_DOCUMENT)
768 $paymentResult = $this->onBeforePaymentCollectionModify($orderImport, $items);
769 if(!$paymentResult->isSuccess())
770 $result->addWarnings($paymentResult->getErrors());
773 $shipmentResult = $this->onBeforeShipmentCollectionModify($orderImport, $items);
775 if(!$shipmentResult->isSuccess())
776 $result->addWarnings($shipmentResult->getErrors());
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"));
790 protected function onAfterShipmentModifyChangeStatusOnDelivery(Exchange\Entity\OrderImport $orderImport)
792 $result =
new Result();
795 $order = $orderImport->getEntity();
799 $settings = $orderImport->getSettings();
800 $status = $settings->finalStatusOnDeliveryFor($orderImport->getOwnerTypeId());
802 $order->setField(
"STATUS_ID", $status);
813 protected function onAfterEntitiesModify(Exchange\Entity\OrderImport $orderImport, $items)
815 $result =
new Result();
817 foreach ($items as $item)
819 if($item->getOwnerTypeId() == static::getParentEntityTypeId())
822 $order = $orderImport->getEntity();
823 $params = $item->getFieldValues();
826 if(
$fields[
'1C_PAYED_DATE'] instanceof DateTime)
835 if(
$fields[
'1C_DELIVERY_DATE'] instanceof DateTime)
853 protected function checkParentById($id, Entity\EntityImport $item)
855 $result =
new Result();
858 $entity = $item->getEntity();
860 if(!empty($id) && empty($entity))
862 switch($item->getOwnerTypeId())
864 case static::getPaymentCashEntityTypeId():
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"));
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"));
888 $params = $itemOrder->getFieldValues();
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"));
904 $order = $orderImport->getEntity();
906 $orderImport->setField(
'MARKED',
'Y');
915 protected function save(Exchange\
Entity\OrderImport $orderImport, $items)
917 foreach ($items as $item)
919 if($item instanceof Exchange\
Entity\EntityImport)
921 $collisions = $item->getCollisions();
922 if (!empty($collisions))
925 $item->markedEntityCollisions($collisions);
929 return $orderImport->save();
938 foreach($items as $item)
940 if($item->hasCollisionErrors())
955 $result->addError(
new Error(
'',
'CASH_BOX_CHECK_IGNORE'));
964 protected function logger(array $items)
967 $itemParent = $this->getEntityByTypeId(static::getParentEntityTypeId(), $items);