/**
  * Exports the item variants
  */
 protected function exportVariants()
 {
     // Verknüpfung mit den Attribut(-werten)
     $ConfiguratorSet = $this->SHOPWARE_Article->getConfiguratorSet();
     if (!$ConfiguratorSet instanceof Shopware\Models\Article\Configurator\Set) {
         return;
     }
     // Active the attribute values at the item --------------------------------------------------------------------
     $objectsActivateLinks = array();
     $Request_SetItemAttributeLinks = new PlentySoapRequest_SetItemAttributeLinks();
     $Request_SetItemAttributeLinks->ItemID = $this->PLENTY_itemID;
     // int
     $Request_SetItemAttributeLinks->AttributeLinks = array();
     /** @var Shopware\Models\Article\Configurator\Option $ConfiguratorOption */
     foreach ($ConfiguratorSet->getOptions() as $ConfiguratorOption) {
         $PLENTY_attributeID = PlentymarketsMappingController::getAttributeGroupByShopwareID($ConfiguratorOption->getGroup()->getId());
         $PLENTY_attributeValueID = PlentymarketsMappingController::getAttributeOptionByShopwareID($ConfiguratorOption->getId());
         $Object_AttributeLink = new PlentySoapObject_AttributeLink();
         $Object_AttributeLink->AttributeID = $PLENTY_attributeID;
         // int
         $Object_AttributeLink->AttributeValueID = $PLENTY_attributeValueID;
         // int
         $Object_AttributeLink->Activate = true;
         $objectsActivateLinks[] = $Object_AttributeLink;
     }
     // Run the calls
     foreach (array_chunk($objectsActivateLinks, 100) as $activateLinks) {
         $Request_SetItemAttributeLinks->AttributeLinks = $activateLinks;
         PlentymarketsSoapClient::getInstance()->SetItemAttributeLinks($Request_SetItemAttributeLinks);
     }
     // generate the attribute value sets --------------------------------------------------------------------------
     $objectsSetAttributeValueSets = array();
     $cacheAttributeValueSets = array();
     $Request_SetItemAttributeVariants = new PlentySoapRequest_SetItemAttributeVariants();
     $Request_SetItemAttributeVariants->ItemID = $this->PLENTY_itemID;
     // int
     $Request_SetItemAttributeVariants->SetAttributeValueSets = array();
     $Details = $this->SHOPWARE_Article->getDetails();
     /**
      * @var Shopware\Models\Article\Detail $ItemVariation
      * @var Shopware\Models\Article\Configurator\Option $ConfiguratorOption
      */
     foreach ($Details as $ItemVariation) {
         $cacheAttributeValueSets[$ItemVariation->getId()] = array();
         $Object_AttributeVariantList = new PlentySoapObject_AttributeVariantList();
         foreach ($ItemVariation->getConfiguratorOptions() as $ConfiguratorOption) {
             $PLENTY_attributeValueID = PlentymarketsMappingController::getAttributeOptionByShopwareID($ConfiguratorOption->getId());
             $cacheAttributeValueSets[$ItemVariation->getId()][] = $PLENTY_attributeValueID;
             $Object_Integer = new PlentySoapObject_Integer();
             $Object_Integer->intValue = $PLENTY_attributeValueID;
             $Object_AttributeVariantList->AttributeValueIDs[] = $Object_Integer;
         }
         $objectsSetAttributeValueSets[] = $Object_AttributeVariantList;
     }
     foreach (array_chunk($objectsSetAttributeValueSets, 100) as $setAttributeValueSets) {
         // Complete the request
         $Request_SetItemAttributeVariants->SetAttributeValueSets = $setAttributeValueSets;
         // and go for it
         $Response_SetItemAttributeVariants = PlentymarketsSoapClient::getInstance()->SetItemAttributeVariants($Request_SetItemAttributeVariants);
         // Matching der Varianten
         foreach ($Response_SetItemAttributeVariants->ResponseMessages->item as $ResponseMessage) {
             if ($ResponseMessage->IdentificationKey != 'AttributeValueIDs') {
                 continue;
             }
             // If there is an error message, go ahead
             if (!is_null($ResponseMessage->ErrorMessages)) {
                 continue;
             }
             $PLENTY_attributeValueIDs = array_map('intval', explode(';', $ResponseMessage->IdentificationValue));
             $PLENTY_variantID = (int) $ResponseMessage->SuccessMessages->item[0]->Value;
             foreach ($cacheAttributeValueSets as $SHOPWARE_variantID => $attributeValueIDs) {
                 if (PlentymarketsUtils::arraysAreEqual($attributeValueIDs, $PLENTY_attributeValueIDs)) {
                     PlentymarketsMappingController::addItemVariant($SHOPWARE_variantID, sprintf('%s-%s-%s', $this->PLENTY_itemID, $this->PLENTY_priceID, $PLENTY_variantID));
                     break;
                 }
             }
         }
     }
     // Set the variation details ----------------------------------------------------------------------------------
     $objectsAttributeValueSetsDetails = array();
     // start the request
     $Request_SetAttributeValueSetsDetails = new PlentySoapRequest_SetAttributeValueSetsDetails();
     $Request_SetAttributeValueSetsDetails->AttributeValueSetsDetails = array();
     /** @var Shopware\Models\Article\Detail $ItemVariation */
     foreach ($Details as $ItemVariation) {
         try {
             $sku = PlentymarketsMappingController::getItemVariantByShopwareID($ItemVariation->getId());
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             // Roll back the item
             $this->rollback();
             // and quit
             throw new PlentymarketsExportException('The item variation with the number »' . $ItemVariation->getNumber() . '« could not be created (corrupt data)', 2880);
         }
         $Object_SetAttributeValueSetsDetails = new PlentySoapObject_SetAttributeValueSetsDetails();
         $Object_SetAttributeValueSetsDetails->Availability = $ItemVariation->getActive();
         // int
         $Object_SetAttributeValueSetsDetails->EAN1 = $ItemVariation->getEan();
         // string
         $Object_SetAttributeValueSetsDetails->MaxStock = null;
         // float
         $Object_SetAttributeValueSetsDetails->PurchasePrice = null;
         // float
         $Object_SetAttributeValueSetsDetails->SKU = $sku;
         $Object_SetAttributeValueSetsDetails->Variantnumber = $ItemVariation->getNumber();
         // string
         $objectsAttributeValueSetsDetails[] = $Object_SetAttributeValueSetsDetails;
     }
     foreach (array_chunk($objectsAttributeValueSetsDetails, 50) as $attributeValueSetsDetails) {
         $Request_SetAttributeValueSetsDetails->AttributeValueSetsDetails = $attributeValueSetsDetails;
         PlentymarketsSoapClient::getInstance()->SetAttributeValueSetsDetails($Request_SetAttributeValueSetsDetails);
     }
 }
 /**
  * Export the missing attribtues to plentymarkets and save the mapping
  */
 protected function doExport()
 {
     // Repository
     $Repository = Shopware()->Models()->getRepository('Shopware\\Models\\Article\\Configurator\\Group');
     // Chunk configuration
     $chunk = 0;
     $size = PlentymarketsConfig::getInstance()->getInitialExportChunkSize(PlentymarketsExportController::DEFAULT_CHUNK_SIZE);
     do {
         PlentymarketsLogger::getInstance()->message('Export:Initial:Attribute', 'Chunk: ' . ($chunk + 1));
         $Groups = $Repository->findBy(array(), null, $size, $chunk * $size);
         /** @var Shopware\Models\Article\Configurator\Group $Attribute */
         foreach ($Groups as $Attribute) {
             $Request_SetItemAttributes = new PlentySoapRequest_SetItemAttributes();
             $Object_SetItemAttribute = new PlentySoapObject_SetItemAttribute();
             $Object_SetItemAttribute->BackendName = sprintf('%s (Sw %d)', $Attribute->getName(), $Attribute->getId());
             $Object_SetItemAttribute->FrontendLang = 'de';
             $Object_SetItemAttribute->FrontendName = $Attribute->getName();
             $Object_SetItemAttribute->Position = $Attribute->getPosition();
             try {
                 $attributeIdAdded = PlentymarketsMappingController::getAttributeGroupByShopwareID($Attribute->getId());
             } catch (PlentymarketsMappingExceptionNotExistant $E) {
                 if (isset($this->PLENTY_name2ID[strtolower($Object_SetItemAttribute->BackendName)])) {
                     $attributeIdAdded = $this->PLENTY_name2ID[strtolower($Object_SetItemAttribute->BackendName)];
                 } else {
                     $Request_SetItemAttributes->Attributes[] = $Object_SetItemAttribute;
                     $Response = PlentymarketsSoapClient::getInstance()->SetItemAttributes($Request_SetItemAttributes);
                     if (!$Response->Success) {
                         throw new PlentymarketsExportException('The item attribute »' . $Object_SetItemAttribute->BackendName . '« could not be created', 2911);
                     }
                     $attributeIdAdded = (int) $Response->ResponseMessages->item[0]->SuccessMessages->item[0]->Value;
                 }
                 // Add the mapping
                 PlentymarketsMappingController::addAttributeGroup($Attribute->getId(), $attributeIdAdded);
                 $this->exportAttributeTranslations($Attribute->getId(), $attributeIdAdded);
             }
             // Values
             /** @var Shopware\Models\Article\Configurator\Option $AttributeValue */
             foreach ($Attribute->getOptions() as $AttributeValue) {
                 $Request_SetItemAttributes = new PlentySoapRequest_SetItemAttributes();
                 $Object_SetItemAttribute = new PlentySoapObject_SetItemAttribute();
                 $Object_SetItemAttribute->Id = $attributeIdAdded;
                 $Object_SetItemAttributeValue = new PlentySoapObject_SetItemAttributeValue();
                 $Object_SetItemAttributeValue->BackendName = sprintf('%s (Sw %d)', $AttributeValue->getName(), $AttributeValue->getId());
                 $Object_SetItemAttributeValue->FrontendName = $AttributeValue->getName();
                 $Object_SetItemAttributeValue->Position = $AttributeValue->getPosition();
                 $Object_SetItemAttribute->Values[] = $Object_SetItemAttributeValue;
                 $Request_SetItemAttributes->Attributes[] = $Object_SetItemAttribute;
                 try {
                     PlentymarketsMappingController::getAttributeOptionByShopwareID($AttributeValue->getId());
                 } catch (PlentymarketsMappingExceptionNotExistant $E) {
                     // Workaround
                     $checknameValue = strtolower(str_replace(',', '.', $Object_SetItemAttributeValue->BackendName));
                     if (isset($this->PLENTY_idAndValueName2ID[$attributeIdAdded][$checknameValue])) {
                         PlentymarketsMappingController::addAttributeOption($AttributeValue->getId(), $this->PLENTY_idAndValueName2ID[$attributeIdAdded][$checknameValue]);
                     } else {
                         $Response = PlentymarketsSoapClient::getInstance()->SetItemAttributes($Request_SetItemAttributes);
                         if (!$Response->Success) {
                             throw new PlentymarketsExportException('The item attribute option »' . $Object_SetItemAttributeValue->BackendName . '« could not be created', 2912);
                         }
                         foreach ($Response->ResponseMessages->item[0]->SuccessMessages->item as $MessageItem) {
                             if ($MessageItem->Key == 'AttributeValueID') {
                                 PlentymarketsMappingController::addAttributeOption($AttributeValue->getId(), $MessageItem->Value);
                                 $this->exportAttributeValuesTranslations($attributeIdAdded, $AttributeValue->getId(), $MessageItem->Value);
                             }
                         }
                     }
                 }
             }
         }
         ++$chunk;
     } while (!empty($Groups) && count($Groups) == $size);
 }
    /**
     * Generates "reverse mapping"
     *
     * @param array $article
     */
    public function map($article)
    {
        $details = $article['details'];
        $details[] = $article['mainDetail'];
        foreach ($details as $detail) {
            foreach ($detail['configuratorOptions'] as $option) {
                try {
                    // Mapping for the Group -> plentymarkets Attribute
                    if (!isset(self::$mapping['group'][$option['groupId']])) {
                        $plentyGroupId = PlentymarketsMappingController::getAttributeGroupByShopwareID($option['groupId']);
                    } else {
                        $plentyGroupId = self::$mapping['group'][$option['groupId']];
                    }
                } catch (PlentymarketsMappingExceptionNotExistant $E) {
                    // Name auslesen
                    $group = Shopware()->Db()->fetchRow('
						SELECT name FROM s_article_configurator_groups WHERE id = ?
					', array($option['groupId']));
                    //
                    $plentyGroupId = $this->groupName2plentyId[$group['name']];
                    //
                    PlentymarketsMappingController::addAttributeGroup($option['groupId'], $plentyGroupId);
                }
                try {
                    // Mapping for the Group -> plentymarkets Attribute
                    if (!isset(self::$mapping['option'][$option['id']])) {
                        $plentyOptionId = PlentymarketsMappingController::getAttributeOptionByShopwareID($option['id']);
                    } else {
                        $plentyOptionId = self::$mapping['option'][$option['id']];
                    }
                } catch (PlentymarketsMappingExceptionNotExistant $E) {
                    //
                    $plentyOptionId = $this->groupId2optionName2plentyId[$plentyGroupId][$option['name']];
                    //
                    PlentymarketsMappingController::addAttributeOption($option['id'], $plentyOptionId);
                    //
                }
                self::$mapping['group'][$option['groupId']] = $plentyGroupId;
                self::$mapping['option'][$option['id']] = $plentyOptionId;
            }
        }
    }