GetInventoryRequestFromOrder.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySourceSelectionApi\Model;
  8. use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestExtensionInterfaceFactory;
  9. use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;
  10. use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
  11. use Magento\InventorySourceSelectionApi\Api\Data\AddressInterfaceFactory;
  12. use Magento\InventorySourceSelectionApi\Api\Data\AddressInterface;
  13. use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
  14. use Magento\Sales\Api\Data\OrderInterface;
  15. use Magento\Sales\Model\Order\Address;
  16. use Magento\Sales\Api\OrderRepositoryInterface;
  17. use Magento\Store\Model\StoreManagerInterface;
  18. /**
  19. * Build inventory request based on Order Id
  20. *
  21. * @api
  22. */
  23. class GetInventoryRequestFromOrder
  24. {
  25. /**
  26. * @var InventoryRequestInterfaceFactory
  27. */
  28. private $inventoryRequestFactory;
  29. /**
  30. * @var InventoryRequestExtensionInterfaceFactory
  31. */
  32. private $inventoryRequestExtensionFactory;
  33. /**
  34. * @var OrderRepositoryInterface
  35. */
  36. private $orderRepository;
  37. /**
  38. * @var AddressInterfaceFactory
  39. */
  40. private $addressInterfaceFactory;
  41. /**
  42. * @var StoreManagerInterface
  43. */
  44. private $storeManager;
  45. /**
  46. * @var StockByWebsiteIdResolverInterface
  47. */
  48. private $stockByWebsiteIdResolver;
  49. /**
  50. * @param InventoryRequestInterfaceFactory $inventoryRequestFactory
  51. * @param InventoryRequestExtensionInterfaceFactory $inventoryRequestExtensionFactory
  52. * @param OrderRepositoryInterface $orderRepository
  53. * @param AddressInterfaceFactory $addressInterfaceFactory
  54. * @param StoreManagerInterface $storeManager
  55. * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
  56. */
  57. public function __construct(
  58. InventoryRequestInterfaceFactory $inventoryRequestFactory,
  59. InventoryRequestExtensionInterfaceFactory $inventoryRequestExtensionFactory,
  60. OrderRepositoryInterface $orderRepository,
  61. AddressInterfaceFactory $addressInterfaceFactory,
  62. StoreManagerInterface $storeManager,
  63. StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
  64. ) {
  65. $this->inventoryRequestFactory = $inventoryRequestFactory;
  66. $this->inventoryRequestExtensionFactory = $inventoryRequestExtensionFactory;
  67. $this->orderRepository = $orderRepository;
  68. $this->addressInterfaceFactory = $addressInterfaceFactory;
  69. $this->storeManager = $storeManager;
  70. $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
  71. }
  72. /**
  73. * Build inventory request based on Order Id and provided request items
  74. *
  75. * @param int $orderId
  76. * @param array $requestItems
  77. */
  78. public function execute(int $orderId, array $requestItems): InventoryRequestInterface
  79. {
  80. /** @var OrderInterface $order */
  81. $order = $this->orderRepository->get($orderId);
  82. $store = $this->storeManager->getStore($order->getStoreId());
  83. $stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId());
  84. $inventoryRequest = $this->inventoryRequestFactory->create([
  85. 'stockId' => $stock->getStockId(),
  86. 'items' => $requestItems
  87. ]);
  88. $address = $this->getAddressFromOrder($order);
  89. if ($address !== null) {
  90. $extensionAttributes = $this->inventoryRequestExtensionFactory->create();
  91. $extensionAttributes->setDestinationAddress($address);
  92. $inventoryRequest->setExtensionAttributes($extensionAttributes);
  93. }
  94. return $inventoryRequest;
  95. }
  96. /**
  97. * Create an address from an order
  98. *
  99. * @param OrderInterface $order
  100. * @return null|AddressInterface
  101. */
  102. private function getAddressFromOrder(OrderInterface $order): ?AddressInterface
  103. {
  104. /** @var Address $shippingAddress */
  105. $shippingAddress = $order->getShippingAddress();
  106. if ($shippingAddress === null) {
  107. return null;
  108. }
  109. return $this->addressInterfaceFactory->create([
  110. 'country' => $shippingAddress->getCountryId(),
  111. 'postcode' => $shippingAddress->getPostcode(),
  112. 'street' => implode("\n", $shippingAddress->getStreet()),
  113. 'region' => $shippingAddress->getRegion() ?? $shippingAddress->getRegionCode() ?? '',
  114. 'city' => $shippingAddress->getCity()
  115. ]);
  116. }
  117. }