ShipmentDocumentFactory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Sales\Api\Data\ShipmentInterface;
  9. use Magento\Sales\Api\Data\ShipmentItemCreationInterface;
  10. use Magento\Sales\Api\Data\ShipmentPackageCreationInterface;
  11. use Magento\Sales\Api\Data\ShipmentTrackCreationInterface;
  12. use Magento\Framework\EntityManager\HydratorPool;
  13. use Magento\Sales\Model\Order\Shipment\TrackFactory;
  14. use Magento\Sales\Api\Data\OrderInterface;
  15. use Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
  16. use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface;
  17. use Magento\Sales\Api\Data\OrderItemInterface;
  18. use Magento\Sales\Model\Order\ShipmentDocumentFactory\ExtensionAttributesProcessor;
  19. /**
  20. * Class ShipmentDocumentFactory
  21. *
  22. * @api
  23. * @since 100.1.2
  24. */
  25. class ShipmentDocumentFactory
  26. {
  27. /**
  28. * @var ShipmentFactory
  29. */
  30. private $shipmentFactory;
  31. /**
  32. * @var TrackFactory
  33. */
  34. private $trackFactory;
  35. /**
  36. * @var HydratorPool
  37. */
  38. private $hydratorPool;
  39. /**
  40. * @var ExtensionAttributesProcessor
  41. */
  42. private $extensionAttributesProcessor;
  43. /**
  44. * ShipmentDocumentFactory constructor.
  45. *
  46. * @param ShipmentFactory $shipmentFactory
  47. * @param HydratorPool $hydratorPool
  48. * @param TrackFactory $trackFactory
  49. * @param ExtensionAttributesProcessor $extensionAttributesProcessor
  50. */
  51. public function __construct(
  52. ShipmentFactory $shipmentFactory,
  53. HydratorPool $hydratorPool,
  54. TrackFactory $trackFactory,
  55. ExtensionAttributesProcessor $extensionAttributesProcessor = null
  56. ) {
  57. $this->shipmentFactory = $shipmentFactory;
  58. $this->trackFactory = $trackFactory;
  59. $this->hydratorPool = $hydratorPool;
  60. $this->extensionAttributesProcessor = $extensionAttributesProcessor ?: ObjectManager::getInstance()
  61. ->get(ExtensionAttributesProcessor::class);
  62. }
  63. /**
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. *
  66. * @param OrderInterface $order
  67. * @param ShipmentItemCreationInterface[] $items
  68. * @param ShipmentTrackCreationInterface[] $tracks
  69. * @param ShipmentCommentCreationInterface|null $comment
  70. * @param bool $appendComment
  71. * @param ShipmentPackageCreationInterface[] $packages
  72. * @param ShipmentCreationArgumentsInterface|null $arguments
  73. * @return ShipmentInterface
  74. * @since 100.1.2
  75. */
  76. public function create(
  77. OrderInterface $order,
  78. array $items = [],
  79. array $tracks = [],
  80. ShipmentCommentCreationInterface $comment = null,
  81. $appendComment = false,
  82. array $packages = [],
  83. ShipmentCreationArgumentsInterface $arguments = null
  84. ) {
  85. $shipmentItems = empty($items)
  86. ? $this->getQuantitiesFromOrderItems($order->getItems())
  87. : $this->getQuantitiesFromShipmentItems($items);
  88. /** @var Shipment $shipment */
  89. $shipment = $this->shipmentFactory->create(
  90. $order,
  91. $shipmentItems
  92. );
  93. if (null !== $arguments) {
  94. $this->extensionAttributesProcessor->execute($shipment, $arguments);
  95. }
  96. foreach ($tracks as $track) {
  97. $hydrator = $this->hydratorPool->getHydrator(
  98. \Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class
  99. );
  100. $shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)]));
  101. }
  102. if ($comment) {
  103. $shipment->addComment(
  104. $comment->getComment(),
  105. $appendComment,
  106. $comment->getIsVisibleOnFront()
  107. );
  108. if ($appendComment) {
  109. $shipment->setCustomerNote($comment->getComment());
  110. $shipment->setCustomerNoteNotify($appendComment);
  111. }
  112. }
  113. return $shipment;
  114. }
  115. /**
  116. * Translate OrderItemInterface array to product id => product quantity array.
  117. *
  118. * @param OrderItemInterface[] $items
  119. * @return int[]
  120. */
  121. private function getQuantitiesFromOrderItems(array $items)
  122. {
  123. $shipmentItems = [];
  124. foreach ($items as $item) {
  125. if (!$item->getIsVirtual() && (!$item->getParentItem() || $item->isShipSeparately())) {
  126. $shipmentItems[$item->getItemId()] = $item->getQtyOrdered();
  127. }
  128. }
  129. return $shipmentItems;
  130. }
  131. /**
  132. * Translate ShipmentItemCreationInterface array to product id => product quantity array.
  133. *
  134. * @param ShipmentItemCreationInterface[] $items
  135. * @return int[]
  136. */
  137. private function getQuantitiesFromShipmentItems(array $items)
  138. {
  139. $shipmentItems = [];
  140. foreach ($items as $item) {
  141. $shipmentItems[$item->getOrderItemId()] = $item->getQty();
  142. }
  143. return $shipmentItems;
  144. }
  145. }