Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetItemsToDeductFromShipment.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\Sales\Model\Order\Item as OrderItem;
15 use Magento\InventorySourceDeductionApi\Model\ItemToDeductInterfaceFactory;
18 
20 {
24  private $getSkuFromOrderItem;
25 
29  private $jsonSerializer;
30 
34  private $itemToDeduct;
35 
41  public function __construct(
42  GetSkuFromOrderItemInterface $getSkuFromOrderItem,
43  Json $jsonSerializer,
44  ItemToDeductInterfaceFactory $itemToDeduct
45  ) {
46  $this->jsonSerializer = $jsonSerializer;
47  $this->itemToDeduct = $itemToDeduct;
48  $this->getSkuFromOrderItem = $getSkuFromOrderItem;
49  }
50 
56  public function execute(Shipment $shipment): array
57  {
58  $itemsToShip = [];
59 
61  foreach ($shipment->getAllItems() as $shipmentItem) {
62  $orderItem = $shipmentItem->getOrderItem();
63  // This code was added as quick fix for merge mainline
64  // https://github.com/magento-engcom/msi/issues/1586
65  if (null === $orderItem) {
66  continue;
67  }
68  if ($orderItem->getHasChildren()) {
69  if (!$orderItem->isDummy(true)) {
70  foreach ($this->processComplexItem($shipmentItem) as $item) {
71  $itemsToShip[] = $item;
72  }
73  }
74  } else {
75  $itemSku = $this->getSkuFromOrderItem->execute($orderItem);
76  $qty = $this->castQty($orderItem, $shipmentItem->getQty());
77  $itemsToShip[] = $this->itemToDeduct->create([
78  'sku' => $itemSku,
79  'qty' => $qty
80  ]);
81  }
82  }
83 
84  return $this->groupItemsBySku($itemsToShip);
85  }
86 
91  private function groupItemsBySku(array $shipmentItems): array
92  {
93  $processingItems = $groupedItems = [];
94  foreach ($shipmentItems as $shipmentItem) {
95  if (empty($processingItems[$shipmentItem->getSku()])) {
96  $processingItems[$shipmentItem->getSku()] = $shipmentItem->getQty();
97  } else {
98  $processingItems[$shipmentItem->getSku()] += $shipmentItem->getQty();
99  }
100  }
101 
102  foreach ($processingItems as $sku => $qty) {
103  $groupedItems[] = $this->itemToDeduct->create([
104  'sku' => $sku,
105  'qty' => $qty
106  ]);
107  }
108 
109  return $groupedItems;
110  }
111 
116  private function processComplexItem(Item $shipmentItem): array
117  {
118  $orderItem = $shipmentItem->getOrderItem();
119  $itemsToShip = [];
120  foreach ($orderItem->getChildrenItems() as $item) {
121  if ($item->getIsVirtual() || $item->getLockedDoShip()) {
122  continue;
123  }
124  $productOptions = $item->getProductOptions();
125  if (isset($productOptions['bundle_selection_attributes'])) {
126  $bundleSelectionAttributes = $this->jsonSerializer->unserialize(
127  $productOptions['bundle_selection_attributes']
128  );
129  if ($bundleSelectionAttributes) {
130  $qty = $bundleSelectionAttributes['qty'] * $shipmentItem->getQty();
131  $qty = $this->castQty($item, $qty);
132  $itemSku = $this->getSkuFromOrderItem->execute($item);
133  $itemsToShip[] = $this->itemToDeduct->create([
134  'sku' => $itemSku,
135  'qty' => $qty
136  ]);
137  continue;
138  }
139  } else {
140  // configurable product
141  $itemSku = $this->getSkuFromOrderItem->execute($orderItem);
142  $qty = $this->castQty($orderItem, $shipmentItem->getQty());
143  $itemsToShip[] = $this->itemToDeduct->create([
144  'sku' => $itemSku,
145  'qty' => $qty
146  ]);
147  }
148  }
149 
150  return $itemsToShip;
151  }
152 
158  private function castQty(OrderItem $item, $qty)
159  {
160  if ($item->getIsQtyDecimal()) {
161  $qty = (double)$qty;
162  } else {
163  $qty = (int)$qty;
164  }
165 
166  return $qty > 0 ? $qty : 0;
167  }
168 }
$orderItem
Definition: order.php:30
__construct(GetSkuFromOrderItemInterface $getSkuFromOrderItem, Json $jsonSerializer, ItemToDeductInterfaceFactory $itemToDeduct)
$shipmentItem
foreach($order->getItems() as $orderItem) $shipment