Skip to content

Commit 936efb1

Browse files
committed
Merge remote-tracking branch 'goinc/MAGETWO-50893' into pr-515
2 parents 876aafd + b798c85 commit 936efb1

File tree

31 files changed

+318
-384
lines changed

31 files changed

+318
-384
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,27 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
5959
"bo.parent_id = product.$linkField",
6060
[]
6161
);
62-
$this->_addWebsiteJoinToSelect($select, false);
6362
$status = new \Zend_Db_Expr(
6463
'MAX(' . $connection->getCheckSql('e.required_options = 0', 'i.stock_status', '0') . ')'
6564
);
66-
$select->columns(
67-
'website_id',
68-
'cw'
69-
)->join(
65+
$select->join(
7066
['cis' => $this->getTable('cataloginventory_stock')],
7167
'',
72-
['stock_id']
68+
['website_id', 'stock_id']
7369
)->joinLeft(
7470
['bs' => $this->getTable('catalog_product_bundle_selection')],
7571
'bs.option_id = bo.option_id',
7672
[]
7773
)->joinLeft(
7874
['i' => $idxTable],
79-
'i.product_id = bs.product_id AND i.website_id = cw.website_id AND i.stock_id = cis.stock_id',
75+
'i.product_id = bs.product_id AND i.website_id = cis.website_id AND i.stock_id = cis.stock_id',
8076
[]
8177
)->joinLeft(
8278
['e' => $this->getTable('catalog_product_entity')],
8379
'e.entity_id = bs.product_id',
8480
[]
85-
)->where(
86-
'cw.website_id != 0'
8781
)->group(
88-
['product.entity_id', 'cw.website_id', 'cis.stock_id', 'bo.option_id']
82+
['product.entity_id', 'cis.website_id', 'cis.stock_id', 'bo.option_id']
8983
)->columns(
9084
['option_id' => 'bo.option_id', 'status' => $status]
9185
);
@@ -118,45 +112,19 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
118112
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
119113
{
120114
$this->_prepareBundleOptionStockData($entityIds, $usePrimaryTable);
121-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
122115
$connection = $this->getConnection();
123-
$select = $connection->select()->from(
124-
['e' => $this->getTable('catalog_product_entity')],
125-
['entity_id']
126-
);
127-
$this->_addWebsiteJoinToSelect($select, true);
128-
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
129-
$select->columns(
130-
'cw.website_id'
131-
)->join(
132-
['cis' => $this->getTable('cataloginventory_stock')],
133-
'',
134-
['stock_id']
135-
)->joinLeft(
136-
['cisi' => $this->getTable('cataloginventory_stock_item')],
137-
'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
138-
[]
116+
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
117+
$select->reset(
118+
\Magento\Framework\DB\Select::COLUMNS
119+
)->columns(
120+
['e.entity_id', 'cis.website_id', 'cis.stock_id']
139121
)->joinLeft(
140122
['o' => $this->_getBundleOptionTable()],
141-
'o.entity_id = e.entity_id AND o.website_id = cw.website_id AND o.stock_id = cis.stock_id',
123+
'o.entity_id = e.entity_id AND o.website_id = cis.website_id AND o.stock_id = cis.stock_id',
142124
[]
143125
)->columns(
144126
['qty' => new \Zend_Db_Expr('0')]
145-
)->where(
146-
'cw.website_id != 0'
147-
)->where(
148-
'e.type_id = ?',
149-
$this->getTypeId()
150-
)->group(
151-
['e.entity_id', 'cw.website_id', 'cis.stock_id']
152-
);
153-
154-
// add limitation of status
155-
$condition = $connection->quoteInto(
156-
'=?',
157-
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
158127
);
159-
$this->_addAttributeToSelect($select, 'status', "e.$linkField", 'cs.store_id', $condition);
160128

161129
$statusExpr = $this->getStatusExpression($connection);
162130
$select->columns(

app/code/Magento/CatalogInventory/Helper/Stock.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
1313
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
1414
use Magento\Catalog\Model\Product;
15+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1516

1617
/**
1718
* Class Stock
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1820
*/
1921
class Stock
2022
{
@@ -47,6 +49,11 @@ class Stock
4749
*/
4850
private $stockRegistryProvider;
4951

52+
/**
53+
* @var StockConfigurationInterface
54+
*/
55+
private $stockConfiguration;
56+
5057
/**
5158
* @param StoreManagerInterface $storeManager
5259
* @param ScopeConfigInterface $scopeConfig
@@ -75,7 +82,7 @@ public function __construct(
7582
public function assignStatusToProduct(Product $product, $status = null)
7683
{
7784
if ($status === null) {
78-
$websiteId = $product->getStore()->getWebsiteId();
85+
$websiteId = $this->getStockConfiguration()->getDefaultScopeId();
7986
$stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId);
8087
$status = $stockStatus->getStockStatus();
8188
}
@@ -90,7 +97,7 @@ public function assignStatusToProduct(Product $product, $status = null)
9097
*/
9198
public function addStockStatusToProducts(AbstractCollection $productCollection)
9299
{
93-
$websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
100+
$websiteId = $this->getStockConfiguration()->getDefaultScopeId();
94101
foreach ($productCollection as $product) {
95102
$productId = $product->getId();
96103
$stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
@@ -161,4 +168,18 @@ protected function getStockStatusResource()
161168
}
162169
return $this->stockStatusResource;
163170
}
171+
172+
/**
173+
* @return StockConfigurationInterface
174+
*
175+
* @deprecated
176+
*/
177+
private function getStockConfiguration()
178+
{
179+
if ($this->stockConfiguration === null) {
180+
$this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
181+
->get('Magento\CatalogInventory\Api\StockConfigurationInterface');
182+
}
183+
return $this->stockConfiguration;
184+
}
164185
}

app/code/Magento/CatalogInventory/Model/Config/Backend/Backorders.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function afterSave()
2525
|| $this->getValue() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
2626
)
2727
) {
28-
$this->stockIndex->rebuild();
2928
$this->_stockIndexerProcessor->markIndexerAsInvalid();
3029
}
3130
return parent::afterSave();

app/code/Magento/CatalogInventory/Model/Config/Backend/Managestock.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class Managestock extends AbstractValue
2020
public function afterSave()
2121
{
2222
if ($this->isValueChanged()) {
23-
$this->stockIndex->rebuild();
2423
$this->_stockIndexerProcessor->markIndexerAsInvalid();
2524
}
2625
return parent::afterSave();

app/code/Magento/CatalogInventory/Model/Configuration.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ public function __construct(
148148
*/
149149
public function getDefaultScopeId()
150150
{
151-
return (int) $this->storeManager->getWebsite()->getId();
151+
// TODO: should be fixed in MAGETWO-46043
152+
// "0" is id of admin website, which is used in backend during save entity
153+
return 0;
152154
}
153155

154156
/**

app/code/Magento/CatalogInventory/Model/Plugin/AroundProductRepositorySave.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AroundProductRepositorySave
2424

2525
/**
2626
* @var StoreManagerInterface
27+
* @deprecated
2728
*/
2829
protected $storeManager;
2930

@@ -80,7 +81,7 @@ public function aroundSave(
8081

8182
// set fields that the customer should not care about
8283
$stockItem->setProductId($product->getId());
83-
$stockItem->setWebsiteId($this->storeManager->getStore($product->getStoreId())->getWebsiteId());
84+
$stockItem->setWebsiteId($this->stockConfiguration->getDefaultScopeId());
8485

8586
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
8687

app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer;
1010
use Magento\CatalogInventory\Model\Stock;
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1213

1314
/**
1415
* CatalogInventory Default Stock Status Indexer Resource Model
@@ -42,6 +43,11 @@ class DefaultStock extends AbstractIndexer implements StockInterface
4243
*/
4344
private $queryProcessorComposite;
4445

46+
/**
47+
* @var StockConfigurationInterface
48+
*/
49+
protected $stockConfiguration;
50+
4551
/**
4652
* Class constructor
4753
*
@@ -176,43 +182,29 @@ protected function _isManageStock()
176182
*/
177183
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
178184
{
179-
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
180185
$connection = $this->getConnection();
181186
$qtyExpr = $connection->getCheckSql('cisi.qty > 0', 'cisi.qty', 0);
182187
$select = $connection->select()->from(
183188
['e' => $this->getTable('catalog_product_entity')],
184189
['entity_id']
185190
);
186-
$this->_addWebsiteJoinToSelect($select, true);
187-
$this->_addProductWebsiteJoinToSelect($select, 'cw.website_id', 'e.entity_id');
188-
$select->columns('cw.website_id')->join(
191+
$select->join(
189192
['cis' => $this->getTable('cataloginventory_stock')],
190193
'',
191-
['stock_id']
192-
)->joinLeft(
194+
['website_id', 'stock_id']
195+
)->joinInner(
193196
['cisi' => $this->getTable('cataloginventory_stock_item')],
194197
'cisi.stock_id = cis.stock_id AND cisi.product_id = e.entity_id',
195198
[]
196-
)->columns(['qty' => new \Zend_Db_Expr('SUM(' . $qtyExpr . ')')])
197-
->where('cw.website_id != 0')
198-
->where('e.type_id = ?', $this->getTypeId())
199-
->group(['e.entity_id', 'cw.website_id']);
200-
201-
// add limitation of status
202-
$condition = $connection->quoteInto(
203-
'=?',
204-
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
205-
);
206-
$this->_addAttributeToSelect(
207-
$select,
208-
'status',
209-
'e.' . $metadata->getLinkField(),
210-
'cs.store_id',
211-
$condition
212-
);
199+
)->columns(
200+
['qty' => $qtyExpr]
201+
)->where(
202+
'cis.website_id = ?',
203+
$this->getStockConfiguration()->getDefaultScopeId()
204+
)->where('e.type_id = ?', $this->getTypeId())
205+
->group(['e.entity_id', 'cis.website_id', 'cis.stock_id']);
213206

214207
$select->columns(['status' => $this->getStatusExpression($connection, true)]);
215-
216208
if ($entityIds !== null) {
217209
$select->where('e.entity_id IN(?)', $entityIds);
218210
}
@@ -325,6 +317,20 @@ protected function getStatusExpression(AdapterInterface $connection, $isAggregat
325317
return $statusExpr;
326318
}
327319

320+
/**
321+
* @return StockConfigurationInterface
322+
*
323+
* @deprecated
324+
*/
325+
protected function getStockConfiguration()
326+
{
327+
if ($this->stockConfiguration === null) {
328+
$this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
329+
->get('Magento\CatalogInventory\Api\StockConfigurationInterface');
330+
}
331+
return $this->stockConfiguration;
332+
}
333+
328334
/**
329335
* @return QueryProcessorComposite
330336
*/

app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Stock extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb impleme
7575

7676
/**
7777
* @var StoreManagerInterface
78+
* @deprecated
7879
*/
7980
protected $storeManager;
8081

@@ -189,11 +190,12 @@ protected function _initConfig()
189190
* Set items out of stock basing on their quantities and config settings
190191
*
191192
* @param string|int $website
193+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
192194
* @return void
193195
*/
194196
public function updateSetOutOfStock($website = null)
195197
{
196-
$websiteId = $this->storeManager->getWebsite($website)->getId();
198+
$websiteId = $this->stockConfiguration->getDefaultScopeId();
197199
$this->_initConfig();
198200
$connection = $this->getConnection();
199201
$values = ['is_in_stock' => 0, 'stock_status_changed_auto' => 1];
@@ -223,11 +225,12 @@ public function updateSetOutOfStock($website = null)
223225
* Set items in stock basing on their quantities and config settings
224226
*
225227
* @param int|string $website
228+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
226229
* @return void
227230
*/
228231
public function updateSetInStock($website)
229232
{
230-
$websiteId = $this->storeManager->getWebsite($website)->getId();
233+
$websiteId = $this->stockConfiguration->getDefaultScopeId();
231234
$this->_initConfig();
232235
$connection = $this->getConnection();
233236
$values = ['is_in_stock' => 1];
@@ -255,11 +258,12 @@ public function updateSetInStock($website)
255258
* Update items low stock date basing on their quantities and config settings
256259
*
257260
* @param int|string $website
261+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
258262
* @return void
259263
*/
260264
public function updateLowStockDate($website)
261265
{
262-
$websiteId = $this->storeManager->getWebsite($website)->getId();
266+
$websiteId = $this->stockConfiguration->getDefaultScopeId();
263267
$this->_initConfig();
264268

265269
$connection = $this->getConnection();

app/code/Magento/CatalogInventory/Model/ResourceModel/Stock/Item.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ protected function _construct()
5757
*
5858
* @param \Magento\CatalogInventory\Api\Data\StockItemInterface $item
5959
* @param int $productId
60-
* @param int $websiteId
60+
* @param int $stockId
6161
* @return $this
6262
*/
63-
public function loadByProductId(\Magento\CatalogInventory\Api\Data\StockItemInterface $item, $productId, $websiteId)
63+
public function loadByProductId(\Magento\CatalogInventory\Api\Data\StockItemInterface $item, $productId, $stockId)
6464
{
65-
$select = $this->_getLoadSelect('product_id', $productId, $item)->where('website_id = :website_id');
66-
$data = $this->getConnection()->fetchRow($select, [':website_id' => $websiteId]);
65+
$select = $this->_getLoadSelect('product_id', $productId, $item)->where('stock_id = :stock_id');
66+
$data = $this->getConnection()->fetchRow($select, [':stock_id' => $stockId]);
6767
if ($data) {
6868
$item->setData($data);
6969
} else {

0 commit comments

Comments
 (0)