StockDataProvider.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\InventoryAdminUi\Ui\DataProvider;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\Api\Search\ReportingInterface;
  10. use Magento\Framework\Api\Search\SearchCriteriaBuilder as SearchSearchCriteriaBuilder;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\Framework\Api\SortOrderBuilder;
  13. use Magento\Framework\App\RequestInterface;
  14. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
  15. use Magento\InventoryApi\Api\Data\SourceInterface;
  16. use Magento\InventoryApi\Api\Data\StockInterface;
  17. use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
  18. use Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface;
  19. use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
  20. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  21. use Magento\InventoryApi\Api\StockRepositoryInterface;
  22. use Magento\Ui\DataProvider\SearchResultFactory;
  23. /**
  24. * @api
  25. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  26. */
  27. class StockDataProvider extends DataProvider
  28. {
  29. /**
  30. * @var StockRepositoryInterface
  31. */
  32. private $stockRepository;
  33. /**
  34. * @var SearchResultFactory
  35. */
  36. private $searchResultFactory;
  37. /**
  38. * @var GetStockSourceLinksInterface
  39. */
  40. private $getStockSourceLinks;
  41. /**
  42. * @var SourceRepositoryInterface
  43. */
  44. private $sourceRepository;
  45. /**
  46. * @var SearchCriteriaBuilder
  47. */
  48. private $apiSearchCriteriaBuilder;
  49. /**
  50. * @var SortOrderBuilder
  51. */
  52. private $sortOrderBuilder;
  53. /**
  54. * @var GetSourcesAssignedToStockOrderedByPriorityInterface
  55. */
  56. private $getSourcesAssignedToStockOrderedByPriority;
  57. /**
  58. * @param string $name
  59. * @param string $primaryFieldName
  60. * @param string $requestFieldName
  61. * @param ReportingInterface $reporting
  62. * @param SearchSearchCriteriaBuilder $searchCriteriaBuilder
  63. * @param RequestInterface $request
  64. * @param FilterBuilder $filterBuilder
  65. * @param StockRepositoryInterface $stockRepository
  66. * @param SearchResultFactory $searchResultFactory
  67. * @param GetStockSourceLinksInterface $getStockSourceLinks
  68. * @param SourceRepositoryInterface $sourceRepository
  69. * @param SearchCriteriaBuilder $apiSearchCriteriaBuilder
  70. * @param SortOrderBuilder $sortOrderBuilder
  71. * @param array $meta
  72. * @param array $data
  73. * @param GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority
  74. * @SuppressWarnings(PHPMD.ExcessiveParameterList) All parameters are needed for backward compatibility
  75. */
  76. public function __construct(
  77. $name,
  78. $primaryFieldName,
  79. $requestFieldName,
  80. ReportingInterface $reporting,
  81. SearchSearchCriteriaBuilder $searchCriteriaBuilder,
  82. RequestInterface $request,
  83. FilterBuilder $filterBuilder,
  84. StockRepositoryInterface $stockRepository,
  85. SearchResultFactory $searchResultFactory,
  86. GetStockSourceLinksInterface $getStockSourceLinks,
  87. SourceRepositoryInterface $sourceRepository,
  88. SearchCriteriaBuilder $apiSearchCriteriaBuilder,
  89. SortOrderBuilder $sortOrderBuilder,
  90. GetSourcesAssignedToStockOrderedByPriorityInterface $getSourcesAssignedToStockOrderedByPriority,
  91. array $meta = [],
  92. array $data = []
  93. ) {
  94. parent::__construct(
  95. $name,
  96. $primaryFieldName,
  97. $requestFieldName,
  98. $reporting,
  99. $searchCriteriaBuilder,
  100. $request,
  101. $filterBuilder,
  102. $meta,
  103. $data
  104. );
  105. $this->stockRepository = $stockRepository;
  106. $this->searchResultFactory = $searchResultFactory;
  107. $this->getStockSourceLinks = $getStockSourceLinks;
  108. $this->sourceRepository = $sourceRepository;
  109. $this->apiSearchCriteriaBuilder = $apiSearchCriteriaBuilder;
  110. $this->sortOrderBuilder = $sortOrderBuilder;
  111. $this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getData()
  117. {
  118. $data = parent::getData();
  119. if ('inventory_stock_form_data_source' === $this->name) {
  120. // It is need for support of several fieldsets.
  121. // For details see \Magento\Ui\Component\Form::getDataSourceData
  122. if ($data['totalRecords'] > 0) {
  123. $stockId = (int)$data['items'][0][StockInterface::STOCK_ID];
  124. $stockGeneralData = $data['items'][0];
  125. $dataForSingle[$stockId] = [
  126. 'general' => $stockGeneralData,
  127. 'sources' => [
  128. 'assigned_sources' => $this->getAssignedSourcesData($stockId),
  129. ],
  130. ];
  131. $data = $dataForSingle;
  132. } else {
  133. $data = [];
  134. }
  135. } elseif ('inventory_stock_listing_data_stock' === $this->name) {
  136. if ($data['totalRecords'] > 0) {
  137. foreach ($data['items'] as $index => $stock) {
  138. $data['items'][$index]['assigned_sources'] = $this->getAssignedSourcesById($stock['stock_id']);
  139. }
  140. }
  141. }
  142. return $data;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getSearchResult()
  148. {
  149. $searchCriteria = $this->getSearchCriteria();
  150. $result = $this->stockRepository->getList($searchCriteria);
  151. $searchResult = $this->searchResultFactory->create(
  152. $result->getItems(),
  153. $result->getTotalCount(),
  154. $searchCriteria,
  155. StockInterface::STOCK_ID
  156. );
  157. return $searchResult;
  158. }
  159. /**
  160. * @param int $stockId
  161. * @return array
  162. */
  163. private function getAssignedSourcesData(int $stockId): array
  164. {
  165. $sortOrder = $this->sortOrderBuilder
  166. ->setField(StockSourceLinkInterface::PRIORITY)
  167. ->setAscendingDirection()
  168. ->create();
  169. $searchCriteria = $this->apiSearchCriteriaBuilder
  170. ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
  171. ->addSortOrder($sortOrder)
  172. ->create();
  173. $searchResult = $this->getStockSourceLinks->execute($searchCriteria);
  174. if ($searchResult->getTotalCount() === 0) {
  175. return [];
  176. }
  177. $assignedSourcesData = [];
  178. foreach ($searchResult->getItems() as $link) {
  179. $source = $this->sourceRepository->get($link->getSourceCode());
  180. $assignedSourcesData[] = [
  181. SourceInterface::NAME => $source->getName(),
  182. StockSourceLinkInterface::SOURCE_CODE => $link->getSourceCode(),
  183. StockSourceLinkInterface::STOCK_ID => $link->getStockId(),
  184. StockSourceLinkInterface::PRIORITY => $link->getPriority(),
  185. ];
  186. }
  187. return $assignedSourcesData;
  188. }
  189. /**
  190. * @param int $stockId
  191. * @return array
  192. * @throws \Magento\Framework\Exception\InputException
  193. * @throws \Magento\Framework\Exception\LocalizedException
  194. */
  195. private function getAssignedSourcesById(int $stockId): array
  196. {
  197. $sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stockId);
  198. $sourcesData = [];
  199. foreach ($sources as $source) {
  200. $sourcesData[] = [
  201. 'sourceCode' => $source->getSourceCode(),
  202. 'name' => $source->getName()
  203. ];
  204. }
  205. return $sourcesData;
  206. }
  207. }