messageManager = $messageManager; $this->registry = $registry; $this->shipmentRepository = $shipmentRepository; $this->orderRepository = $orderRepository; $this->documentFactory = $documentFactory; $this->trackFactory = $trackFactory; $this->itemFactory = $itemFactory; parent::__construct($data); } /** * Initialize shipment model instance * * @return bool|\Magento\Sales\Model\Order\Shipment * @throws \Magento\Framework\Exception\LocalizedException */ public function load() { $shipment = false; $orderId = $this->getOrderId(); $shipmentId = $this->getShipmentId(); if ($shipmentId) { $shipment = $this->shipmentRepository->get($shipmentId); } elseif ($orderId) { $order = $this->orderRepository->get($orderId); /** * Check order existing */ if (!$order->getId()) { $this->messageManager->addError(__('The order no longer exists.')); return false; } /** * Check shipment is available to create separate from invoice */ if ($order->getForcedShipmentWithInvoice()) { $this->messageManager->addError(__('Cannot do shipment for the order separately from invoice.')); return false; } /** * Check shipment create availability */ if (!$order->canShip()) { $this->messageManager->addError(__('Cannot do shipment for the order.')); return false; } $shipmentItems = $this->getShipmentItems($this->getShipment()); $shipment = $this->documentFactory->create( $order, $shipmentItems, $this->getTrackingArray() ); } $this->registry->register('current_shipment', $shipment); return $shipment; } /** * Convert UI-generated tracking array to Data Object array * * @return ShipmentTrackCreationInterface[] * @throws LocalizedException */ private function getTrackingArray() { $tracks = $this->getTracking() ?: []; $trackingCreation = []; foreach ($tracks as $track) { if (!isset($track['number']) || !isset($track['title']) || !isset($track['carrier_code'])) { throw new LocalizedException( __('Tracking information must contain title, carrier code, and tracking number') ); } /** @var ShipmentTrackCreationInterface $trackCreation */ $trackCreation = $this->trackFactory->create(); $trackCreation->setTrackNumber($track['number']); $trackCreation->setTitle($track['title']); $trackCreation->setCarrierCode($track['carrier_code']); $trackingCreation[] = $trackCreation; } return $trackingCreation; } /** * Extract product id => product quantity array from shipment data. * * @param array $shipmentData * @return int[] */ private function getShipmentItems(array $shipmentData) { $shipmentItems = []; $itemQty = isset($shipmentData['items']) ? $shipmentData['items'] : []; foreach ($itemQty as $itemId => $quantity) { /** @var ShipmentItemCreationInterface $item */ $item = $this->itemFactory->create(); $item->setOrderItemId($itemId); $item->setQty($quantity); $shipmentItems[] = $item; } return $shipmentItems; } /** * Retrieve shipment * * @return array */ public function getShipment() { return $this->getData(self::SHIPMENT) ?: []; } }