/**
  * Deletes a page of invalid data
  *
  * @param integer $start
  * @param integer $offset
  */
 public function deleteInvalidData($start, $offset)
 {
     // Customer group
     $customerGroupKey = PlentymarketsConfig::getInstance()->getDefaultCustomerGroupKey();
     $customerGroupRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Customer\\Group');
     $customerGroups = $customerGroupRepository->findBy(array('key' => $customerGroupKey));
     $customerGroup = array_pop($customerGroups);
     foreach ($this->getInvalidData($start, $offset) as $data) {
         try {
             /** @var \Shopware\Models\Article\Article $Item */
             $Item = Shopware()->Models()->find('\\Shopware\\Models\\Article\\Article', $data['itemId']);
             $detail = new \Shopware\Models\Article\Detail();
             $detail->setArticle($Item);
             // The number will be changed by the sync process
             $detail->setNumber(PlentymarketsImportItemHelper::getItemNumber());
             $price = new Shopware\Models\Article\Price();
             $price->setFrom(1);
             $price->setPrice(1);
             $price->setPercent(0);
             $price->setArticle($Item);
             $price->setDetail($detail);
             $price->setCustomerGroup($customerGroup);
             $Item->setMainDetail($detail);
             Shopware()->Models()->persist($Item);
             Shopware()->Models()->remove($Item);
         } catch (Exception $E) {
         }
     }
     Shopware()->Models()->flush();
 }
コード例 #2
0
ファイル: Article.php プロジェクト: nhp/shopware-4
    /**
     * Called when the user clicks the "generateVariants" button in the article backend module.
     * The function expects that an article id passed and an array with active groups passed.
     */
    public function createConfiguratorVariantsAction()
    {
        try {
            //first get the id parameter of the request object
            $articleId = $this->Request()->getParam('articleId', 1);
            $groups = $this->Request()->getParam('groups');
            $offset = $this->Request()->getParam('offset', 0);
            $limit = $this->Request()->getParam('limit', 50);

            //the merge type defines if all variants has to been regenerated or if only new variants will be added.
            //1 => Regenerate all variants
            //2 => Merge variants
            $mergeType = $this->Request()->getParam('mergeType', 1);

            /**@var $article \Shopware\Models\Article\Article*/
            $article = $this->getRepository()->find($articleId);

            $generatorData = $this->prepareGeneratorData($groups, $offset, $limit);
            if ($offset === 0 && $mergeType === 1) {
                $this->removeAllConfiguratorVariants($articleId);
            } else if ($offset === 0 && $mergeType === 2) {
                $this->deleteVariantsForAllDeactivatedOptions($article, $generatorData['allOptions']);
            }

            $detailData = $this->getDetailDataForVariantGeneration($article);

            $configuratorSet = $article->getConfiguratorSet();

            $dependencies = $this->getRepository()->getConfiguratorDependenciesQuery($configuratorSet->getId())->getArrayResult();
            $priceSurcharges = $this->getRepository()->getConfiguratorPriceSurchargesQuery($configuratorSet->getId())->getArrayResult();

            if (empty($generatorData)) {
                return;
            }

            $sql = $generatorData['sql'];
            $originals = $generatorData['originals'];
            $variants = Shopware()->Db()->fetchAll($sql);

            if ($mergeType === 1) {
                $counter = $offset;
            } else {
                $sql= "SELECT COUNT(id) FROM s_articles_details WHERE articleID = ?";
                $counter = Shopware()->Db()->fetchOne($sql, array($articleId));
            }
            $allOptions = $this->getRepository()->getAllConfiguratorOptionsIndexedByIdQuery()->getResult();

            //iterate all selected variants to insert them into the database
            foreach($variants as $variant) {
                $variantData = $this->prepareVariantData($variant, $detailData, $counter, $dependencies, $priceSurcharges, $allOptions, $originals, $article, $mergeType);
                if ($variantData === false) {
                    continue;
                }
                //merge the data with the original main detail data
                $data = array_merge($detailData, $variantData);
                if(!$counter) {
                    $detail = $article->getMainDetail();
                } else {
                    $detail = new \Shopware\Models\Article\Detail();
                }
                $detail->fromArray($data);
                $detail->setArticle($article);
                Shopware()->Models()->persist($detail);
                $counter++;
            }
            Shopware()->Models()->flush();

            $article = $this->getArticle($articleId);
            $this->View()->assign(array(
                'success' => true,
                'data' => $article
            ));
        }
        catch (Exception $e) {
            $this->View()->assign(array(
                'success' => true,
                'message' => $e->getMessage()
            ));
        }
    }