Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IsSalableWithReservationsCondition.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
15 use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
18 
23 {
27  private $getStockItemData;
28 
32  private $getReservationsQuantity;
33 
37  private $getStockItemConfiguration;
38 
42  private $productSalabilityErrorFactory;
43 
47  private $productSalableResultFactory;
48 
56  public function __construct(
57  GetStockItemDataInterface $getStockItemData,
58  GetReservationsQuantityInterface $getReservationsQuantity,
59  GetStockItemConfigurationInterface $getStockItemConfiguration,
60  ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory,
61  ProductSalableResultInterfaceFactory $productSalableResultFactory
62  ) {
63  $this->getStockItemData = $getStockItemData;
64  $this->getReservationsQuantity = $getReservationsQuantity;
65  $this->getStockItemConfiguration = $getStockItemConfiguration;
66  $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
67  $this->productSalableResultFactory = $productSalableResultFactory;
68  }
69 
74  public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
75  {
76  $stockItemData = $this->getStockItemData->execute($sku, $stockId);
77  if (null === $stockItemData) {
78  $errors = [
79  $this->productSalabilityErrorFactory->create([
80  'code' => 'is_salable_with_reservations-no_data',
81  'message' => __('The requested sku is not assigned to given stock')
82  ])
83  ];
84  return $this->productSalableResultFactory->create(['errors' => $errors]);
85  }
86 
88  $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
89 
90  $qtyWithReservation = $stockItemData[GetStockItemDataInterface::QUANTITY] +
91  $this->getReservationsQuantity->execute($sku, $stockId);
92  $qtyLeftInStock = $qtyWithReservation - $stockItemConfiguration->getMinQty() - $requestedQty;
93  $isEnoughQty = (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] && $qtyLeftInStock >= 0;
94  if (!$isEnoughQty) {
95  $errors = [
96  $this->productSalabilityErrorFactory->create([
97  'code' => 'is_salable_with_reservations-not_enough_qty',
98  'message' => __('The requested qty is not available')
99  ])
100  ];
101  return $this->productSalableResultFactory->create(['errors' => $errors]);
102  }
103  return $this->productSalableResultFactory->create(['errors' => []]);
104  }
105 }
__()
Definition: __.php:13
__construct(GetStockItemDataInterface $getStockItemData, GetReservationsQuantityInterface $getReservationsQuantity, GetStockItemConfigurationInterface $getStockItemConfiguration, ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory, ProductSalableResultInterfaceFactory $productSalableResultFactory)
$errors
Definition: overview.phtml:9
execute(string $sku, int $stockId, float $requestedQty)