Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockManagement.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
27 
31  protected $stockState;
32 
37 
41  protected $productRepository;
42 
46  protected $resource;
47 
51  private $qtyCounter;
52 
56  private $stockRegistryStorage;
57 
67  public function __construct(
68  ResourceStock $stockResource,
73  QtyCounterInterface $qtyCounter,
74  StockRegistryStorage $stockRegistryStorage = null
75  ) {
76  $this->stockRegistryProvider = $stockRegistryProvider;
77  $this->stockState = $stockState;
78  $this->stockConfiguration = $stockConfiguration;
79  $this->productRepository = $productRepository;
80  $this->qtyCounter = $qtyCounter;
81  $this->resource = $stockResource;
82  $this->stockRegistryStorage = $stockRegistryStorage ?: \Magento\Framework\App\ObjectManager::getInstance()
83  ->get(StockRegistryStorage::class);
84  }
85 
96  public function registerProductsSale($items, $websiteId = null)
97  {
98  //if (!$websiteId) {
99  $websiteId = $this->stockConfiguration->getDefaultScopeId();
100  //}
101  $this->getResource()->beginTransaction();
102  $lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId);
103  $fullSaveItems = $registeredItems = [];
104  foreach ($lockedItems as $lockedItemRecord) {
105  $productId = $lockedItemRecord['product_id'];
106  $this->stockRegistryStorage->removeStockItem($productId, $websiteId);
107 
109  $orderedQty = $items[$productId];
110  $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
111  $stockItem->setQty($lockedItemRecord['qty']); // update data from locked item
112  $canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
113  if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
114  continue;
115  }
116  if (!$stockItem->hasAdminArea()
117  && !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())
118  ) {
119  $this->getResource()->commit();
120  throw new \Magento\Framework\Exception\LocalizedException(
121  __('Not all of your products are available in the requested quantity.')
122  );
123  }
124  if ($this->canSubtractQty($stockItem)) {
125  $stockItem->setQty($stockItem->getQty() - $orderedQty);
126  }
127  $registeredItems[$productId] = $orderedQty;
128  if (!$this->stockState->verifyStock($productId, $stockItem->getWebsiteId())
129  || $this->stockState->verifyNotification(
130  $productId,
131  $stockItem->getWebsiteId()
132  )
133  ) {
134  $fullSaveItems[] = $stockItem;
135  }
136  }
137  $this->qtyCounter->correctItemsQty($registeredItems, $websiteId, '-');
138  $this->getResource()->commit();
139 
140  return $fullSaveItems;
141  }
142 
148  public function revertProductsSale($items, $websiteId = null)
149  {
150  //if (!$websiteId) {
151  $websiteId = $this->stockConfiguration->getDefaultScopeId();
152  //}
153  $this->qtyCounter->correctItemsQty($items, $websiteId, '+');
154  return true;
155  }
156 
165  public function backItemQty($productId, $qty, $scopeId = null)
166  {
167  //if (!$scopeId) {
168  $scopeId = $this->stockConfiguration->getDefaultScopeId();
169  //}
170  $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
171  if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) {
172  if ($this->canSubtractQty($stockItem)) {
173  $stockItem->setQty($stockItem->getQty() + $qty);
174  }
175  if ($this->stockConfiguration->getCanBackInStock($stockItem->getStoreId()) && $stockItem->getQty()
176  > $stockItem->getMinQty()
177  ) {
178  $stockItem->setIsInStock(true);
179  $stockItem->setStockStatusChangedAutomaticallyFlag(true);
180  }
181  $stockItem->save();
182  }
183  return true;
184  }
185 
192  protected function getProductType($productId)
193  {
194  return $this->productRepository->getById($productId)->getTypeId();
195  }
196 
200  protected function getResource()
201  {
202  return $this->resource;
203  }
204 
212  {
213  return $stockItem->getManageStock() && $this->stockConfiguration->canSubtractQty();
214  }
215 }
canSubtractQty(StockItemInterface $stockItem)
__()
Definition: __.php:13
backItemQty($productId, $qty, $scopeId=null)
__construct(ResourceStock $stockResource, StockRegistryProviderInterface $stockRegistryProvider, StockState $stockState, StockConfigurationInterface $stockConfiguration, ProductRepositoryInterface $productRepository, QtyCounterInterface $qtyCounter, StockRegistryStorage $stockRegistryStorage=null)
$items