/**
  * @param int $prodId Magento ID for the product
  * @param array $categories Odoo IDs of the categories.
  */
 public function replicateCategories($prodId, $categories)
 {
     /* get current categories links for the product */
     $prod = $this->_mageRepoProd->getById($prodId);
     $sku = $prod->getSku();
     $catsExist = $prod->getCategoryIds();
     $catsFound = [];
     if (is_array($categories)) {
         foreach ($categories as $catOdooId) {
             $catMageId = $this->_repoRegistry->getCategoryMageIdByOdooId($catOdooId);
             if (!in_array($catMageId, $catsExist)) {
                 /* create new product link if not exists */
                 /** @var CategoryProductLinkInterface $prodLink */
                 $prodLink = $this->_manObj->create(CategoryProductLinkInterface::class);
                 $prodLink->setCategoryId($catMageId);
                 $prodLink->setSku($sku);
                 $prodLink->setPosition(1);
                 $this->_mageRepoCatLink->save($prodLink);
             }
             /* register found link */
             $catsFound[] = $catMageId;
         }
     }
     /* get difference between exist & found */
     $diff = array_diff($catsExist, $catsFound);
     foreach ($diff as $catMageId) {
         $this->_mageRepoCatLink->deleteByIds($catMageId, $sku);
     }
 }
 /**
  * @param int $productIdMage
  * @param \Praxigento\Odoo\Data\Odoo\Inventory\Product\IWarehouse[] $warehouses
  */
 public function processWarehouses($productIdMage, $warehouses)
 {
     /* get all stock items and create map by stock id (warehouse ID)*/
     $stockItems = $this->_getStockItems($productIdMage);
     $mapItemsByStock = $this->_mapStockIds($stockItems);
     $stocksFound = [];
     // array of the replicated warehouses with correspondence in $stockItems
     foreach ($warehouses as $warehouse) {
         $stockIdOdoo = $warehouse->getIdOdoo();
         $pvWarehouse = $warehouse->getPvWarehouse();
         $priceWarehouse = $warehouse->getPriceWarehouse();
         /* get warehouse data by Odoo ID */
         $stockIdMage = $this->_repoRegistry->getWarehouseMageIdByOdooId($stockIdOdoo);
         /* create or update product data for warehouse (stock)*/
         if (isset($mapItemsByStock[$stockIdMage])) {
             /* there is item for the stock, update item data */
             $stocksFound[] = $stockIdMage;
             /* get stock item ID by stock ID */
             $stockItemIdMage = $mapItemsByStock[$stockIdMage];
             $stockItem = $stockItems[$stockItemIdMage];
             /* update warehouse price & PV */
             $this->_subDataHandler->updateWarehouseData($stockItemIdMage, $priceWarehouse, $pvWarehouse);
         } else {
             /* there is no item for the stock, create new item */
             $stockItem = $this->_subDataHandler->createWarehouseData($productIdMage, $stockIdMage, $priceWarehouse, $pvWarehouse);
         }
         /* create or update lot/quantity data */
         $lots = $warehouse->getLots();
         $this->_subDataHandler->processLots($lots, $stockItem);
     }
 }
 public function test_isOdooProductRegisteredInMage()
 {
     /** === Test Data === */
     $idOdoo = 43;
     /** === Call and asserts  === */
     $res = $this->obj->isProductRegisteredInMage($idOdoo);
     $this->assertTrue($res);
 }
 /**
  * @param \Praxigento\Odoo\Data\Odoo\Inventory\Product $product
  */
 public function processProductItem($product)
 {
     assert($product instanceof \Praxigento\Odoo\Data\Odoo\Inventory\Product);
     $idOdoo = $product->getIdOdoo();
     $sku = $product->getSku();
     $name = $product->getName();
     $isActive = $product->getIsActive();
     $skipReplication = false;
     // skip replication for inactive products are missed in Mage
     $priceWholesale = $product->getPriceWholesale();
     $weight = $product->getWeight();
     $pvWholesale = $product->getPvWholesale();
     /* check does product item is already registered in Magento */
     if (!$this->_repoRegistry->isProductRegisteredInMage($idOdoo)) {
         if ($isActive) {
             /* create new product in Magento */
             $idMage = $this->_subProduct->create($sku, $name, $isActive, $priceWholesale, $weight);
             $this->_repoRegistry->registerProduct($idMage, $idOdoo);
             $this->_repoPv->registerProductWholesalePv($idMage, $pvWholesale);
         } else {
             /* skip product replication for not active and not existing products */
             $skipReplication = true;
         }
     } else {
         /* update attributes for magento product */
         $idMage = $this->_repoRegistry->getProductMageIdByOdooId($idOdoo);
         $this->_subProduct->update($idMage, $name, $isActive, $priceWholesale, $weight);
         $this->_repoPv->updateProductWholesalePv($idMage, $pvWholesale);
     }
     if (!$skipReplication) {
         /* check that categories are registered in Magento */
         $categories = $product->getCategories();
         $this->_subProdCategory->checkCategoriesExistence($categories);
         /* check product to categories links (add/remove) */
         $this->_subProdCategory->replicateCategories($idMage, $categories);
         /* update warehouse/lot/qty data  */
         $warehouses = $product->getWarehouses();
         $this->_subProdWarehouse->processWarehouses($idMage, $warehouses);
     }
 }