Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Shipment.php
Go to the documentation of this file.
1 <?php
7 
13 
28 {
29  const STATUS_NEW = 1;
30 
31  const REPORT_DATE_TYPE_ORDER_CREATED = 'order_created';
32 
33  const REPORT_DATE_TYPE_SHIPMENT_CREATED = 'shipment_created';
34 
38  const XML_PATH_STORE_ADDRESS1 = 'shipping/origin/street_line1';
39 
40  const XML_PATH_STORE_ADDRESS2 = 'shipping/origin/street_line2';
41 
42  const XML_PATH_STORE_CITY = 'shipping/origin/city';
43 
44  const XML_PATH_STORE_REGION_ID = 'shipping/origin/region_id';
45 
46  const XML_PATH_STORE_ZIP = 'shipping/origin/postcode';
47 
48  const XML_PATH_STORE_COUNTRY_ID = 'shipping/origin/country_id';
49 
55  protected $entityType = 'shipment';
56 
60  protected $_order;
61 
65  protected $_eventPrefix = 'sales_order_shipment';
66 
70  protected $_eventObject = 'shipment';
71 
76 
81 
85  protected $_commentFactory;
86 
91 
95  protected $orderRepository;
96 
100  private $tracksCollection;
101 
105  private $commentsCollection;
106 
122  public function __construct(
123  \Magento\Framework\Model\Context $context,
124  \Magento\Framework\Registry $registry,
125  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
127  \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory,
128  \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory,
129  \Magento\Sales\Model\Order\Shipment\CommentFactory $commentFactory,
130  \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory,
132  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
133  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
134  array $data = []
135  ) {
136  $this->_shipmentItemCollectionFactory = $shipmentItemCollectionFactory;
137  $this->_trackCollectionFactory = $trackCollectionFactory;
138  $this->_commentFactory = $commentFactory;
139  $this->_commentCollectionFactory = $commentCollectionFactory;
140  $this->orderRepository = $orderRepository;
141  parent::__construct(
142  $context,
143  $registry,
144  $extensionFactory,
146  $resource,
147  $resourceCollection,
148  $data
149  );
150  }
151 
157  protected function _construct()
158  {
159  $this->_init(\Magento\Sales\Model\ResourceModel\Order\Shipment::class);
160  }
161 
168  public function loadByIncrementId($incrementId)
169  {
170  $ids = $this->getCollection()->addAttributeToFilter('increment_id', $incrementId)->getAllIds();
171 
172  if (!empty($ids)) {
173  reset($ids);
174  $this->load(current($ids));
175  }
176  return $this;
177  }
178 
185  public function setOrder(\Magento\Sales\Model\Order $order)
186  {
187  $this->_order = $order;
188  $this->setOrderId($order->getId())->setStoreId($order->getStoreId());
189  return $this;
190  }
191 
197  public function getProtectCode()
198  {
199  return (string)$this->getOrder()->getProtectCode();
200  }
201 
207  public function getOrder()
208  {
209  if (!$this->_order instanceof \Magento\Sales\Model\Order) {
210  $this->_order = $this->orderRepository->get($this->getOrderId());
211  }
212  return $this->_order->setHistoryEntityName($this->entityType);
213  }
214 
222  public function getEntityType()
223  {
224  return $this->entityType;
225  }
226 
232  public function getBillingAddress()
233  {
234  return $this->getOrder()->getBillingAddress();
235  }
236 
242  public function getShippingAddress()
243  {
244  return $this->getOrder()->getShippingAddress();
245  }
246 
253  public function register()
254  {
255  if ($this->getId()) {
256  throw new \Magento\Framework\Exception\LocalizedException(
257  __('We cannot register an existing shipment')
258  );
259  }
260 
261  $totalQty = 0;
262 
264  foreach ($this->getAllItems() as $item) {
265  if ($item->getQty() > 0) {
266  $item->register();
267 
268  if (!$item->getOrderItem()->isDummy(true)) {
269  $totalQty += $item->getQty();
270  }
271  }
272  }
273 
274  $this->setTotalQty($totalQty);
275 
276  return $this;
277  }
278 
284  public function getItemsCollection()
285  {
286  if (!$this->hasData(ShipmentInterface::ITEMS)) {
287  $this->setItems($this->_shipmentItemCollectionFactory->create()->setShipmentFilter($this->getId()));
288 
289  if ($this->getId()) {
290  foreach ($this->getItems() as $item) {
291  $item->setShipment($this);
292  }
293  }
294  }
295 
296  return $this->getItems();
297  }
298 
304  public function getAllItems()
305  {
306  $items = [];
307  foreach ($this->getItemsCollection() as $item) {
308  if (!$item->isDeleted()) {
309  $items[] = $item;
310  }
311  }
312  return $items;
313  }
314 
321  public function getItemById($itemId)
322  {
323  foreach ($this->getItemsCollection() as $item) {
324  if ($item->getId() == $itemId) {
325  return $item;
326  }
327  }
328  return false;
329  }
330 
337  public function addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
338  {
339  $item->setShipment($this)->setParentId($this->getId())->setStoreId($this->getStoreId());
340  if (!$item->getId()) {
341  $this->setItems(array_merge(
342  $this->getItems() ?? [],
343  [$item]
344  ));
345  }
346  return $this;
347  }
348 
354  public function getTracksCollection()
355  {
356  if ($this->tracksCollection === null) {
357  $this->tracksCollection = $this->_trackCollectionFactory->create()->setShipmentFilter($this->getId());
358  }
359 
360  return $this->tracksCollection;
361  }
362 
368  public function getAllTracks()
369  {
370  $tracks = [];
371  foreach ($this->getTracksCollection() as $track) {
372  if (!$track->isDeleted()) {
373  $tracks[] = $track;
374  }
375  }
376  return $tracks;
377  }
378 
385  public function getTrackById($trackId)
386  {
387  foreach ($this->getTracksCollection() as $track) {
388  if ($track->getId() == $trackId) {
389  return $track;
390  }
391  }
392  return false;
393  }
394 
401  public function addTrack(\Magento\Sales\Model\Order\Shipment\Track $track)
402  {
403  $track->setShipment(
404  $this
405  )->setParentId(
406  $this->getId()
407  )->setOrderId(
408  $this->getOrderId()
409  )->setStoreId(
410  $this->getStoreId()
411  );
412  if (!$track->getId()) {
413  $this->getTracksCollection()->addItem($track);
414  }
415 
420  $this->_hasDataChanges = true;
421 
422  return $this;
423  }
424 
433  public function addComment($comment, $notify = false, $visibleOnFront = false)
434  {
435  if (!$comment instanceof \Magento\Sales\Model\Order\Shipment\Comment) {
436  $comment = $this->_commentFactory->create()
437  ->setComment($comment)
438  ->setIsCustomerNotified($notify)
439  ->setIsVisibleOnFront($visibleOnFront);
440  }
441  $comment->setShipment($this)
442  ->setParentId($this->getId())
443  ->setStoreId($this->getStoreId());
444  if (!$comment->getId()) {
445  $this->getCommentsCollection()->addItem($comment);
446  }
447  $comments = $this->getComments();
448  $comments[] = $comment;
449  $this->setComments($comments);
450  $this->_hasDataChanges = true;
451  return $this;
452  }
453 
460  public function getCommentsCollection($reload = false)
461  {
462  if ($this->commentsCollection === null || $reload) {
463  $this->commentsCollection = $this->_commentCollectionFactory->create();
464  if ($this->getId()) {
465  $this->commentsCollection->setShipmentFilter($this->getId())
466  ->setCreatedAtOrder();
467 
468  foreach ($this->commentsCollection as $comment) {
469  $comment->setShipment($this);
470  }
471  }
472  }
473 
474  return $this->commentsCollection;
475  }
476 
482  public function getStore()
483  {
484  return $this->getOrder()->getStore();
485  }
486 
493  public function setShippingLabel($label)
494  {
495  $this->setData('shipping_label', $label);
496  return $this;
497  }
498 
504  public function getShippingLabel()
505  {
506  $label = $this->getData('shipping_label');
507  if ($label) {
508  return $this->getResource()->getConnection()->decodeVarbinary($label);
509  }
510  return $label;
511  }
512 
520  public function getIncrementId()
521  {
522  return $this->getData('increment_id');
523  }
524 
532  public function getPackages()
533  {
534  return $this->getData(ShipmentInterface::PACKAGES);
535  }
536 
541  public function setPackages(array $packages = null)
542  {
543  return $this->setData(ShipmentInterface::PACKAGES, $packages);
544  }
545 
551  public function getItems()
552  {
553  if ($this->getData(ShipmentInterface::ITEMS) === null) {
554  $collection = $this->_shipmentItemCollectionFactory->create()->setShipmentFilter($this->getId());
555  if ($this->getId()) {
556  foreach ($collection as $item) {
557  $item->setShipment($this);
558  }
559  $this->setData(ShipmentInterface::ITEMS, $collection->getItems());
560  }
561  }
562  $shipmentItems = $this->getData(ShipmentInterface::ITEMS);
563  if ($shipmentItems !== null && !is_array($shipmentItems)) {
564  $shipmentItems = $shipmentItems->getItems();
565  }
566  return $shipmentItems;
567  }
568 
577  public function setItems($items)
578  {
579  return $this->setData(ShipmentInterface::ITEMS, $items);
580  }
581 
587  public function getTracks()
588  {
589  if ($this->getData(ShipmentInterface::TRACKS) === null) {
590  foreach ($this->getTracksCollection() as $item) {
591  $item->setShipment($this);
592  }
594  }
595  return $this->getData(ShipmentInterface::TRACKS);
596  }
597 
598  //@codeCoverageIgnoreStart
599 
606  public function setTracks($tracks)
607  {
608  return $this->setData(ShipmentInterface::TRACKS, $tracks);
609  }
610 
616  public function getBillingAddressId()
617  {
619  }
620 
626  public function getCreatedAt()
627  {
628  return $this->getData(ShipmentInterface::CREATED_AT);
629  }
630 
634  public function setCreatedAt($createdAt)
635  {
636  return $this->setData(ShipmentInterface::CREATED_AT, $createdAt);
637  }
638 
644  public function getCustomerId()
645  {
647  }
648 
654  public function getEmailSent()
655  {
656  return $this->getData(ShipmentInterface::EMAIL_SENT);
657  }
658 
664  public function getOrderId()
665  {
666  return $this->getData(ShipmentInterface::ORDER_ID);
667  }
668 
674  public function getShipmentStatus()
675  {
677  }
678 
684  public function getShippingAddressId()
685  {
687  }
688 
694  public function getStoreId()
695  {
696  return $this->getData(ShipmentInterface::STORE_ID);
697  }
698 
704  public function getTotalQty()
705  {
706  return $this->getData(ShipmentInterface::TOTAL_QTY);
707  }
708 
714  public function getTotalWeight()
715  {
717  }
718 
724  public function getUpdatedAt()
725  {
726  return $this->getData(ShipmentInterface::UPDATED_AT);
727  }
728 
734  public function getComments()
735  {
736  if (!$this->getId()) {
737  return $this->getData(ShipmentInterface::COMMENTS);
738  }
739 
740  if ($this->getData(ShipmentInterface::COMMENTS) == null) {
741  $collection = $this->_commentCollectionFactory->create()
742  ->setShipmentFilter($this->getId());
743 
744  foreach ($collection as $item) {
745  $item->setShipment($this);
746  }
747  $this->setData(ShipmentInterface::COMMENTS, $collection->getItems());
748  }
749  return $this->getData(ShipmentInterface::COMMENTS);
750  }
751 
758  public function setComments($comments = null)
759  {
761  }
762 
766  public function setStoreId($id)
767  {
768  return $this->setData(ShipmentInterface::STORE_ID, $id);
769  }
770 
774  public function setTotalWeight($totalWeight)
775  {
776  return $this->setData(ShipmentInterface::TOTAL_WEIGHT, $totalWeight);
777  }
778 
782  public function setTotalQty($qty)
783  {
784  return $this->setData(ShipmentInterface::TOTAL_QTY, $qty);
785  }
786 
790  public function setEmailSent($emailSent)
791  {
792  return $this->setData(ShipmentInterface::EMAIL_SENT, $emailSent);
793  }
794 
798  public function setOrderId($id)
799  {
800  return $this->setData(ShipmentInterface::ORDER_ID, $id);
801  }
802 
806  public function setCustomerId($id)
807  {
809  }
810 
814  public function setShippingAddressId($id)
815  {
817  }
818 
822  public function setBillingAddressId($id)
823  {
825  }
826 
830  public function setShipmentStatus($shipmentStatus)
831  {
832  return $this->setData(ShipmentInterface::SHIPMENT_STATUS, $shipmentStatus);
833  }
834 
838  public function setIncrementId($id)
839  {
841  }
842 
846  public function setUpdatedAt($timestamp)
847  {
848  return $this->setData(ShipmentInterface::UPDATED_AT, $timestamp);
849  }
850 
856  public function getExtensionAttributes()
857  {
858  return $this->_getExtensionAttributes();
859  }
860 
867  public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentExtensionInterface $extensionAttributes)
868  {
869  return $this->_setExtensionAttributes($extensionAttributes);
870  }
871 
872  //@codeCoverageIgnoreEnd
873 }
addComment($comment, $notify=false, $visibleOnFront=false)
Definition: Shipment.php:433
$track
Definition: details.phtml:12
setOrder(\Magento\Sales\Model\Order $order)
Definition: Shipment.php:185
$id
Definition: fieldset.phtml:14
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
addItem(\Magento\Sales\Model\Order\Shipment\Item $item)
Definition: Shipment.php:337
$order
Definition: order.php:55
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$label
Definition: details.phtml:21
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Item\CollectionFactory $shipmentItemCollectionFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory $trackCollectionFactory, \Magento\Sales\Model\Order\Shipment\CommentFactory $commentFactory, \Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\CollectionFactory $commentCollectionFactory, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Shipment.php:122
getCommentsCollection($reload=false)
Definition: Shipment.php:460
setShipmentStatus($shipmentStatus)
Definition: Shipment.php:830
setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentExtensionInterface $extensionAttributes)
Definition: Shipment.php:867
addTrack(\Magento\Sales\Model\Order\Shipment\Track $track)
Definition: Shipment.php:401
setPackages(array $packages=null)
Definition: Shipment.php:541
$items