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