/** * @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; } }