GetSourcesByOrderIdSkuAndQty.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\InventoryShippingAdminUi\Ui\DataProvider;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\InventorySourceSelectionApi\Model\GetInventoryRequestFromOrder;
  10. use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;
  11. use Magento\InventorySourceSelectionApi\Api\SourceSelectionServiceInterface;
  12. use Magento\InventorySourceSelectionApi\Api\GetDefaultSourceSelectionAlgorithmCodeInterface;
  13. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  14. /**
  15. * Class GetSourcesByOrderIdSkuAndQty
  16. */
  17. class GetSourcesByOrderIdSkuAndQty
  18. {
  19. /**
  20. * @var ItemRequestInterfaceFactory
  21. */
  22. private $itemRequestFactory;
  23. /**
  24. * @var SourceSelectionServiceInterface
  25. */
  26. private $sourceSelectionService;
  27. /**
  28. * @var GetDefaultSourceSelectionAlgorithmCodeInterface
  29. */
  30. private $getDefaultSourceSelectionAlgorithmCode;
  31. /**
  32. * @var SourceRepositoryInterface
  33. */
  34. private $sourceRepository;
  35. /**
  36. * @var GetInventoryRequestFromOrder
  37. */
  38. private $getInventoryRequestFromOrder;
  39. /**
  40. * @var array
  41. */
  42. private $sources = [];
  43. /**
  44. * @param ItemRequestInterfaceFactory $itemRequestFactory
  45. * @param SourceSelectionServiceInterface $sourceSelectionService
  46. * @param GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode
  47. * @param GetInventoryRequestFromOrder $getInventoryRequestFromOrder
  48. * @param SourceRepositoryInterface $sourceRepository
  49. * @SuppressWarnings(PHPMD.LongVariable)
  50. */
  51. public function __construct(
  52. ItemRequestInterfaceFactory $itemRequestFactory,
  53. SourceSelectionServiceInterface $sourceSelectionService,
  54. GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode,
  55. GetInventoryRequestFromOrder $getInventoryRequestFromOrder,
  56. SourceRepositoryInterface $sourceRepository
  57. ) {
  58. $this->itemRequestFactory = $itemRequestFactory;
  59. $this->sourceSelectionService = $sourceSelectionService;
  60. $this->getDefaultSourceSelectionAlgorithmCode = $getDefaultSourceSelectionAlgorithmCode;
  61. $this->sourceRepository = $sourceRepository;
  62. $this->getInventoryRequestFromOrder = $getInventoryRequestFromOrder;
  63. }
  64. /**
  65. * Get sources by order id sku and qty
  66. *
  67. * @param int $orderId
  68. * @param string $sku
  69. * @param float $qty
  70. * @return array
  71. * @throws NoSuchEntityException
  72. * @SuppressWarnings(PHPMD.LongVariable)
  73. */
  74. public function execute(int $orderId, string $sku, float $qty): array
  75. {
  76. $algorithmCode = $this->getDefaultSourceSelectionAlgorithmCode->execute();
  77. $requestItem = $this->itemRequestFactory->create([
  78. 'sku' => $sku,
  79. 'qty' => $qty
  80. ]);
  81. $inventoryRequest = $this->getInventoryRequestFromOrder->execute($orderId, [$requestItem]);
  82. $sourceSelectionResult = $this->sourceSelectionService->execute(
  83. $inventoryRequest,
  84. $algorithmCode
  85. );
  86. $result = [];
  87. foreach ($sourceSelectionResult->getSourceSelectionItems() as $item) {
  88. $sourceCode = $item->getSourceCode();
  89. $result[] = [
  90. 'sourceName' => $this->getSourceName($sourceCode),
  91. 'sourceCode' => $sourceCode,
  92. 'qtyAvailable' => $item->getQtyAvailable(),
  93. 'qtyToDeduct' => $item->getQtyToDeduct()
  94. ];
  95. }
  96. return $result;
  97. }
  98. /**
  99. * Get source name by code
  100. *
  101. * @param string $sourceCode
  102. * @return mixed
  103. * @throws NoSuchEntityException
  104. */
  105. private function getSourceName(string $sourceCode): string
  106. {
  107. if (!isset($this->sources[$sourceCode])) {
  108. $this->sources[$sourceCode] = $this->sourceRepository->get($sourceCode)->getName();
  109. }
  110. return $this->sources[$sourceCode];
  111. }
  112. }