/**
  * Update stock item on the stock and distribute qty by lots.
  *
  * @param \Magento\CatalogInventory\Model\StockManagement $subject
  * @param \Closure $proceed
  * @param array $items
  * @param int $websiteId is not used
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return null
  */
 public function aroundRegisterProductsSale(\Magento\CatalogInventory\Model\StockManagement $subject, \Closure $proceed, array $items, $websiteId)
 {
     /* This code is moved from original 'registerProductsSale' method. */
     /* replace websiteId by stockId */
     $stockId = $this->_manStock->getCurrentStockId();
     $def = $this->_manTrans->begin();
     $lockedItems = $this->_resourceStock->lockProductsStock(array_keys($items), $stockId);
     $fullSaveItems = $registeredItems = [];
     foreach ($lockedItems as $lockedItemRecord) {
         $productId = $lockedItemRecord['product_id'];
         $orderedQty = $items[$productId];
         /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
         $stockItem = $this->_providerStockRegistry->getStockItem($productId, $stockId);
         $stockItemId = $stockItem->getItemId();
         $canSubtractQty = $stockItemId && $this->_canSubtractQty($stockItem);
         if (!$canSubtractQty || !$this->_configStock->isQty($lockedItemRecord['type_id'])) {
             continue;
         }
         if (!$stockItem->hasAdminArea() && !$this->_stockState->checkQty($productId, $orderedQty)) {
             $this->_manTrans->rollback($def);
             throw new \Magento\Framework\Exception\LocalizedException(__('Not all of your products are available in the requested quantity.'));
         }
         if ($this->_canSubtractQty($stockItem)) {
             $stockItem->setQty($stockItem->getQty() - $orderedQty);
         }
         $registeredItems[$productId] = $orderedQty;
         if (!$this->_stockState->verifyStock($productId) || $this->_stockState->verifyNotification($productId)) {
             $fullSaveItems[] = $stockItem;
         }
     }
     $this->_resourceStock->correctItemsQty($registeredItems, $stockId, '-');
     $this->_manTrans->commit($def);
     return $fullSaveItems;
 }
Exemplo n.º 2
0
 public function test_main()
 {
     $this->_logger->debug('Story02 in PV Integration tests is started.');
     $def = $this->_manTrans->begin();
     try {
         $this->_createMageCustomers();
         $this->_createDownlineCustomers();
         $CUST_1 = $this->_mapCustomerMageIdByIndex[1];
         $CUST_2 = $this->_mapCustomerMageIdByIndex[2];
         $VAL_1 = 12.34;
         $VAL_2 = 6.54;
         $VAL_3 = 5.8;
         $VAL_4 = 6;
         $VAL_5 = 0.54;
         $this->_transferToCustomer($CUST_1, $VAL_1);
         $this->_checkAccount($CUST_1, $VAL_1);
         $this->_transferBetweenCustomers($CUST_1, $CUST_2, $VAL_2);
         $this->_checkAccount($CUST_1, $VAL_3);
         $this->_checkAccount($CUST_2, $VAL_2);
         $this->_transferFromCustomer($CUST_2, $VAL_4);
         $this->_checkAccount($CUST_2, $VAL_5);
     } finally {
         $this->_manTrans->rollback($def);
     }
     $this->_logger->debug('Story02 in PV Integration tests is completed, all transactions are rolled back.');
 }