|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Plugin; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Framework\DB\Transaction; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Sales\Api\Data\ShipmentInterface; |
| 14 | +use Magento\Sales\Model\Order; |
| 15 | +use Magento\Sales\Model\Order\Shipment\Item; |
| 16 | +use Magento\Sales\Model\Order\ShipmentRepository; |
| 17 | +use Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader; |
| 18 | + |
| 19 | +/** |
| 20 | + * Plugin to update order data before and after saving shipment via API |
| 21 | + */ |
| 22 | +class ProcessOrderAndShipmentViaAPI |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ShipmentLoader |
| 26 | + */ |
| 27 | + private $shipmentLoader; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Transaction |
| 31 | + */ |
| 32 | + private $transaction; |
| 33 | + |
| 34 | + /** |
| 35 | + * Init plugin |
| 36 | + * |
| 37 | + * @param ShipmentLoader $shipmentLoader |
| 38 | + * @param Transaction $transaction |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + ShipmentLoader $shipmentLoader, |
| 42 | + Transaction $transaction |
| 43 | + ) { |
| 44 | + $this->shipmentLoader = $shipmentLoader; |
| 45 | + $this->transaction = $transaction; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Process shipping details before saving shipment via API |
| 50 | + * |
| 51 | + * @param ShipmentRepository $shipmentRepository |
| 52 | + * @param ShipmentInterface $shipmentData |
| 53 | + * @return array |
| 54 | + * @throws LocalizedException |
| 55 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 56 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
| 57 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 58 | + */ |
| 59 | + public function beforeSave( |
| 60 | + ShipmentRepository $shipmentRepository, |
| 61 | + ShipmentInterface $shipmentData |
| 62 | + ): array { |
| 63 | + $this->shipmentLoader->setOrderId($shipmentData->getOrderId()); |
| 64 | + $trackData = !empty($shipmentData->getTracks()) ? |
| 65 | + $this->getShipmentTracking($shipmentData) : []; |
| 66 | + $this->shipmentLoader->setTracking($trackData); |
| 67 | + $shipmentItems = !empty($shipmentData) ? |
| 68 | + $this->getShipmentItems($shipmentData) : []; |
| 69 | + $orderItems = []; |
| 70 | + if (!empty($shipmentData)) { |
| 71 | + $order = $shipmentData->getOrder(); |
| 72 | + $orderItems = $order ? $this->getOrderItems($order) : []; |
| 73 | + } |
| 74 | + $data = (!empty($shipmentItems) && !empty($orderItems)) ? |
| 75 | + $this->getShippingData($shipmentItems, $orderItems) : []; |
| 76 | + $this->shipmentLoader->setShipment($data); |
| 77 | + $shipment = $this->shipmentLoader->load(); |
| 78 | + $shipment = empty($shipment) ? $shipmentData |
| 79 | + : $this->processShippingDetails($shipmentData, $shipment); |
| 80 | + return [$shipment]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Save order data after saving shipment via API |
| 85 | + * |
| 86 | + * @param ShipmentRepository $shipmentRepository |
| 87 | + * @param ShipmentInterface $shipment |
| 88 | + * @return ShipmentInterface |
| 89 | + * @throws Exception |
| 90 | + */ |
| 91 | + public function afterSave( |
| 92 | + ShipmentRepository $shipmentRepository, |
| 93 | + ShipmentInterface $shipment |
| 94 | + ): ShipmentInterface { |
| 95 | + $shipmentDetails = $shipmentRepository->get($shipment->getEntityId()); |
| 96 | + $order = $shipmentDetails->getOrder(); |
| 97 | + $shipmentItems = !empty($shipment) ? |
| 98 | + $this->getShipmentItems($shipment) : []; |
| 99 | + $this->processOrderItems($order, $shipmentItems); |
| 100 | + $order->setIsInProcess(true); |
| 101 | + $this->transaction |
| 102 | + ->addObject($order) |
| 103 | + ->save(); |
| 104 | + return $shipment; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Process shipment items |
| 109 | + * |
| 110 | + * @param ShipmentInterface $shipment |
| 111 | + * @return array |
| 112 | + * @throws LocalizedException |
| 113 | + */ |
| 114 | + private function getShipmentItems(ShipmentInterface $shipment): array |
| 115 | + { |
| 116 | + $shipmentItems = []; |
| 117 | + foreach ($shipment->getItems() as $item) { |
| 118 | + $sku = $item->getSku(); |
| 119 | + if (isset($sku)) { |
| 120 | + $shipmentItems[$sku]['qty'] = $item->getQty(); |
| 121 | + } |
| 122 | + } |
| 123 | + return $shipmentItems; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get shipment tracking data from the shipment array |
| 128 | + * |
| 129 | + * @param ShipmentInterface $shipment |
| 130 | + * @return array |
| 131 | + */ |
| 132 | + private function getShipmentTracking(ShipmentInterface $shipment): array |
| 133 | + { |
| 134 | + $trackData = []; |
| 135 | + foreach ($shipment->getTracks() as $key => $track) { |
| 136 | + $trackData[$key]['number'] = $track->getTrackNumber(); |
| 137 | + $trackData[$key]['title'] = $track->getTitle(); |
| 138 | + $trackData[$key]['carrier_code'] = $track->getCarrierCode(); |
| 139 | + } |
| 140 | + return $trackData; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Get orderItems from shipment order |
| 145 | + * |
| 146 | + * @param Order $order |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function getOrderItems(Order $order): array |
| 150 | + { |
| 151 | + $orderItems = []; |
| 152 | + foreach ($order->getItems() as $item) { |
| 153 | + $orderItems[$item->getSku()] = $item->getItemId(); |
| 154 | + } |
| 155 | + return $orderItems; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get available shipping data from shippingItems and orderItems |
| 160 | + * |
| 161 | + * @param array $shipmentItems |
| 162 | + * @param array $orderItems |
| 163 | + * @return array |
| 164 | + * @throws LocalizedException |
| 165 | + */ |
| 166 | + private function getShippingData(array $shipmentItems, array $orderItems): array |
| 167 | + { |
| 168 | + $data = []; |
| 169 | + foreach ($shipmentItems as $shippingItemSku => $shipmentItem) { |
| 170 | + if (isset($orderItems[$shippingItemSku])) { |
| 171 | + $itemId = (int) $orderItems[$shippingItemSku]; |
| 172 | + $data['items'][$itemId] = $shipmentItem['qty']; |
| 173 | + } |
| 174 | + } |
| 175 | + return $data; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Process shipping comments if available |
| 180 | + * |
| 181 | + * @param ShipmentInterface $shipmentData |
| 182 | + * @param ShipmentInterface $shipment |
| 183 | + * @return void |
| 184 | + */ |
| 185 | + private function processShippingComments(ShipmentInterface $shipmentData, ShipmentInterface $shipment): void |
| 186 | + { |
| 187 | + foreach ($shipmentData->getComments() as $comment) { |
| 188 | + $shipment->addComment( |
| 189 | + $comment->getComment(), |
| 190 | + $comment->getIsCustomerNotified(), |
| 191 | + $comment->getIsVisibleOnFront() |
| 192 | + ); |
| 193 | + $shipment->setCustomerNote($comment->getComment()); |
| 194 | + $shipment->setCustomerNoteNotify((bool) $comment->getIsCustomerNotified()); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Process shipping details |
| 200 | + * |
| 201 | + * @param ShipmentInterface $shipmentData |
| 202 | + * @param ShipmentInterface $shipment |
| 203 | + * @return ShipmentInterface |
| 204 | + */ |
| 205 | + private function processShippingDetails( |
| 206 | + ShipmentInterface $shipmentData, |
| 207 | + ShipmentInterface $shipment |
| 208 | + ): ShipmentInterface { |
| 209 | + if (empty($shipment->getItems())) { |
| 210 | + $shipment->setItems($shipmentData->getItems()); |
| 211 | + } |
| 212 | + if (!empty($shipmentData->getComments())) { |
| 213 | + $this->processShippingComments($shipmentData, $shipment); |
| 214 | + } |
| 215 | + if ((int) $shipment->getTotalQty() < 1) { |
| 216 | + $shipment->setTotalQty($shipmentData->getTotalQty()); |
| 217 | + } |
| 218 | + return $shipment; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Process order items data and set the proper item qty |
| 223 | + * |
| 224 | + * @param Order $order |
| 225 | + * @param array $shipmentItems |
| 226 | + * @throws LocalizedException |
| 227 | + */ |
| 228 | + private function processOrderItems(Order $order, array $shipmentItems): void |
| 229 | + { |
| 230 | + /** @var Item $item */ |
| 231 | + foreach ($order->getAllItems() as $item) { |
| 232 | + if (isset($shipmentItems[$item->getSku()])) { |
| 233 | + $qty = (float)$shipmentItems[$item->getSku()]['qty']; |
| 234 | + $item->setQty($qty); |
| 235 | + if ((float)$item->getQtyToShip() > 0) { |
| 236 | + $item->setQtyShipped((float)$item->getQtyToShip()); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments