Skip to content

Commit

Permalink
Merge pull request magento#1347 from DaniloEmpire/issue-717
Browse files Browse the repository at this point in the history
Fix for issue magento#717
  • Loading branch information
maghamed authored Jun 10, 2018
2 parents a3d75c4 + 9818a9b commit cdb58dc
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
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\InventorySales\Model\IsProductSalableCondition;

use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;

/**
* @inheritdoc
*/
class BackOrderNotifyCustomerCondition implements IsProductSalableForRequestedQtyInterface
{
/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;

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

/**
* @var ProductSalableResultInterfaceFactory
*/
private $productSalableResultFactory;

/**
* @var ProductSalabilityErrorInterfaceFactory
*/
private $productSalabilityErrorFactory;

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

/**
* @inheritdoc
*/
public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
{
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
if (null === $stockItemConfiguration) {
$this->productSalableResultFactory->create(['errors' => []]);
}

if ($stockItemConfiguration->getBackorders() === StockItemConfigurationInterface::BACKORDERS_YES_NOTIFY) {
$stockItemData = $this->getStockItemData->execute($sku, $stockId);
if (null === $stockItemData) {
$this->productSalableResultFactory->create(['errors' => []]);
}

$backOrderQty = $requestedQty - $stockItemData[GetStockItemDataInterface::QUANTITY];
if ($backOrderQty > 0) {
$errors = [
$this->productSalabilityErrorFactory->create([
'code' => 'back_order-not-enough',
'message' => __(
'We don\'t have as many quantity as you requested, '
. 'but we\'ll back order the remaining %1.',
$backOrderQty * 1
)])
];
return $this->productSalableResultFactory->create(['errors' => $errors]);
}
}

return $this->productSalableResultFactory->create(['errors' => []]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Locale\FormatInterface;
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
use Magento\InventorySales\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition;
use Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\ProductSalabilityError;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
Expand Down Expand Up @@ -52,28 +53,36 @@ class CheckQuoteItemQtyPlugin
*/
private $storeManager;

/**
* @var BackOrderNotifyCustomerCondition
*/
private $backOrderNotifyCustomerCondition;

/**
* @param ObjectFactory $objectFactory
* @param FormatInterface $format
* @param IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty
* @param GetSkusByProductIdsInterface $getSkusByProductIds
* @param StockResolverInterface $stockResolver
* @param StoreManagerInterface $storeManager
* @param BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition
*/
public function __construct(
ObjectFactory $objectFactory,
FormatInterface $format,
IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty,
GetSkusByProductIdsInterface $getSkusByProductIds,
StockResolverInterface $stockResolver,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
BackOrderNotifyCustomerCondition $backOrderNotifyCustomerCondition
) {
$this->objectFactory = $objectFactory;
$this->format = $format;
$this->isProductSalableForRequestedQty = $isProductSalableForRequestedQty;
$this->getSkusByProductIds = $getSkusByProductIds;
$this->stockResolver = $stockResolver;
$this->storeManager = $storeManager;
$this->backOrderNotifyCustomerCondition = $backOrderNotifyCustomerCondition;
}

/**
Expand Down Expand Up @@ -121,6 +130,14 @@ public function aroundCheckQuoteItemQty(
}
}

$productSalableResult = $this->backOrderNotifyCustomerCondition->execute($productSku, (int)$stockId, $qty);
if ($productSalableResult->getErrors()) {
/** @var ProductSalabilityError $error */
foreach ($productSalableResult->getErrors() as $error) {
$result->setMessage($error->getMessage());
}
}

return $result;
}

Expand Down

0 comments on commit cdb58dc

Please sign in to comment.