|
6 | 6 | namespace Magento\Shipping\Controller\Adminhtml\Order;
|
7 | 7 |
|
8 | 8 | use Magento\Framework\DataObject;
|
| 9 | +use Magento\Framework\Exception\LocalizedException; |
| 10 | +use Magento\Framework\Message\ManagerInterface; |
| 11 | +use Magento\Framework\Registry; |
| 12 | +use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; |
| 13 | +use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory; |
| 14 | +use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory; |
| 15 | +use Magento\Sales\Api\ShipmentRepositoryInterface; |
| 16 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 17 | +use Magento\Sales\Model\Order\ShipmentDocumentFactory; |
| 18 | +use Magento\Sales\Api\Data\ShipmentItemCreationInterface; |
9 | 19 |
|
10 | 20 | /**
|
11 | 21 | * Class ShipmentLoader
|
|
17 | 27 | * @method ShipmentLoader setTracking($tracking)
|
18 | 28 | * @method int getOrderId()
|
19 | 29 | * @method int getShipmentId()
|
20 |
| - * @method array getShipment() |
21 | 30 | * @method array getTracking()
|
22 | 31 | */
|
23 | 32 | class ShipmentLoader extends DataObject
|
24 | 33 | {
|
| 34 | + const SHIPMENT = 'shipment'; |
| 35 | + |
25 | 36 | /**
|
26 |
| - * @var \Magento\Framework\Message\ManagerInterface |
| 37 | + * @var ManagerInterface |
27 | 38 | */
|
28 |
| - protected $messageManager; |
| 39 | + private $messageManager; |
29 | 40 |
|
30 | 41 | /**
|
31 |
| - * @var \Magento\Framework\Registry |
| 42 | + * @var Registry |
32 | 43 | */
|
33 |
| - protected $registry; |
| 44 | + private $registry; |
34 | 45 |
|
35 | 46 | /**
|
36 |
| - * @var \Magento\Sales\Api\ShipmentRepositoryInterface |
| 47 | + * @var ShipmentRepositoryInterface |
37 | 48 | */
|
38 |
| - protected $shipmentRepository; |
| 49 | + private $shipmentRepository; |
39 | 50 |
|
40 | 51 | /**
|
41 |
| - * @var \Magento\Sales\Model\Order\ShipmentFactory |
| 52 | + * @var OrderRepositoryInterface |
42 | 53 | */
|
43 |
| - protected $shipmentFactory; |
| 54 | + private $orderRepository; |
44 | 55 |
|
45 | 56 | /**
|
46 |
| - * @var \Magento\Sales\Model\Order\Shipment\TrackFactory |
| 57 | + * @var ShipmentDocumentFactory |
47 | 58 | */
|
48 |
| - protected $trackFactory; |
| 59 | + private $documentFactory; |
49 | 60 |
|
50 | 61 | /**
|
51 |
| - * @var \Magento\Sales\Api\OrderRepositoryInterface |
| 62 | + * @var ShipmentTrackCreationInterfaceFactory |
52 | 63 | */
|
53 |
| - protected $orderRepository; |
| 64 | + private $trackFactory; |
54 | 65 |
|
55 | 66 | /**
|
56 |
| - * @param \Magento\Framework\Message\ManagerInterface $messageManager |
57 |
| - * @param \Magento\Framework\Registry $registry |
58 |
| - * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository |
59 |
| - * @param \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory |
60 |
| - * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory |
61 |
| - * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository |
| 67 | + * @var ShipmentItemCreationInterfaceFactory |
| 68 | + */ |
| 69 | + private $itemFactory; |
| 70 | + |
| 71 | + /** |
| 72 | + * @param ManagerInterface $messageManager |
| 73 | + * @param Registry $registry |
| 74 | + * @param ShipmentRepositoryInterface $shipmentRepository |
| 75 | + * @param OrderRepositoryInterface $orderRepository |
| 76 | + * @param ShipmentDocumentFactory $documentFactory |
| 77 | + * @param ShipmentTrackCreationInterfaceFactory $trackFactory |
| 78 | + * @param ShipmentItemCreationInterfaceFactory $itemFactory |
62 | 79 | * @param array $data
|
63 | 80 | */
|
64 | 81 | public function __construct(
|
65 |
| - \Magento\Framework\Message\ManagerInterface $messageManager, |
66 |
| - \Magento\Framework\Registry $registry, |
67 |
| - \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepository, |
68 |
| - \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory, |
69 |
| - \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory, |
70 |
| - \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, |
| 82 | + ManagerInterface $messageManager, |
| 83 | + Registry $registry, |
| 84 | + ShipmentRepositoryInterface $shipmentRepository, |
| 85 | + OrderRepositoryInterface $orderRepository, |
| 86 | + ShipmentDocumentFactory $documentFactory, |
| 87 | + ShipmentTrackCreationInterfaceFactory $trackFactory, |
| 88 | + ShipmentItemCreationInterfaceFactory $itemFactory, |
71 | 89 | array $data = []
|
72 | 90 | ) {
|
73 | 91 | $this->messageManager = $messageManager;
|
74 | 92 | $this->registry = $registry;
|
75 | 93 | $this->shipmentRepository = $shipmentRepository;
|
76 |
| - $this->shipmentFactory = $shipmentFactory; |
77 |
| - $this->trackFactory = $trackFactory; |
78 | 94 | $this->orderRepository = $orderRepository;
|
| 95 | + $this->documentFactory = $documentFactory; |
| 96 | + $this->trackFactory = $trackFactory; |
| 97 | + $this->itemFactory = $itemFactory; |
79 | 98 | parent::__construct($data);
|
80 | 99 | }
|
81 | 100 |
|
82 |
| - /** |
83 |
| - * Initialize shipment items QTY |
84 |
| - * |
85 |
| - * @return array |
86 |
| - */ |
87 |
| - protected function getItemQtys() |
88 |
| - { |
89 |
| - $data = $this->getShipment(); |
90 |
| - |
91 |
| - return isset($data['items']) ? $data['items'] : []; |
92 |
| - } |
93 |
| - |
94 | 101 | /**
|
95 | 102 | * Initialize shipment model instance
|
96 | 103 | *
|
@@ -129,14 +136,73 @@ public function load()
|
129 | 136 | return false;
|
130 | 137 | }
|
131 | 138 |
|
132 |
| - $shipment = $this->shipmentFactory->create( |
| 139 | + $shipmentItems = $this->getShipmentItems($this->getShipment()); |
| 140 | + |
| 141 | + $shipment = $this->documentFactory->create( |
133 | 142 | $order,
|
134 |
| - $this->getItemQtys(), |
135 |
| - $this->getTracking() |
| 143 | + $shipmentItems, |
| 144 | + $this->getTrackingArray() |
136 | 145 | );
|
137 | 146 | }
|
138 | 147 |
|
139 | 148 | $this->registry->register('current_shipment', $shipment);
|
140 | 149 | return $shipment;
|
141 | 150 | }
|
| 151 | + |
| 152 | + /** |
| 153 | + * Convert UI-generated tracking array to Data Object array |
| 154 | + * |
| 155 | + * @return ShipmentTrackCreationInterface[] |
| 156 | + * @throws LocalizedException |
| 157 | + */ |
| 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 | + } |
| 168 | + /** @var ShipmentTrackCreationInterface $trackCreation */ |
| 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 | + |
| 179 | + /** |
| 180 | + * Extract product id => product quantity array from shipment data. |
| 181 | + * |
| 182 | + * @param array $shipmentData |
| 183 | + * @return int[] |
| 184 | + */ |
| 185 | + private function getShipmentItems(array $shipmentData) |
| 186 | + { |
| 187 | + $shipmentItems = []; |
| 188 | + $itemQty = isset($shipmentData['items']) ? $shipmentData['items'] : []; |
| 189 | + foreach ($itemQty as $itemId => $quantity) { |
| 190 | + /** @var ShipmentItemCreationInterface $item */ |
| 191 | + $item = $this->itemFactory->create(); |
| 192 | + $item->setOrderItemId($itemId); |
| 193 | + $item->setQty($quantity); |
| 194 | + $shipmentItems[] = $item; |
| 195 | + } |
| 196 | + return $shipmentItems; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Retrieve shipment |
| 201 | + * |
| 202 | + * @return array |
| 203 | + */ |
| 204 | + public function getShipment() |
| 205 | + { |
| 206 | + return $this->getData(self::SHIPMENT) ?: []; |
| 207 | + } |
142 | 208 | }
|
0 commit comments