/**
  * Exports the item bundle head item
  *
  * @throws PlentymarketsExportException
  */
 protected function exportHead()
 {
     // The shopware item on which the bundle is based on
     $shopwareBundleHead = $this->SHOPWARE_bundle->getArticle();
     $shopwareBundleHeadIsVariant = !is_null($shopwareBundleHead->getConfiguratorSet());
     // If the bundle head is a variant, the bundle can't be exported
     // since that feature is not provided by plentymarkets
     if ($shopwareBundleHeadIsVariant) {
         throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the master item is a variant.', 2230);
     }
     if ($this->SHOPWARE_bundle->getDiscountType() != 'abs') {
         throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the discount type is not supported.', 2240);
     }
     if ($this->SHOPWARE_bundle->getType() != 1) {
         throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the bundle type is not supported.', 2250);
     }
     // The shopware bundle head needs to be added as a plenty-bundle-item
     // The bundle head in plentymarkets is a "special" item
     $bundleItemId = PlentymarketsMappingController::getItemByShopwareID($shopwareBundleHead->getId());
     $sku = sprintf('%d-0', $bundleItemId);
     // If this item is also a bundle item in shopware,
     // we need to increase the quantity of it
     if (isset($this->PLENTY_bundleSkuList[$sku])) {
         $this->PLENTY_bundleSkuList[$sku] += 1;
     } else {
         $this->PLENTY_bundleSkuList[$sku] = 1;
     }
     // Create the bundle head
     $Request_AddItemsBase = new PlentySoapRequest_AddItemsBase();
     $Request_AddItemsBase->BaseItems = array();
     $Object_AddItemsBaseItemBase = new PlentySoapObject_AddItemsBaseItemBase();
     $Object_ItemAvailability = new PlentySoapObject_ItemAvailability();
     $validTo = $this->SHOPWARE_bundle->getValidTo();
     if ($validTo instanceof DateTime) {
         $Object_ItemAvailability->AvailableUntil = $this->SHOPWARE_bundle->getValidTo()->getTimestamp();
     }
     $Object_ItemAvailability->WebAPI = 1;
     $Object_ItemAvailability->Inactive = (int) $this->SHOPWARE_bundle->getActive();
     $Object_ItemAvailability->Webshop = (int) $this->SHOPWARE_bundle->getActive();
     $Object_AddItemsBaseItemBase->Availability = $Object_ItemAvailability;
     $storeIds = array();
     $Object_AddItemsBaseItemBase->Categories = array();
     $Object_AddItemsBaseItemBase->StoreIDs = array();
     foreach ($shopwareBundleHead->getCategories() as $category) {
         /** @var Shopware\Models\Category\Category $category */
         try {
             $categoryPath = PlentymarketsMappingController::getCategoryByShopwareID($category->getId());
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             continue;
         }
         $Object_ItemCategory = new PlentySoapObject_ItemCategory();
         $Object_ItemCategory->ItemCategoryPath = $categoryPath;
         // string
         $Object_AddItemsBaseItemBase->Categories[] = $Object_ItemCategory;
         // Get the store for this category
         $rootId = PlentymarketsUtils::getRootIdByCategory($category);
         $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId);
         foreach ($shops as $shopId) {
             try {
                 $storeId = PlentymarketsMappingController::getShopByShopwareID($shopId);
             } catch (PlentymarketsMappingExceptionNotExistant $E) {
                 continue;
             }
             if (!isset($storeIds[$storeId])) {
                 // Activate the item for this store
                 $Object_Integer = new PlentySoapObject_Integer();
                 $Object_Integer->intValue = $storeId;
                 $Object_AddItemsBaseItemBase->StoreIDs[] = $Object_Integer;
                 // Cache
                 $storeIds[$storeId] = true;
             }
         }
     }
     $Object_AddItemsBaseItemBase->ExternalItemID = 'Swag/Bundle/' . $this->SHOPWARE_bundle->getId();
     // string
     $Object_AddItemsBaseItemBase->ItemNo = $this->SHOPWARE_bundle->getNumber();
     // string
     $Object_ItemPriceSet = new PlentySoapObject_ItemPriceSet();
     $defaultCustomerGroupKey = PlentymarketsConfig::getInstance()->get('DefaultCustomerGroupKey');
     $price = null;
     $isPriceFound = false;
     foreach ($this->SHOPWARE_bundle->getPrices() as $price) {
         /** @var Shopware\CustomModels\Bundle\Price $price */
         if ($price->getCustomerGroup()->getKey() == $defaultCustomerGroupKey) {
             $isPriceFound = true;
             break;
         }
     }
     if ($isPriceFound && $price instanceof Shopware\CustomModels\Bundle\Price) {
         $tax = $this->SHOPWARE_bundle->getArticle()->getTax()->getTax();
         $priceNet = $price->getPrice();
         $price = $priceNet + $priceNet / 100 * $tax;
         $Object_ItemPriceSet->Price = $price;
         $Object_ItemPriceSet->VAT = $tax;
     } else {
         // If there is no price, we have to set one anyway.
         // Otherwise the re-import will crash
         $Object_ItemPriceSet->Price = 1;
     }
     $Object_AddItemsBaseItemBase->PriceSet = $Object_ItemPriceSet;
     $Object_AddItemsBaseItemBase->VATInternalID = PlentymarketsMappingController::getVatByShopwareID($this->SHOPWARE_bundle->getArticle()->getTax()->getId());
     $Object_AddItemsBaseItemBase->ProducerID = PlentymarketsMappingController::getProducerByShopwareID($shopwareBundleHead->getSupplier()->getId());
     // int
     $Object_AddItemsBaseItemBase->Published = null;
     // int
     $Object_ItemTexts = new PlentySoapObject_ItemTexts();
     $Object_ItemTexts->Name = $this->SHOPWARE_bundle->getName();
     // string
     $Object_AddItemsBaseItemBase->Texts = $Object_ItemTexts;
     $Request_AddItemsBase->BaseItems[] = $Object_AddItemsBaseItemBase;
     $Response_AddItemsBase = PlentymarketsSoapClient::getInstance()->AddItemsBase($Request_AddItemsBase);
     $ResponseMessage = $Response_AddItemsBase->ResponseMessages->item[0];
     if (!$Response_AddItemsBase->Success || $ResponseMessage->Code != 100) {
         throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210);
     }
     $PLENTY_priceID = null;
     foreach ($ResponseMessage->SuccessMessages->item as $SubMessage) {
         if ($SubMessage->Key == 'ItemID') {
             $this->PLENTY_bundleHeadId = (int) $SubMessage->Value;
         } else {
             if ($SubMessage->Key == 'PriceID') {
                 $PLENTY_priceID = (int) $SubMessage->Value;
             }
         }
     }
     if ($this->PLENTY_bundleHeadId && $PLENTY_priceID) {
         PlentymarketsLogger::getInstance()->message('Export:Initial:Item:Bundle', 'The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« has been created with the id »' . $this->PLENTY_bundleHeadId . '«.');
         PlentymarketsMappingController::addItemBundle($this->SHOPWARE_bundle->getId(), $this->PLENTY_bundleHeadId);
     } else {
         throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210);
     }
 }
 /**
  * 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);
     }
 }