/** * Update an existing product sale element * * @param ProductSaleElementUpdateEvent $event * @throws \Exception */ public function update(ProductSaleElementUpdateEvent $event) { $salesElement = ProductSaleElementsQuery::create()->findPk($event->getProductSaleElementId()); $con = Propel::getWriteConnection(ProductSaleElementsTableMap::DATABASE_NAME); $con->beginTransaction(); try { // Update the product's tax rule $event->getProduct()->setTaxRuleId($event->getTaxRuleId())->save($con); // If product sale element is not defined, create it. if ($salesElement == null) { $salesElement = new ProductSaleElements(); $salesElement->setProduct($event->getProduct()); } // If this PSE is the default one, be sure to have *only one* default for this product if ($event->getIsDefault()) { ProductSaleElementsQuery::create()->filterByProduct($event->getProduct())->filterByIsDefault(true)->filterById($event->getProductSaleElementId(), Criteria::NOT_EQUAL)->update(['IsDefault' => false], $con); } // Update sale element $salesElement->setRef($event->getReference())->setQuantity($event->getQuantity())->setPromo($event->getOnsale())->setNewness($event->getIsnew())->setWeight($event->getWeight())->setIsDefault($event->getIsDefault())->setEanCode($event->getEanCode())->save(); // Update/create price for current currency $productPrice = ProductPriceQuery::create()->filterByCurrencyId($event->getCurrencyId())->filterByProductSaleElementsId($salesElement->getId())->findOne($con); // If price is not defined, create it. if ($productPrice == null) { $productPrice = new ProductPrice(); $productPrice->setProductSaleElements($salesElement)->setCurrencyId($event->getCurrencyId()); } // Check if we have to store the price $productPrice->setFromDefaultCurrency($event->getFromDefaultCurrency()); if ($event->getFromDefaultCurrency() == 0) { // Store the price $productPrice->setPromoPrice($event->getSalePrice())->setPrice($event->getPrice()); } else { // Do not store the price. $productPrice->setPromoPrice(0)->setPrice(0); } $productPrice->save($con); // Store all the stuff ! $con->commit(); } catch (\Exception $ex) { $con->rollback(); throw $ex; } }
public function checkStock(ProductSaleElementUpdateEvent $productSaleElementUpdateEvent) { if ($productSaleElementUpdateEvent->getQuantity() > 0) { // add extra checking $pse = ProductSaleElementsQuery::create()->findPk($productSaleElementUpdateEvent->getProductSaleElementId()); $availabilityEvent = new ProductSaleElementAvailabilityEvent($pse); $productSaleElementUpdateEvent->getDispatcher()->dispatch(StockAlertEvents::STOCK_ALERT_CHECK_AVAILABILITY, $availabilityEvent); if ($availabilityEvent->isAvailable()) { $subscribers = RestockingAlertQuery::create()->filterByProductSaleElementsId($productSaleElementUpdateEvent->getProductSaleElementId())->find(); if (null !== $subscribers) { foreach ($subscribers as $subscriber) { try { $this->sendEmail($subscriber); $subscriber->delete(); } catch (\Exception $ex) { } } } } } }
/** * @param array $data * @param ProductSaleElements $pse * @param Product $product * * Process update on product sale elements values */ protected function processUpdateAction(array $data, ProductSaleElements $pse, Product $product) { list($price, $salePrice) = $this->extractPrices($data); $event = new ProductSaleElementUpdateEvent($product, $pse->getId()); $event->setWeight($data["weight"])->setTaxRuleId($data["tax_rule_id"])->setEanCode($data["ean_code"])->setOnsale($data["onsale"])->setReference($data["reference"])->setIsdefault($data["isdefault"])->setIsnew($data["isnew"])->setCurrencyId($data["currency_id"])->setPrice($price)->setSalePrice($salePrice)->setQuantity($data["quantity"])->setFromDefaultCurrency($data["use_exchange_rate"]); $this->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $event); }
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); }
/** * Process a single PSE update, using form data array. * * @param array $data the form data */ protected function processSingleProductSaleElementUpdate($data) { $event = new ProductSaleElementUpdateEvent($this->getExistingObject(), $data['product_sale_element_id']); $event->setReference($data['reference'])->setPrice($data['price'])->setCurrencyId($data['currency'])->setWeight($data['weight'])->setQuantity($data['quantity'])->setSalePrice($data['sale_price'])->setOnsale($data['onsale'])->setIsnew($data['isnew'])->setIsdefault($data['isdefault'])->setEanCode($data['ean_code'])->setTaxRuleId($data['tax_rule'])->setFromDefaultCurrency($data['use_exchange_rate']); $this->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $event); // Log object modification if (null !== ($changedObject = $event->getProductSaleElement())) { $this->adminLogAppend($this->resourceCode, AccessManager::UPDATE, sprintf("Product Sale Element (ID %s) for product reference %s modified", $changedObject->getId(), $event->getProduct()->getRef()), $changedObject->getId()); } }
public function updateClonePSE(ProductCloneEvent $event, $clonedProductPSEId, ProductSaleElements $originalProductPSE, $key) { $originalProductPSEPrice = ProductPriceQuery::create()->findOneByProductSaleElementsId($originalProductPSE->getId()); $clonedProductUpdatePSEEvent = new ProductSaleElementUpdateEvent($event->getClonedProduct(), $clonedProductPSEId); $clonedProductUpdatePSEEvent->setReference($event->getClonedProduct()->getRef() . '-' . ($key + 1))->setIsdefault($originalProductPSE->getIsDefault())->setFromDefaultCurrency(0)->setWeight($originalProductPSE->getWeight())->setQuantity($originalProductPSE->getQuantity())->setOnsale($originalProductPSE->getPromo())->setIsnew($originalProductPSE->getNewness())->setEanCode($originalProductPSE->getEanCode())->setTaxRuleId($event->getOriginalProduct()->getTaxRuleId())->setPrice($originalProductPSEPrice->getPrice())->setSalePrice($originalProductPSEPrice->getPromoPrice())->setCurrencyId($originalProductPSEPrice->getCurrencyId()); $this->eventDispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $clonedProductUpdatePSEEvent); }