/**
  * 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
    /**
     * @param array $data
     * @param \Shopware\Models\Article\Article $article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @return array
     */
    protected function prepareVariants($data, ArticleModel $article)
    {
        unset($data['details']);

        if (!isset($data['variants'])) {
            return $data;
        }

        $variants = array();
        foreach ($data['variants'] as $variantData) {

            if (isset($variantData['id'])) {
                $variant = $this->getManager()->getRepository('Shopware\Models\Article\Detail')->findOneBy(array(
                    'id'        => $variantData['id'],
                    'articleId' => $article->getId()
                ));

                if (!$variant) {
                    throw new ApiException\CustomValidationException(sprintf("Variant by id %s not found", $variantData['id']));
                }
            } elseif (isset($variantData['number'])) {
                $variant = $this->getManager()->getRepository('Shopware\Models\Article\Detail')->findOneBy(array(
                    'number'    => $variantData['number'],
                    'articleId' => $article->getId()
                ));
            }

            if (!$variant) {
                $variant = new \Shopware\Models\Article\Detail();
                $variant->setKind(2);
            }

            $variantData = $this->prepareVariantAssociatedData($variantData);
            $variantData = $this->prepareVariantPricesAssociatedData($data, $variantData, $article, $variant);
            $variantData = $this->prepareVariantAttributeAssociatedData($variantData, $article, $variant);

            $variant->fromArray($variantData);

            if (isset($variantData['configuratorOptions']) && is_array($variantData['configuratorOptions'])) {
                $configuratorSet = $article->getConfiguratorSet();

                if (!$configuratorSet && !isset($data['configuratorSet'])) {
                    throw new ApiException\CustomValidationException('A configuratorset has to be defined');
                }

                /** @var \Shopware\Models\Article\Configurator\Set $configuratorSet */
                if ($configuratorSet) {
                    $availableGroups = $configuratorSet->getGroups();
                } else {
                    $configuratorSet = $data['configuratorSet'];
                    $availableGroups = $configuratorSet->getGroups();
                }

                $assignedOptions = new \Doctrine\Common\Collections\ArrayCollection();
                foreach ($variantData['configuratorOptions'] as $configuratorOption) {
                    $group  = $configuratorOption['group'];
                    $option = $configuratorOption['option'];

                    /** @var \Shopware\Models\Article\Configurator\Group $availableGroup */
                    foreach ($availableGroups as $availableGroup) {
                        if ($availableGroup->getName() == $group) {
                            $optionExists = false;
                            /** @var \Shopware\Models\Article\Configurator\Option $availableOption */
                            foreach ($availableGroup->getOptions() as $availableOption) {
                                if ($availableOption->getName() == $option) {
                                    $assignedOptions->add($availableOption);
                                    $optionExists = true;
                                    break;
                                }
                            }

                            if (!$optionExists) {
                                $optionModel = new \Shopware\Models\Article\Configurator\Option();
                                $optionModel->setPosition(0);
                                $optionModel->setName($option);
                                $optionModel->setGroup($availableGroup);
                                $this->getManager()->persist($optionModel);
                                $assignedOptions->add($optionModel);
                            }
                        }
                    }
                }

                $variant->setConfiguratorOptions($assignedOptions);
            }

            if (count($variant->getConfiguratorOptions()) === 0) {
                throw new \Exception('No Configurator Options assigned');
            }

            if ($variantData['isMain'] || $variantData['standard']) {
                $newMain = $variant;
                $newMain->setKind(1);

                // todo@dn Check if has configurator - delete else
                $oldMain = $data['mainDetail'];
                $oldMain['kind'] = 2;

                $data['mainDetail'] = $newMain;
//                $variant = $oldMain;
            }

            $variants[] = $variant;
        }

        $data['details'] = $variants;
        unset($data['variants']);

        return $data;
    }
예제 #3
0
 /**
  * Internal helper function to duplicate the variant configuration from the passed article
  * to the new article.
  *
  * @param integer $articleId
  * @param integer $newArticleId
  * @param integer $mailDetailId
  */
 protected function duplicateArticleDetails($articleId, $newArticleId, $mailDetailId = null)
 {
     $article = Shopware()->Models()->find('Shopware\\Models\\Article\\Article', $newArticleId);
     $builder = Shopware()->Models()->createQueryBuilder();
     $builder->select(array('details', 'prices', 'attribute', 'images'))->from('Shopware\\Models\\Article\\Detail', 'details')->leftJoin('details.prices', 'prices')->leftJoin('details.attribute', 'attribute')->leftJoin('details.images', 'images')->where('details.articleId = ?1');
     if ($mailDetailId !== null) {
         $builder->andWhere('details.id = ?2');
         $details = $builder->setParameter(1, $articleId)->setParameter(2, $mailDetailId)->getQuery()->getArrayResult();
     } else {
         $details = $builder->setParameter(1, $articleId)->getQuery()->getArrayResult();
     }
     $newArticleData = $this->getNewArticleData();
     $number = $newArticleData['number'];
     foreach ($details as $data) {
         $prices = array();
         $data['number'] = $number;
         $detail = new \Shopware\Models\Article\Detail();
         foreach ($data['prices'] as $priceData) {
             if (empty($priceData['customerGroupKey'])) {
                 continue;
             }
             $customerGroup = $this->getCustomerGroupRepository()->findOneBy(array('key' => $priceData['customerGroupKey']));
             if ($customerGroup instanceof \Shopware\Models\Customer\Group) {
                 $priceData['customerGroup'] = $customerGroup;
                 $priceData['article'] = $article;
                 $prices[] = $priceData;
             }
         }
         $data['prices'] = $prices;
         // unset configuratorOptions and images. These are variant specific and are going to be recreated later
         unset($data['images']);
         unset($data['configuratorOptions']);
         if (!empty($data['unitId'])) {
             $data['unit'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Unit', $data['unitId']);
         } else {
             $data['unit'] = null;
         }
         if (!empty($data['attribute'])) {
             $data['attribute']['article'] = $article;
         }
         $data['article'] = $article;
         $detail->fromArray($data);
         Shopware()->Models()->persist($detail);
         if ($detail->getAttribute()) {
             Shopware()->Models()->persist($detail->getAttribute());
         }
     }
     Shopware()->Models()->flush();
     $this->increaseAutoNumber($newArticleData['autoNumber'], $number);
 }
예제 #4
0
 /**
  * Helper function to check if the stock is valid if the article is on sale
  *
  * @param \Shopware\Models\Article\Detail $variant
  * @param int $newStock
  * @return bool
  */
 private function isValidStock(Shopware\Models\Article\Detail $variant, $newStock)
 {
     if ($variant->getArticle()->getLastStock() && $newStock < 0) {
         return false;
     }
     return true;
 }
예제 #5
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()
            ));
        }
    }
 /**
  * Updates the stock for the given item detail
  *
  * @param Shopware\Models\Article\Detail $Detail
  * @param float $stock
  */
 protected function updateByDetail(Shopware\Models\Article\Detail $Detail, $stock)
 {
     if ($stock > 0) {
         // At least one
         $stock = max(1, ceil($stock / 100 * $this->itemWarehousePercentage));
     }
     // Remember the last stock (for the log message)
     $previousStock = $Detail->getInStock();
     $diff = $stock - $previousStock;
     // Nothing to to
     if ($previousStock == $stock || $diff == 0) {
         return;
     }
     // Set the stock
     $Detail->setInStock($stock);
     // And save it
     Shopware()->Models()->persist($Detail);
     Shopware()->Models()->flush();
     // Log
     if ($diff > 0) {
         $diff = '+' . $diff;
     }
     PlentymarketsLogger::getInstance()->message('Sync:Item:Stock', 'The stock of the item »' . $Detail->getArticle()->getName() . '« with the number »' . $Detail->getNumber() . '« has been rebooked to ' . $stock . ' (' . $diff . ')');
 }