1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Model\Observer; |
5
|
|
|
|
6
|
|
|
use Magento\CatalogInventory\Api\StockRegistryInterface; |
7
|
|
|
use Magento\CatalogInventory\Observer\ItemsForReindex; |
8
|
|
|
use Magento\CatalogInventory\Observer\ProductQty; |
9
|
|
|
use Magento\CatalogInventory\Api\StockManagementInterface; |
10
|
|
|
use Magento\Framework\Event\Observer as EventObserver; |
11
|
|
|
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class SubtractQuoteInventoryObserver |
15
|
|
|
*/ |
16
|
|
|
class SubtractQuoteInventoryObserver extends \Magento\CatalogInventory\Observer\SubtractQuoteInventoryObserver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var StockRegistryInterface |
20
|
|
|
*/ |
21
|
|
|
private $stockRegistry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var StockbaseStockManagement |
25
|
|
|
*/ |
26
|
|
|
private $stockbaseStockManagement; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SubtractQuoteInventoryObserver constructor. |
30
|
|
|
* @param StockManagementInterface $stockManagement |
31
|
|
|
* @param ProductQty $productQty |
32
|
|
|
* @param ItemsForReindex $itemsForReindex |
33
|
|
|
* @param StockRegistryInterface $stockRegistry |
34
|
|
|
* @param StockbaseStockManagement $stockbaseStockManagement |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct( |
37
|
|
|
StockManagementInterface $stockManagement, |
38
|
|
|
ProductQty $productQty, |
39
|
|
|
ItemsForReindex $itemsForReindex, |
40
|
|
|
StockRegistryInterface $stockRegistry, |
41
|
|
|
StockbaseStockManagement $stockbaseStockManagement |
42
|
|
|
) { |
43
|
4 |
|
parent::__construct($stockManagement, $productQty, $itemsForReindex); |
44
|
4 |
|
$this->stockRegistry = $stockRegistry; |
45
|
4 |
|
$this->stockbaseStockManagement = $stockbaseStockManagement; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
4 |
|
public function execute(EventObserver $observer) |
52
|
|
|
{ |
53
|
|
|
/** @var \Magento\Quote\Model\Quote $quote */ |
54
|
4 |
|
$quote = $observer->getEvent()->getQuote(); |
55
|
|
|
|
56
|
|
|
/** @var \Magento\Sales\Model\Order $order */ |
57
|
4 |
|
$order = $observer->getEvent()->getOrder(); |
58
|
|
|
|
59
|
|
|
// Maybe we've already processed this quote in some event during order placement |
60
|
|
|
// e.g. call in event 'sales_model_service_quote_submit_before' and later in 'checkout_submit_all_after' |
61
|
4 |
|
if ($quote->getInventoryProcessed()) { |
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
$items = []; |
66
|
3 |
|
foreach ($quote->getAllItems() as $item) { |
67
|
2 |
|
$productId = $item->getProductId(); |
68
|
2 |
|
if (!$productId) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
2 |
|
$children = $item->getChildrenItems() ?: [$item]; |
72
|
2 |
|
foreach ($children as $childItem) { |
73
|
2 |
|
$productId = $childItem->getProductId(); |
74
|
2 |
|
if (!$productId) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if (!isset($items[$productId])) { |
79
|
2 |
|
$items[$productId] = 0; |
80
|
|
|
} |
81
|
2 |
|
$items[$productId] += $childItem->getTotalQty(); |
82
|
|
|
|
83
|
2 |
|
$stockItem = $this->stockRegistry->getStockItem($productId); |
84
|
2 |
|
$stockQty = max($stockItem->getQty(), 0); |
85
|
2 |
|
if ($this->stockbaseStockManagement->isStockbaseProduct($productId) && |
86
|
2 |
|
$items[$productId] > $stockQty |
87
|
|
|
) { |
88
|
2 |
|
$diff = $items[$productId] - $stockQty; |
89
|
2 |
|
$items[$productId] = $stockQty; |
90
|
|
|
|
91
|
2 |
|
$stockbaseAmount = $this->stockbaseStockManagement->getStockbaseStockAmount($productId); |
92
|
2 |
|
if ($stockbaseAmount - $diff < 0) { |
93
|
1 |
|
throw new \Magento\Framework\Exception\LocalizedException( |
94
|
1 |
|
__('Not all of your products are available in the requested quantity.') |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$reserveItem = $this->stockbaseStockManagement->createReserve($childItem, $diff, $childItem->getTotalQty() - $diff); |
99
|
1 |
|
$order->addStatusHistoryComment(__( |
100
|
1 |
|
'Local Stockbase reserve created for EAN "%1" (%2 pc.)', |
101
|
1 |
|
$reserveItem->getEan(), |
102
|
1 |
|
$reserveItem->getAmount() |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remember items |
110
|
|
|
*/ |
111
|
2 |
|
$itemsForReindex = $this->stockManagement->registerProductsSale( |
112
|
2 |
|
$items, |
113
|
2 |
|
$quote->getStore()->getWebsiteId() |
114
|
|
|
); |
115
|
2 |
|
$this->itemsForReindex->setItems($itemsForReindex); |
116
|
|
|
|
117
|
2 |
|
$quote->setInventoryProcessed(true); |
118
|
|
|
|
119
|
2 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|