1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Stockbase\Integration\Model\Inventory; |
5
|
|
|
|
6
|
|
|
use Magento\Catalog\Model\ProductFactory; |
7
|
|
|
use Magento\CatalogInventory\Api\Data\StockItemInterface; |
8
|
|
|
use Magento\Framework\Locale\FormatInterface; |
9
|
|
|
use Magento\Framework\Math\Division as MathDivision; |
10
|
|
|
use Magento\Framework\DataObject\Factory as ObjectFactory; |
11
|
|
|
use Magento\Framework\ObjectManagerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class StockStateProvider |
15
|
|
|
*/ |
16
|
|
|
class StockStateProvider extends \Magento\CatalogInventory\Model\StockStateProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var StockbaseStockManagement |
20
|
|
|
*/ |
21
|
|
|
private $stockbaseStockManagement; |
22
|
|
|
/** |
23
|
|
|
* @var ObjectManagerInterface |
24
|
|
|
*/ |
25
|
|
|
private $objectManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ObjectManagerInterface $objectManager |
29
|
|
|
* @param MathDivision $mathDivision |
30
|
|
|
* @param FormatInterface $localeFormat |
31
|
|
|
* @param ObjectFactory $objectFactory |
32
|
|
|
* @param ProductFactory $productFactory |
33
|
|
|
* @param bool $qtyCheckApplicable |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
ObjectManagerInterface $objectManager, |
37
|
|
|
MathDivision $mathDivision, |
38
|
|
|
FormatInterface $localeFormat, |
39
|
|
|
ObjectFactory $objectFactory, |
40
|
|
|
ProductFactory $productFactory, |
41
|
|
|
$qtyCheckApplicable = true |
42
|
|
|
) { |
43
|
|
|
parent::__construct($mathDivision, $localeFormat, $objectFactory, $productFactory, $qtyCheckApplicable); |
44
|
|
|
$this->objectManager = $objectManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function verifyStock(StockItemInterface $stockItem) |
51
|
|
|
{ |
52
|
|
|
return parent::verifyStock($this->ensureStockbaseStockItem($stockItem)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQty, $origQty = 0) |
59
|
|
|
{ |
60
|
|
|
return parent::checkQuoteItemQty($this->ensureStockbaseStockItem($stockItem), $qty, $summaryQty, $origQty); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function checkQty(StockItemInterface $stockItem, $qty) |
67
|
|
|
{ |
68
|
|
|
return parent::checkQty($this->ensureStockbaseStockItem($stockItem), $qty); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getStockQty(StockItemInterface $stockItem) |
75
|
|
|
{ |
76
|
|
|
return parent::getStockQty($this->ensureStockbaseStockItem($stockItem)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getStockbaseStockManagement() |
80
|
|
|
{ |
81
|
|
|
// Lazy initialization to avoid circular dependency injection |
82
|
|
|
if ($this->stockbaseStockManagement == null) { |
83
|
|
|
$this->stockbaseStockManagement = $this->objectManager->create(StockbaseStockManagement::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->stockbaseStockManagement; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function ensureStockbaseStockItem(StockItemInterface $stockItem) |
90
|
|
|
{ |
91
|
|
|
if (!($stockItem instanceof CombinedStockbaseStockItem)) { |
92
|
|
|
$stockbaseQty = $this->getStockbaseStockManagement()->getStockbaseStockAmount($stockItem->getProductId()); |
93
|
|
|
$stockItem = new CombinedStockbaseStockItem($stockItem, $stockbaseQty); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $stockItem; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|