Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions app/code/Magento/CatalogInventory/Model/StockStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

// @codingStandardsIgnoreFile

Expand All @@ -14,6 +15,7 @@
use Magento\Framework\Locale\FormatInterface;
use Magento\Framework\Math\Division as MathDivision;
use Magento\Framework\DataObject\Factory as ObjectFactory;
use Magento\InventorySales\Model\GetProductQuantityInterface;

/**
* Interface StockStateProvider
Expand Down Expand Up @@ -45,25 +47,33 @@ class StockStateProvider implements StockStateProviderInterface
*/
protected $qtyCheckApplicable;

/**
* @var GetProductQuantityInterface
*/
private $getProductQuantity;

/**
* @param MathDivision $mathDivision
* @param FormatInterface $localeFormat
* @param ObjectFactory $objectFactory
* @param ProductFactory $productFactory
* @param GetProductQuantityInterface $getProductQuantity
* @param bool $qtyCheckApplicable
*/
public function __construct(
MathDivision $mathDivision,
FormatInterface $localeFormat,
ObjectFactory $objectFactory,
ProductFactory $productFactory,
GetProductQuantityInterface $getProductQuantity,
$qtyCheckApplicable = true
) {
$this->mathDivision = $mathDivision;
$this->localeFormat = $localeFormat;
$this->objectFactory = $objectFactory;
$this->productFactory = $productFactory;
$this->qtyCheckApplicable = $qtyCheckApplicable;
$this->getProductQuantity = $getProductQuantity;
}

/**
Expand All @@ -72,11 +82,14 @@ public function __construct(
*/
public function verifyStock(StockItemInterface $stockItem)
{
if ($stockItem->getQty() === null && $stockItem->getManageStock()) {
// prototype code
// $productQty = $this->getProductQuantity->execute($stockItem);
$productQty = $stockItem->getQty();
if ($productQty === null && $stockItem->getManageStock()) {
return false;
}
if ($stockItem->getBackorders() == StockItemInterface::BACKORDERS_NO
&& $stockItem->getQty() <= $stockItem->getMinQty()
&& $productQty <= $stockItem->getMinQty()
) {
return false;
}
Expand All @@ -89,7 +102,7 @@ public function verifyStock(StockItemInterface $stockItem)
*/
public function verifyNotification(StockItemInterface $stockItem)
{
return (float)$stockItem->getQty() < $stockItem->getNotifyStockQty();
return $this->getProductQuantity->execute($stockItem) < $stockItem->getNotifyStockQty();
}

/**
Expand Down Expand Up @@ -166,10 +179,11 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
$result->setHasError(true)->setMessage($message)->setQuoteMessage($message)->setQuoteMessageIndex('qty');
return $result;
} else {
if ($stockItem->getQty() - $summaryQty < 0) {
$productQty = $this->getProductQuantity->execute($stockItem);
if ($productQty - $summaryQty < 0) {
if ($stockItem->getProductName()) {
if ($stockItem->getIsChildItem()) {
$backOrderQty = $stockItem->getQty() > 0 ? ($summaryQty - $stockItem->getQty()) * 1 : $qty * 1;
$backOrderQty = $productQty > 0 ? ($summaryQty - $productQty) * 1 : $qty * 1;
if ($backOrderQty > $qty) {
$backOrderQty = $qty;
}
Expand All @@ -179,7 +193,7 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
$orderedItems = (int)$stockItem->getOrderedItems();

// Available item qty in stock excluding item qty in other quotes
$qtyAvailable = ($stockItem->getQty() - ($summaryQty - $qty)) * 1;
$qtyAvailable = ($productQty - ($summaryQty - $qty)) * 1;
if ($qtyAvailable > 0) {
$backOrderQty = $qty * 1 - $qtyAvailable;
} else {
Expand Down Expand Up @@ -241,7 +255,7 @@ public function checkQty(StockItemInterface $stockItem, $qty)
if (!$stockItem->getManageStock()) {
return true;
}
if ($stockItem->getQty() - $stockItem->getMinQty() - $qty < 0) {
if ($this->getProductQuantity->execute($stockItem) - $stockItem->getMinQty() - $qty < 0) {
switch ($stockItem->getBackorders()) {
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NONOTIFY:
case \Magento\CatalogInventory\Model\Stock::BACKORDERS_YES_NOTIFY:
Expand Down Expand Up @@ -277,7 +291,10 @@ public function suggestQty(StockItemInterface $stockItem, $qty)
$minQty = max($stockItem->getMinSaleQty(), $qtyIncrements);
$divisibleMin = ceil($minQty / $qtyIncrements) * $qtyIncrements;

$maxQty = min($stockItem->getQty() - $stockItem->getMinQty(), $stockItem->getMaxSaleQty());
$maxQty = min(
$this->getProductQuantity->execute($stockItem) - $stockItem->getMinQty(),
$stockItem->getMaxSaleQty()
);
$divisibleMax = floor($maxQty / $qtyIncrements) * $qtyIncrements;

if ($qty < $minQty || $qty > $maxQty || $divisibleMin > $divisibleMax) {
Expand Down Expand Up @@ -343,7 +360,7 @@ public function getStockQty(StockItemInterface $stockItem)
$product->load($stockItem->getProductId());
// prevent possible recursive loop
if (!$product->isComposite()) {
$stockQty = $stockItem->getQty();
$stockQty = $this->getProductQuantity->execute($stockItem);
} else {
$stockQty = null;
$productsByGroups = $product->getTypeInstance()->getProductsToPurchaseByReqGroups($product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\InventorySales\Model\GetProductQuantityInterface;

/**
* Class StockStateProviderTest
Expand Down Expand Up @@ -48,6 +49,11 @@ class StockStateProviderTest extends \PHPUnit\Framework\TestCase
*/
protected $objectFactory;

/**
* @var \Magento\InventorySales\Model\GetProductQuantityInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $getProductQuantity;

/**
* @var \Magento\Framework\DataObject|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -130,13 +136,16 @@ protected function setUp()
$this->productFactory = $this->createPartialMock(\Magento\Catalog\Model\ProductFactory::class, ['create']);
$this->productFactory->expects($this->any())->method('create')->willReturn($this->product);

$this->getProductQuantity = $this->createPartialMock(GetProductQuantityInterface::class, ['execute']);

$this->stockStateProvider = $this->objectManagerHelper->getObject(
\Magento\CatalogInventory\Model\StockStateProvider::class,
[
'mathDivision' => $this->mathDivision,
'localeFormat' => $this->localeFormat,
'objectFactory' => $this->objectFactory,
'productFactory' => $this->productFactory,
'getProductQuantity' => $this->getProductQuantity,
'qtyCheckApplicable' => $this->qtyCheckApplicable
]
);
Expand All @@ -154,6 +163,7 @@ protected function tearDown()
*/
public function testVerifyStock(StockItemInterface $stockItem, $expectedResult)
{
$this->getProductQuantity->expects($this->any())->method('execute')->willReturn($stockItem->getQty());
$this->assertEquals(
$expectedResult,
$this->stockStateProvider->verifyStock($stockItem)
Expand All @@ -167,6 +177,7 @@ public function testVerifyStock(StockItemInterface $stockItem, $expectedResult)
*/
public function testVerifyNotification(StockItemInterface $stockItem, $expectedResult)
{
$this->getProductQuantity->expects($this->any())->method('execute')->willReturn($stockItem->getQty());
$this->assertEquals(
$expectedResult,
$this->stockStateProvider->verifyNotification($stockItem)
Expand All @@ -193,6 +204,7 @@ public function testCheckQty(StockItemInterface $stockItem, $expectedResult)
*/
public function testSuggestQty(StockItemInterface $stockItem, $expectedResult)
{
$this->getProductQuantity->expects($this->any())->method('execute')->willReturn($stockItem->getQty());
$this->assertEquals(
$expectedResult,
$this->stockStateProvider->suggestQty($stockItem, $this->qty)
Expand Down
18 changes: 6 additions & 12 deletions app/code/Magento/InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@
<preference for="Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkus"/>
<preference for="Magento\InventoryCatalog\Model\GetSkusByProductIdsInterface" type="Magento\InventoryCatalog\Model\GetSkusByProductIds"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
<plugin name="prevent_default_stock_deleting"
type="Magento\InventoryCatalog\Plugin\InventoryApi\StockRepository\PreventDeleting\DefaultStockPlugin"/>
<plugin name="prevent_default_stock_deleting" type="Magento\InventoryCatalog\Plugin\InventoryApi\StockRepository\PreventDeleting\DefaultStockPlugin"/>
</type>
<type name="Magento\InventoryApi\Api\AssignSourcesToStockInterface">
<plugin name="prevent_assign_sources_to_default_stock"
type="Magento\InventoryCatalog\Plugin\InventoryApi\AssignSourcesToStock\PreventAssignSourcesToDefaultStockPlugin"/>
<plugin name="prevent_assign_sources_to_default_stock" type="Magento\InventoryCatalog\Plugin\InventoryApi\AssignSourcesToStock\PreventAssignSourcesToDefaultStockPlugin"/>
</type>
<type name="Magento\InventoryApi\Api\SourceItemsSaveInterface">
<plugin name="set_data_to_legacy_catalog_inventory_at_source_items_save"
type="Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin"/>
<plugin name="set_data_to_legacy_catalog_inventory_at_source_items_save" type="Magento\InventoryCatalog\Plugin\InventoryApi\SetDataToLegacyCatalogInventoryAtSourceItemsSavePlugin"/>
</type>
<type name="Magento\InventoryApi\Api\SourceItemsDeleteInterface">
<plugin name="set_to_zero_legacy_catalog_inventory_at_source_items_delete"
type="Magento\InventoryCatalog\Plugin\InventoryApi\SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin"/>
<plugin name="set_to_zero_legacy_catalog_inventory_at_source_items_delete" type="Magento\InventoryCatalog\Plugin\InventoryApi\SetToZeroLegacyCatalogInventoryAtSourceItemsDeletePlugin"/>
</type>
<type name="Magento\InventoryApi\Api\AppendReservationsInterface">
<plugin name="apply_data_to_legacy_catalog_inventory_at_reservation_placing"
type="Magento\InventoryCatalog\Plugin\InventoryApi\ApplyDataToLegacyCatalogInventoryAtReservationPlacingPlugin"/>
<plugin name="apply_data_to_legacy_catalog_inventory_at_reservation_placing" type="Magento\InventoryCatalog\Plugin\InventoryApi\ApplyDataToLegacyCatalogInventoryAtReservationPlacingPlugin"/>
</type>
<type name="Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface">
<plugin name="update_source_item_at_legacy_qty_counter" type="Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyQtyCounterPlugin"/>
</type>
<type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
<plugin name="update_source_item_at_legacy_stock_item_save"
type="Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin"/>
<plugin name="update_source_item_at_legacy_stock_item_save" type="Magento\InventoryCatalog\Plugin\CatalogInventory\UpdateSourceItemAtLegacyStockItemSavePlugin"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Model;

use Magento\CatalogInventory\Api\Data\StockItemInterface;

/**
* Get Product Quantity from legacy catalog stock item
*/
interface GetProductQuantityInterface
{

/**
* @param StockItemInterface $legacyStockItem
*
* @return float|null
*/
public function execute(StockItemInterface $legacyStockItem);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventorySales\Model\ResourceModel;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryApi\Api\GetProductQuantityInStockInterface;
use Magento\InventoryCatalog\Model\GetSkusByProductIdsInterface;
use Magento\InventorySales\Model\GetProductQuantityInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* This resource model is responsible for retrieving Product Quantity from Legacy Catalog Stock Item
* by Stock Resolver. This model provides backward compatibility to Legacy Catalog Stock Item
*/
class GetProductQuantity implements GetProductQuantityInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var GetSkusByProductIdsInterface
*/
private $getSkusByProductIds;

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

/**
* @var GetProductQuantityInStockInterface
*/
private $getProductQuantityInStock;

/**
* @param StoreManagerInterface $storeManager
* @param GetSkusByProductIdsInterface $getSkusByProductIds
* @param StockResolverInterface $stockResolver
* @param GetProductQuantityInStockInterface $getProductQuantityInStock
*/
public function __construct(
StoreManagerInterface $storeManager,
GetSkusByProductIdsInterface $getSkusByProductIds,
StockResolverInterface $stockResolver,
GetProductQuantityInStockInterface $getProductQuantityInStock
) {
$this->storeManager = $storeManager;
$this->getSkusByProductIds = $getSkusByProductIds;
$this->stockResolver = $stockResolver;
$this->getProductQuantityInStock = $getProductQuantityInStock;
}

/**
* {@inheritdoc}
*/
public function execute(StockItemInterface $legacyStockItem)
{
try {
$websiteCode = $this->storeManager->getWebsite()->getCode();

$productIds = $this->getSkusByProductIds->execute([$legacyStockItem->getProductId()]);
$productSku = $productIds[$legacyStockItem->getProductId()];

$stock = $this->stockResolver->get(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);

return $this->getProductQuantityInStock->execute($productSku, (int)$stock->getStockId());
} catch (NoSuchEntityException $e) {
return $legacyStockItem->getQty();
} catch (LocalizedException $e) {
return $legacyStockItem->getQty();
}
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<preference for="Magento\InventorySales\Ui\SalesChannelNameResolverInterface" type="Magento\InventorySales\Ui\WebsiteNameResolver"/>
<preference for="Magento\InventorySales\Model\GetAssignedStockIdForWebsiteInterface" type="Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite"/>
<preference for="Magento\InventorySales\Model\DeleteSalesChannelToStockLinkInterface" type="Magento\InventorySales\Model\ResourceModel\DeleteSalesChannelToStockLink"/>
<preference for="Magento\InventorySales\Model\GetProductQuantityInterface" type="Magento\InventorySales\Model\ResourceModel\GetProductQuantity"/>
<preference for="Magento\InventorySalesApi\Api\StockResolverInterface" type="Magento\InventorySales\Model\StockResolver"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
<plugin name="load_sales_channels_on_get_list" type="Magento\InventorySales\Plugin\InventoryApi\StockRepository\LoadSalesChannelsOnGetListPlugin"/>
<plugin name="load_sales_channels_on_get" type="Magento\InventorySales\Plugin\InventoryApi\StockRepository\LoadSalesChannelsOnGetPlugin"/>
<plugin name="save_sales_channels_links" type="Magento\InventorySales\Plugin\InventoryApi\StockRepository\SaveSalesChannelsLinksPlugin"/>
<plugin name="prevent_deleting_assigned_to_sales_channels_stock"
type="Magento\InventorySales\Plugin\InventoryApi\StockRepository\PreventDeletingAssignedToSalesChannelsStockPlugin"/>
<plugin name="prevent_deleting_assigned_to_sales_channels_stock" type="Magento\InventorySales\Plugin\InventoryApi\StockRepository\PreventDeletingAssignedToSalesChannelsStockPlugin"/>
</type>
<type name="Magento\Inventory\Ui\DataProvider\StockDataProvider">
<plugin name="sales_channel_data" type="Magento\InventorySales\Plugin\Inventory\Ui\StockDataProvider\SalesChannels" />
Expand Down