Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SourceSelectionDataProvider.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
21 
23 {
27  private $request;
28 
32  private $orderRepository;
33 
37  private $stockByWebsiteIdResolver;
38 
42  private $getStockItemConfiguration;
43 
47  private $getSourcesByStockIdSkuAndQty;
48 
52  private $getSkuFromOrderItem;
53 
57  private $sources = [];
58 
73  public function __construct(
74  $name,
77  RequestInterface $request,
78  OrderRepository $orderRepository,
79  StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
80  GetStockItemConfigurationInterface $getStockItemConfiguration,
81  GetSourcesByStockIdSkuAndQty $getSourcesByStockIdSkuAndQty,
82  GetSkuFromOrderItemInterface $getSkuFromOrderItem,
83  array $meta = [],
84  array $data = []
85  ) {
86  parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
87  $this->request = $request;
88  $this->orderRepository = $orderRepository;
89  $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
90  $this->getStockItemConfiguration = $getStockItemConfiguration;
91  $this->getSourcesByStockIdSkuAndQty = $getSourcesByStockIdSkuAndQty;
92  $this->getSkuFromOrderItem = $getSkuFromOrderItem;
93  }
94 
102  public function addFilter(Filter $filter)
103  {
104  return null;
105  }
106 
110  public function getData()
111  {
112  $data = [];
113  $orderId = $this->request->getParam('order_id');
115  $order = $this->orderRepository->get($orderId);
116  $websiteId = $order->getStore()->getWebsiteId();
117  $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
118 
119  foreach ($order->getAllItems() as $orderItem) {
120  if ($orderItem->getIsVirtual()
121  || $orderItem->getLockedDoShip()
122  || $orderItem->getHasChildren()) {
123  continue;
124  }
125 
126  $item = $orderItem->isDummy(true) ? $orderItem->getParentItem() : $orderItem;
127  $qty = $item->getSimpleQtyToShip();
128  $qty = $this->castQty($item, $qty);
129  $sku = $this->getSkuFromOrderItem->execute($item);
130  $data[$orderId]['items'][] = [
131  'orderItemId' => $item->getId(),
132  'sku' => $sku,
133  'product' => $this->getProductName($orderItem),
134  'qtyToShip' => $qty,
135  'sources' => $this->getSources($stockId, $sku, $qty),
136  'isManageStock' => $this->isManageStock($sku, $stockId)
137  ];
138  }
139  $data[$orderId]['websiteId'] = $websiteId;
140  $data[$orderId]['order_id'] = $orderId;
141  foreach ($this->sources as $code => $name) {
142  $data[$orderId]['sourceCodes'][] = [
143  'value' => $code,
144  'label' => $name
145  ];
146  }
147 
148  return $data;
149  }
150 
158  private function getSources(int $stockId, string $sku, float $qty): array
159  {
160  $sources = $this->getSourcesByStockIdSkuAndQty->execute($stockId, $sku, $qty);
161  foreach ($sources as $source) {
162  $this->sources[$source['sourceCode']] = $source['sourceName'];
163  }
164  return $sources;
165  }
166 
173  private function isManageStock($itemSku, $stockId)
174  {
175  $stockItemConfiguration = $this->getStockItemConfiguration->execute($itemSku, $stockId);
176 
177  return $stockItemConfiguration->isManageStock();
178  }
179 
185  private function getProductName(Item $item)
186  {
187  //TODO: need to transfer this to html block and render on Ui
188  $name = $item->getName();
189  if ($parentItem = $item->getParentItem()) {
190  $name = $parentItem->getName();
191  $options = [];
192  if ($productOptions = $parentItem->getProductOptions()) {
193  if (isset($productOptions['options'])) {
194  $options = array_merge($options, $productOptions['options']);
195  }
196  if (isset($productOptions['additional_options'])) {
197  $options = array_merge($options, $productOptions['additional_options']);
198  }
199  if (isset($productOptions['attributes_info'])) {
200  $options = array_merge($options, $productOptions['attributes_info']);
201  }
202  if (count($options)) {
203  foreach ($options as $option) {
204  $name .= '<dd>' . $option['label'] . ': ' . $option['value'] .'</dd>';
205  }
206  } else {
207  $name .= '<dd>' . $item->getName() . '</dd>';
208  }
209  }
210  }
211 
212  return $name;
213  }
214 
220  private function castQty(Item $item, $qty)
221  {
222  if ($item->getIsQtyDecimal()) {
223  $qty = (double)$qty;
224  } else {
225  $qty = (int)$qty;
226  }
227 
228  return $qty > 0 ? $qty : 0;
229  }
230 }
$orderItem
Definition: order.php:30
$orderRepository
Definition: order.php:69
$source
Definition: source.php:23
$order
Definition: order.php:55
__construct( $name, $primaryFieldName, $requestFieldName, RequestInterface $request, OrderRepository $orderRepository, StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver, GetStockItemConfigurationInterface $getStockItemConfiguration, GetSourcesByStockIdSkuAndQty $getSourcesByStockIdSkuAndQty, GetSkuFromOrderItemInterface $getSkuFromOrderItem, array $meta=[], array $data=[])
$code
Definition: info.phtml:12