1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryShipping\Model;
use Magento\Sales\Model\Order\Shipment\Item;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\InventorySalesApi\Model\GetSkuFromOrderItemInterface;
use Magento\InventorySourceDeductionApi\Model\ItemToDeductInterface;
use Magento\InventorySourceDeductionApi\Model\ItemToDeductInterfaceFactory;
use Magento\Sales\Model\Order\Shipment;
use Magento\Framework\Exception\NoSuchEntityException;
class GetItemsToDeductFromShipment
{
/**
* @var GetSkuFromOrderItemInterface
*/
private $getSkuFromOrderItem;
/**
* @var Json
*/
private $jsonSerializer;
/**
* @var ItemToDeductInterfaceFactory
*/
private $itemToDeduct;
/**
* @param GetSkuFromOrderItemInterface $getSkuFromOrderItem
* @param Json $jsonSerializer
* @param ItemToDeductInterfaceFactory $itemToDeduct
*/
public function __construct(
GetSkuFromOrderItemInterface $getSkuFromOrderItem,
Json $jsonSerializer,
ItemToDeductInterfaceFactory $itemToDeduct
) {
$this->jsonSerializer = $jsonSerializer;
$this->itemToDeduct = $itemToDeduct;
$this->getSkuFromOrderItem = $getSkuFromOrderItem;
}
/**
* @param Shipment $shipment
* @return ItemToDeductInterface[]
* @throws NoSuchEntityException
*/
public function execute(Shipment $shipment): array
{
$itemsToShip = [];
/** @var \Magento\Sales\Model\Order\Shipment\Item $shipmentItem */
foreach ($shipment->getAllItems() as $shipmentItem) {
$orderItem = $shipmentItem->getOrderItem();
// This code was added as quick fix for merge mainline
// https://github.com/magento-engcom/msi/issues/1586
if (null === $orderItem) {
continue;
}
if ($orderItem->getHasChildren()) {
if (!$orderItem->isDummy(true)) {
foreach ($this->processComplexItem($shipmentItem) as $item) {
$itemsToShip[] = $item;
}
}
} else {
$itemSku = $this->getSkuFromOrderItem->execute($orderItem);
$qty = $this->castQty($orderItem, $shipmentItem->getQty());
$itemsToShip[] = $this->itemToDeduct->create([
'sku' => $itemSku,
'qty' => $qty
]);
}
}
return $this->groupItemsBySku($itemsToShip);
}
/**
* @param array $shipmentItems
* @return array
*/
private function groupItemsBySku(array $shipmentItems): array
{
$processingItems = $groupedItems = [];
foreach ($shipmentItems as $shipmentItem) {
if (empty($processingItems[$shipmentItem->getSku()])) {
$processingItems[$shipmentItem->getSku()] = $shipmentItem->getQty();
} else {
$processingItems[$shipmentItem->getSku()] += $shipmentItem->getQty();
}
}
foreach ($processingItems as $sku => $qty) {
$groupedItems[] = $this->itemToDeduct->create([
'sku' => $sku,
'qty' => $qty
]);
}
return $groupedItems;
}
/**
* @param Item $shipmentItem
* @return array
*/
private function processComplexItem(Item $shipmentItem): array
{
$orderItem = $shipmentItem->getOrderItem();
$itemsToShip = [];
foreach ($orderItem->getChildrenItems() as $item) {
if ($item->getIsVirtual() || $item->getLockedDoShip()) {
continue;
}
$productOptions = $item->getProductOptions();
if (isset($productOptions['bundle_selection_attributes'])) {
$bundleSelectionAttributes = $this->jsonSerializer->unserialize(
$productOptions['bundle_selection_attributes']
);
if ($bundleSelectionAttributes) {
$qty = $bundleSelectionAttributes['qty'] * $shipmentItem->getQty();
$qty = $this->castQty($item, $qty);
$itemSku = $this->getSkuFromOrderItem->execute($item);
$itemsToShip[] = $this->itemToDeduct->create([
'sku' => $itemSku,
'qty' => $qty
]);
continue;
}
} else {
// configurable product
$itemSku = $this->getSkuFromOrderItem->execute($orderItem);
$qty = $this->castQty($orderItem, $shipmentItem->getQty());
$itemsToShip[] = $this->itemToDeduct->create([
'sku' => $itemSku,
'qty' => $qty
]);
}
}
return $itemsToShip;
}
/**
* @param OrderItem $item
* @param string|int|float $qty
* @return float|int
*/
private function castQty(OrderItem $item, $qty)
{
if ($item->getIsQtyDecimal()) {
$qty = (double)$qty;
} else {
$qty = (int)$qty;
}
return $qty > 0 ? $qty : 0;
}
}