41 public function resetCollection(
Basket $basket)
48 if (!empty($this->collection))
51 foreach ($this->collection as $shipmentItem)
53 $shipmentItem->setFieldNoDemand(
'QUANTITY', 0);
54 $shipmentItem->delete();
61 $shipmentCollection = $this->
getShipment()->getCollection();
64 foreach ($basket as $basketItem)
66 $quantityList[$basketItem->getBasketCode()] = $shipmentCollection->getBasketItemDistributedQuantity($basketItem);
70 $itemClassName = static::getItemCollectionClassName();
73 foreach ($basket as $basketItem)
75 $shipmentItem = $itemClassName::create($this, $basketItem);
76 $this->addItem($shipmentItem);
78 $basketItemQuantity = 0;
80 if (array_key_exists($basketItem->getBasketCode(), $quantityList))
82 $basketItemQuantity = $quantityList[$basketItem->getBasketCode()];
85 $quantity = $basketItem->getQuantity() - $basketItemQuantity;
87 $shipmentItem->setFieldNoDemand(
"QUANTITY", $quantity);
89 if ($basketItem->isBundleParent())
91 $this->addBundleToCollection($basketItem);
105 public function createItem(BasketItem $basketItem)
113 if ($shipmentItem !==
null)
115 return $shipmentItem;
119 $itemClassName = static::getItemCollectionClassName();
121 $shipmentItem = $itemClassName::create($this, $basketItem);
123 $shipmentItem->setCollection($this);
124 $this->addItem($shipmentItem);
128 if ($basketItem->isBundleParent() && !
$shipment->isSystem())
130 $this->addBundleToCollection($basketItem);
133 return $shipmentItem;
144 private function addBundleToCollection(BasketItem $basketItem)
146 $result =
new Result();
149 if (!$bundleCollection = $basketItem->getBundleCollection())
151 throw new Main\ObjectNotFoundException(
'Entity "BundleCollection" not found');
154 if ($bundleCollection->getOrder() ===
null)
157 if ($basketCollection = $basketItem->getCollection())
159 if ($order = $basketCollection->getOrder())
161 $bundleCollection->setOrder($order);
170 if (!$shipmentCollection =
$shipment->getCollection())
172 throw new Main\ObjectNotFoundException(
'Entity "ShipmentCollection" not found');
176 if (!$systemShipment = $shipmentCollection->getSystemShipment())
178 throw new Main\ObjectNotFoundException(
'Entity "System Shipment" not found');
182 if (!$systemShipmentItemCollection = $systemShipment->getShipmentItemCollection())
184 throw new Main\ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
187 $baseQuantity = $basketItem->getQuantity();
190 if ($systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
192 $baseQuantity = $systemShipmentItem->getQuantity();
195 $bundleBaseQuantity = $basketItem->getBundleBaseQuantity();
198 foreach ($bundleCollection as $bundleBasketItem)
201 if ($this->isExistsBasketItem($bundleBasketItem))
206 $bundleProductId = $bundleBasketItem->getProductId();
208 if (!isset($bundleBaseQuantity[$bundleProductId]))
209 throw new Main\ArgumentOutOfRangeException(
"bundle product id");
211 $quantity = $bundleBaseQuantity[$bundleProductId] * $baseQuantity;
217 $itemClassName = static::getItemCollectionClassName();
218 $shipmentItemBundle = $itemClassName::create($this, $bundleBasketItem);
219 $this->addItem($shipmentItemBundle);
225 $shipmentItemBundle->setFieldNoDemand(
'QUANTITY', $quantity);
229 $r = $shipmentItemBundle->setQuantity($quantity);
230 if (!$r->isSuccess())
232 $result->addErrors($r->getErrors());
245 protected function addItem(Internals\CollectableEntity $shipmentItem)
247 parent::addItem($shipmentItem);
249 $this->shipmentItemIndexMap[$shipmentItem->getBasketCode()] = $shipmentItem->getInternalIndex();
258 $index = parent::createIndex();
260 return $shipment->getInternalIndex().
"_".$index;
273 $oldShipmentItem = parent::deleteItem($index);
275 unset($this->shipmentItemIndexMap[$oldShipmentItem->getBasketCode()]);
303 isset($this->shipmentItemIndexMap[$itemCode])
304 && isset($this->collection[$this->shipmentItemIndexMap[$itemCode]])
307 return $this->collection[$this->shipmentItemIndexMap[$itemCode]];
317 public function getItemByBasketId($itemId)
319 $itemId = (int)$itemId;
320 foreach ($this->collection as $shippedItem)
323 $shippedItemId = (int)($shippedItem->getBasketId());
324 if ($itemId === $shippedItemId)
340 return $shipmentItem->isShippable();
353 $basketItem = $shipmentItem->getBasketItem();
355 return !$basketItem->isBundleChild();
367 public function getPrice()
373 foreach ($sellableItems as $shipmentItem)
376 if ($basketItem = $shipmentItem->getBasketItem())
389 public function getWeight() : float
396 $basketItem = $shipmentItem->getBasketItem();
398 $weight += $basketItem->getWeight() * $shipmentItem->getQuantity();
419 public function save()
421 $result =
new Main\Entity\Result();
430 if (!$shipmentCollection =
$shipment->getCollection())
436 if (!$order = $shipmentCollection->getOrder())
438 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
441 $itemsFromDb = array();
444 $itemsFromDbList = static::getList(
446 "filter" => array(
"ORDER_DELIVERY_ID" => $this->
getShipment()->getId()),
447 "select" => array(
"ID",
'BASKET_ID')
450 while ($itemsFromDbItem = $itemsFromDbList->fetch())
451 $itemsFromDb[$itemsFromDbItem[
"ID"]] = $itemsFromDbItem;
456 foreach ($this->collection as $shipmentItem)
459 if (!$basketItem = $shipmentItem->getBasketItem())
464 if ($basketItem->isBundleParent())
466 $this->addBundleToCollection($basketItem);
473 throw new Main\ObjectNotFoundException(
'Entity "Shipment" not found');
476 $changeMeaningfulFields = array(
482 foreach ($this->collection as $shipmentItem)
485 if (!$basketItem = $shipmentItem->getBasketItem())
490 $isNew = (bool)($shipmentItem->getId() <= 0);
496 "BASKET_ID" => $basketItem->getId(),
497 "BASKET_ITEM_NAME" => $basketItem->getField(
"NAME"),
498 "BASKET_ITEM_PRODUCT_ID" => $basketItem->getField(
"PRODUCT_ID"),
499 "ORDER_DELIVERY_ID" => $shipmentItem->getField(
"ORDER_DELIVERY_ID"),
502 $fields = $shipmentItem->getFields();
503 $originalValues = $fields->getOriginalValues();
505 foreach($originalValues as $originalFieldName => $originalFieldValue)
507 if (in_array($originalFieldName, $changeMeaningfulFields) && $shipmentItem->getField($originalFieldName) != $originalFieldValue)
509 $logFields[$originalFieldName] = $shipmentItem->getField($originalFieldName);
511 $logFields[
'OLD_'.$originalFieldName] = $originalFieldValue;
516 $r = $shipmentItem->save();
519 if ($order->getId() > 0 && $isChanged)
524 $orderHistory = $registry->getOrderHistoryClassName();
525 $orderHistory::addLog(
528 $isNew ?
'SHIPMENT_ITEM_ADD' :
'SHIPMENT_ITEM_UPDATE',
529 $shipmentItem->getId(),
532 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
538 $result->addErrors($r->getErrors());
541 if (isset($itemsFromDb[$shipmentItem->getId()]))
543 unset($itemsFromDb[$shipmentItem->getId()]);
548 if (!$basket = $order->getBasket())
550 throw new Main\ObjectNotFoundException(
'Entity "Basket" not found');
553 foreach ($itemsFromDb as $k => $v)
555 $v[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
558 $event =
new Main\Event(
'sale',
"OnBeforeSaleShipmentItemDeleted", array(
563 static::deleteInternal($k);
566 $event =
new Main\Event(
'sale',
"OnSaleShipmentItemDeleted", array(
572 if ($basketItem = $basket->getItemById($k))
577 $orderHistory = $registry->getOrderHistoryClassName();
578 $orderHistory::addAction(
581 'SHIPMENT_ITEM_BASKET_REMOVED',
585 'NAME' => $basketItem->getField(
'NAME'),
586 'QUANTITY' => $basketItem->getQuantity(),
587 'PRODUCT_ID' => $basketItem->getProductId(),
593 if ($order->getId() > 0)
598 $orderHistory = $registry->getOrderHistoryClassName();
599 $orderHistory::collectEntityFields(
'SHIPMENT_ITEM', $order->getId());
608 private static function createShipmentItemCollectionObject()
611 $shipmentItemCollectionClassName = $registry->getShipmentItemCollectionClassName();
613 return new $shipmentItemCollectionClassName();
627 private static function getItemCollectionClassName()
630 return $registry->getShipmentItemClassName();
640 public static function load(Shipment
$shipment)
643 $shipmentItemCollection = static::createShipmentItemCollectionObject();
644 $shipmentItemCollection->shipment =
$shipment;
650 if (!$shipmentCollection =
$shipment->getCollection())
656 if (!$order = $shipmentCollection->getOrder())
658 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
662 $itemClassName = static::getItemCollectionClassName();
663 $shipmentItemList = $itemClassName::loadForShipment(
$shipment->getId());
666 foreach ($shipmentItemList as $shipmentItem)
668 $shipmentItem->setCollection($shipmentItemCollection);
669 $shipmentItemCollection->addItem($shipmentItem);
671 if (!$basketItem = $shipmentItem->getBasketItem())
673 $msg =
Loc::getMessage(
"SALE_SHIPMENT_ITEM_COLLECTION_BASKET_ITEM_NOT_FOUND", array(
674 '#BASKET_ITEM_ID#' => $shipmentItem->getBasketId(),
676 '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
680 $r->addError(
new ResultError($msg,
'SALE_SHIPMENT_ITEM_COLLECTION_BASKET_ITEM_NOT_FOUND'));
685 $entityMarker = $registry->getEntityMarkerClassName();
686 $entityMarker::addMarker($order,
$shipment, $r);
695 return $shipmentItemCollection;
703 public static function getList(array $parameters = array())
705 return Internals\ShipmentItemTable::getList($parameters);
714 return Internals\ShipmentItemTable::deleteWithItems($primary);
730 $r = $this->deleteByBasketItem($basketItem);
731 if (!$r->isSuccess())
733 $result->addErrors($r->getErrors());
758 $shipmentItem = $this->createItem($basketItem);
761 $shipmentItem->onBasketModify($action, $basketItem, $name, $oldValue, $value);
772 $shipmentItem = $this->createItem($basketItem);
775 $r = $shipmentItem->setField(
'QUANTITY', $value);
776 if (!$r->isSuccess())
778 return $result->addErrors($r->getErrors());
790 protected function isExistsBasketItem(
BasketItem $basketItem)
793 foreach ($this->collection as $shipmentItem)
795 if ($shipmentItem->getBasketCode() == $basketItem->
getBasketCode())
811 public function deleteByBasketItem(BasketItem $basketItem)
813 $result =
new Result();
814 $systemShipmentItem =
null;
819 throw new Main\ObjectNotFoundException(
'Entity "Shipment" not found');
823 foreach ($this->collection as $shipmentItem)
825 if ($shipmentItem->getBasketCode() == $basketItem->getBasketCode())
829 $systemShipmentItem = $shipmentItem;
833 $r = $shipmentItem->delete();
834 if (!$r->isSuccess())
836 $result->addErrors($r->getErrors());
841 if ($systemShipmentItem !==
null)
843 if ($systemShipmentItem->getReservedQuantity() > 0)
846 $r = $systemShipmentItem->tryUnreserve();
847 if (!$r->isSuccess())
849 $result->addErrors($r->getErrors());
853 if ($result->isSuccess())
855 $systemShipmentItem->setFieldNoDemand(
'QUANTITY', 0);
856 $r = $systemShipmentItem->delete();
857 if (!$r->isSuccess())
859 $result->addErrors($r->getErrors());
872 if (
count($this->collection) == 0)
876 foreach ($this->collection as $item)
878 if ($item->getQuantity() > 0)
897 $quantity = $shipmentItem->getQuantity();
909 public function isExistBasketItem(
BasketItem $basketItem)
912 foreach ($this->collection as $shipmentItem)
914 if ($shipmentItem->getBasketCode() == $basketItem->
getBasketCode())
932 public function createClone(\SplObjectStorage $cloneEntity)
934 if ($this->
isClone() && $cloneEntity->contains($this))
936 return $cloneEntity[$this];
940 $shipmentItemCollectionClone = parent::createClone($cloneEntity);
952 $shipmentItemCollectionClone->shipment = $cloneEntity[
$shipment];
956 return $shipmentItemCollectionClone;
964 public function getErrorEntity($value)
969 foreach ($this->collection as $shipmentItem)
971 if ($className = $shipmentItem->getErrorEntity($value))
985 public function canAutoFixError($value)
990 foreach ($this->collection as $shipmentItem)
992 if ($autoFix = $shipmentItem->canAutoFixError($value))