AutoFulfill.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Order\AutoProcessing;
  6. use Magento\Sales\Api\Data\OrderInterface;
  7. use Magento\Sales\Api\Data\ShipmentCommentCreationInterfaceFactory;
  8. use Magento\Sales\Api\Data\ShipmentCreationArgumentsExtensionInterfaceFactory;
  9. use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterfaceFactory;
  10. use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
  11. use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
  12. use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
  13. use Magento\Sales\Api\ShipOrderInterface;
  14. use Temando\Shipping\Model\DocumentationInterface;
  15. use Temando\Shipping\Model\Shipment\PackageInterface;
  16. use Temando\Shipping\Model\Shipment\PackageItemInterface;
  17. use Temando\Shipping\Model\Shipping\Carrier;
  18. use Temando\Shipping\Webservice\Response\Type\OrderResponseType;
  19. /**
  20. * Temando Order Fulfillment Processor.
  21. *
  22. * Create shipments locally based on AllocateOrder API response.
  23. *
  24. * @package Temando\Shipping\Model
  25. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. * @link http://www.temando.com/
  28. */
  29. class AutoFulfill implements AutoFulfillInterface
  30. {
  31. /**
  32. * @var OrderStatusHistoryUpdater
  33. */
  34. private $historyUpdater;
  35. /**
  36. * @var ShipmentFilter
  37. */
  38. private $shipmentFilter;
  39. /**
  40. * @var ShipmentItemCreationInterfaceFactory
  41. */
  42. private $itemFactory;
  43. /**
  44. * @var ShipmentCommentCreationInterfaceFactory
  45. */
  46. private $commentFactory;
  47. /**
  48. * @var ShipmentTrackCreationInterfaceFactory
  49. */
  50. private $trackCreationFactory;
  51. /**
  52. * @var ShipmentCreationArgumentsInterfaceFactory
  53. */
  54. private $shipmentCreationArgumentsFactory;
  55. /**
  56. * @var ShipmentCreationArgumentsExtensionInterfaceFactory
  57. */
  58. private $shipmentCreationArgumentsExtensionFactory;
  59. /**
  60. * @var ShipOrderInterface
  61. */
  62. private $shipOrder;
  63. /**
  64. * AutoFulfill constructor.
  65. * @param OrderStatusHistoryUpdater $historyUpdater
  66. * @param ShipmentFilter $shipmentFilter
  67. * @param ShipmentItemCreationInterfaceFactory $itemFactory
  68. * @param ShipmentCommentCreationInterfaceFactory $commentFactory
  69. * @param ShipmentTrackCreationInterfaceFactory $trackCreationFactory
  70. * @param ShipmentCreationArgumentsInterfaceFactory $shipmentCreationArgumentsFactory
  71. * @param ShipmentCreationArgumentsExtensionInterfaceFactory $shipmentCreationArgumentsExtensionFactory
  72. * @param ShipOrderInterface $shipOrder
  73. */
  74. public function __construct(
  75. OrderStatusHistoryUpdater $historyUpdater,
  76. ShipmentFilter $shipmentFilter,
  77. ShipmentItemCreationInterfaceFactory $itemFactory,
  78. ShipmentCommentCreationInterfaceFactory $commentFactory,
  79. ShipmentTrackCreationInterfaceFactory $trackCreationFactory,
  80. ShipmentCreationArgumentsInterfaceFactory $shipmentCreationArgumentsFactory,
  81. ShipmentCreationArgumentsExtensionInterfaceFactory $shipmentCreationArgumentsExtensionFactory,
  82. ShipOrderInterface $shipOrder
  83. ) {
  84. $this->historyUpdater = $historyUpdater;
  85. $this->shipmentFilter = $shipmentFilter;
  86. $this->itemFactory = $itemFactory;
  87. $this->commentFactory = $commentFactory;
  88. $this->trackCreationFactory = $trackCreationFactory;
  89. $this->shipmentCreationArgumentsFactory = $shipmentCreationArgumentsFactory;
  90. $this->shipmentCreationArgumentsExtensionFactory = $shipmentCreationArgumentsExtensionFactory;
  91. $this->shipOrder = $shipOrder;
  92. }
  93. /**
  94. * @param OrderInterface $salesOrder
  95. * @param string $sku
  96. * @return int|null
  97. */
  98. private function getOrderItemIdBySku(OrderInterface $salesOrder, $sku)
  99. {
  100. foreach ($salesOrder->getItems() as $item) {
  101. if ($item->getSku() === $sku) {
  102. return $item->getItemId();
  103. }
  104. }
  105. return null;
  106. }
  107. /**
  108. * @param \Magento\Sales\Api\Data\OrderInterface $salesOrder
  109. * @param \Temando\Shipping\Webservice\Response\Type\OrderResponseType $orderResponse
  110. * @return int[]
  111. * @throws \Magento\Framework\Exception\CouldNotSaveException
  112. */
  113. public function createShipments(OrderInterface $salesOrder, OrderResponseType $orderResponse)
  114. {
  115. if ($orderResponse->getErrors()) {
  116. $this->historyUpdater->addErrors($salesOrder, $orderResponse->getErrors());
  117. return [];
  118. }
  119. $shipments = (array)$orderResponse->getShipments();
  120. $shipmentIds = [];
  121. $fulfilledShipments = $this->shipmentFilter->getFulfilledShipments($shipments);
  122. $returnShipments = $this->shipmentFilter->getReturnShipments($shipments);
  123. /** @var \Temando\Shipping\Model\ShipmentInterface $fulfilledShipment */
  124. foreach ($fulfilledShipments as $index => $fulfilledShipment) {
  125. // items
  126. $creationItems = [];
  127. $packages = $fulfilledShipment->getPackages() ?: [];
  128. /** @var PackageItemInterface[] $fulfilledItems */
  129. $fulfilledItems = array_reduce($packages, function (array $items, PackageInterface $package) {
  130. $items = array_merge($items, $package->getItems());
  131. return $items;
  132. }, []);
  133. foreach ($fulfilledItems as $fulfilledItem) {
  134. $orderItemId = $this->getOrderItemIdBySku($salesOrder, $fulfilledItem->getSku());
  135. /** @var ShipmentItemCreationInterface $item */
  136. $creationItem = $this->itemFactory->create();
  137. $creationItem->setQty($fulfilledItem->getQty());
  138. $creationItem->setOrderItemId($orderItemId);
  139. $creationItems[]= $creationItem;
  140. }
  141. // tracking
  142. $fulfillment = $fulfilledShipment->getFulfillment();
  143. $tracking = $this->trackCreationFactory->create();
  144. $tracking->setCarrierCode(Carrier::CODE);
  145. $tracking->setTitle($fulfillment->getServiceName());
  146. $tracking->setTrackNumber($fulfillment->getTrackingReference());
  147. // remote references and shipping label
  148. /** @var DocumentationInterface $documentation */
  149. $documentation = current($fulfilledShipment->getDocumentation());
  150. $labelUrl = empty($documentation) ? '' : $documentation->getUrl();
  151. $extensionAttributes = $this->shipmentCreationArgumentsExtensionFactory->create();
  152. $extensionAttributes->setExtLocationId($fulfilledShipment->getOriginId());
  153. $extensionAttributes->setExtShipmentId($fulfilledShipment->getShipmentId());
  154. $extensionAttributes->setExtTrackingReference($fulfilledShipment->getFulfillment()->getTrackingReference());
  155. $extensionAttributes->setShippingLabel($labelUrl);
  156. if (isset($returnShipments[$index])) {
  157. // add forward fulfillment return shipment ID
  158. $extensionAttributes->setExtReturnShipmentId($returnShipments[$index]->getShipmentId());
  159. }
  160. $arguments = $this->shipmentCreationArgumentsFactory->create();
  161. $arguments->setExtensionAttributes($extensionAttributes);
  162. // comment
  163. $comment = $this->commentFactory->create();
  164. $comment->setComment('This shipment was automatically created with Magento Shipping.');
  165. $shipmentIds[]= $this->shipOrder->execute(
  166. $salesOrder->getEntityId(),
  167. $creationItems,
  168. false,
  169. true,
  170. $comment,
  171. [$tracking],
  172. [],
  173. $arguments
  174. );
  175. }
  176. return $shipmentIds;
  177. }
  178. }