Skip to content

Commit

Permalink
Merge pull request magento#1292 from magento-engcom/issue-1236
Browse files Browse the repository at this point in the history
Issue 1236: support of order operations for deleted from Catlog products
  • Loading branch information
maghamed authored Jun 4, 2018
2 parents 36c638e + 7b4f27e commit 28757c2
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public function __construct(
public function execute(array $skus)
{
$typesBySkus = $this->getProductTypesBySkusResource->execute($skus);
$notFoundedSkus = array_diff($skus, array_keys($typesBySkus));

if (!empty($notFoundedSkus)) {
throw new InputException(
__('Following products with requested skus were not found: %1', implode($notFoundedSkus, ', '))
);
}

$preparedTypesBySkus = [];
foreach ($typesBySkus as $sku => $type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ public function testExecute()

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryCatalog/Test/_files/products_all_types.php
*
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage Following products with requested skus were not found: not_existed_1, not_existed_2
*/
public function testExecuteWithNotExistedSkus()
{
$skus = ['not_existed_1', 'not_existed_2', 'simple_sku'];

$this->getProductTypesBySkus->execute($skus);
self::assertEquals(['simple_sku' => 'simple'], $this->getProductTypesBySkus->execute($skus));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Magento\InventoryCatalogApi\Model;

use Magento\Framework\Exception\InputException;

/**
* Get product types id by product skus.
*
Expand All @@ -19,7 +17,6 @@ interface GetProductTypesBySkusInterface
/**
* @param array $skus
* @return array (key: 'sku', value: 'product_type')
* @throws InputException
*/
public function execute(array $skus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\InputException;

/**
* @inheritdoc
Expand Down Expand Up @@ -94,17 +95,21 @@ private function getLegacyStockItem(string $sku): StockItemInterface
{
$searchCriteria = $this->legacyStockItemCriteriaFactory->create();

$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];
try {
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];
} catch (InputException $skuNotFoundInCatalog) {
$stockItem = \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class);
$stockItem->setManageStock(true); // Make possible to Manage Stock for Products removed from Catalog
return $stockItem;
}
$searchCriteria->addFilter(StockItemInterface::PRODUCT_ID, StockItemInterface::PRODUCT_ID, $productId);

// TODO We use Stock::DEFAULT_STOCK_ID until we have proper multi-stock item configuration
// Stock::DEFAULT_STOCK_ID is used until we have proper multi-stock item configuration
$searchCriteria->addFilter(StockItemInterface::STOCK_ID, StockItemInterface::STOCK_ID, Stock::DEFAULT_STOCK_ID);

$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'));
}

$stockItems = $stockItemCollection->getItems();
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/InventoryIndexer/Indexer/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public function execute(int $stockId): Select
$sourceCodes = $this->getSourceCodes($stockId);

$select = $connection->select();
$select->joinInner(
$select->joinLeft(
['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
'product_entity.sku = source_item.sku',
[]
)->joinInner(
)->joinLeft(
['legacy_stock_item' => $this->resourceConnection->getTableName('cataloginventory_stock_item')],
'product_entity.entity_id = legacy_stock_item.product_id',
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public function execute(array $items, SalesChannelInterface $salesChannel, Sales
$reservations = [];
/** @var ItemToSellInterface $item */
foreach ($items as $item) {
if (true === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$item->getSku()])) {
$currentSku = $item->getSku();
$skuNotExistInCatalog = !isset($productTypes[$currentSku]);
if ($skuNotExistInCatalog ||
$this->isSourceItemManagementAllowedForProductType->execute($productTypes[$currentSku])) {
$reservations[] = $this->reservationBuilder
->setSku($item->getSku())
->setQuantity((float)$item->getQuantity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
namespace Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition;

use Magento\Framework\DB\Select;
use Magento\InventoryApi\Api\Data\SourceItemInterface;

/**
* Source Item status condition
* Still build Stock Item index even when there is no corresponding SKU in catalog_product_entity table
*/
class SourceItemStatusCondition implements GetIsStockItemSalableConditionInterface
class SkuIsAbsentInCatalogCondition implements GetIsStockItemSalableConditionInterface
{
/**
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(Select $select): string
{
return 'MAX(source_item.' . SourceItemInterface::STATUS . ') = ' . SourceItemInterface::STATUS_IN_STOCK;
return 'product_entity.sku IS NULL';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function aroundExecute(AppendReservationsInterface $subject, \Closure $pr
$reservation->getStockId()
);

if ($stockItemConfiguration->isManageStock()) {
if ($stockItemConfiguration === null || $stockItemConfiguration->isManageStock()) {
$reservationToAppend[] = $reservation;
}
}
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/InventorySales/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<item name="manage_stock" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\ManageStockCondition</item>
<!-- min_qty condition includes source_item_status check (need to proper work of min_qty) -->
<item name="min_qty" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\MinQtyStockCondition</item>
<item name="non_existing_legacy_sku" xsi:type="object">Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\SkuIsAbsentInCatalogCondition</item>
</argument>
</arguments>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function execute(SourceDeductionRequestInterface $sourceDeductionRequest)
$stockId
);

if (!$stockItemConfiguration->isManageStock()) {
if ($stockItemConfiguration === null || !$stockItemConfiguration->isManageStock()) {
//Product not assigned to Given Stock or we No need to Manage Stock
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private function getSources(int $stockId, string $sku, float $qty): array
private function isManageStock($itemSku, $stockId)
{
$stockItemConfiguration = $this->getStockItemConfiguration->execute($itemSku, $stockId);

if (!empty($stockItemConfiguration)) {
return $stockItemConfiguration->isManageStock();
}
Expand Down

0 comments on commit 28757c2

Please sign in to comment.