Skip to content

fix stock item configuration#1353

Merged
maghamed merged 18 commits into2.3-developfrom
fix-stock-item-configuration
Jun 26, 2018
Merged

fix stock item configuration#1353
maghamed merged 18 commits into2.3-developfrom
fix-stock-item-configuration

Conversation

@seruymt
Copy link
Copy Markdown
Contributor

@seruymt seruymt commented Jun 11, 2018

@seruymt seruymt added the WIP label Jun 11, 2018
@seruymt seruymt added this to the MSI Part I milestone Jun 11, 2018
@seruymt seruymt self-assigned this Jun 11, 2018
@seruymt seruymt requested a review from maghamed June 11, 2018 07:55
// Sku is not assigned to Stock
return null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it would be great to throw NoSuchEntityException exception instead of removed code. It would be more reliable for a code of business logic.
But for doing this we need to introduce one more service isProductAssignedToStock::execute($sku, $stockId) to InventoryApi.

Because currently we create and return configuration with default values for a product which is actually not belonging to specified Stock

$stockItemData = $this->getStockItemData->execute($sku, $stockId);
if (null === $stockItemData) {
// Sku is not assigned to Stock
return false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good candidate for usage of isProductAssignedToStock::execute($sku, $stockId)

}

// iterate over the required conditions: return error as soon as a condition fails
$stockItemData = $this->getStockItemData->execute($sku, $stockId);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another candidate for isProductAssignedToStock::execute($sku, $stockId) usage

$requiredConditionsErrors = array_merge(...$requiredConditionsErrors);
if (count($requiredConditionsErrors)) {
return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+        $requiredConditionsErrors = array_merge(...$requiredConditionsErrors);
+        if (count($requiredConditionsErrors)) {
+            return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]);
+        }

this looks like a code which could be extracted to separate private method

);

if ($stockItemConfiguration === null || $stockItemConfiguration->isManageStock()) {
if ($stockItemConfiguration->isManageStock()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct fix, because of condition $stockItemConfiguration === null was added to support Order processing for deleted products from Catalog.

and condition $stockItemConfiguration === null checks that product does not exists anymore

isProductAssignedToStock::execute($sku, $stockId) should be used here as well, but Exception should be caught here and Reservation should be appended anyway


if ($stockItemConfiguration === null || !$stockItemConfiguration->isManageStock()) {
//Product not assigned to Given Stock or we No need to Manage Stock
if (!$stockItemConfiguration->isManageStock()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@seruymt seruymt force-pushed the fix-stock-item-configuration branch from 6054445 to a7b4fad Compare June 16, 2018 17:04
$productId = $this->getProductIdsBySkus->execute([$sku])[$sku];
} catch (NoSuchEntityException $skuNotFoundInCatalog) {
$stockItem = \Magento\Framework\App\ObjectManager::getInstance()->create(StockItemInterface::class);
$stockItem = ObjectManager::getInstance()->create(StockItemInterface::class);
Copy link
Copy Markdown

@naydav naydav Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use ObjectManager manager directly instead of construct dependency? Or is it a temporal solution?

*
* @api
*/
interface IsSourceItemManagementAllowedForSkuInterface
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that we need to introduce this interface

return true;
}
}
} catch (NoSuchEntityException $e) {
Copy link
Copy Markdown

@naydav naydav Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need to move this logic in some of a condition instead of applying it under all of the conditions

return $this->productSalableResultFactory->create(['errors' => $requiredConditionsErrors]);
}

$sufficientConditionsErrors = $this->processSufficientConditions($sku, $stockId, $requestedQty);
Copy link
Copy Markdown

@naydav naydav Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not bad to describe in dockblock
What is a difference between requiredConditions and sufficientConditions


if ($stockItemConfiguration === null || !$stockItemConfiguration->isManageStock()) {
//Product not assigned to Given Stock or we No need to Manage Stock
try {
Copy link
Copy Markdown

@naydav naydav Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we have a lot of boilerplate code like

try {
+            $stockItemConfig = $this->getStockItemConfiguration->execute($sku, $stockId);
+        } catch (NoSuchEntityException $e) {
+            // GetStockItemConfiguration throw NoSuchEntityException when SKU is not assigned to Stock
             return false;
         }

Maybe it will be better to return some $stockItemConfiguration with predefined values manage_stock=0 (like as Special Case pattern)
If some sku doesn't have configuration for some stock will be sensible use fallback on global configuration

In another words, move concept is sku assigned to stock our of get stock item configuration
And you should always wrap work this service in try...catch

}

if (!$stockItemConfiguration->isManageStock()) {
//We No need to Manage Stock
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to manage

if (null === $stockItemData) {
// Sku is not assigned to Stock
return null;
if ($this->defaultStockProvider->getId() !== $stockId
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need $this->defaultStockProvider->getId() !== $stockId condition?

@maghamed maghamed removed the WIP label Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants