GetSourcesByStockIdSkuAndQty.php 4.2 KB

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