Skip to content

Commit

Permalink
Merge pull request #334 from magento-nord/MAGETWO-48153
Browse files Browse the repository at this point in the history
[NORD] MAGETWO-48153 fix
  • Loading branch information
Onischenko, Yaroslav(yonischenko) committed Jan 22, 2016
2 parents 32b8b85 + dd18ea2 commit 43e3fd7
Show file tree
Hide file tree
Showing 20 changed files with 289 additions and 529 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ protected function _saveStockItem()
$productIdsToReindex[] = $row['product_id'];

$row['website_id'] = $this->stockConfiguration->getDefaultScopeId();
$row['stock_id'] = $this->stockRegistry->getStock()->getStockId();
$row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();

$stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
$existStockData = $stockItemDo->getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
interface StockRegistryInterface
{
/**
* @param int|null $stockId
* @param int $scopeId
* @return \Magento\CatalogInventory\Api\Data\StockInterface
*/
public function getStock($stockId = null);
public function getStock($scopeId = null);

/**
* @param int $productId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ interface StockStatusCriteriaInterface extends \Magento\Framework\Api\CriteriaIn
* Add Criteria object
*
* @param \Magento\CatalogInventory\Api\StockStatusCriteriaInterface $criteria
* @return void
* @return bool
*/
public function addCriteria(\Magento\CatalogInventory\Api\StockStatusCriteriaInterface $criteria);

/**
* Filter by scope(s)
*
* @param int $scope
* @return void
* @return bool
*/
public function setScopeFilter($scope);

/**
* Add product(s) filter
*
* @param int $products
* @return void
* @return bool
*/
public function setProductsFilter($products);

/**
* Add filter by quantity
*
* @param float $qty
* @return void
* @return bool
*/
public function setQtyFilter($qty);
}
19 changes: 4 additions & 15 deletions app/code/Magento/CatalogInventory/Helper/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Magento\CatalogInventory\Helper;

use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use Magento\CatalogInventory\Model\Spi\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
Expand Down Expand Up @@ -48,30 +47,22 @@ class Stock
*/
private $stockRegistryProvider;

/**
* @var StockResolverInterface
*/
protected $stockResolver;

/**
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param StatusFactory $stockStatusFactory
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param StockResolverInterface $stockResolver
*/
public function __construct(
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
StatusFactory $stockStatusFactory,
StockRegistryProviderInterface $stockRegistryProvider,
StockResolverInterface $stockResolver
StockRegistryProviderInterface $stockRegistryProvider
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->stockStatusFactory = $stockStatusFactory;
$this->stockRegistryProvider = $stockRegistryProvider;
$this->stockResolver = $stockResolver;
}

/**
Expand All @@ -84,9 +75,8 @@ public function __construct(
public function assignStatusToProduct(Product $product, $stockStatus = null)
{
if ($stockStatus === null) {
$productId = $product->getId();
$stockId = $this->stockResolver->getStockId($productId, $product->getStore()->getWebsiteId());
$stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $stockId);
$websiteId = $product->getStore()->getWebsiteId();
$stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId);
$status = $stockStatus->getStockStatus();
}
$product->setIsSalable($status);
Expand All @@ -103,8 +93,7 @@ public function addStockStatusToProducts(AbstractCollection $productCollection)
$websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
foreach ($productCollection as $product) {
$productId = $product->getId();
$stockId = $this->stockResolver->getStockId($productId, $websiteId);
$stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $stockId);
$stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId);
$status = $stockStatus->getStockStatus();
$product->setIsSalable($status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
interface StockRegistryProviderInterface
{
/**
* @param int|null $stockId
* @param int $scopeId
* @return \Magento\CatalogInventory\Api\Data\StockInterface
*/
public function getStock($stockId);
public function getStock($scopeId);

/**
* @param int $productId
* @param int $stockId
* @param int $scopeId
* @return \Magento\CatalogInventory\Api\Data\StockItemInterface
*/
public function getStockItem($productId, $stockId);
public function getStockItem($productId, $scopeId);

/**
* @param int $productId
* @param int $stockId
* @param int $scopeId
* @return \Magento\CatalogInventory\Api\Data\StockStatusInterface
*/
public function getStockStatus($productId, $stockId);
public function getStockStatus($productId, $scopeId);
}

This file was deleted.

2 changes: 1 addition & 1 deletion app/code/Magento/CatalogInventory/Model/Stock/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function getStockId()
{
$stockId = $this->getData(static::STOCK_ID);
if ($stockId === null) {
$stockId = $this->stockRegistry->getStock()->getStockId();
$stockId = $this->stockRegistry->getStock($this->getWebsiteId())->getStockId();
}
return (int) $stockId;
}
Expand Down
18 changes: 3 additions & 15 deletions app/code/Magento/CatalogInventory/Model/StockIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\Catalog\Model\ProductFactory;
use Magento\CatalogInventory\Api\StockIndexInterface;
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use Magento\CatalogInventory\Model\Spi\StockResolverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

/**
Expand Down Expand Up @@ -56,31 +55,22 @@ class StockIndex implements StockIndexInterface
*/
protected $productTypes = [];


/**
* @var StockResolverInterface
*/
protected $stockResolver;

/**
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param ProductRepositoryInterface $productRepository
* @param ProductWebsite $productWebsite
* @param ProductType $productType
* @param StockResolverInterface $stockResolver
*/
public function __construct(
StockRegistryProviderInterface $stockRegistryProvider,
ProductRepositoryInterface $productRepository,
ProductWebsite $productWebsite,
ProductType $productType,
StockResolverInterface $stockResolver
ProductType $productType
) {
$this->stockRegistryProvider = $stockRegistryProvider;
$this->productRepository = $productRepository;
$this->productWebsite = $productWebsite;
$this->productType = $productType;
$this->stockResolver = $stockResolver;
}

/**
Expand Down Expand Up @@ -122,8 +112,7 @@ public function rebuild($productId = null, $scopeId = null)
*/
public function updateProductStockStatus($productId, $websiteId)
{
$stockId = $this->stockResolver->getStockId($productId, $websiteId);
$item = $this->stockRegistryProvider->getStockItem($productId, $stockId);
$item = $this->stockRegistryProvider->getStockItem($productId, $websiteId);

$status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK;
$qty = 0;
Expand Down Expand Up @@ -242,8 +231,7 @@ protected function processParents($productId, $websiteId)
}

foreach ($parentIds as $parentId) {
$stockId = $this->stockResolver->getStockId($productId, $websiteId);
$item = $this->stockRegistryProvider->getStockItem($parentId, $stockId);
$item = $this->stockRegistryProvider->getStockItem($parentId, $websiteId);
$status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK;
$qty = 0;
if ($item->getItemId()) {
Expand Down
35 changes: 12 additions & 23 deletions app/code/Magento/CatalogInventory/Model/StockManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\CatalogInventory\Api\StockManagementInterface;
use Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface;
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use Magento\CatalogInventory\Model\Spi\StockResolverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock as ResourceStock;

Expand Down Expand Up @@ -49,36 +48,28 @@ class StockManagement implements StockManagementInterface
*/
private $qtyCounter;

/**
* @var StockResolverInterface
*/
protected $stockResolver;

/**
* @param ResourceStock $stockResource
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param StockState $stockState
* @param StockConfigurationInterface $stockConfiguration
* @param ProductRepositoryInterface $productRepository
* @param QtyCounterInterface $qtyCounter
* @param StockResolverInterface $stockResolver
*/
public function __construct(
ResourceStock $stockResource,
StockRegistryProviderInterface $stockRegistryProvider,
StockState $stockState,
StockConfigurationInterface $stockConfiguration,
ProductRepositoryInterface $productRepository,
QtyCounterInterface $qtyCounter,
StockResolverInterface $stockResolver
QtyCounterInterface $qtyCounter
) {
$this->stockRegistryProvider = $stockRegistryProvider;
$this->stockState = $stockState;
$this->stockConfiguration = $stockConfiguration;
$this->productRepository = $productRepository;
$this->qtyCounter = $qtyCounter;
$this->resource = $stockResource;
$this->stockResolver = $stockResolver;
}

/**
Expand All @@ -93,18 +84,17 @@ public function __construct(
*/
public function registerProductsSale($items, $websiteId = null)
{
if (!$websiteId) {
$websiteId = $this->stockConfiguration->getDefaultScopeId();
}
//if (!$websiteId) {
$websiteId = $this->stockConfiguration->getDefaultScopeId();
//}
$this->getResource()->beginTransaction();
$lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId);
$fullSaveItems = $registeredItems = [];
foreach ($lockedItems as $lockedItemRecord) {
$productId = $lockedItemRecord['product_id'];
/** @var StockItemInterface $stockItem */
$orderedQty = $items[$productId];
$stockId = $this->stockResolver->getStockId($productId, $websiteId);
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId);
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
$canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
continue;
Expand Down Expand Up @@ -142,9 +132,9 @@ public function registerProductsSale($items, $websiteId = null)
*/
public function revertProductsSale($items, $websiteId = null)
{
if (!$websiteId) {
$websiteId = $this->stockConfiguration->getDefaultScopeId();
}
//if (!$websiteId) {
$websiteId = $this->stockConfiguration->getDefaultScopeId();
//}
$this->qtyCounter->correctItemsQty($items, $websiteId, '+');
return true;
}
Expand All @@ -159,11 +149,10 @@ public function revertProductsSale($items, $websiteId = null)
*/
public function backItemQty($productId, $qty, $scopeId = null)
{
if (!$scopeId) {
$scopeId = $this->stockConfiguration->getDefaultScopeId();
}
$stockId = $this->stockResolver->getStockId($productId, $scopeId);
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId);
//if (!$scopeId) {
$scopeId = $this->stockConfiguration->getDefaultScopeId();
//}
$stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) {
if ($this->canSubtractQty($stockItem)) {
$stockItem->setQty($stockItem->getQty() + $qty);
Expand Down
Loading

0 comments on commit 43e3fd7

Please sign in to comment.