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
7 changes: 5 additions & 2 deletions app/code/Magento/InventorySales/Model/CheckItemsQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventorySales\Model;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Exception\LocalizedException;
use Magento\InventoryCatalog\Model\GetProductTypesBySkusInterface;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
Expand Down Expand Up @@ -67,11 +68,13 @@ public function execute(array $items, int $stockId) : void
{
$productTypes = $this->getProductTypesBySkus->execute(array_keys($items));
foreach ($items as $sku => $qty) {
if (false === $this->isSourceItemsAllowedForProductType->execute($productTypes[$sku])) {
if (false === $this->isSourceItemsAllowedForProductType->execute($productTypes[$sku])
Copy link

Choose a reason for hiding this comment

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

InventorySales module must not have the dependency on ConfigurableProduct module

&& $productTypes[$sku] != Configurable::TYPE_CODE
) {
$defaultStockId = $this->defaultStockProvider->getId();
if ($defaultStockId !== $stockId) {
throw new LocalizedException(
__('Product type is not supported on Default Stock.')
__('Product type is not supported on not Default Stock.')
);
}
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition;

use Magento\InventoryCatalog\Model\GetProductTypesBySkusInterface;
use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductType;
use Magento\InventoryReservations\Model\GetReservationsQuantityInterface;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventorySales\Model\GetStockItemDataInterface;
Expand Down Expand Up @@ -46,25 +48,41 @@ class IsSalableWithReservationsCondition implements IsProductSalableForRequested
*/
private $productSalableResultFactory;

/**
* @var IsSourceItemsAllowedForProductType
*/
private $isSourceItemsAllowedForProductType;

/**
* @var GetProductTypesBySkusInterface
*/
private $getProductTypesBySkus;

/**
* @param GetStockItemDataInterface $getStockItemData
* @param GetReservationsQuantityInterface $getReservationsQuantity
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
* @param ProductSalableResultInterfaceFactory $productSalableResultFactory
* @param IsSourceItemsAllowedForProductType $isSourceItemsAllowedForProductType
* @param GetProductTypesBySkusInterface $getProductTypesBySkus
*/
public function __construct(
GetStockItemDataInterface $getStockItemData,
GetReservationsQuantityInterface $getReservationsQuantity,
GetStockItemConfigurationInterface $getStockItemConfiguration,
ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
ProductSalableResultInterfaceFactory $productSalableResultFactory
ProductSalableResultInterfaceFactory $productSalableResultFactory,
IsSourceItemsAllowedForProductType $isSourceItemsAllowedForProductType,
GetProductTypesBySkusInterface $getProductTypesBySkus
) {
$this->getStockItemData = $getStockItemData;
$this->getReservationsQuantity = $getReservationsQuantity;
$this->getStockItemConfiguration = $getStockItemConfiguration;
$this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
$this->productSalableResultFactory = $productSalableResultFactory;
$this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType;
$this->getProductTypesBySkus = $getProductTypesBySkus;
}

/**
Expand All @@ -84,21 +102,31 @@ public function execute(string $sku, int $stockId, float $requestedQty): Product
return $this->productSalableResultFactory->create(['errors' => $errors]);
}

$qtyWithReservation = $stockItemData[GetStockItemDataInterface::QUANTITY] +
$this->getReservationsQuantity->execute($sku, $stockId);
/** @var StockItemConfigurationInterface $stockItemConfiguration */
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
$qtyLeftInStock = $qtyWithReservation - $stockItemConfiguration->getMinQty() - $requestedQty;
$isEnoughQty = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] && $qtyLeftInStock >= 0;
if (!$isEnoughQty) {
$productType = $this->getProductTypesBySkus->execute([$sku])[$sku];

if (true === $this->isSourceItemsAllowedForProductType->execute($productType)) {
$qtyWithReservation = $stockItemData[GetStockItemDataInterface::QUANTITY] +
$this->getReservationsQuantity->execute($sku, $stockId);

/** @var StockItemConfigurationInterface $stockItemConfiguration */
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
$qtyLeftInStock = $qtyWithReservation - $stockItemConfiguration->getMinQty() - $requestedQty;
$isSalable = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE];
$isSalableForRequestedQty = $isSalable && $qtyLeftInStock >= 0;
} else {
$isSalableForRequestedQty = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE];
Copy link

@naydav naydav May 14, 2018

Choose a reason for hiding this comment

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

Need to replace on Exception
Because it is unexpected and ambiguous behaviour to check quantity for complex products

}

$errors = [];
if (!$isSalableForRequestedQty) {
$errors = [
$this->productSalabilityErrorFactory->create([
'code' => 'is_salable_with_reservations-not_enough_qty',
'message' => __('The requested qty is not available')
])
];
return $this->productSalableResultFactory->create(['errors' => $errors]);
}
return $this->productSalableResultFactory->create(['errors' => []]);

return $this->productSalableResultFactory->create(['errors' => $errors]);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/InventorySales/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"magento/framework": "*",
"magento/module-catalog-inventory": "*",
"magento/module-catalog": "*",
"magento/module-configurable-product": "*",
"magento/module-inventory-api": "*",
"magento/module-inventory-catalog": "*",
"magento/module-inventory-configuration": "*",
Expand Down