1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Model\Observer; |
5
|
|
|
|
6
|
|
|
use Magento\Catalog\Model\Indexer\Product\Price\Processor as ProductPriceProcessor; |
7
|
|
|
use Magento\CatalogInventory\Api\StockManagementInterface; |
8
|
|
|
use Magento\Framework\Event\Observer as EventObserver; |
9
|
|
|
use Stockbase\Integration\Model\Inventory\StockbaseStockManagement; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CancelOrderItemObserver |
13
|
|
|
*/ |
14
|
|
|
class CancelOrderItemObserver extends \Magento\CatalogInventory\Observer\CancelOrderItemObserver |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var StockbaseStockManagement |
18
|
|
|
*/ |
19
|
|
|
private $stockbaseStockManagement; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param StockManagementInterface $stockManagement |
23
|
|
|
* @param ProductPriceProcessor $priceIndexer |
24
|
|
|
* @param StockbaseStockManagement $stockbaseStockManagement |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct( |
27
|
|
|
StockManagementInterface $stockManagement, |
28
|
|
|
ProductPriceProcessor $priceIndexer, |
29
|
|
|
StockbaseStockManagement $stockbaseStockManagement |
30
|
|
|
) { |
31
|
3 |
|
parent::__construct($stockManagement, $priceIndexer); |
32
|
|
|
|
33
|
3 |
|
$this->stockbaseStockManagement = $stockbaseStockManagement; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
3 |
|
public function execute(EventObserver $observer) |
40
|
|
|
{ |
41
|
|
|
/** @var \Magento\Sales\Model\Order\Item $item */ |
42
|
3 |
|
$item = $observer->getEvent()->getItem(); |
43
|
|
|
|
44
|
3 |
|
$children = $item->getChildrenItems(); |
45
|
3 |
|
$qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled(); |
46
|
3 |
|
if ($item->getId() && $item->getProductId() && empty($children) && $qty) { |
47
|
3 |
|
$reserve = $this->stockbaseStockManagement->getReserveForQuoteItem($item->getQuoteItemId()); |
48
|
3 |
View Code Duplication |
if (!empty($reserve)) { |
|
|
|
|
49
|
3 |
|
$reserve = reset($reserve); |
50
|
3 |
|
$qty -= $reserve->getAmount(); |
51
|
|
|
//if ($qty != $reserve->getMagentoStockAmount()) { |
52
|
|
|
// throw new \Exception('Invalid quote inventory revert.'); //TODO: Enable additional checks |
53
|
|
|
//} |
54
|
3 |
|
$this->stockbaseStockManagement->releaseReserve($reserve); |
|
|
|
|
55
|
|
|
} |
56
|
3 |
|
$this->stockManagement->backItemQty($item->getProductId(), $qty, $item->getStore()->getWebsiteId()); |
57
|
|
|
} |
58
|
3 |
|
$this->priceIndexer->reindexRow($item->getProductId()); |
59
|
3 |
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.