stockRepository = $stockRepository; $this->searchResultFactory = $searchResultFactory; $this->getStockSourceLinks = $getStockSourceLinks; $this->sourceRepository = $sourceRepository; $this->apiSearchCriteriaBuilder = $apiSearchCriteriaBuilder; $this->sortOrderBuilder = $sortOrderBuilder; $this->getSourcesAssignedToStockOrderedByPriority = $getSourcesAssignedToStockOrderedByPriority; } /** * {@inheritdoc} */ public function getData() { $data = parent::getData(); if ('inventory_stock_form_data_source' === $this->name) { // It is need for support of several fieldsets. // For details see \Magento\Ui\Component\Form::getDataSourceData if ($data['totalRecords'] > 0) { $stockId = (int)$data['items'][0][StockInterface::STOCK_ID]; $stockGeneralData = $data['items'][0]; $dataForSingle[$stockId] = [ 'general' => $stockGeneralData, 'sources' => [ 'assigned_sources' => $this->getAssignedSourcesData($stockId), ], ]; $data = $dataForSingle; } else { $data = []; } } elseif ('inventory_stock_listing_data_stock' === $this->name) { if ($data['totalRecords'] > 0) { foreach ($data['items'] as $index => $stock) { $data['items'][$index]['assigned_sources'] = $this->getAssignedSourcesById($stock['stock_id']); } } } return $data; } /** * {@inheritdoc} */ public function getSearchResult() { $searchCriteria = $this->getSearchCriteria(); $result = $this->stockRepository->getList($searchCriteria); $searchResult = $this->searchResultFactory->create( $result->getItems(), $result->getTotalCount(), $searchCriteria, StockInterface::STOCK_ID ); return $searchResult; } /** * @param int $stockId * @return array */ private function getAssignedSourcesData(int $stockId): array { $sortOrder = $this->sortOrderBuilder ->setField(StockSourceLinkInterface::PRIORITY) ->setAscendingDirection() ->create(); $searchCriteria = $this->apiSearchCriteriaBuilder ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId) ->addSortOrder($sortOrder) ->create(); $searchResult = $this->getStockSourceLinks->execute($searchCriteria); if ($searchResult->getTotalCount() === 0) { return []; } $assignedSourcesData = []; foreach ($searchResult->getItems() as $link) { $source = $this->sourceRepository->get($link->getSourceCode()); $assignedSourcesData[] = [ SourceInterface::NAME => $source->getName(), StockSourceLinkInterface::SOURCE_CODE => $link->getSourceCode(), StockSourceLinkInterface::STOCK_ID => $link->getStockId(), StockSourceLinkInterface::PRIORITY => $link->getPriority(), ]; } return $assignedSourcesData; } /** * @param int $stockId * @return array * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\LocalizedException */ private function getAssignedSourcesById(int $stockId): array { $sources = $this->getSourcesAssignedToStockOrderedByPriority->execute($stockId); $sourcesData = []; foreach ($sources as $source) { $sourcesData[] = [ 'sourceCode' => $source->getSourceCode(), 'name' => $source->getName() ]; } return $sourcesData; } }