public function importData(array $data) { $pse = ProductSaleElementsQuery::create()->findPk($data['id']); if ($pse === null) { return Translator::getInstance()->trans('The product sale element id %id doesn\'t exist', ['%id' => $data['id']]); } else { $currency = null; if (isset($data['currency'])) { $currency = CurrencyQuery::create()->findOneByCode($data['currency']); } if ($currency === null) { $currency = Currency::getDefaultCurrency(); } $price = ProductPriceQuery::create()->filterByProductSaleElementsId($pse->getId())->findOneByCurrencyId($currency->getId()); if ($price === null) { $price = new ProductPrice(); $price->setProductSaleElements($pse)->setCurrency($currency); } $price->setPrice($data['price']); if (isset($data['promo_price'])) { $price->setPromoPrice($data['promo_price']); } if (isset($data['promo'])) { $price->getProductSaleElements()->setPromo((int) $data['promo'])->save(); } $price->save(); $this->importedRows++; } return null; }
/** * @param \Thelia\Core\FileFormat\Formatting\FormatterData * @return string|array error messages * * The method does the import routine from a FormatterData */ public function retrieveFromFormatterData(FormatterData $data) { $errors = []; $translator = Translator::getInstance(); while (null !== ($row = $data->popRow())) { $this->checkMandatoryColumns($row); $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]); if ($obj === null) { $errorMessage = $translator->trans("The product sale element reference %ref doesn't exist", ["%ref" => $row["ref"]]); $errors[] = $errorMessage; } else { $currency = null; if (isset($row["currency"])) { $currency = CurrencyQuery::create()->findOneByCode($row["currency"]); } if ($currency === null) { $currency = Currency::getDefaultCurrency(); } $price = ProductPriceQuery::create()->filterByProductSaleElementsId($obj->getId())->findOneByCurrencyId($currency->getId()); if ($price === null) { $price = new ProductPrice(); $price->setProductSaleElements($obj)->setCurrency($currency); } $price->setPrice($row["price"]); if (isset($row["promo_price"])) { $price->setPromoPrice($row["promo_price"]); } if (isset($row["promo"])) { $price->getProductSaleElements()->setPromo((int) $row["promo"])->save(); } $price->save(); $this->importedRows++; } } return $errors; }
/** * Filter the query by a related \Thelia\Model\ProductPrice object * * @param \Thelia\Model\ProductPrice|ObjectCollection $productPrice the related object to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return ChildCurrencyQuery The current query, for fluid interface */ public function filterByProductPrice($productPrice, $comparison = null) { if ($productPrice instanceof \Thelia\Model\ProductPrice) { return $this->addUsingAlias(CurrencyTableMap::ID, $productPrice->getCurrencyId(), $comparison); } elseif ($productPrice instanceof ObjectCollection) { return $this->useProductPriceQuery()->filterByPrimaryKeys($productPrice->getPrimaryKeys())->endUse(); } else { throw new PropelException('filterByProductPrice() only accepts arguments of type \\Thelia\\Model\\ProductPrice or Collection'); } }
/** * @param Product $object * @return ProductModificationForm */ protected function hydrateObjectForm($object) { // Find product's sale elements $saleElements = ProductSaleElementsQuery::create()->filterByProduct($object)->find(); $defaultCurrency = Currency::getDefaultCurrency(); $currentCurrency = $this->getCurrentEditionCurrency(); // Common parts $defaultPseData = $combinationPseData = array("product_id" => $object->getId(), "tax_rule" => $object->getTaxRuleId()); /** @var ProductSaleElements $saleElement */ foreach ($saleElements as $saleElement) { // Get the product price for the current currency $productPrice = ProductPriceQuery::create()->filterByCurrency($currentCurrency)->filterByProductSaleElements($saleElement)->findOne(); // No one exists ? if ($productPrice === null) { $productPrice = new ProductPrice(); // If the current currency is not the default one, calculate the price // using default currency price and current currency rate if ($currentCurrency->getId() != $defaultCurrency->getId()) { $productPrice->setFromDefaultCurrency(true); } } // Caclulate prices if we have to use the rate * default currency price if ($productPrice->getFromDefaultCurrency() == true) { $this->updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency); } $isDefaultPse = count($saleElement->getAttributeCombinations()) == 0; // If this PSE has no combination -> this is the default one // affect it to the thelia.admin.product_sale_element.update form if ($isDefaultPse) { $defaultPseData = array("product_sale_element_id" => $saleElement->getId(), "reference" => $saleElement->getRef(), "price" => $this->formatPrice($productPrice->getPrice()), "price_with_tax" => $this->formatPrice($this->computePrice($productPrice->getPrice(), 'without_tax', $object)), "use_exchange_rate" => $productPrice->getFromDefaultCurrency() ? 1 : 0, "currency" => $productPrice->getCurrencyId(), "weight" => $saleElement->getWeight(), "quantity" => $saleElement->getQuantity(), "sale_price" => $this->formatPrice($productPrice->getPromoPrice()), "sale_price_with_tax" => $this->formatPrice($this->computePrice($productPrice->getPromoPrice(), 'without_tax', $object)), "onsale" => $saleElement->getPromo() > 0 ? 1 : 0, "isnew" => $saleElement->getNewness() > 0 ? 1 : 0, "isdefault" => $saleElement->getIsDefault() > 0 ? 1 : 0, "ean_code" => $saleElement->getEanCode()); } else { if ($saleElement->getIsDefault()) { $combinationPseData['default_pse'] = $saleElement->getId(); $combinationPseData['currency'] = $currentCurrency->getId(); $combinationPseData['use_exchange_rate'] = $productPrice->getFromDefaultCurrency() ? 1 : 0; } $this->appendValue($combinationPseData, "product_sale_element_id", $saleElement->getId()); $this->appendValue($combinationPseData, "reference", $saleElement->getRef()); $this->appendValue($combinationPseData, "price", $this->formatPrice($productPrice->getPrice())); $this->appendValue($combinationPseData, "price_with_tax", $this->formatPrice($this->computePrice($productPrice->getPrice(), 'without_tax', $object))); $this->appendValue($combinationPseData, "weight", $saleElement->getWeight()); $this->appendValue($combinationPseData, "quantity", $saleElement->getQuantity()); $this->appendValue($combinationPseData, "sale_price", $this->formatPrice($productPrice->getPromoPrice())); $this->appendValue($combinationPseData, "sale_price_with_tax", $this->formatPrice($this->computePrice($productPrice->getPromoPrice(), 'without_tax', $object))); $this->appendValue($combinationPseData, "onsale", $saleElement->getPromo() > 0 ? 1 : 0); $this->appendValue($combinationPseData, "isnew", $saleElement->getNewness() > 0 ? 1 : 0); $this->appendValue($combinationPseData, "isdefault", $saleElement->getIsDefault() > 0 ? 1 : 0); $this->appendValue($combinationPseData, "ean_code", $saleElement->getEanCode()); } } $defaultPseForm = $this->createForm(AdminForm::PRODUCT_DEFAULT_SALE_ELEMENT_UPDATE, "form", $defaultPseData); $this->getParserContext()->addForm($defaultPseForm); $combinationPseForm = $this->createForm(AdminForm::PRODUCT_SALE_ELEMENT_UPDATE, "form", $combinationPseData); $this->getParserContext()->addForm($combinationPseForm); // Hydrate the "SEO" tab form $this->hydrateSeoForm($object); // The "General" tab form $data = array('id' => $object->getId(), 'ref' => $object->getRef(), 'locale' => $object->getLocale(), 'title' => $object->getTitle(), 'chapo' => $object->getChapo(), 'description' => $object->getDescription(), 'postscriptum' => $object->getPostscriptum(), 'visible' => $object->getVisible(), 'virtual' => $object->getVirtual(), 'default_category' => $object->getDefaultCategoryId(), 'brand_id' => $object->getBrandId()); // Virtual document if (array_key_exists("product_sale_element_id", $defaultPseData)) { $virtualDocumentId = intval(MetaDataQuery::getVal('virtual', MetaData::PSE_KEY, $defaultPseData['product_sale_element_id'])); if ($virtualDocumentId) { $data["virtual_document_id"] = $virtualDocumentId; } } // Setup the object form return $this->createForm(AdminForm::PRODUCT_MODIFICATION, "form", $data, [], $this->container); }
/** * 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; } }
/** * Exclude object from result * * @param ChildProductPrice $productPrice Object to remove from the list of results * * @return ChildProductPriceQuery The current query, for fluid interface */ public function prune($productPrice = null) { if ($productPrice) { $this->addCond('pruneCond0', $this->getAliasedColName(ProductPriceTableMap::PRODUCT_SALE_ELEMENTS_ID), $productPrice->getProductSaleElementsId(), Criteria::NOT_EQUAL); $this->addCond('pruneCond1', $this->getAliasedColName(ProductPriceTableMap::CURRENCY_ID), $productPrice->getCurrencyId(), Criteria::NOT_EQUAL); $this->combine(array('pruneCond0', 'pruneCond1'), Criteria::LOGICAL_OR); } return $this; }
public function updateClone(ProductCloneEvent $event, ProductPrice $originalProductDefaultPrice) { // Get original product's I18ns $originalProductI18ns = ProductI18nQuery::create()->findById($event->getOriginalProduct()->getId()); /** @var ProductI18n $originalProductI18n */ foreach ($originalProductI18ns as $originalProductI18n) { $clonedProductUpdateEvent = new ProductUpdateEvent($event->getClonedProduct()->getId()); $clonedProductUpdateEvent->setRef($event->getClonedProduct()->getRef())->setVisible($event->getClonedProduct()->getVisible())->setVirtual($event->getClonedProduct()->getVirtual())->setLocale($originalProductI18n->getLocale())->setTitle($originalProductI18n->getTitle())->setChapo($originalProductI18n->getChapo())->setDescription($originalProductI18n->getDescription())->setPostscriptum($originalProductI18n->getPostscriptum())->setBasePrice($originalProductDefaultPrice->getPrice())->setCurrencyId($originalProductDefaultPrice->getCurrencyId())->setBaseWeight($event->getOriginalProduct()->getDefaultSaleElements()->getWeight())->setTaxRuleId($event->getOriginalProduct()->getTaxRuleId())->setBrandId($event->getOriginalProduct()->getBrandId())->setDefaultCategory($event->getOriginalProduct()->getDefaultCategoryId()); $this->eventDispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE, $clonedProductUpdateEvent); // SEO info $clonedProductUpdateSeoEvent = new UpdateSeoEvent($event->getClonedProduct()->getId()); $clonedProductUpdateSeoEvent->setLocale($originalProductI18n->getLocale())->setMetaTitle($originalProductI18n->getMetaTitle())->setMetaDescription($originalProductI18n->getMetaDescription())->setMetaKeywords($originalProductI18n->getMetaKeywords())->setUrl(null); $this->eventDispatcher->dispatch(TheliaEvents::PRODUCT_UPDATE_SEO, $clonedProductUpdateSeoEvent); } $event->setClonedProduct($clonedProductUpdateEvent->getProduct()); // Set clone's template $clonedProductUpdateTemplateEvent = new ProductSetTemplateEvent($event->getClonedProduct(), $event->getOriginalProduct()->getTemplateId(), $originalProductDefaultPrice->getCurrencyId()); $this->eventDispatcher->dispatch(TheliaEvents::PRODUCT_SET_TEMPLATE, $clonedProductUpdateTemplateEvent); }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by find*() * and findPk*() calls. * * @param \Thelia\Model\ProductPrice $obj A \Thelia\Model\ProductPrice object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool($obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if (null === $key) { $key = serialize(array((string) $obj->getProductSaleElementsId(), (string) $obj->getCurrencyId())); } // if key === null self::$instances[$key] = $obj; } }