Exemplo n.º 1
0
 public function testCreate()
 {
     $event = new ProductCreateEvent();
     $defaultCategory = CategoryQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $taxRuleId = TaxRuleQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $currencyId = CurrencyQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $event->setRef('testCreation')->setLocale('fr_FR')->setTitle('test create new product')->setVisible(1)->setDefaultCategory($defaultCategory)->setBasePrice(10)->setTaxRuleId($taxRuleId)->setBaseWeight(10)->setCurrencyId($currencyId)->setDispatcher($this->getDispatcher());
     $action = new Product();
     $action->create($event);
     $createdProduct = $event->getProduct();
     $this->assertInstanceOf('Thelia\\Model\\Product', $createdProduct);
     $this->assertFalse($createdProduct->isNew());
     $createdProduct->setLocale('fr_FR');
     $this->assertEquals('test create new product', $createdProduct->getTitle());
     $this->assertEquals('testCreation', $createdProduct->getRef());
     $this->assertEquals(1, $createdProduct->getVisible());
     $this->assertEquals($defaultCategory, $createdProduct->getDefaultCategoryId());
     $this->assertGreaterThan(0, $createdProduct->getPosition());
     $productSaleElements = $createdProduct->getProductSaleElementss();
     $this->assertEquals(1, count($productSaleElements));
     $defaultProductSaleElement = $productSaleElements->getFirst();
     $this->assertTrue($defaultProductSaleElement->getIsDefault());
     $this->assertEquals(0, $defaultProductSaleElement->getPromo());
     $this->assertEquals(0, $defaultProductSaleElement->getNewness());
     $this->assertEquals($createdProduct->getRef(), $defaultProductSaleElement->getRef());
     $this->assertEquals(10, $defaultProductSaleElement->getWeight());
     $productPrice = $defaultProductSaleElement->getProductPrices()->getFirst();
     $this->assertEquals(10, $productPrice->getPrice());
     $this->assertEquals($currencyId, $productPrice->getCurrencyId());
     return $createdProduct;
 }
Exemplo n.º 2
0
 public function createAction()
 {
     $this->checkAuth(AdminResources::PRODUCT, [], AccessManager::CREATE);
     $form = $this->createForm(ApiForm::PRODUCT_CREATION, 'form', [], ['csrf_protection' => false]);
     try {
         $creationForm = $this->validateForm($form);
         $event = new ProductCreateEvent();
         $event->bindForm($creationForm);
         $this->dispatch(TheliaEvents::PRODUCT_CREATE, $event);
         $product = $event->getProduct();
         $updateEvent = new ProductUpdateEvent($product->getId());
         $updateEvent->bindForm($creationForm);
         $this->dispatch(TheliaEvents::PRODUCT_UPDATE, $updateEvent);
         $this->getRequest()->query->set('lang', $creationForm->get('locale')->getData());
         $response = $this->getProductAction($product->getId());
         $response->setStatusCode(201);
         return $response;
     } catch (\Exception $e) {
         return JsonResponse::create(['error' => $e->getMessage()], 500);
     }
 }
Exemplo n.º 3
0
 public function import($startRecord = 0)
 {
     $count = 0;
     $errors = 0;
     $hdl = $this->t1db->query(sprintf("select * from produit order by rubrique asc limit %d, %d", intval($startRecord), $this->getChunkSize()));
     $image_import = new ProductImageImport($this->dispatcher, $this->t1db);
     $document_import = new ProductDocumentImport($this->dispatcher, $this->t1db);
     while ($hdl && ($produit = $this->t1db->fetch_object($hdl))) {
         $count++;
         // Put contents on the root folder in a special folder
         if ($produit->rubrique == 0) {
             try {
                 $this->cat_corresp->getT2($produit->rubrique);
             } catch (\Exception $ex) {
                 // Create the '0' folder
                 $root = new Category();
                 $root->setParent(0)->setLocale('fr_FR')->setTitle("Rubrique racine Thelia 1")->setLocale('en_US')->setTitle("Thelia 1 root category")->setDescription("")->setChapo("")->setPostscriptum("")->setVisible(true)->save();
                 Tlog::getInstance()->warning("Created pseudo-root category to store products at root level");
                 $this->cat_corresp->addEntry(0, $root->getId());
             }
         }
         $rubrique = $this->cat_corresp->getT2($produit->rubrique);
         if ($rubrique > 0) {
             try {
                 $this->product_corresp->getT2($produit->id);
                 Tlog::getInstance()->warning("Product ID={$produit->id} already imported.");
                 continue;
             } catch (ImportException $ex) {
                 // Okay, the product was not imported.
             }
             try {
                 // Check if the product ref is not already defined, and create a new one if it's the case.
                 $origRef = $destRef = $produit->ref;
                 $refIdx = 1;
                 while (null !== ($dupProd = ProductQuery::create()->findOneByRef($destRef))) {
                     Tlog::getInstance()->warning("Duplicate product reference: '{$destRef}', generating alternate reference.");
                     $destRef = sprintf('%s-%d', $origRef, $refIdx++);
                 }
                 $event = new ProductCreateEvent();
                 $idx = 0;
                 $descs = $this->t1db->query_list("select * from produitdesc where produit = ? order by lang asc", array($produit->id));
                 // Prices should be without axes
                 $produit->prix = round($produit->prix / (1 + $produit->tva / 100), 2);
                 $produit->prix2 = round($produit->prix2 / (1 + $produit->tva / 100), 2);
                 $product_id = 0;
                 foreach ($descs as $objdesc) {
                     $lang = $this->getT2Lang($objdesc->lang);
                     // A title is required to create the rewritten URL
                     if (empty($objdesc->titre)) {
                         $objdesc->titre = sprintf("Untitled-%d-%s", $objdesc->id, $lang->getCode());
                     }
                     if ($idx == 0) {
                         $event->setRef($destRef)->setLocale($lang->getLocale())->setTitle($objdesc->titre)->setDefaultCategory($this->cat_corresp->getT2($produit->rubrique))->setVisible($produit->ligne == 1 ? true : false)->setBasePrice($produit->prix)->setBaseWeight($produit->poids)->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setCurrencyId($this->getT2Currency()->getId());
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_CREATE, $event);
                         $product_id = $event->getProduct()->getId();
                         // Set the product template (resets the product default price)
                         // -----------------------------------------------------------
                         try {
                             $pste = new ProductSetTemplateEvent($event->getProduct(), $this->tpl_corresp->getT2($produit->rubrique), $this->getT2Currency()->getId());
                             $this->dispatcher->dispatch(TheliaEvents::PRODUCT_SET_TEMPLATE, $pste);
                         } catch (ImportException $ex) {
                             Tlog::getInstance()->addWarning("No product template was found for product {$product_id}: ", $ex->getMessage());
                         }
                         // Create the default product sale element, and update it
                         // ------------------------------------------------------
                         $create_pse_event = new ProductSaleElementCreateEvent($event->getProduct(), array(), $this->getT2Currency()->getId());
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_PRODUCT_SALE_ELEMENT, $create_pse_event);
                         $update_pse_event = new ProductSaleElementUpdateEvent($event->getProduct(), $create_pse_event->getProductSaleElement()->getId());
                         $update_pse_event->setReference($destRef)->setPrice($produit->prix)->setCurrencyId($this->getT2Currency()->getId())->setWeight($produit->poids)->setQuantity($produit->stock)->setSalePrice($produit->prix2)->setOnsale($produit->promo ? true : false)->setIsnew($produit->nouveaute ? true : false)->setIsdefault(true)->setEanCode('')->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setFromDefaultCurrency(0);
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $update_pse_event);
                         // Update position
                         // ---------------
                         $update_position_event = new UpdatePositionEvent($product_id, UpdatePositionEvent::POSITION_ABSOLUTE, $produit->classement);
                         $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_POSITION, $update_position_event);
                         Tlog::getInstance()->info("Created product {$product_id} from {$objdesc->titre} ({$produit->id})");
                         $this->product_corresp->addEntry($produit->id, $product_id);
                         // Import related content
                         // ----------------------
                         $contents = $this->t1db->query_list("select * from contenuassoc where objet=? and type=1 order by classement", array($produit->id));
                         // type: 1 = produit, 0=rubrique
                         foreach ($contents as $content) {
                             try {
                                 $content_event = new ProductAddContentEvent($event->getProduct(), $this->content_corresp->getT2($content->contenu));
                                 $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_CONTENT, $content_event);
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to create associated content {$content->contenu} for product {$product_id}: ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         // Update features (= caracteristiques) values
                         // -------------------------------------------
                         $caracvals = $this->t1db->query_list("select * from caracval where produit=?", array($produit->id));
                         foreach ($caracvals as $caracval) {
                             try {
                                 if (intval($caracval->caracdisp) != 0) {
                                     $feature_value = $this->feat_av_corresp->getT2($caracval->caracdisp);
                                     $is_text = false;
                                 } elseif ($caracval->valeur != '') {
                                     $feature_value = $caracval->valeur;
                                     $is_text = true;
                                 } else {
                                     continue;
                                 }
                                 $feature_value_event = new FeatureProductUpdateEvent($product_id, $this->feat_corresp->getT2($caracval->caracteristique), $feature_value, $is_text);
                                 $this->dispatcher->dispatch(TheliaEvents::PRODUCT_FEATURE_UPDATE_VALUE, $feature_value_event);
                             } catch (\Exception $ex) {
                                 Tlog::getInstance()->addError("Failed to update feature value with caracdisp ID={$caracval->caracdisp} (value='{$caracval->valeur}') for product {$product_id}: ", $ex->getMessage());
                                 $errors++;
                             }
                         }
                         // Update Attributes (= declinaisons) options
                         // ------------------------------------------
                         $rubdecs = $this->t1db->query_list("\n                                                                select\n                                                                    declinaison\n                                                                from\n                                                                    rubdeclinaison rd, declinaison d\n                                                                where\n                                                                    rd.declinaison=d.id\n                                                                and\n                                                                    rd.rubrique=?\n                                                                 order by\n                                                                    d.classement", array($produit->rubrique));
                         foreach ($rubdecs as $rubdec) {
                             $declidisps = $this->t1db->query_list("select id from declidisp where declinaison=?", array($rubdec->declinaison));
                             foreach ($declidisps as $declidisp) {
                                 $disabled = $this->t1db->query_list("select id from exdecprod where declidisp=? and produit=?", array($declidisp->id, $produit->id));
                                 if (count($disabled) > 0) {
                                     continue;
                                 }
                                 $stock = $this->t1db->query_obj("select * from stock where declidisp=?and produit=?", array($declidisp->id, $produit->id));
                                 if ($stock == false) {
                                     continue;
                                 }
                                 try {
                                     $pse_create_event = new ProductSaleElementCreateEvent($event->getProduct(), array($this->attr_av_corresp->getT2($stock->declidisp)), $this->getT2Currency()->getId());
                                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_ADD_PRODUCT_SALE_ELEMENT, $pse_create_event);
                                     $pse_update_event = new ProductSaleElementUpdateEvent($event->getProduct(), $pse_create_event->getProductSaleElement()->getId());
                                     $pse_update_event->setReference($destRef)->setPrice($produit->prix + $stock->surplus)->setCurrencyId($this->getT2Currency()->getId())->setWeight($produit->poids)->setQuantity($stock->valeur)->setSalePrice($produit->prix2 + $stock->surplus)->setOnsale($produit->promo ? true : false)->setIsnew($produit->nouveaute ? true : false)->setIsdefault(true)->setEanCode('')->setTaxRuleId($this->tax_corresp->getT2(1000 * $produit->tva))->setFromDefaultCurrency(0);
                                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $pse_update_event);
                                 } catch (\Exception $ex) {
                                     Tlog::getInstance()->addError("Failed to update product sale element value with declidisp ID={$stock->declidisp} for product {$product_id}: ", $ex->getMessage());
                                     $errors++;
                                 }
                             }
                         }
                         // Import images and documents
                         // ---------------------------
                         $image_import->importMedia($produit->id, $product_id);
                         $document_import->importMedia($produit->id, $product_id);
                     }
                     Tlog::getInstance()->info(sprintf("Updating product ID=%d, data for lang %s.", $product_id, $lang->getCode()));
                     // Update the newly created product
                     $update_event = new ProductUpdateEvent($product_id);
                     $update_event->setRef($destRef)->setLocale($lang->getLocale())->setTitle($objdesc->titre)->setChapo($objdesc->chapo)->setDescription($objdesc->description)->setPostscriptum($objdesc->postscriptum)->setVisible($produit->ligne == 1 ? true : false)->setDefaultCategory($this->cat_corresp->getT2($produit->rubrique));
                     $this->dispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE, $update_event);
                     // Update the rewritten URL, if one was defined
                     $this->updateRewrittenUrl($event->getProduct(), $lang->getLocale(), $objdesc->lang, "produit", "%id_produit={$produit->id}&id_rubrique={$produit->rubrique}%");
                     $idx++;
                 }
             } catch (\Exception $ex) {
                 Tlog::getInstance()->addError("Failed to import product ID={$produit->id}: ", $ex->getMessage());
                 $errors++;
             }
         } else {
             Tlog::getInstance()->addError("Cannot import product ID={$produit->id}, which is at root level (e.g., rubrique parent = 0).");
             $errors++;
         }
     }
     return new ImportChunkResult($count, $errors);
 }
Exemplo n.º 4
0
 protected function getCreationEvent($formData)
 {
     $createEvent = new ProductCreateEvent();
     $createEvent->setRef($formData['ref'])->setTitle($formData['title'])->setLocale($formData['locale'])->setDefaultCategory($formData['default_category'])->setVisible($formData['visible'])->setVirtual($formData['virtual'])->setBasePrice($formData['price'])->setBaseWeight($formData['weight'])->setCurrencyId($formData['currency'])->setTaxRuleId($formData['tax_rule'])->setBaseQuantity($formData['quantity'])->setTemplateId($formData['template_id']);
     return $createEvent;
 }
Exemplo n.º 5
0
 public function createClone(ProductCloneEvent $event, $originalProductDefaultI18n, $originalProductDefaultPrice)
 {
     // Build event and dispatch creation of the clone product
     $createCloneEvent = new ProductCreateEvent();
     $createCloneEvent->setTitle($originalProductDefaultI18n->getTitle())->setRef($event->getRef())->setLocale($event->getLang())->setVisible(0)->setVirtual($event->getOriginalProduct()->getVirtual())->setTaxRuleId($event->getOriginalProduct()->getTaxRuleId())->setDefaultCategory($event->getOriginalProduct()->getDefaultCategoryId())->setBasePrice($originalProductDefaultPrice->getPrice())->setCurrencyId($originalProductDefaultPrice->getCurrencyId())->setBaseWeight($event->getOriginalProduct()->getDefaultSaleElements()->getWeight())->setDispatcher($event->getDispatcher());
     $event->getDispatcher()->dispatch(TheliaEvents::PRODUCT_CREATE, $createCloneEvent);
     $event->setClonedProduct($createCloneEvent->getProduct());
 }
Exemplo n.º 6
0
 /**
  * Create a new product entry
  *
  * @param \Thelia\Core\Event\Product\ProductCreateEvent $event
  */
 public function create(ProductCreateEvent $event)
 {
     $product = new ProductModel();
     $product->setDispatcher($event->getDispatcher())->setRef($event->getRef())->setLocale($event->getLocale())->setTitle($event->getTitle())->setVisible($event->getVisible() ? 1 : 0)->setVirtual($event->getVirtual() ? 1 : 0)->setTaxRule(TaxRuleQuery::create()->findOneByIsDefault(true))->create($event->getDefaultCategory(), $event->getBasePrice(), $event->getCurrencyId(), $event->getTaxRuleId(), $event->getBaseWeight());
     $event->setProduct($product);
 }
Exemplo n.º 7
0
 public function testCreateWithOptionalParametersAction()
 {
     $event = new ProductCreateEvent();
     /** @var Category $defaultCategory */
     $defaultCategory = CategoryQuery::create()->addAscendingOrderByColumn('RAND()')->findOne();
     $taxRuleId = TaxRuleQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $currencyId = CurrencyQuery::create()->select('id')->addAscendingOrderByColumn('RAND()')->findOne();
     $templateId = $defaultCategory->getDefaultTemplateId();
     if (null === $templateId) {
         $templateId = TemplateQuery::create()->addAscendingOrderByColumn('RAND()')->findOne()->getId();
     }
     $newRef = 'testCreateWithOptionalParameters' . uniqid('_');
     $event->setRef($newRef)->setLocale('fr_FR')->setTitle('test create new product with optional parameters')->setVisible(1)->setDefaultCategory($defaultCategory->getId())->setBasePrice(10)->setTaxRuleId($taxRuleId)->setBaseWeight(10)->setCurrencyId($currencyId)->setBaseQuantity(10)->setTemplateId($templateId);
     $action = new Product($this->getMockEventDispatcher());
     $action->create($event);
     $createdProduct = $event->getProduct();
     $this->assertInstanceOf('Thelia\\Model\\Product', $createdProduct);
     $this->assertFalse($createdProduct->isNew());
     $createdProduct->setLocale('fr_FR');
     $this->assertEquals('test create new product with optional parameters', $createdProduct->getTitle());
     $this->assertEquals($newRef, $createdProduct->getRef());
     $this->assertEquals(1, $createdProduct->getVisible());
     $this->assertEquals($defaultCategory->getId(), $createdProduct->getDefaultCategoryId());
     $this->assertGreaterThan(0, $createdProduct->getPosition());
     $this->assertEquals($templateId, $createdProduct->getTemplateId());
     $productSaleElements = $createdProduct->getProductSaleElementss();
     $this->assertEquals(1, count($productSaleElements));
     $defaultProductSaleElement = $productSaleElements->getFirst();
     $this->assertTrue($defaultProductSaleElement->getIsDefault());
     $this->assertEquals(0, $defaultProductSaleElement->getPromo());
     $this->assertEquals(0, $defaultProductSaleElement->getNewness());
     $this->assertEquals($createdProduct->getRef(), $defaultProductSaleElement->getRef());
     $this->assertEquals(10, $defaultProductSaleElement->getWeight());
     $this->assertEquals(10, $defaultProductSaleElement->getQuantity());
     /** @var ProductPrice $productPrice */
     $productPrice = $defaultProductSaleElement->getProductPrices()->getFirst();
     $this->assertEquals(10, $productPrice->getPrice());
     $this->assertEquals($currencyId, $productPrice->getCurrencyId());
 }