Bitrix-D7  20.0.0
shipmentitemcollection.php
См. документацию.
1 <?php
2 /**
3  * Bitrix Framework
4  * @package bitrix
5  * @subpackage sale
6  * @copyright 2001-2012 Bitrix
7  */
8 namespace Bitrix\Sale;
9 
10 use Bitrix\Main;
13 
14 Loc::loadMessages(__FILE__);
15 
18 {
19  /** @var Shipment */
20  protected $shipment;
21 
22  protected $shipmentItemIndexMap = array();
23 
24  /**
25  * @return Shipment
26  */
27  protected function getEntityParent()
28  {
29  return $this->getShipment();
30  }
31 
32  /**
33  * @param Basket $basket
34  *
35  * @throws Main\ArgumentOutOfRangeException
36  * @throws Main\NotSupportedException
37  * @throws Main\ObjectNotFoundException
38  * @throws \ErrorException
39  */
40  public function resetCollection(Basket $basket)
41  {
42  if ($this->getShipment()->isShipped())
43  throw new Main\NotSupportedException();
44 
45  /** @var Shipment $shipment */
46  if (!$shipment = $this->getShipment())
47  {
48  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
49  }
50 
51  /** @var ShipmentCollection $shipmentCollection */
52  if (!$shipmentCollection = $shipment->getCollection())
53  {
54  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
55  }
56 
57  if (!empty($this->collection))
58  {
59  /** @var ShipmentItem $shipmentItem */
60  foreach ($this->collection as $shipmentItem)
61  {
62  $shipmentItem->setFieldNoDemand('QUANTITY', 0);
63  $shipmentItem->delete();
64  }
65  }
66 
67  $quantityList = array();
68 
69  /** @var BasketItem $basketItem */
70  foreach ($basket as $basketItem)
71  {
72  $quantityList[$basketItem->getBasketCode()] = $shipmentCollection->getBasketItemDistributedQuantity($basketItem);
73  }
74 
75  /** @var ShipmentItem $itemClassName */
76  $itemClassName = static::getItemCollectionClassName();
77 
78  /** @var BasketItem $basketItem */
79  foreach ($basket as $basketItem)
80  {
81  $shipmentItem = $itemClassName::create($this, $basketItem);
82  $this->addItem($shipmentItem);
83 
84  $basketItemQuantity = 0;
85 
86  if (array_key_exists($basketItem->getBasketCode(), $quantityList))
87  {
88  $basketItemQuantity = $quantityList[$basketItem->getBasketCode()];
89  }
90 
91  $quantity = $basketItem->getQuantity() - $basketItemQuantity;
92 
93  $shipmentItem->setFieldNoDemand("QUANTITY", $quantity);
94 
95  if ($basketItem->isBundleParent())
96  {
97  $this->addBundleToCollection($basketItem);
98  }
99 
100  }
101  }
102 
103  /**
104  * @param BasketItem $basketItem
105  * @return ShipmentItem
106  * @throws Main\ArgumentOutOfRangeException
107  */
108  public function createItem(BasketItem $basketItem)
109  {
110  if ($this->getShipment()->isShipped())
111  {
112  return null;
113  }
114 
115  $shipmentItem = $this->getItemByBasketCode($basketItem->getBasketCode());
116  if ($shipmentItem !== null)
117  {
118  return $shipmentItem;
119  }
120 
121  /** @var ShipmentItem $itemClassName */
122  $itemClassName = static::getItemCollectionClassName();
123 
124  $shipmentItem = $itemClassName::create($this, $basketItem);
125 
126  $shipmentItem->setCollection($this);
127  $this->addItem($shipmentItem);
128 
129  $shipment = $this->getShipment();
130 
131  if ($basketItem->isBundleParent() && !$shipment->isSystem())
132  {
133  $this->addBundleToCollection($basketItem);
134  }
135 
136  return $shipmentItem;
137  }
138 
139  /**
140  * @param BasketItem $basketItem
141  *
142  * @return Result
143  * @throws Main\ArgumentOutOfRangeException
144  * @throws Main\ArgumentTypeException
145  * @throws Main\ObjectNotFoundException
146  */
147  private function addBundleToCollection(BasketItem $basketItem)
148  {
149  $result = new Result();
150 
151  /** @var Basket $bundleCollection */
152  if (!$bundleCollection = $basketItem->getBundleCollection())
153  {
154  throw new Main\ObjectNotFoundException('Entity "BundleCollection" not found');
155  }
156 
157  if ($bundleCollection->getOrder() === null)
158  {
159  /** @var Basket $basketCollection */
160  if ($basketCollection = $basketItem->getCollection())
161  {
162  if ($order = $basketCollection->getOrder())
163  {
164  $bundleCollection->setOrder($order);
165  }
166  }
167  }
168 
169  /** @var Shipment $shipment */
170  $shipment = $this->getShipment();
171 
172  /** @var ShipmentCollection $shipmentCollection */
173  if (!$shipmentCollection = $shipment->getCollection())
174  {
175  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
176  }
177 
178  /** @var Shipment $systemShipment */
179  if (!$systemShipment = $shipmentCollection->getSystemShipment())
180  {
181  throw new Main\ObjectNotFoundException('Entity "System Shipment" not found');
182  }
183 
184  /** @var ShipmentItemCollection $systemShipmentItemCollection */
185  if (!$systemShipmentItemCollection = $systemShipment->getShipmentItemCollection())
186  {
187  throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
188  }
189 
190  $baseQuantity = $basketItem->getQuantity();
191 
192  /** @var ShipmentItem $systemShipmentItem */
193  if ($systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))
194  {
195  $baseQuantity = $systemShipmentItem->getQuantity();
196  }
197 
198  $bundleBaseQuantity = $basketItem->getBundleBaseQuantity();
199 
200  /** @var BasketItem $bundleBasketItem */
201  foreach ($bundleCollection as $bundleBasketItem)
202  {
203 
204  if ($this->isExistsBasketItem($bundleBasketItem))
205  {
206  continue;
207  }
208 
209  $bundleProductId = $bundleBasketItem->getProductId();
210 
211  if (!isset($bundleBaseQuantity[$bundleProductId]))
212  throw new Main\ArgumentOutOfRangeException("bundle product id");
213 
214  $quantity = $bundleBaseQuantity[$bundleProductId] * $baseQuantity;
215 
216  if ($quantity == 0)
217  continue;
218 
219  /** @var ShipmentItem $itemClassName */
220  $itemClassName = static::getItemCollectionClassName();
221  $shipmentItemBundle = $itemClassName::create($this, $bundleBasketItem);
222  $this->addItem($shipmentItemBundle);
223 
224 
225 
226  if ($shipment->isSystem())
227  {
228  $shipmentItemBundle->setFieldNoDemand('QUANTITY', $quantity);
229  }
230  else
231  {
232  $r = $shipmentItemBundle->setQuantity($quantity);
233  if (!$r->isSuccess())
234  {
235  $result->addErrors($r->getErrors());
236  }
237  }
238  }
239 
240  return $result;
241  }
242 
243  /**
244  * @param Internals\CollectableEntity $shipmentItem
245  * @return Internals\CollectableEntity|void
246  * @throws Main\NotSupportedException
247  */
248  protected function addItem(Internals\CollectableEntity $shipmentItem)
249  {
250  parent::addItem($shipmentItem);
251 
252  $this->shipmentItemIndexMap[$shipmentItem->getBasketCode()] = $shipmentItem->getInternalIndex();
253 
254  /** @var Shipment $shipment */
255  $shipment = $this->getShipment();
256  $shipment->onShipmentItemCollectionModify(EventActions::ADD, $shipmentItem);
257  }
258 
259  protected function createIndex()
260  {
261  $index = parent::createIndex();
262  $shipment = $this->getShipment();
263  return $shipment->getInternalIndex()."_".$index;
264  }
265 
266  /**
267  * @internal
268  *
269  * @param $index
270  * @return mixed|void
271  * @throws Main\ArgumentOutOfRangeException
272  * @throws Main\NotSupportedException
273  */
274  public function deleteItem($index)
275  {
276  $oldShipmentItem = parent::deleteItem($index);
277 
278  unset($this->shipmentItemIndexMap[$oldShipmentItem->getBasketCode()]);
279 
280  $shipment = $this->getShipment();
281  $shipment->onShipmentItemCollectionModify(EventActions::DELETE, $oldShipmentItem);
282  }
283 
284  /**
285  * @param Internals\CollectableEntity $item
286  * @param null $name
287  * @param null $oldValue
288  * @param null $value
289  * @return Result
290  * @throws Main\NotSupportedException
291  * @throws Main\SystemException
292  */
293  public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
294  {
295  $shipment = $this->getShipment();
296  return $shipment->onShipmentItemCollectionModify(EventActions::UPDATE, $item, $name, $oldValue, $value);
297  }
298 
299  /**
300  * @param $itemCode
301  * @return ShipmentItem|null
302  */
303  public function getItemByBasketCode($itemCode)
304  {
305  if (
306  isset($this->shipmentItemIndexMap[$itemCode])
307  && isset($this->collection[$this->shipmentItemIndexMap[$itemCode]])
308  )
309  {
310  return $this->collection[$this->shipmentItemIndexMap[$itemCode]];
311  }
312 
313  return null;
314  }
315 
316  /**
317  * @param $itemId
318  * @return ShipmentItem|null
319  */
320  public function getItemByBasketId($itemId)
321  {
322  $itemId = (int)$itemId;
323  foreach ($this->collection as $shippedItem)
324  {
325  /** @var ShipmentItem $shippedItem */
326  $shippedItemId = (int)($shippedItem->getBasketId());
327  if ($itemId === $shippedItemId)
328  return $shippedItem;
329  }
330 
331  return null;
332  }
333 
334  /**
335  * @return Internals\CollectionFilterIterator
336  */
337  public function getShippableItems()
338  {
339  $callback = function (ShipmentItem $shipmentItem)
340  {
341  $basketItem = $shipmentItem->getBasketItem();
342  if ($basketItem)
343  return !$basketItem->isBundleParent();
344 
345  return true;
346  };
347 
348  return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
349  }
350 
351  /**
352  * @return Internals\CollectionFilterIterator
353  */
354  public function getSellableItems()
355  {
356  $callback = function (ShipmentItem $shipmentItem)
357  {
358  $basketItem = $shipmentItem->getBasketItem();
359  if ($basketItem)
360  return !$basketItem->isBundleChild();
361 
362  return true;
363  };
364 
365  return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
366  }
367 
368  /**
369  * @return float|int
370  * @throws Main\ArgumentNullException
371  */
372  public function getPrice()
373  {
374  $price = 0;
375  $sellableItems = $this->getSellableItems();
376  /** @var ShipmentItem $shipmentItem */
377  foreach ($sellableItems as $shipmentItem)
378  {
379  /** @var BasketItem $basketItem */
380  if ($basketItem = $shipmentItem->getBasketItem())
381  {
382  $price += $basketItem->getPrice() * $shipmentItem->getQuantity();
383  }
384  }
385 
386  return $price;
387  }
388 
389  /**
390  * @return float
391  * @throws Main\ArgumentNullException
392  */
393  public function getWeight() : float
394  {
395  $weight = 0;
396 
397  /** @var ShipmentItem $shipmentItem */
398  foreach ($this->getShippableItems() as $shipmentItem)
399  {
400  $basketItem = $shipmentItem->getBasketItem();
401 
402  $weight += $basketItem->getWeight() * $shipmentItem->getQuantity();
403  }
404 
405  return $weight;
406  }
407 
408  /**
409  * @return Shipment
410  */
411  public function getShipment()
412  {
413  return $this->shipment;
414  }
415 
416  /**
417  * @return Main\Entity\Result
418  * @throws Main\ArgumentException
419  * @throws Main\ArgumentNullException
420  * @throws Main\ArgumentOutOfRangeException
421  * @throws Main\ObjectNotFoundException
422  */
423  public function save()
424  {
425  $result = new Main\Entity\Result();
426 
427  /** @var Shipment $shipment */
428  if (!$shipment = $this->getShipment())
429  {
430  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
431  }
432 
433  /** @var ShipmentCollection $shipmentCollection */
434  if (!$shipmentCollection = $shipment->getCollection())
435  {
436  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
437  }
438 
439  /** @var Order $order */
440  if (!$order = $shipmentCollection->getOrder())
441  {
442  throw new Main\ObjectNotFoundException('Entity "Order" not found');
443  }
444 
445  $itemsFromDb = array();
446  if ($this->getShipment()->getId() > 0)
447  {
448  $itemsFromDbList = static::getList(
449  array(
450  "filter" => array("ORDER_DELIVERY_ID" => $this->getShipment()->getId()),
451  "select" => array("ID", 'BASKET_ID')
452  )
453  );
454  while ($itemsFromDbItem = $itemsFromDbList->fetch())
455  $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
456  }
457 
458 
459  /** @var ShipmentItem $shipmentItem */
460  foreach ($this->collection as $shipmentItem)
461  {
462  /** @var BasketItem $basketItem */
463  if (!$basketItem = $shipmentItem->getBasketItem())
464  {
465  continue;
466  }
467 
468  if ($basketItem->isBundleParent())
469  {
470  $this->addBundleToCollection($basketItem);
471  }
472  }
473 
474  /** @var Shipment $shipment */
475  if (!$shipment = $this->getShipment())
476  {
477  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
478  }
479 
480  $changeMeaningfulFields = array(
481  "QUANTITY",
482  "RESERVED_QUANTITY",
483  );
484 
485  /** @var ShipmentItem $shipmentItem */
486  foreach ($this->collection as $shipmentItem)
487  {
488  /** @var BasketItem $basketItem */
489  if (!$basketItem = $shipmentItem->getBasketItem())
490  {
491  continue;
492  }
493 
494  $isNew = (bool)($shipmentItem->getId() <= 0);
495  $isChanged = $shipmentItem->isChanged();
496 
497  if ($order->getId() > 0 && $isChanged)
498  {
499  $logFields = array(
500  "BASKET_ID" => $basketItem->getId(),
501  "BASKET_ITEM_NAME" => $basketItem->getField("NAME"),
502  "BASKET_ITEM_PRODUCT_ID" => $basketItem->getField("PRODUCT_ID"),
503  "ORDER_DELIVERY_ID" => $shipmentItem->getField("ORDER_DELIVERY_ID"),
504  );
505 
506  $fields = $shipmentItem->getFields();
507  $originalValues = $fields->getOriginalValues();
508 
509  foreach($originalValues as $originalFieldName => $originalFieldValue)
510  {
511  if (in_array($originalFieldName, $changeMeaningfulFields) && $shipmentItem->getField($originalFieldName) != $originalFieldValue)
512  {
513  $logFields[$originalFieldName] = $shipmentItem->getField($originalFieldName);
514  if (!$isNew)
515  $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
516  }
517  }
518  }
519 
520  $r = $shipmentItem->save();
521  if ($r->isSuccess())
522  {
523  if ($order->getId() > 0 && $isChanged)
524  {
525  $registry = Registry::getInstance(static::getRegistryType());
526 
527  /** @var OrderHistory $orderHistory */
528  $orderHistory = $registry->getOrderHistoryClassName();
529  $orderHistory::addLog(
530  'SHIPMENT_ITEM',
531  $order->getId(),
532  $isNew ? 'SHIPMENT_ITEM_ADD' : 'SHIPMENT_ITEM_UPDATE',
533  $shipmentItem->getId(),
534  $shipmentItem,
535  $logFields,
536  $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
537  );
538  }
539  }
540  else
541  {
542  $result->addErrors($r->getErrors());
543  }
544 
545  if (isset($itemsFromDb[$shipmentItem->getId()]))
546  {
547  unset($itemsFromDb[$shipmentItem->getId()]);
548  }
549  }
550 
551  /** @var Basket $basket */
552  if (!$basket = $order->getBasket())
553  {
554  throw new Main\ObjectNotFoundException('Entity "Basket" not found');
555  }
556 
557  foreach ($itemsFromDb as $k => $v)
558  {
559  $v['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
560 
561  /** @var Main\Event $event */
562  $event = new Main\Event('sale', "OnBeforeSaleShipmentItemDeleted", array(
563  'VALUES' => $v,
564  ));
565  $event->send();
566 
567  static::deleteInternal($k);
568 
569  /** @var Main\Event $event */
570  $event = new Main\Event('sale', "OnSaleShipmentItemDeleted", array(
571  'VALUES' => $v,
572  ));
573  $event->send();
574 
575  /** @var BasketItem $basketItem */
576  if ($basketItem = $basket->getItemById($k))
577  {
578  $registry = Registry::getInstance(static::getRegistryType());
579 
580  /** @var OrderHistory $orderHistory */
581  $orderHistory = $registry->getOrderHistoryClassName();
582  $orderHistory::addAction(
583  'SHIPMENT',
584  $order->getId(),
585  'SHIPMENT_ITEM_BASKET_REMOVED',
586  $shipment->getId(),
587  null,
588  array(
589  'NAME' => $basketItem->getField('NAME'),
590  'QUANTITY' => $basketItem->getQuantity(),
591  'PRODUCT_ID' => $basketItem->getProductId(),
592  )
593  );
594  }
595  }
596 
597  if ($order->getId() > 0)
598  {
599  $registry = Registry::getInstance(static::getRegistryType());
600 
601  /** @var OrderHistory $orderHistory */
602  $orderHistory = $registry->getOrderHistoryClassName();
603  $orderHistory::collectEntityFields('SHIPMENT_ITEM', $order->getId());
604  }
605 
606  return $result;
607  }
608 
609  /**
610  * @return ShipmentItemCollection
611  */
612  private static function createShipmentItemCollectionObject()
613  {
614  $registry = Registry::getInstance(static::getRegistryType());
615  $shipmentItemCollectionClassName = $registry->getShipmentItemCollectionClassName();
616 
617  return new $shipmentItemCollectionClassName();
618  }
619 
620  /**
621  * @return string
622  */
623  public static function getRegistryType()
624  {
626  }
627 
628  /**
629  * @return string
630  */
631  private static function getItemCollectionClassName()
632  {
633  $registry = Registry::getInstance(static::getRegistryType());
634  return $registry->getShipmentItemClassName();
635  }
636 
637  /**
638  * @param Shipment $shipment
639  *
640  * @return ShipmentItemCollection
641  * @throws Main\ArgumentNullException
642  * @throws Main\ObjectNotFoundException
643  */
644  public static function load(Shipment $shipment)
645  {
646  /** @var ShipmentItemCollection $shipmentItemCollection */
647  $shipmentItemCollection = static::createShipmentItemCollectionObject();
648  $shipmentItemCollection->shipment = $shipment;
649 
650  if ($shipment->getId() > 0)
651  {
652 
653  /** @var ShipmentCollection $shipmentCollection */
654  if (!$shipmentCollection = $shipment->getCollection())
655  {
656  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
657  }
658 
659  /** @var Order $order */
660  if (!$order = $shipmentCollection->getOrder())
661  {
662  throw new Main\ObjectNotFoundException('Entity "Order" not found');
663  }
664 
665  /** @var ShipmentItem $itemClassName */
666  $itemClassName = static::getItemCollectionClassName();
667  $shipmentItemList = $itemClassName::loadForShipment($shipment->getId());
668 
669  /** @var ShipmentItem $shipmentItem */
670  foreach ($shipmentItemList as $shipmentItem)
671  {
672  $shipmentItem->setCollection($shipmentItemCollection);
673  $shipmentItemCollection->addItem($shipmentItem);
674 
675  if (!$basketItem = $shipmentItem->getBasketItem())
676  {
677  $msg = Loc::getMessage("SALE_SHIPMENT_ITEM_COLLECTION_BASKET_ITEM_NOT_FOUND", array(
678  '#BASKET_ITEM_ID#' => $shipmentItem->getBasketId(),
679  '#SHIPMENT_ID#' => $shipment->getId(),
680  '#SHIPMENT_ITEM_ID#' => $shipmentItem->getId(),
681  ));
682 
683  $r = new Result();
684  $r->addError( new ResultError($msg, 'SALE_SHIPMENT_ITEM_COLLECTION_BASKET_ITEM_NOT_FOUND'));
685 
686 
687  $registry = Registry::getInstance(static::getRegistryType());
688  /** @var EntityMarker $entityMarker */
689  $entityMarker = $registry->getEntityMarkerClassName();
690  $entityMarker::addMarker($order, $shipment, $r);
691  if (!$shipment->isSystem())
692  {
693  $shipment->setField('MARKED', 'Y');
694  }
695  }
696  }
697  }
698 
699  return $shipmentItemCollection;
700  }
701 
702  /**
703  * @param array $parameters
704  * @return Main\DB\Result
705  * @throws Main\ArgumentException
706  */
707  public static function getList(array $parameters = array())
708  {
709  return Internals\ShipmentItemTable::getList($parameters);
710  }
711 
712  /**
713  * @param $primary
714  * @return Main\Entity\DeleteResult
715  */
716  protected function deleteInternal($primary)
717  {
719  }
720 
721  /**
722  * @param BasketItem $basketItem
723  * @return Result
724  * @throws Main\ArgumentNullException
725  * @throws Main\ArgumentOutOfRangeException
726  * @throws Main\NotSupportedException
727  * @throws Main\ObjectNotFoundException
728  * @throws Main\SystemException
729  */
730  public function onBeforeBasketItemDelete(BasketItem $basketItem)
731  {
732  $result = new Result();
733 
734  $r = $this->deleteByBasketItem($basketItem);
735  if (!$r->isSuccess())
736  {
737  $result->addErrors($r->getErrors());
738  }
739 
740  return $result;
741  }
742 
743  /**
744  * @param $action
745  * @param BasketItem $basketItem
746  * @param null $name
747  * @param null $oldValue
748  * @param null $value
749  * @return Result
750  * @throws Main\ArgumentNullException
751  * @throws Main\ArgumentOutOfRangeException
752  * @throws Main\NotSupportedException
753  * @throws Main\ObjectNotFoundException
754  * @throws Main\SystemException
755  */
756  public function onBasketModify($action, BasketItem $basketItem, $name = null, $oldValue = null, $value = null)
757  {
758  $result = new Result();
759 
760  if ($action === EventActions::ADD)
761  {
762  $shipmentItem = $this->createItem($basketItem);
763  if ($shipmentItem)
764  {
765  $shipmentItem->onBasketModify($action, $basketItem, $name, $oldValue, $value);
766  }
767 
768  return $result;
769  }
770  elseif ($action === EventActions::UPDATE)
771  {
772  $shipmentItem = $this->getItemByBasketCode($basketItem->getBasketCode());
773 
774  if (!$shipmentItem)
775  {
776  $shipmentItem = $this->createItem($basketItem);
777  }
778 
779  $shipmentItem->setField('QUANTITY', $value);
780  }
781 
782  return $result;
783  }
784 
785  /**
786  * @param BasketItem $basketItem
787  * @return bool
788  * @throws Main\ObjectNotFoundException
789  */
790  protected function isExistsBasketItem(BasketItem $basketItem)
791  {
792  /** @var ShipmentItem $shipmentItem */
793  foreach ($this->collection as $shipmentItem)
794  {
795  if ($shipmentItem->getBasketCode() == $basketItem->getBasketCode())
796  return true;
797  }
798 
799  return false;
800  }
801 
802  /**
803  * @param BasketItem $basketItem
804  * @return Result
805  * @throws Main\ArgumentNullException
806  * @throws Main\ArgumentOutOfRangeException
807  * @throws Main\NotSupportedException
808  * @throws Main\ObjectNotFoundException
809  * @throws Main\SystemException
810  */
811  public function deleteByBasketItem(BasketItem $basketItem)
812  {
813  $result = new Result();
814  $systemShipmentItem = null;
815 
816  /** @var Shipment $shipment */
817  if (!$shipment = $this->getShipment())
818  {
819  throw new Main\ObjectNotFoundException('Entity "Shipment" not found');
820  }
821 
822  /** @var ShipmentItem $shipmentItem */
823  foreach ($this->collection as $shipmentItem)
824  {
825  if ($shipmentItem->getBasketCode() == $basketItem->getBasketCode())
826  {
827  if ($shipment->isSystem())
828  {
829  $systemShipmentItem = $shipmentItem;
830  continue;
831  }
832 
833  $r = $shipmentItem->delete();
834  if (!$r->isSuccess())
835  {
836  $result->addErrors($r->getErrors());
837  }
838  }
839  }
840 
841  if ($systemShipmentItem !== null)
842  {
843  if ($systemShipmentItem->getReservedQuantity() > 0)
844  {
845  /** @var Result $r */
846  $r = $systemShipmentItem->tryUnreserve();
847  if (!$r->isSuccess())
848  {
849  $result->addErrors($r->getErrors());
850  }
851  }
852 
853  if ($result->isSuccess())
854  {
855  $systemShipmentItem->setFieldNoDemand('QUANTITY', 0);
856  $r = $systemShipmentItem->delete();
857  if (!$r->isSuccess())
858  {
859  $result->addErrors($r->getErrors());
860  }
861  }
862  }
863 
864  return $result;
865  }
866 
867  /**
868  * @return bool
869  */
870  public function isEmpty()
871  {
872  if (count($this->collection) == 0)
873  return true;
874 
875  /** @var ShipmentItem $item */
876  foreach ($this->collection as $item)
877  {
878  if ($item->getQuantity() > 0)
879  return false;
880  }
881 
882  return true;
883  }
884 
885  /**
886  * @param BasketItem $basketItem
887  * @return float|int
888  * @throws Main\ArgumentNullException
889  */
890  public function getBasketItemQuantity(BasketItem $basketItem)
891  {
892  $quantity = 0;
893 
894  $shipmentItem = $this->getItemByBasketCode($basketItem->getBasketCode());
895  if ($shipmentItem)
896  {
897  $quantity = $shipmentItem->getQuantity();
898  }
899 
900  return $quantity;
901  }
902 
903  /**
904  * @param BasketItem $basketItem
905  *
906  * @return bool
907  * @throws Main\ObjectNotFoundException
908  */
909  public function isExistBasketItem(BasketItem $basketItem)
910  {
911  /** @var ShipmentItem $shipmentItem */
912  foreach ($this->collection as $shipmentItem)
913  {
914  if ($shipmentItem->getBasketCode() == $basketItem->getBasketCode())
915  {
916  return true;
917  }
918  }
919 
920  return false;
921  }
922 
923  /**
924  * @internal
925  *
926  * @param \SplObjectStorage $cloneEntity
927  * @return Internals\EntityCollection|object
928  * @throws Main\ArgumentException
929  * @throws Main\ArgumentNullException
930  * @throws Main\ObjectNotFoundException
931  */
932  public function createClone(\SplObjectStorage $cloneEntity)
933  {
934  if ($this->isClone() && $cloneEntity->contains($this))
935  {
936  return $cloneEntity[$this];
937  }
938 
939  /** @var ShipmentItemCollection $shipmentItemCollectionClone */
940  $shipmentItemCollectionClone = parent::createClone($cloneEntity);
941 
942  /** @var Shipment $shipment */
943  if ($shipment = $this->shipment)
944  {
945  if (!$cloneEntity->contains($shipment))
946  {
947  $cloneEntity[$shipment] = $shipment->createClone($cloneEntity);
948  }
949 
950  if ($cloneEntity->contains($shipment))
951  {
952  $shipmentItemCollectionClone->shipment = $cloneEntity[$shipment];
953  }
954  }
955 
956  return $shipmentItemCollectionClone;
957  }
958 
959  /**
960  * @param $value
961  *
962  * @return string
963  */
964  public function getErrorEntity($value)
965  {
966  $className = null;
967 
968  /** @var ShipmentItem $shipmentItem */
969  foreach ($this->collection as $shipmentItem)
970  {
971  if ($className = $shipmentItem->getErrorEntity($value))
972  {
973  break;
974  }
975  }
976 
977  return $className;
978  }
979 
980  /**
981  * @param $value
982  *
983  * @return string
984  */
985  public function canAutoFixError($value)
986  {
987  $autoFix = false;
988 
989  /** @var ShipmentItem $shipmentItem */
990  foreach ($this->collection as $shipmentItem)
991  {
992  if ($autoFix = $shipmentItem->canAutoFixError($value))
993  {
994  break;
995  }
996  }
997  return $autoFix;
998  }
999 
1000 }
Bitrix\Sale\BasketItem\isBundleParent
isBundleParent()
Definition: basketitem.php:431
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Returns translation by message code.
Definition: loc.php:29
Bitrix\Sale\ShipmentItemCollection\onBasketModify
onBasketModify($action, BasketItem $basketItem, $name=null, $oldValue=null, $value=null)
Definition: shipmentitemcollection.php:756
Bitrix\Sale\BasketItem\isBundleChild
isBundleChild()
Definition: basketitem.php:439
Bitrix\Sale\Internals\CollectableEntity
Definition: collectableentity.php:12
Bitrix\Sale\Internals\CollectionBase\getIterator
getIterator()
Definition: collectionbase.php:26
Bitrix\Sale\BasketItemBase\getId
getId()
Definition: basketitembase.php:957
Bitrix\Sale\ShipmentItemCollection\createItem
createItem(BasketItem $basketItem)
Definition: shipmentitemcollection.php:108
Bitrix\Sale\ShipmentItemCollection\getWeight
getWeight()
Definition: shipmentitemcollection.php:393
Bitrix\Sale\EventActions\ADD
const ADD
Definition: eventactions.php:8
Bitrix\Sale\ShipmentItemCollection\getBasketItemQuantity
getBasketItemQuantity(BasketItem $basketItem)
Definition: shipmentitemcollection.php:890
Bitrix\Sale\BasketItemBase\getQuantity
getQuantity()
Definition: basketitembase.php:1021
Bitrix\Sale\ShipmentItemCollection\save
save()
Definition: shipmentitemcollection.php:423
Bitrix\Sale\Internals\CollectionFilterIterator
Definition: collectionfilteriterator.php:8
Bitrix\Sale\Registry\REGISTRY_TYPE_ORDER
const REGISTRY_TYPE_ORDER
Definition: sale/lib/registry.php:16
Bitrix\Sale\ShipmentItemCollection\getItemByBasketId
getItemByBasketId($itemId)
Definition: shipmentitemcollection.php:320
Bitrix\Sale\Internals\CollectableEntity\getCollection
getCollection()
Definition: collectableentity.php:46
Bitrix\Sale\EventActions\DELETE
const DELETE
Definition: eventactions.php:10
Bitrix\Main
Bitrix\Main\ObjectNotFoundException
Exception is thrown when an object is not present.
Definition: main/lib/exception.php:202
Bitrix\Sale\ShipmentItemCollection\deleteInternal
deleteInternal($primary)
Definition: shipmentitemcollection.php:716
Bitrix\Main\DB\Result
Definition: main/lib/db/result.php:18
Bitrix\Sale\Internals\CollectionBase\count
count()
Count elements of an object.
Definition: collectionbase.php:79
Bitrix\Sale\ShipmentItemCollection\isEmpty
isEmpty()
Definition: shipmentitemcollection.php:870
Bitrix\Sale\Internals
Definition: accountnumber.php:3
Bitrix\Sale\Shipment
Definition: shipment.php:19
Bitrix\Sale\Registry\getInstance
static getInstance($type)
Definition: sale/lib/registry.php:161
Bitrix\Sale\EventActions\UPDATE
const UPDATE
Definition: eventactions.php:9
Bitrix\Sale\ShipmentItemCollection\isExistsBasketItem
isExistsBasketItem(BasketItem $basketItem)
Definition: shipmentitemcollection.php:790
Bitrix\Sale\ShipmentItemCollection\resetCollection
resetCollection(Basket $basket)
Definition: shipmentitemcollection.php:40
Bitrix\Sale\ShipmentItemCollection\getItemByBasketCode
getItemByBasketCode($itemCode)
Definition: shipmentitemcollection.php:303
Bitrix\Sale\ShipmentItemCollection\getPrice
getPrice()
Definition: shipmentitemcollection.php:372
Bitrix\Sale\BasketItemBase\getField
getField($name)
Definition: basketitembase.php:464
Bitrix\Sale\ShipmentItemCollection\load
static load(Shipment $shipment)
Definition: shipmentitemcollection.php:644
Bitrix\Sale\ShipmentItemCollection\canAutoFixError
canAutoFixError($value)
Definition: shipmentitemcollection.php:985
Bitrix\Main\Event
Definition: main/lib/event.php:4
Bitrix\Sale\ShipmentItemCollection\onItemModify
onItemModify(Internals\CollectableEntity $item, $name=null, $oldValue=null, $value=null)
Definition: shipmentitemcollection.php:293
Bitrix\Sale\BasketItemBase\getBasketCode
getBasketCode()
Definition: basketitembase.php:78
Bitrix\Catalog\Product\Basket
Definition: catalog/lib/product/basket.php:17
Bitrix\Sale\ShipmentItemCollection\createClone
createClone(\SplObjectStorage $cloneEntity)
Definition: shipmentitemcollection.php:932
Bitrix\Sale\ShipmentItem
Definition: shipmentitem.php:10
Bitrix\Sale\BasketItem\getBundleCollection
getBundleCollection()
Definition: basketitem.php:528
Bitrix\Sale
Created by PhpStorm.
Bitrix\Main\NotSupportedException
Exception is thrown when operation is not supported.
Definition: main/lib/exception.php:158
Bitrix\Main\Localization\Loc
Definition: loc.php:10
Bitrix\Sale\ShipmentItemCollection\isExistBasketItem
isExistBasketItem(BasketItem $basketItem)
Definition: shipmentitemcollection.php:909
Bitrix\Sale\ShipmentItemCollection\onBeforeBasketItemDelete
onBeforeBasketItemDelete(BasketItem $basketItem)
Definition: shipmentitemcollection.php:730
Bitrix\Sale\BasketItem
Definition: basketitem.php:26
Bitrix\Sale\ShipmentItemCollection\deleteByBasketItem
deleteByBasketItem(BasketItem $basketItem)
Definition: shipmentitemcollection.php:811
Bitrix\Sale\BasketItemBase\getWeight
getWeight()
Definition: basketitembase.php:1030
Bitrix\Sale\ResultError
Definition: sale/lib/result.php:159
Bitrix\Sale\Internals\EntityCollection
Definition: entitycollection.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
Bitrix\Sale\ShipmentItemCollection
Definition: shipmentitemcollection.php:16
Bitrix\Sale\BasketItemBase\getPrice
getPrice()
Definition: basketitembase.php:975
Bitrix\Sale\ShipmentItemCollection\deleteItem
deleteItem($index)
Definition: shipmentitemcollection.php:274
Bitrix\Sale\ShipmentItemCollection\getShippableItems
getShippableItems()
Definition: shipmentitemcollection.php:337
Bitrix\Sale\ShipmentItemCollection\getRegistryType
static getRegistryType()
Definition: shipmentitemcollection.php:623
Bitrix\Sale\ShipmentItemCollection\getShipment
getShipment()
Definition: shipmentitemcollection.php:411
Bitrix\Sale\ShipmentItemCollection\getList
static getList(array $parameters=array())
Definition: shipmentitemcollection.php:707
Bitrix\Sale\ShipmentItemCollection\getSellableItems
getSellableItems()
Definition: shipmentitemcollection.php:354
Bitrix\Sale\ShipmentItemCollection\getEntityParent
getEntityParent()
Definition: shipmentitemcollection.php:27
Bitrix\Sale\ShipmentItemCollection\createIndex
createIndex()
Definition: shipmentitemcollection.php:259
Bitrix\Sale\BasketItemBase\getProductId
getProductId()
Definition: basketitembase.php:966
Bitrix\Sale\ShipmentItemCollection\$shipment
$shipment
Definition: shipmentitemcollection.php:20
Bitrix\Sale\Internals\ShipmentItemTable\deleteWithItems
static deleteWithItems($id)
Definition: internals/shipmentitem.php:49
Bitrix\Sale\ShipmentItemCollection\$shipmentItemIndexMap
$shipmentItemIndexMap
Definition: shipmentitemcollection.php:22
Bitrix\Sale\ShipmentItemCollection\addItem
addItem(Internals\CollectableEntity $shipmentItem)
Definition: shipmentitemcollection.php:248
Bitrix\Sale\Internals\EntityCollection\isClone
isClone()
Definition: entitycollection.php:358
Bitrix\Sale\ShipmentItemCollection\getErrorEntity
getErrorEntity($value)
Definition: shipmentitemcollection.php:964
Bitrix\Sale\BasketItem\getBundleBaseQuantity
getBundleBaseQuantity()
Definition: basketitem.php:455