Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessAlgorithm.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;
17 use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
21 
25 class ProcessAlgorithm extends Action
26 {
30  const ADMIN_RESOURCE = 'Magento_InventoryApi::source';
31 
35  private $stockByWebsiteIdResolver;
36 
40  private $itemRequestFactory;
41 
45  private $inventoryRequestFactory;
46 
50  private $sourceSelectionService;
51 
55  private $getDefaultSourceSelectionAlgorithmCode;
56 
60  private $sourceRepository;
61 
65  private $sources = [];
66 
76  public function __construct(
77  Context $context,
78  StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
79  ItemRequestInterfaceFactory $itemRequestFactory,
80  InventoryRequestInterfaceFactory $inventoryRequestFactory,
81  SourceSelectionServiceInterface $sourceSelectionService,
82  GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode,
83  SourceRepositoryInterface $sourceRepository
84  ) {
85  parent::__construct($context);
86  $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
87  $this->itemRequestFactory = $itemRequestFactory;
88  $this->inventoryRequestFactory = $inventoryRequestFactory;
89  $this->sourceSelectionService = $sourceSelectionService;
90  $this->getDefaultSourceSelectionAlgorithmCode = $getDefaultSourceSelectionAlgorithmCode;
91  $this->sourceRepository = $sourceRepository;
92  }
93 
97  public function execute(): ResultInterface
98  {
100  $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
101  $request = $this->getRequest();
102  $postRequest = $request->getPost()->toArray();
103 
104  if ($request->isPost() && !empty($postRequest['requestData'])) {
105  $requestData = $postRequest['requestData'];
106  $defaultCode = $this->getDefaultSourceSelectionAlgorithmCode->execute();
107  $algorithmCode = !empty($postRequest['algorithmCode']) ? $postRequest['algorithmCode'] : $defaultCode;
108 
109  //TODO: maybe need to add exception when websiteId empty
110  $websiteId = $postRequest['websiteId'] ?? 1;
111  $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
112 
113  $result = $requestItems = [];
114  foreach ($requestData as $data) {
115  $requestItems[] = $this->itemRequestFactory->create([
116  'sku' => $data['sku'],
117  'qty' => $data['qty']
118  ]);
119  }
120  $inventoryRequest = $this->inventoryRequestFactory->create([
121  'stockId' => $stockId,
122  'items' => $requestItems
123  ]);
124 
125  $sourceSelectionResult = $this->sourceSelectionService->execute($inventoryRequest, $algorithmCode);
126 
127  foreach ($requestData as $data) {
128  $orderItem = $data['orderItem'];
129  foreach ($sourceSelectionResult->getSourceSelectionItems() as $item) {
130  if ($item->getSku() === $data['sku']) {
131  $result[$orderItem][] = [
132  'sourceName' => $this->getSourceName($item->getSourceCode()),
133  'sourceCode' => $item->getSourceCode(),
134  'qtyAvailable' => $item->getQtyAvailable(),
135  'qtyToDeduct' => $item->getQtyToDeduct()
136  ];
137  }
138  }
139  }
140 
141  foreach ($this->sources as $value => $label) {
142  $result['sourceCodes'][] = [
143  'value' => $value,
144  'label' => $label
145  ];
146  }
147  $resultJson->setData($result);
148  }
149 
150  return $resultJson;
151  }
152 
160  public function getSourceName(string $sourceCode): string
161  {
162  if (!isset($this->sources[$sourceCode])) {
163  $this->sources[$sourceCode] = $this->sourceRepository->get($sourceCode)->getName();
164  }
165 
166  return $this->sources[$sourceCode];
167  }
168 }
$orderItem
Definition: order.php:30
__construct(Context $context, StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver, ItemRequestInterfaceFactory $itemRequestFactory, InventoryRequestInterfaceFactory $inventoryRequestFactory, SourceSelectionServiceInterface $sourceSelectionService, GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode, SourceRepositoryInterface $sourceRepository)
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$sourceCode
Definition: inventory.phtml:11
$sourceRepository
Definition: source.php:20