/**
  * Sets the categories. Non-existing categories will be created immediately.
  */
 protected function setCategories()
 {
     // No categories
     if (is_null($this->ItemBase->Categories)) {
         return;
     }
     if (is_null(self::$CategoryApi)) {
         self::$CategoryApi = Shopware\Components\Api\Manager::getResource('Category');
     }
     if (is_null(self::$CategoryRepository)) {
         self::$CategoryRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category');
     }
     // Categories
     /** @var PlentySoapObject_ItemCategory $Category */
     foreach ($this->ItemBase->Categories->item as $Category) {
         // FIX: corrupt category within plenty
         if ((int) $Category->ItemCategoryID <= 0 || empty($Category->ItemCategoryPathNames)) {
             continue;
         }
         try {
             $categoryId = PlentymarketsMappingEntityCategory::getCategoryByPlentyID($Category->ItemCategoryID, $this->storeId);
             $this->categories[] = array('id' => $categoryId);
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             $importEntityItemCategoryTree = new PlentymarketsImportEntityItemCategoryTree($Category, $this->storeId);
             $categoryId = $importEntityItemCategoryTree->import();
             // Only create a mapping and connect the category to the item,
             // of nothing went wrong during creation
             if ($categoryId) {
                 $this->categories[] = array('id' => $categoryId);
             }
         }
     }
 }
 /**
  * Exports the categories
  *
  * @param $shopwareCategories
  * @param $plentyTree
  * @param $storeId
  */
 private function export($shopwareCategories, $plentyTree, $storeId)
 {
     try {
         $shopId = PlentymarketsMappingController::getShopByPlentyID($storeId);
     } catch (PlentymarketsMappingExceptionNotExistant $e) {
         return;
     }
     /** @var Shopware\Models\Category\Category $shopwareCategory */
     foreach ($shopwareCategories as $shopwareCategory) {
         if ($shopwareCategory->getBlog()) {
             continue;
         }
         try {
             PlentymarketsMappingEntityCategory::getCategoryByShopwareID($shopwareCategory->getId(), $shopId);
             continue;
         } catch (PlentymarketsMappingExceptionNotExistant $e) {
         }
         $branchId = null;
         if (!isset($plentyTree['children'])) {
             $plentyTree['children'] = array();
         }
         $shopwareName = trim($shopwareCategory->getName());
         /** @var array $plentyChild1 */
         foreach ($plentyTree['children'] as $name => $plentyChild1) {
             if ($name == $shopwareName) {
                 $branchId = $plentyChild1['id'];
                 PlentymarketsMappingEntityCategory::addCategory($shopwareCategory->getId(), $shopId, $branchId, $storeId);
                 break;
             }
         }
         $branchId = $this->exportCategory($shopwareCategory, $storeId, $shopId, $branchId);
         // Active the category
         if ($storeId > 0 && $shopwareCategory->getActive()) {
             $Request_SetStoreCategories = new PlentySoapRequest_SetStoreCategories();
             $Request_SetStoreCategories->StoreCategories = array();
             $Object_SetStoreCategory = new PlentySoapObject_SetStoreCategory();
             $Object_SetStoreCategory->BranchID = $branchId;
             $Object_SetStoreCategory->StoreID = $storeId;
             $Request_SetStoreCategories->StoreCategories[] = $Object_SetStoreCategory;
             PlentymarketsSoapClient::getInstance()->SetStoreCategories($Request_SetStoreCategories);
         }
         $shopwareChildren1 = $shopwareCategory->getChildren();
         // search for the next categories of shopware in plentymarkets
         $this->export($shopwareChildren1, $plentyChild1, $storeId);
     }
 }
 /**
  * Exports the base item
  *
  * @throws PlentymarketsExportException
  * @return boolean
  */
 protected function exportItemBase()
 {
     $Item = $this->SHOPWARE_Article;
     $Request_SetItemsBase = new PlentySoapRequest_SetItemsBase();
     $Request_SetItemsBase->BaseItems = array();
     $Object_SetItemsBaseItemBase = new PlentySoapObject_SetItemsBaseItemBase();
     $ItemDetails = $Item->getMainDetail();
     if (!$ItemDetails instanceof \Shopware\Models\Article\Detail) {
         throw new PlentymarketsExportException('The item »' . $this->SHOPWARE_Article->getName() . '« with the id »' . $this->SHOPWARE_Article->getId() . '« could not be exported (no main detail)', 2810);
     }
     try {
         // Release date
         $ReleaseDate = $ItemDetails->getReleaseDate();
     } catch (Doctrine\ORM\EntityNotFoundException $E) {
         throw new PlentymarketsExportException('The item »' . $this->SHOPWARE_Article->getName() . '« with the id »' . $this->SHOPWARE_Article->getId() . '« could not be exported (missing main detail)', 2811);
     }
     // Set the release date
     if ($ReleaseDate instanceof \DateTime) {
         $Object_SetItemsBaseItemBase->Published = $ReleaseDate->getTimestamp();
     }
     $Object_SetItemsBaseItemBase->ExternalItemID = PlentymarketsUtils::getExternalItemID($Item->getId());
     if ($Item->getSupplier() instanceof Shopware\Models\Article\Supplier) {
         $Object_SetItemsBaseItemBase->ProducerID = PlentymarketsMappingController::getProducerByShopwareID($Item->getSupplier()->getId());
     }
     $Object_SetItemsBaseItemBase->EAN1 = $ItemDetails->getEan();
     $Object_SetItemsBaseItemBase->VATInternalID = PlentymarketsMappingController::getVatByShopwareID($Item->getTax()->getId());
     $Object_SetItemsBaseItemBase->ItemNo = $ItemDetails->getNumber();
     //
     $Object_SetItemsBaseItemBase->Availability = $this->getObjectAvailabiliy();
     $Object_SetItemsBaseItemBase->PriceSet = $this->getObjectPriceSet();
     //
     $Object_SetItemsBaseItemBase->Categories = array();
     $Object_SetItemsBaseItemBase->StoreIDs = array();
     /** @var Shopware\Models\Category\Category $Category */
     foreach ($Item->getCategories() as $Category) {
         // Get the store for this category
         $rootId = PlentymarketsUtils::getRootIdByCategory($Category);
         $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId);
         $plentyCategoryBranchId = null;
         foreach ($shops as $shopId) {
             try {
                 $plentyCategoryBranchId = PlentymarketsMappingEntityCategory::getCategoryByShopwareID($Category->getId(), $shopId);
             } catch (PlentymarketsMappingExceptionNotExistant $E) {
                 PlentymarketsLogger::getInstance()->error('Export:Initial:Item', 'The category »' . $Category->getName() . '« with the id »' . $Category->getId() . '« will not be assigned to the item with the number »' . $ItemDetails->getNumber() . '«', 2820);
                 continue;
             }
             try {
                 $storeId = PlentymarketsMappingController::getShopByShopwareID($shopId);
             } catch (PlentymarketsMappingExceptionNotExistant $E) {
                 continue;
             }
             if (!isset($this->storeIds[$storeId])) {
                 // Activate the item for this store
                 $Object_Integer = new PlentySoapObject_Integer();
                 $Object_Integer->intValue = $storeId;
                 $Object_SetItemsBaseItemBase->StoreIDs[] = $Object_Integer;
                 // Cache
                 $this->storeIds[$storeId] = true;
             }
         }
         if (!$plentyCategoryBranchId) {
             continue;
         }
         // Activate the category
         $Object_ItemCategory = new PlentySoapObject_ItemCategory();
         $Object_ItemCategory->ItemCategoryID = $plentyCategoryBranchId;
         // string
         $Object_SetItemsBaseItemBase->Categories[] = $Object_ItemCategory;
     }
     $Object_SetItemsBaseItemBase->Texts = $this->getObjectTexts();
     $Object_SetItemsBaseItemBase->FreeTextFields = $this->getObjectFreeTextFields();
     $Request_SetItemsBase->BaseItems[] = $Object_SetItemsBaseItemBase;
     $Response_SetItemsBase = PlentymarketsSoapClient::getInstance()->SetItemsBase($Request_SetItemsBase);
     $ResponseMessage = $Response_SetItemsBase->ResponseMessages->item[0];
     if (!$Response_SetItemsBase->Success || $ResponseMessage->Code != 100) {
         throw new PlentymarketsExportException('The item with the number »' . $ItemDetails->getNumber() . '« could not be exported', 2800);
     }
     foreach ($ResponseMessage->SuccessMessages->item as $SubMessage) {
         if ($SubMessage->Key == 'ItemID') {
             $this->PLENTY_itemID = (int) $SubMessage->Value;
         } else {
             if ($SubMessage->Key == 'PriceID') {
                 $this->PLENTY_priceID = (int) $SubMessage->Value;
             }
         }
     }
     if ($this->PLENTY_itemID && $this->PLENTY_priceID) {
         PlentymarketsLogger::getInstance()->message('Export:Initial:Item', 'The item with the number »' . $ItemDetails->getNumber() . '« has been created (ItemId: ' . $this->PLENTY_itemID . ', PriceId: ' . $this->PLENTY_priceID . ')');
         PlentymarketsMappingController::addItem($Item->getId(), $this->PLENTY_itemID);
     } else {
         throw new PlentymarketsExportException('The item with the number »' . $ItemDetails->getNumber() . '« could not be exported (no item ID and price ID respectively)', 2830);
     }
 }
 /**
  * Does the actual import
  */
 public function import()
 {
     $parentId = Shopware()->Models()->find('Shopware\\Models\\Shop\\Shop', $this->shopId)->getCategory()->getId();
     // Trigger to indicate an error while creating new category
     $addError = false;
     foreach ($this->plentyCategoryBranch as $plentyCategory) {
         $plentyCategoryId = $plentyCategory['branchId'];
         $plentyCategoryName = $plentyCategory['name'];
         if ($this->categoryNodeLang != $this->shopLang) {
             $plentyLocalCategoryName = $this->getLocalCategoryName($plentyCategoryId);
             if ($plentyLocalCategoryName) {
                 $plentyCategoryName = $plentyLocalCategoryName;
             }
         }
         // Root category id (out of the shop)
         $CategoryFound = self::$CategoryRepository->findOneBy(array('name' => $plentyCategoryName, 'parentId' => $parentId));
         if ($CategoryFound instanceof Shopware\Models\Category\Category) {
             //
             PlentymarketsMappingEntityCategory::addCategory($CategoryFound->getId(), $this->shopId, $plentyCategoryId, $this->storeId);
             $parentId = $CategoryFound->getId();
         } else {
             $params = array();
             $params['name'] = $plentyCategoryName;
             $params['parentId'] = $parentId;
             try {
                 // Create
                 $CategoryModel = self::$CategoryApi->create($params);
                 // Add mapping and save into the object
                 PlentymarketsMappingEntityCategory::addCategory($CategoryModel->getId(), $this->shopId, $plentyCategoryId, $this->storeId);
                 // Parent
                 $parentCategoryName = $CategoryModel->getParent()->getName();
                 // Log
                 PlentymarketsLogger::getInstance()->message('Sync:Item:Category', 'The category »' . $plentyCategoryName . '« has been created beneath the category »' . $parentCategoryName . '«');
                 // Id to connect with the item
                 $parentId = $CategoryModel->getId();
             } catch (Exception $E) {
                 // Log
                 PlentymarketsLogger::getInstance()->error('Sync:Item:Category', 'The category »' . $plentyCategoryName . '« with the parentId »' . $parentId . '« could not be created (' . $E->getMessage() . ')', 3300);
                 // Set the trigger - the category will not be connected with the item
                 $addError = true;
             }
         }
     }
     if ($addError) {
         return false;
     } else {
         return $parentId;
     }
 }