Skip to content

Add IsProductSalableForRequestedQty service #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
29a12a5
Rename test class to reflect the condition tested
aleron75 Mar 1, 2018
6ce906b
Add IsProductSalableForRequestedQty implementation
aleron75 Mar 1, 2018
77cd18f
Fix coding style issues
aleron75 Mar 1, 2018
64cd6ba
Fix dependency type and simplify some checks
aleron75 Mar 2, 2018
b133a26
Remove IsSalableWithReservationsCondition
aleron75 Mar 2, 2018
13eb14a
Fix possible wrong type of return value
aleron75 Mar 10, 2018
3b5f8d9
Update `IsProductSalableForRequestedQtyInterface` and add related cla…
aleron75 Mar 10, 2018
1734fef
Update IsCorrectQtyCondition and update tests
aleron75 Mar 10, 2018
5770005
Add SaveStockItemConfiguration service
aleron75 Mar 15, 2018
d08a65c
Fix SaveStockItemConfiguration service and update (WIP) test for IsCo…
aleron75 Mar 15, 2018
0bbce3d
Fix static tests. Resolve cyclomatic complexity. Fix dependency Inven…
maghamed Mar 15, 2018
f8d6a53
Fix MinQty Condition Check
maghamed Mar 15, 2018
1044a3f
Fix Min Qty check for Is Product Salable Interface
maghamed Mar 15, 2018
51f2252
Fix MinSaleQty and MaxSaleQty Condiion checks
maghamed Mar 15, 2018
182bb18
Fix SaveStockItemConfiguration persistence
maghamed Mar 16, 2018
a7d30af
Fix conflicts with develop
maghamed Mar 16, 2018
8af7da7
Fix static tests and broken GetStockItemConfigurationInterface
maghamed Mar 16, 2018
79a900a
Fix DI configuration for Slalability errors and result
maghamed Mar 16, 2018
c0fd9de
Fix usage of Default Stock Id Provider instead of legacy constant
maghamed Mar 16, 2018
eb0fcad
Fix Integration tests for Min Qty
maghamed Mar 16, 2018
94dfee2
Fix IsCorrectQtyConditionTest
aleron75 Mar 16, 2018
50b3968
Remove unused reference
aleron75 Mar 17, 2018
cd918c3
Update GetStockItemConfiguration and SaveStockItemConfiguration and r…
aleron75 Mar 17, 2018
7ac989a
Remove commented line
aleron75 Mar 17, 2018
604519f
Restore previous code that doesn't throw exception if legacy stock it…
aleron75 Mar 17, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

namespace Magento\InventoryConfiguration\Model;

use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\CatalogInventory\Api\StockRepositoryInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySales\Model\GetStockItemDataInterface;
Expand All @@ -28,16 +26,6 @@ class GetStockItemConfiguration implements GetStockItemConfigurationInterface
*/
private $getStockItemData;

/**
* @var StockCriteriaInterfaceFactory
*/
private $legacyStockCriteriaFactory;

/**
* @var StockRepositoryInterface
*/
private $legacyStockRepository;

/**
* @var StockItemCriteriaInterfaceFactory
*/
Expand All @@ -53,44 +41,30 @@ class GetStockItemConfiguration implements GetStockItemConfigurationInterface
*/
private $getProductIdsBySkus;

/**
* @var StockConfigurationInterface
*/
private $legacyStockConfiguration;

/**
* @var StockItemConfigurationFactory
*/
private $stockItemConfigurationFactory;

/**
* @param GetStockItemDataInterface $getStockItemData
* @param StockCriteriaInterfaceFactory $legacyStockCriteriaFactory
* @param StockRepositoryInterface $legacyStockRepository
* @param StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory
* @param StockItemRepositoryInterface $legacyStockItemRepository
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
* @param StockConfigurationInterface $legacyStockConfiguration
* @param StockItemConfigurationFactory $stockItemConfigurationFactory
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

public function __construct(
GetStockItemDataInterface $getStockItemData,
StockCriteriaInterfaceFactory $legacyStockCriteriaFactory,
StockRepositoryInterface $legacyStockRepository,
StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory,
StockItemRepositoryInterface $legacyStockItemRepository,
GetProductIdsBySkusInterface $getProductIdsBySkus,
StockConfigurationInterface $legacyStockConfiguration,
StockItemConfigurationFactory $stockItemConfigurationFactory
StockItemConfigurationFactory $stockItemConfigurationFactory,
DefaultStockProviderInterface $defaultStockProvider
) {
$this->getStockItemData = $getStockItemData;
$this->legacyStockCriteriaFactory = $legacyStockCriteriaFactory;
$this->legacyStockRepository = $legacyStockRepository;
$this->legacyStockItemCriteriaFactory = $legacyStockItemCriteriaFactory;
$this->legacyStockItemRepository = $legacyStockItemRepository;
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->legacyStockConfiguration = $legacyStockConfiguration;
$this->stockItemConfigurationFactory = $stockItemConfigurationFactory;
$this->defaultStockProvider = $defaultStockProvider;
}

/**
Expand Down Expand Up @@ -123,35 +97,20 @@ private function getLegacyStockItem(string $sku): StockItemInterface
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];
$searchCriteria->addFilter(StockItemInterface::PRODUCT_ID, StockItemInterface::PRODUCT_ID, $productId);

$legacyStockId = $this->resolveLegacyStockId();
// TODO We use $legacyStockId until we have proper multi-stock item configuration
$legacyStockId = $this->defaultStockProvider->getId();

$searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, $legacyStockId);

$stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria);
if ($stockItemCollection->getTotalCount() === 0) {
// TODO:
return \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class);
throw new LocalizedException(__('Legacy stock item is not found'));
#throw new LocalizedException(__('Legacy stock item is not found'));
}

$stockItems = $stockItemCollection->getItems();
$stockItem = reset($stockItems);
return $stockItem;
}

/**
* In legacy approach configuration has been saved only for default stock
*
* @return int
*/
private function resolveLegacyStockId(): int
{
$scopeId = $this->legacyStockConfiguration->getDefaultScopeId();

$criteria = $this->legacyStockCriteriaFactory->create();
$criteria->setScopeFilter($scopeId);
$collection = $this->legacyStockRepository->getList($criteria);

$legacyStock = current($collection->getItems());
return (int)$legacyStock->getStockId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryConfiguration\Model;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\SaveStockItemConfigurationInterface;

/**
* @inheritdoc
*/
class SaveStockItemConfiguration implements SaveStockItemConfigurationInterface
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

public function __construct(
ResourceConnection $resourceConnection,
GetProductIdsBySkusInterface $getProductIdsBySkus,
DefaultStockProviderInterface $defaultStockProvider
) {
$this->resourceConnection = $resourceConnection;
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->defaultStockProvider = $defaultStockProvider;
}

/**
* @inheritdoc
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(string $sku, int $stockId, StockItemConfigurationInterface $stockItemConfiguration)
{
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];

// TODO We ignore $stockId and use $legacyStockId until we have proper multi-stock item configuration
$legacyStockId = $this->defaultStockProvider->getId();

$connection = $this->resourceConnection->getConnection();
$connection->update(
$this->resourceConnection->getTableName('cataloginventory_stock_item'),
[
StockItemInterface::IS_QTY_DECIMAL => $stockItemConfiguration->isQtyDecimal(),
StockItemInterface::USE_CONFIG_MIN_QTY => $stockItemConfiguration->isUseConfigMinQty(),
StockItemInterface::MIN_QTY => $stockItemConfiguration->getMinQty(),
StockItemInterface::USE_CONFIG_MIN_SALE_QTY => $stockItemConfiguration->isUseConfigMinSaleQty(),
StockItemInterface::MIN_SALE_QTY => $stockItemConfiguration->getMinSaleQty(),
StockItemInterface::USE_CONFIG_MAX_SALE_QTY => $stockItemConfiguration->isUseConfigMaxSaleQty(),
StockItemInterface::MAX_SALE_QTY => $stockItemConfiguration->getMaxSaleQty(),
StockItemInterface::USE_CONFIG_BACKORDERS => $stockItemConfiguration->isUseConfigBackorders(),
StockItemInterface::BACKORDERS => $stockItemConfiguration->getBackorders(),
StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY => $stockItemConfiguration->isUseConfigNotifyStockQty(),
StockItemInterface::NOTIFY_STOCK_QTY => $stockItemConfiguration->getNotifyStockQty(),
StockItemInterface::USE_CONFIG_QTY_INCREMENTS => $stockItemConfiguration->isUseConfigQtyIncrements(),
StockItemInterface::QTY_INCREMENTS => $stockItemConfiguration->getQtyIncrements(),
StockItemInterface::USE_CONFIG_ENABLE_QTY_INC => $stockItemConfiguration->isUseConfigEnableQtyInc(),
StockItemInterface::ENABLE_QTY_INCREMENTS => $stockItemConfiguration->isEnableQtyIncrements(),
StockItemInterface::USE_CONFIG_MANAGE_STOCK => $stockItemConfiguration->isUseConfigManageStock(),
StockItemInterface::MANAGE_STOCK => $stockItemConfiguration->isManageStock(),
StockItemInterface::LOW_STOCK_DATE => $stockItemConfiguration->getLowStockDate(),
StockItemInterface::IS_DECIMAL_DIVIDED => $stockItemConfiguration->isDecimalDivided(),
StockItemInterface::STOCK_STATUS_CHANGED_AUTO => $stockItemConfiguration->getStockStatusChangedAuto(),
],
[
StockItemInterface::PRODUCT_ID . ' = ?' => $productId,
StockItemInterface::STOCK_ID . ' = ?' => $legacyStockId,
'website_id = ?' => 0,
]
);
}
}
Loading