28 parent::__construct($parentEntityContext);
36 return Exchange\EntityType::SHIPMENT;
59 if(intval(
$fields[
'ORDER_ID'])<=0 &&
63 $result->addError(
new Error(
'ORDER_ID is not defined',
''));
72 public function save()
83 public function add(array $params)
89 $result->addError(
new Error(GetMessage(
'SALE_EXCHANGE_ENTITY_SHIPMENT_ORDER_IS_NOT_LOADED_ERROR'),
'ENTITY_SHIPMENT_ORDER_IS_NOT_LOADED_ERROR'));
96 if(($shipmentService = Manager::getObjectById(
$fields[
'DELIVERY_ID'])) ==
null)
98 $result->addError(
new Error(GetMessage(
'SALE_EXCHANGE_ENTITY_SHIPMENT_DELIVERY_SERVICE_IS_NOT_AVAILABLE_ERROR'),
'DELIVERY_SERVICE_IS_NOT_AVAILABLE_ERROR'));
102 $shipmentCollection =
$parentEntity->getShipmentCollection();
103 $shipment = $shipmentCollection->createItem($shipmentService);
105 $shipment->setField(
'DELIVERY_NAME', $shipmentService->getName());
108 $result = $this->fillShipmentItems($shipment, $basket, $params);
109 if(!$result->isSuccess())
114 $result = $shipment->setFields(
$fields);
116 if($result->isSuccess())
129 public function update(array $params)
135 $result->addError(
new Error(GetMessage(
'SALE_EXCHANGE_ENTITY_SHIPMENT_ORDER_IS_NOT_LOADED_ERROR'),
'ORDER_IS_NOT_LOADED_ERROR'));
144 $criterion = $this->getCurrentCriterion($this->
getEntity());
147 if($criterion->equals(
$fields))
150 $result = $this->fillShipmentItems($shipment, $basket, $params);
151 if(!$result->isSuccess())
157 $result = $shipment->setFields(
$fields);
167 public function delete(array $params =
null)
172 if($result->isSuccess())
189 return 'EXTERNAL_DELIVERY';
209 $this->loadParentEntity([
'ID'=>
$fields[
'ORDER_ID']])
223 if(!empty($shipment) && !$shipment->isSystem())
232 return new Sale\Result();
241 private function getBasketItemQuantity(Order $order,
Sale\BasketItem $basketItem)
245 foreach ($order->getShipmentCollection() as $shipment)
247 if($shipment->isShipped())
250 $allQuantity += $shipment->getBasketItemQuantity($basketItem);
263 return OrderImport::getBasketItemByItem($basket, $item);
278 $order = $basket->getOrder();
280 $fieldsBasketItems = $params[
'ITEMS'];
282 if(is_array($fieldsBasketItems))
284 foreach($fieldsBasketItems as $items)
286 foreach($items as $productXML_ID => $item)
295 $basketItemQuantity = $this->getBasketItemQuantity($order, $basketItem);
297 $shipmentItem = static::getShipmentItem($shipment, $basketItem);
299 $deltaQuantity = $item[
'QUANTITY'] - $shipmentItem->getQuantity();
301 if($deltaQuantity < 0)
303 $result = $this->fillShipmentItem($shipmentItem, 0, abs($deltaQuantity));
305 elseif($deltaQuantity > 0)
307 if($basketItemQuantity >= $item[
'QUANTITY'])
309 $systemShipment = $order->getShipmentCollection()->getSystemShipment();
310 $systemBasketQuantity = $systemShipment->getBasketItemQuantity($basketItem);
312 if($systemBasketQuantity >= $deltaQuantity)
314 $this->fillShipmentItem($shipmentItem, $item[
'QUANTITY'], $shipmentItem->getQuantity());
318 $needQuantity = $deltaQuantity - $systemBasketQuantity;
320 $r = $this->synchronizeQuantityShipmentItems($basketItem, $needQuantity);
323 $this->fillShipmentItem($shipmentItem, $item[
'QUANTITY'], $shipmentItem->getQuantity());
338 isset($item[
'MARKINGS'])
339 && is_array($item[
'MARKINGS'])
340 && count($item[
'MARKINGS']) > 0
343 $result = $this->fillMarkingsShipmentItem($shipmentItem, $item[
'MARKINGS']);
367 private static function getShipmentItem(
Sale\Shipment $shipment,
Sale\BasketItem $basketItem)
370 if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
372 throw new Main\ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
375 $shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode());
376 if (empty($shipmentItem))
378 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
380 return $shipmentItem;
383 protected function fillMarkingsShipmentItem(
Sale\ShipmentItem $item, $markings)
385 $result =
new Sale\Result();
387 $itemStoreCollection = $item->getShipmentItemStoreCollection();
388 if (!$itemStoreCollection)
393 $this->resetMarkingsShipmentItem($item);
395 $delta = min(count($markings), $item->getQuantity());
397 if ($itemStoreCollection->count() < $delta)
399 for ($i = (count($markings) - $itemStoreCollection->count()); $i > 0; $i--)
401 $itemStore = $itemStoreCollection->createItem($itemStoreCollection->getShipmentItem()->getBasketItem());
402 $r = $itemStore->setFields([
406 if($r->isSuccess() ===
false)
408 $result->addErrors($r->getErrors());
414 if ($result->isSuccess())
418 foreach ($itemStoreCollection as $storeItem)
420 $r = $storeItem->setField(
'MARKING_CODE', $markings[$k++]);
421 if ($r->isSuccess() ===
false)
423 $result->addErrors($r->getErrors());
432 protected function resetMarkingsShipmentItem(
Sale\ShipmentItem $item)
434 $itemStoreCollection = $item->getShipmentItemStoreCollection();
435 if ($itemStoreCollection)
438 foreach ($itemStoreCollection as $barcode)
440 $barcode->setField(
'MARKING_CODE',
null);
445 private function syncRelationBarcodeMarkingsCode(
Sale\ShipmentItem $shipmentItem, $value)
447 if ($shipmentItem->getBasketItem()->isSupportedMarkingCode())
449 $after = $shipmentItem->getQuantity() + $value;
450 if ($after < $shipmentItem->getQuantity())
452 $deltaQuantity = $shipmentItem->getQuantity() - $after;
454 $storeCollection = $shipmentItem->getShipmentItemStoreCollection();
455 if ($storeCollection)
458 foreach ($storeCollection as $store)
460 if ($deltaQuantity > 0)
478 private function fillShipmentItem(
Sale\ShipmentItem $shipmentItem, $value, $oldValue)
480 $result =
new Sale\Result();
482 $deltaQuantity = $value - $oldValue;
484 if($shipmentItem->getQuantity() + $deltaQuantity == 0)
486 $r = $shipmentItem->delete();
490 $this->syncRelationBarcodeMarkingsCode($shipmentItem, $deltaQuantity);
492 $r = $shipmentItem->setField(
494 $shipmentItem->getQuantity() + $deltaQuantity
499 $shipmentItemCollection = $shipmentItem->getCollection();
502 if ($shipment = $shipmentItemCollection->getShipment())
506 $result->addErrors($r->getErrors());
532 public function synchronizeQuantityShipmentItems(
Sale\BasketItem $basketItem, $needQuantity)
534 $result =
new Sale\Result();
536 if(intval($needQuantity) <= 0)
545 $shipmentCollection = $order->getShipmentCollection();
548 foreach ($shipmentCollection as $shipment)
554 if($shipment->isShipped() || $shipment->isSystem())
557 $basketQuantity = $shipment->getBasketItemQuantity($basketItem);
558 if(empty($basketQuantity))
561 $shipmentItem = static::getShipmentItem($shipment, $basketItem);
563 if($basketQuantity >= $needQuantity)
565 $this->fillShipmentItem($shipmentItem, 0, $needQuantity);
570 $this->fillShipmentItem($shipmentItem, 0, $basketQuantity);
571 $needQuantity -= $basketQuantity;
576 if($needQuantity == 0)
580 if($needQuantity != 0)
581 $result->addError(
new Error(GetMessage(
'SALE_EXCHANGE_ENTITY_SHIPMENT_SYNCHRONIZE_QUANTITY_ERROR'),
'SYNCHRONIZE_QUANTITY_ERROR'));
593 foreach(
$fields[
"ITEMS"] as $items)
595 foreach($items as $item)
615 $item = static::getFieldsDeliveryService(
$fields);
619 "CUSTOM_PRICE_DELIVERY" =>
"Y",
620 "BASE_PRICE_DELIVERY" => $item[
"PRICE"],
621 "CURRENCY" => $this->settings->getCurrency()
638 $entity->setField(
'DEDUCTED',
'N');
649 if(!($shipment instanceof
Shipment))
652 return Exchange\EntityType::SHIPMENT;
660 'ITEMS' => $this->getFieldsItems(),
661 'STORIES' => $this->getFieldsStories()
672 return OrderImport::getAttributesItem($basket);
678 protected function getFieldsItems()
684 $order = $shipment->getParentOrder();
686 foreach ($order->getBasket() as $basket)
689 $shipmentItem = $shipment->getShipmentItemCollection()
690 ->getItemByBasketCode($basket->getBasketCode());
692 if($shipmentItem !==
null)
694 $itemFields = $basket->getFieldValues();
695 $itemFields[
'QUANTITY'] = $shipmentItem->getQuantity();
697 $attributes = array();
699 if(count($attributeFields)>0)
700 $attributes[
'ATTRIBUTES'] = $attributeFields;
702 $result[] = array_merge($itemFields, $attributes);
713 protected function getFieldsStories()
717 if(
$entity instanceof Shipment)
719 $shipmentItemCollection =
$entity->getShipmentItemCollection();
720 if($shipmentItemCollection->count()>0)
723 foreach ($shipmentItemCollection as $shipmentItem)
725 $shipmentItemStoreCollection = $shipmentItem->getShipmentItemStoreCollection();
726 if ($shipmentItemStoreCollection && $shipmentItemStoreCollection->count() > 0)
729 foreach ($shipmentItemStoreCollection as $shipmentItemStore)
731 $result[] = array(
'ID'=>$shipmentItemStore->getStoreId());
746 if(!(
$entity instanceof Shipment))
747 throw new Main\ArgumentException(
"entity must be instanceof Shipment");
750 $collection =
$entity->getCollection();
752 return $collection->getOrder();