/**
  * @param string $name Stock name.
  *
  * @return StockInterface
  */
 private function _createMageStock($name)
 {
     /** @var  $stock \Magento\CatalogInventory\Api\Data\StockInterface */
     $stock = $this->_manObj->create(StockInterface::class);
     $stock->setStockName($name);
     $saved = $this->_repoMageStock->save($stock);
     $result = $this->_repoMageStock->get($saved->getStockId());
     return $result;
 }
 public function testGetStock()
 {
     $this->stockCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockCriteria);
     $this->stockCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null);
     $stockCollection = $this->getMock('\\Magento\\CatalogInventory\\Model\\ResourceModel\\Stock\\Collection', ['getFirstItem', '__wakeup', 'getItems'], [], '', false);
     $stockCollection->expects($this->once())->method('getItems')->willReturn([$this->stock]);
     $this->stockRepository->expects($this->once())->method('getList')->willReturn($stockCollection);
     $this->stock->expects($this->once())->method('getStockId')->willReturn(true);
     $this->assertEquals($this->stock, $this->stockRegistryProvider->getStock($this->scopeId));
 }
Пример #3
0
 /**
  * Update default stock (switch to website #1 from website #0), add new stock and 2 warehouses; bind warehouses
  * with stocks.
  */
 private function processStocks()
 {
     /* update default stock as Baltic */
     /** @var \Magento\CatalogInventory\Model\Stock $stockBaltic */
     $stockBaltic = $this->mageRepoStock->get(self::DEF_STOCK_ID_DEFAULT);
     $stockBaltic->setStockName('Baltic');
     $stockBaltic->setWebsiteId(self::DEF_WEBSITE_ID_ADMIN);
     $this->mageRepoStock->save($stockBaltic);
     /* create new stock as Russian */
     try {
         $stockRussian = $this->mageRepoStock->get(self::DEF_STOCK_ID_RUSSIAN);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         $stockRussian = $this->_manObj->create(\Magento\CatalogInventory\Model\Stock::class);
         // $stockRussian->setStockId(self::DEF_STOCK_ID_RUSSIAN);
     } finally {
         $stockRussian->setStockName('Russian');
         $stockRussian->setWebsiteId(self::DEF_WEBSITE_ID_ADMIN);
         $this->mageRepoStock->save($stockRussian);
     }
     /* create new warehouses */
     /* Baltic */
     $wrhsBaltic = $this->repoWrhs->getById(self::DEF_STOCK_ID_BALTIC);
     $shoudCreate = is_null($wrhsBaltic->getOdooId());
     $wrhsBaltic->setCode('Baltic');
     $wrhsBaltic->setCurrency('USD');
     $wrhsBaltic->setCountryCode('LV');
     $wrhsBaltic->setNote('Warehouse for Baltic states (LV, LT, EE)');
     $wrhsBaltic->setOdooId(self::DEF_WRHS_ODOO_ID_BALTIC);
     if ($shoudCreate) {
         $this->repoWrhs->create($wrhsBaltic);
     } else {
         $this->repoWrhs->updateById($wrhsBaltic->getId(), $wrhsBaltic);
     }
     /* Russian */
     $wrhsRussian = $this->repoWrhs->getById(self::DEF_STOCK_ID_RUSSIAN);
     if (!$wrhsRussian) {
         $wrhsRussian = new \Praxigento\Odoo\Data\Agg\Warehouse();
     }
     $shoudCreate = is_null($wrhsRussian->getOdooId());
     $wrhsRussian->setCode('Russian');
     $wrhsRussian->setCurrency('USD');
     $wrhsRussian->setCountryCode('RU');
     $wrhsRussian->setNote('Warehouse for Russian Federation');
     $wrhsRussian->setOdooId(self::DEF_WRHS_ODOO_ID_RUSSIAN);
     if ($shoudCreate) {
         $wrhsRussian->setId($stockRussian->getStockId());
         $this->repoWrhs->create($wrhsRussian);
     } else {
         $this->repoWrhs->updateById($wrhsRussian->getId(), $wrhsRussian);
     }
     return;
 }
 /**
  * @param int|null $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockInterface
  */
 public function getStock($scopeId)
 {
     if (!isset($this->stocks[$scopeId])) {
         $criteria = $this->stockCriteriaFactory->create();
         $criteria->setScopeFilter($scopeId);
         $collection = $this->stockRepository->getList($criteria);
         $stock = current($collection->getItems());
         if ($stock && $stock->getStockId()) {
             $this->stocks[$scopeId] = $stock;
         } else {
             return $this->stockFactory->create();
         }
     }
     return $this->stocks[$scopeId];
 }
 /**
  * @param int|null $scopeId
  * @return \Magento\CatalogInventory\Api\Data\StockInterface
  */
 public function getStock($scopeId)
 {
     $stock = $this->stockRegistryStorage->getStock($scopeId);
     if (null === $stock) {
         $criteria = $this->stockCriteriaFactory->create();
         $criteria->setScopeFilter($scopeId);
         $collection = $this->stockRepository->getList($criteria);
         $stock = current($collection->getItems());
         if ($stock && $stock->getStockId()) {
             $this->stockRegistryStorage->setStock($scopeId, $stock);
         } else {
             $stock = $this->stockFactory->create();
         }
     }
     return $stock;
 }