Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShipmentLoader.php
Go to the documentation of this file.
1 <?php
7 
13 use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
14 use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
19 
33 {
34  const SHIPMENT = 'shipment';
35 
39  private $messageManager;
40 
44  private $registry;
45 
49  private $shipmentRepository;
50 
54  private $orderRepository;
55 
59  private $documentFactory;
60 
64  private $trackFactory;
65 
69  private $itemFactory;
70 
81  public function __construct(
82  ManagerInterface $messageManager,
83  Registry $registry,
84  ShipmentRepositoryInterface $shipmentRepository,
85  OrderRepositoryInterface $orderRepository,
86  ShipmentDocumentFactory $documentFactory,
87  ShipmentTrackCreationInterfaceFactory $trackFactory,
88  ShipmentItemCreationInterfaceFactory $itemFactory,
89  array $data = []
90  ) {
91  $this->messageManager = $messageManager;
92  $this->registry = $registry;
93  $this->shipmentRepository = $shipmentRepository;
94  $this->orderRepository = $orderRepository;
95  $this->documentFactory = $documentFactory;
96  $this->trackFactory = $trackFactory;
97  $this->itemFactory = $itemFactory;
98  parent::__construct($data);
99  }
100 
107  public function load()
108  {
109  $shipment = false;
110  $orderId = $this->getOrderId();
111  $shipmentId = $this->getShipmentId();
112  if ($shipmentId) {
113  $shipment = $this->shipmentRepository->get($shipmentId);
114  } elseif ($orderId) {
115  $order = $this->orderRepository->get($orderId);
116 
120  if (!$order->getId()) {
121  $this->messageManager->addError(__('The order no longer exists.'));
122  return false;
123  }
127  if ($order->getForcedShipmentWithInvoice()) {
128  $this->messageManager->addError(__('Cannot do shipment for the order separately from invoice.'));
129  return false;
130  }
134  if (!$order->canShip()) {
135  $this->messageManager->addError(__('Cannot do shipment for the order.'));
136  return false;
137  }
138 
139  $shipmentItems = $this->getShipmentItems($this->getShipment());
140 
141  $shipment = $this->documentFactory->create(
142  $order,
143  $shipmentItems,
144  $this->getTrackingArray()
145  );
146  }
147 
148  $this->registry->register('current_shipment', $shipment);
149  return $shipment;
150  }
151 
158  private function getTrackingArray()
159  {
160  $tracks = $this->getTracking() ?: [];
161  $trackingCreation = [];
162  foreach ($tracks as $track) {
163  if (!isset($track['number']) || !isset($track['title']) || !isset($track['carrier_code'])) {
164  throw new LocalizedException(
165  __('Tracking information must contain title, carrier code, and tracking number')
166  );
167  }
169  $trackCreation = $this->trackFactory->create();
170  $trackCreation->setTrackNumber($track['number']);
171  $trackCreation->setTitle($track['title']);
172  $trackCreation->setCarrierCode($track['carrier_code']);
173  $trackingCreation[] = $trackCreation;
174  }
175 
176  return $trackingCreation;
177  }
178 
185  private function getShipmentItems(array $shipmentData)
186  {
187  $shipmentItems = [];
188  $itemQty = isset($shipmentData['items']) ? $shipmentData['items'] : [];
189  foreach ($itemQty as $itemId => $quantity) {
191  $item = $this->itemFactory->create();
192  $item->setOrderItemId($itemId);
193  $item->setQty($quantity);
194  $shipmentItems[] = $item;
195  }
196  return $shipmentItems;
197  }
198 
204  public function getShipment()
205  {
206  return $this->getData(self::SHIPMENT) ?: [];
207  }
208 }
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$track
Definition: details.phtml:12
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
__()
Definition: __.php:13
__construct(ManagerInterface $messageManager, Registry $registry, ShipmentRepositoryInterface $shipmentRepository, OrderRepositoryInterface $orderRepository, ShipmentDocumentFactory $documentFactory, ShipmentTrackCreationInterfaceFactory $trackFactory, ShipmentItemCreationInterfaceFactory $itemFactory, array $data=[])
foreach($order->getItems() as $orderItem) $shipment