SourceSelectionDataProvider.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Api\Filter;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Sales\Model\OrderRepository;
  13. use Magento\Sales\Model\Order\Item;
  14. use Magento\Ui\DataProvider\AbstractDataProvider;
  15. use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
  16. use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
  17. use Magento\Framework\App\RequestInterface;
  18. use Magento\InventorySalesApi\Model\GetSkuFromOrderItemInterface;
  19. class SourceSelectionDataProvider extends AbstractDataProvider
  20. {
  21. /**
  22. * @var RequestInterface
  23. */
  24. private $request;
  25. /**
  26. * @var OrderRepository
  27. */
  28. private $orderRepository;
  29. /**
  30. * @var GetStockItemConfigurationInterface
  31. */
  32. private $getStockItemConfiguration;
  33. /**
  34. * @var GetSkuFromOrderItemInterface
  35. */
  36. private $getSkuFromOrderItem;
  37. /**
  38. * @var array
  39. */
  40. private $sources = [];
  41. /**
  42. * @var GetSourcesByOrderIdSkuAndQty
  43. */
  44. private $getSourcesByOrderIdSkuAndQty;
  45. /**
  46. * @var StockByWebsiteIdResolverInterface
  47. */
  48. private $stockByWebsiteIdResolver;
  49. /**
  50. * @param string $name
  51. * @param string $primaryFieldName
  52. * @param string $requestFieldName
  53. * @param RequestInterface $request
  54. * @param OrderRepository $orderRepository
  55. * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
  56. * @param GetStockItemConfigurationInterface $getStockItemConfiguration
  57. * @param null $getSourcesByStockIdSkuAndQty @deprecated
  58. * @param GetSkuFromOrderItemInterface $getSkuFromOrderItem
  59. * @param GetSourcesByOrderIdSkuAndQty $getSourcesByOrderIdSkuAndQty
  60. * @param array $meta
  61. * @param array $data
  62. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  63. * @SuppressWarnings(PHPMD.LongVariable)
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. public function __construct(
  67. $name,
  68. $primaryFieldName,
  69. $requestFieldName,
  70. RequestInterface $request,
  71. OrderRepository $orderRepository,
  72. StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
  73. GetStockItemConfigurationInterface $getStockItemConfiguration,
  74. $getSourcesByStockIdSkuAndQty,
  75. GetSkuFromOrderItemInterface $getSkuFromOrderItem,
  76. GetSourcesByOrderIdSkuAndQty $getSourcesByOrderIdSkuAndQty = null,
  77. array $meta = [],
  78. array $data = []
  79. ) {
  80. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  81. $this->request = $request;
  82. $this->orderRepository = $orderRepository;
  83. $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
  84. $this->getStockItemConfiguration = $getStockItemConfiguration;
  85. $this->getSkuFromOrderItem = $getSkuFromOrderItem;
  86. $this->getSourcesByOrderIdSkuAndQty = $getSourcesByOrderIdSkuAndQty ?:
  87. ObjectManager::getInstance()->get(GetSourcesByOrderIdSkuAndQty::class);
  88. }
  89. /**
  90. * Disable for collection processing
  91. *
  92. * @param Filter $filter
  93. * @return bool
  94. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  95. */
  96. public function addFilter(Filter $filter)
  97. {
  98. return null;
  99. }
  100. /**
  101. * @inheritdoc
  102. */
  103. public function getData()
  104. {
  105. $data = [];
  106. $orderId = (int) $this->request->getParam('order_id');
  107. /** @var \Magento\Sales\Model\Order $order */
  108. $order = $this->orderRepository->get($orderId);
  109. $websiteId = $order->getStore()->getWebsiteId();
  110. $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
  111. foreach ($order->getAllItems() as $orderItem) {
  112. if ($orderItem->getIsVirtual()
  113. || $orderItem->getLockedDoShip()
  114. || $orderItem->getHasChildren()) {
  115. continue;
  116. }
  117. $item = $orderItem->isDummy(true) ? $orderItem->getParentItem() : $orderItem;
  118. $qty = $item->getSimpleQtyToShip();
  119. $qty = $this->castQty($item, $qty);
  120. $sku = $this->getSkuFromOrderItem->execute($item);
  121. $data[$orderId]['items'][] = [
  122. 'orderItemId' => $item->getId(),
  123. 'sku' => $sku,
  124. 'product' => $this->getProductName($orderItem),
  125. 'qtyToShip' => $qty,
  126. 'sources' => $this->getSources($orderId, $sku, $qty),
  127. 'isManageStock' => $this->isManageStock($sku, $stockId)
  128. ];
  129. }
  130. $data[$orderId]['websiteId'] = $websiteId;
  131. $data[$orderId]['order_id'] = $orderId;
  132. foreach ($this->sources as $code => $name) {
  133. $data[$orderId]['sourceCodes'][] = [
  134. 'value' => $code,
  135. 'label' => $name
  136. ];
  137. }
  138. return $data;
  139. }
  140. /**
  141. * Get sources
  142. *
  143. * @param int $orderId
  144. * @param string $sku
  145. * @param float $qty
  146. * @return array
  147. * @throws NoSuchEntityException
  148. */
  149. private function getSources(int $orderId, string $sku, float $qty): array
  150. {
  151. $sources = $this->getSourcesByOrderIdSkuAndQty->execute($orderId, $sku, $qty);
  152. foreach ($sources as $source) {
  153. $this->sources[$source['sourceCode']] = $source['sourceName'];
  154. }
  155. return $sources;
  156. }
  157. /**
  158. * @param $itemSku
  159. * @param $stockId
  160. * @return bool
  161. * @throws LocalizedException
  162. */
  163. private function isManageStock($itemSku, $stockId)
  164. {
  165. $stockItemConfiguration = $this->getStockItemConfiguration->execute($itemSku, $stockId);
  166. return $stockItemConfiguration->isManageStock();
  167. }
  168. /**
  169. * Generate display product name
  170. * @param Item $item
  171. * @return null|string
  172. */
  173. private function getProductName(Item $item)
  174. {
  175. //TODO: need to transfer this to html block and render on Ui
  176. $name = $item->getName();
  177. if ($parentItem = $item->getParentItem()) {
  178. $name = $parentItem->getName();
  179. $options = [];
  180. if ($productOptions = $parentItem->getProductOptions()) {
  181. if (isset($productOptions['options'])) {
  182. $options = array_merge($options, $productOptions['options']);
  183. }
  184. if (isset($productOptions['additional_options'])) {
  185. $options = array_merge($options, $productOptions['additional_options']);
  186. }
  187. if (isset($productOptions['attributes_info'])) {
  188. $options = array_merge($options, $productOptions['attributes_info']);
  189. }
  190. if (count($options)) {
  191. foreach ($options as $option) {
  192. $name .= '<dd>' . $option['label'] . ': ' . $option['value'] .'</dd>';
  193. }
  194. } else {
  195. $name .= '<dd>' . $item->getName() . '</dd>';
  196. }
  197. }
  198. }
  199. return $name;
  200. }
  201. /**
  202. * @param Item $item
  203. * @param string|int|float $qty
  204. * @return float|int
  205. */
  206. private function castQty(Item $item, $qty)
  207. {
  208. if ($item->getIsQtyDecimal()) {
  209. $qty = (double)$qty;
  210. } else {
  211. $qty = (int)$qty;
  212. }
  213. return $qty > 0 ? $qty : 0;
  214. }
  215. }