ProcessAlgorithm.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Controller\Adminhtml\SourceSelection;
  8. use Magento\Backend\App\Action;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Backend\Model\View\Result\Page;
  11. use Magento\Framework\App\Action\HttpPostActionInterface;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\Controller\ResultFactory;
  14. use Magento\Framework\Controller\ResultInterface;
  15. use Magento\Framework\Exception\NoSuchEntityException;
  16. use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterfaceFactory;
  17. use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;
  18. use Magento\InventorySourceSelectionApi\Api\SourceSelectionServiceInterface;
  19. use Magento\InventorySourceSelectionApi\Api\GetDefaultSourceSelectionAlgorithmCodeInterface;
  20. use Magento\InventoryApi\Api\SourceRepositoryInterface;
  21. use Magento\InventorySourceSelectionApi\Model\GetInventoryRequestFromOrder;
  22. /**
  23. * ProcessAlgorithm Controller
  24. */
  25. class ProcessAlgorithm extends Action implements HttpPostActionInterface
  26. {
  27. /**
  28. * @see _isAllowed()
  29. */
  30. const ADMIN_RESOURCE = 'Magento_InventoryApi::source';
  31. /**
  32. * @var SourceSelectionServiceInterface
  33. */
  34. private $sourceSelectionService;
  35. /**
  36. * @var GetDefaultSourceSelectionAlgorithmCodeInterface
  37. */
  38. private $getDefaultSourceSelectionAlgorithmCode;
  39. /**
  40. * @var SourceRepositoryInterface
  41. */
  42. private $sourceRepository;
  43. /**
  44. * @var ItemRequestInterfaceFactory
  45. */
  46. private $itemRequestFactory;
  47. /**
  48. * @var array
  49. */
  50. private $sources = [];
  51. /**
  52. * @var GetInventoryRequestFromOrder
  53. */
  54. private $getInventoryRequestFromOrder;
  55. /**
  56. * ProcessAlgorithm constructor.
  57. *
  58. * @param Context $context
  59. * @param null $stockByWebsiteIdResolver @deprecated
  60. * @param ItemRequestInterfaceFactory $itemRequestFactory
  61. * @param null $inventoryRequestFactory @deprecated
  62. * @param SourceSelectionServiceInterface $sourceSelectionService
  63. * @param GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode
  64. * @param SourceRepositoryInterface $sourceRepository
  65. * @param GetInventoryRequestFromOrder|null $getInventoryRequestFromOrder
  66. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  67. */
  68. public function __construct(
  69. Context $context,
  70. $stockByWebsiteIdResolver,
  71. ItemRequestInterfaceFactory $itemRequestFactory,
  72. $inventoryRequestFactory,
  73. SourceSelectionServiceInterface $sourceSelectionService,
  74. GetDefaultSourceSelectionAlgorithmCodeInterface $getDefaultSourceSelectionAlgorithmCode,
  75. SourceRepositoryInterface $sourceRepository,
  76. GetInventoryRequestFromOrder $getInventoryRequestFromOrder = null
  77. ) {
  78. parent::__construct($context);
  79. $this->sourceSelectionService = $sourceSelectionService;
  80. $this->getDefaultSourceSelectionAlgorithmCode = $getDefaultSourceSelectionAlgorithmCode;
  81. $this->sourceRepository = $sourceRepository;
  82. $this->itemRequestFactory = $itemRequestFactory;
  83. $this->getInventoryRequestFromOrder = $getInventoryRequestFromOrder ?:
  84. ObjectManager::getInstance()->get(GetInventoryRequestFromOrder::class);
  85. }
  86. /**
  87. * Get request items
  88. *
  89. * @param array $requestData
  90. * @return array
  91. */
  92. private function getRequestItems(array $requestData): array
  93. {
  94. $requestItems = [];
  95. foreach ($requestData as $data) {
  96. $requestItems[] = $this->itemRequestFactory->create([
  97. 'sku' => $data['sku'],
  98. 'qty' => $data['qty']
  99. ]);
  100. }
  101. return $requestItems;
  102. }
  103. /**
  104. * @inheritdoc
  105. * @throws NoSuchEntityException
  106. */
  107. public function execute(): ResultInterface
  108. {
  109. /** @var Page $result */
  110. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  111. $request = $this->getRequest();
  112. $postRequest = $request->getPost()->toArray();
  113. $orderId = (int) $postRequest['orderId'];
  114. if (!empty($postRequest['requestData'])) {
  115. $requestData = $postRequest['requestData'];
  116. $defaultCode = $this->getDefaultSourceSelectionAlgorithmCode->execute();
  117. $algorithmCode = !empty($postRequest['algorithmCode']) ? $postRequest['algorithmCode'] : $defaultCode;
  118. $requestItems = $this->getRequestItems($requestData);
  119. $inventoryRequest = $this->getInventoryRequestFromOrder->execute($orderId, $requestItems);
  120. $sourceSelectionResult = $this->sourceSelectionService->execute($inventoryRequest, $algorithmCode);
  121. foreach ($requestData as $data) {
  122. $orderItem = $data['orderItem'];
  123. foreach ($sourceSelectionResult->getSourceSelectionItems() as $item) {
  124. if ($item->getSku() === $data['sku']) {
  125. $result[$orderItem][] = [
  126. 'sourceName' => $this->getSourceName($item->getSourceCode()),
  127. 'sourceCode' => $item->getSourceCode(),
  128. 'qtyAvailable' => $item->getQtyAvailable(),
  129. 'qtyToDeduct' => $item->getQtyToDeduct()
  130. ];
  131. }
  132. }
  133. }
  134. foreach ($this->sources as $value => $label) {
  135. $result['sourceCodes'][] = [
  136. 'value' => $value,
  137. 'label' => $label
  138. ];
  139. }
  140. $resultJson->setData($result);
  141. }
  142. return $resultJson;
  143. }
  144. /**
  145. * Get source name by code
  146. *
  147. * @param string $sourceCode
  148. * @return mixed
  149. * @throws NoSuchEntityException
  150. */
  151. public function getSourceName(string $sourceCode): string
  152. {
  153. if (!isset($this->sources[$sourceCode])) {
  154. $this->sources[$sourceCode] = $this->sourceRepository->get($sourceCode)->getName();
  155. }
  156. return $this->sources[$sourceCode];
  157. }
  158. }