Skip to content

Commit

Permalink
msi support
Browse files Browse the repository at this point in the history
  • Loading branch information
drc0 committed May 17, 2021
1 parent a503d3d commit fc3b5d5
Showing 1 changed file with 22 additions and 75 deletions.
97 changes: 22 additions & 75 deletions src/Model/Resolver/SaveCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
use Magento\Bundle\Model\Product\Type;
use Magento\Framework\DataObject;
use Magento\Catalog\Model\Product\Attribute\Repository;
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\Downloadable\Model\Product\Type as DownloadableType;
use ScandiPWA\QuoteGraphQl\Helper\ImageUpload;

Expand Down Expand Up @@ -87,24 +87,19 @@ class SaveCartItem implements ResolverInterface
protected $configurableType;

/**
* @var StockStatusRepositoryInterface
* @var StockByWebsiteIdResolverInterface
*/
protected $stockStatusRepository;
private $stockByWebsiteId;

/**
* @var GetStockItemDataInterface
* @var IsProductSalableForRequestedQtyInterface
*/
private $getStockItemData;
private $isProductSalableForRequestedQty;

/**
* @var GetReservationsQuantityInterface
* @var StoreManagerInterface
*/
private $getReservationsQuantity;

/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;
private $storeManager;

/**
* @var ImageUpload
Expand All @@ -121,10 +116,9 @@ class SaveCartItem implements ResolverInterface
* @param Repository $attributeRepository
* @param QuoteIdMask $quoteIdMaskResource
* @param Configurable $configurableType
* @param StockStatusRepositoryInterface $stockStatusRepository
* @param GetStockItemDataInterface $getStockItemData
* @param GetReservationsQuantityInterface $getReservationsQuantity
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
* @param StockItemConfigurationInterfac $stockByWebsiteId
* @param IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty
* @param StoreManagerInterface $storeManager
* @param ImageUpload $imageUpload
*/
public function __construct(
Expand All @@ -135,10 +129,9 @@ public function __construct(
Repository $attributeRepository,
QuoteIdMask $quoteIdMaskResource,
Configurable $configurableType,
StockStatusRepositoryInterface $stockStatusRepository,
GetStockItemDataInterface $getStockItemData,
GetReservationsQuantityInterface $getReservationsQuantity,
GetStockItemConfigurationInterface $getStockItemConfiguration,
StockByWebsiteIdResolverInterface $stockByWebsiteId,
IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty,
StoreManagerInterface $storeManager,
ImageUpload $imageUpload
) {
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
Expand All @@ -147,11 +140,10 @@ public function __construct(
$this->productRepository = $productRepository;
$this->attributeRepository = $attributeRepository;
$this->quoteIdMaskResource = $quoteIdMaskResource;
$this->configurableType = $configurableType;
$this->stockStatusRepository = $stockStatusRepository;
$this->getStockItemData = $getStockItemData;
$this->getReservationsQuantity = $getReservationsQuantity;
$this->getStockItemConfiguration = $getStockItemConfiguration;
$this->configurableType = $configurableType;
$this->stockByWebsiteId = $stockByWebsiteId;
$this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty;
$this->storeManager = $storeManager;
$this->imageUpload = $imageUpload;
}

Expand Down Expand Up @@ -443,56 +435,11 @@ protected function checkItemQty(CartItemInterface $cartItem, $qty): void
$attributesInfo = $cartItem->getBuyRequest()->getDataByKey('super_attribute');
$product = $this->configurableType->getProductByAttributes($attributesInfo, $product);
}

$stockStatus = $this->stockStatusRepository->get($product->getId());
$stockItem = $stockStatus->getStockItem();

if (!$stockItem->getManageStock()) { // just skip all checks, if stock is not managed
return;
}

$allowedBackorder = $stockItem->getBackorders();
$fitsInStock = $qty <= $stockItem->getQty();

if (!$fitsInStock && !$allowedBackorder) {
$stock = $this->stockByWebsiteId->execute((int)$this->storeManager->getStore()->getId());
$fitsInStock = $this->isProductSalableForRequestedQty->execute($product->getSku(), (int)$stock->getId(), $qty);
if (!$fitsInStock) {
throw new GraphQlInputException(new Phrase('Provided quantity exceeds stock limits'));
}

$isMinSaleQuantityCheckFailed = $qty < $stockItem->getMinSaleQty();

if ($isMinSaleQuantityCheckFailed) {
throw new GraphQlInputException(
new Phrase('The fewest you may purchase is %1', [$stockItem->getMinSaleQty()])
);
}

$isMaxSaleQuantityCheckFailed = $qty > $stockItem->getMaxSaleQty();

if ($isMaxSaleQuantityCheckFailed) {
throw new GraphQlInputException(
new Phrase('The requested qty exceeds the maximum qty allowed in shopping cart')
);
}

$stockId = $stockItem->getStockId();
$sku = $product->getSku();

$stockItemData = $this->getStockItemData->execute($sku, $stockId);

/** @var StockItemConfigurationInterface $stockItemConfiguration */
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);

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

$qtyLeftInStock = $qtyWithReservation - $stockItemConfiguration->getMinQty();

$isInStock = bccomp((string) $qtyLeftInStock, (string) $qty, 4) >= 0 || $allowedBackorder;
$isEnoughQty = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] && $isInStock;

if (!$isEnoughQty) {
throw new GraphQlInputException(new Phrase('The requested quantity is not available'));
}
}

/**
Expand Down

0 comments on commit fc3b5d5

Please sign in to comment.