From 4f7e7660ca18982b0ac380db7366cbd50295e26f Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Thu, 24 Oct 2013 16:39:22 +0200 Subject: [PATCH] Finished PSE management for products with no combinations --- core/lib/Thelia/Action/ProductSaleElement.php | 31 ++++++++--- .../Controller/Admin/ProductController.php | 52 ++++++++++++++++--- .../ProductSaleElementUpdateEvent.php | 22 ++++++-- .../lib/Thelia/Core/Template/Loop/Product.php | 1 + .../ProductDefaultSaleElementUpdateForm.php | 2 +- .../Form/ProductSaleElementUpdateForm.php | 4 +- .../default/includes/product-details-tab.html | 4 +- 7 files changed, 91 insertions(+), 25 deletions(-) diff --git a/core/lib/Thelia/Action/ProductSaleElement.php b/core/lib/Thelia/Action/ProductSaleElement.php index 730fe0764..facadfc4b 100644 --- a/core/lib/Thelia/Action/ProductSaleElement.php +++ b/core/lib/Thelia/Action/ProductSaleElement.php @@ -67,11 +67,11 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface ->findOne($con); if ($salesElement == null) { - // Create a new product sale element + // Create a new default product sale element $salesElement = $event->getProduct()->createDefaultProductSaleElement($con, 0, 0, $event->getCurrencyId(), true); } else { - // This one is the default + // This (new) one is the default $salesElement->setIsDefault(true)->save($con); } @@ -122,6 +122,9 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface 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(); @@ -158,11 +161,25 @@ class ProductSaleElement extends BaseAction implements EventSubscriberInterface ; } - $productPrice - ->setPromoPrice($event->getSalePrice()) - ->setPrice($event->getPrice()) - ->save($con) - ; + // 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(); diff --git a/core/lib/Thelia/Controller/Admin/ProductController.php b/core/lib/Thelia/Controller/Admin/ProductController.php index 7e54e6345..a7ee65ccd 100644 --- a/core/lib/Thelia/Controller/Admin/ProductController.php +++ b/core/lib/Thelia/Controller/Admin/ProductController.php @@ -189,6 +189,23 @@ class ProductController extends AbstractCrudController return $event->hasProduct(); } + protected function updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency) { + + // Get price for default currency + $priceForDefaultCurrency = ProductPriceQuery::create() + ->filterByCurrency($defaultCurrency) + ->filterByProductSaleElements($saleElement) + ->findOne() + ; + + if ($priceForDefaultCurrency !== null) { + $productPrice + ->setPrice($priceForDefaultCurrency->getPrice() * $currentCurrency->getRate()) + ->setPromoPrice($priceForDefaultCurrency->getPromoPrice() * $currentCurrency->getRate()) + ; + } + } + protected function hydrateObjectForm($object) { $defaultPseData = $combinationPseData = array(); @@ -198,18 +215,35 @@ class ProductController extends AbstractCrudController ->filterByProduct($object) ->find(); + $defaultCurrency = Currency::getDefaultCurrency(); + $currentCurrency = $this->getCurrentEditionCurrency(); + foreach($saleElements as $saleElement) { // Get the product price for the current currency - $productPrice = ProductPriceQuery::create() - ->filterByCurrency($this->getCurrentEditionCurrency()) + ->filterByCurrency($currentCurrency) ->filterByProductSaleElements($saleElement) ->findOne() ; - if ($productPrice == null) { + // 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); + + $this->updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency); + } + } + + // Caclulate prices if we have to use the rate * defaulkt currency price + if ($productPrice->getFromDefaultCurrency() == true) { + $this->updatePriceFromDefaultCurrency($productPrice, $saleElement, $defaultCurrency, $currentCurrency); } $isDefaultPse = count($saleElement->getAttributeCombinations()) == 0; @@ -236,7 +270,6 @@ class ProductController extends AbstractCrudController "isdefault" => $saleElement->getIsDefault() > 0 ? 1 : 0, "ean_code" => $saleElement->getEanCode() ); - } else { } @@ -246,9 +279,13 @@ class ProductController extends AbstractCrudController $combinationPseForm = new ProductSaleElementUpdateForm($this->getRequest(), "form", $combinationPseData); $this->getParserContext()->addForm($combinationPseForm); + + var_dump($defaultPseData); + + var_dump($combinationPseData); } - // Prepare the data that will hydrate the form + // Prepare the data that will hydrate the form(s) $data = array( 'id' => $object->getId(), 'ref' => $object->getRef(), @@ -260,8 +297,6 @@ class ProductController extends AbstractCrudController 'visible' => $object->getVisible(), 'url' => $object->getRewrittenUrl($this->getCurrentEditionLocale()), 'default_category' => $object->getDefaultCategoryId() - - // A terminer pour les prix ); // Setup the object form @@ -881,7 +916,8 @@ class ProductController extends AbstractCrudController ->setIsnew($data['isnew']) ->setIsdefault($data['isdefault']) ->setEanCode($data['ean_code']) - ->setTaxrule($data['tax_rule']) + ->setTaxRuleId($data['tax_rule']) + ->setFromDefaultCurrency($data['use_exchange_rate']) ; $this->dispatch(TheliaEvents::PRODUCT_UPDATE_PRODUCT_SALE_ELEMENT, $event); diff --git a/core/lib/Thelia/Core/Event/ProductSaleElement/ProductSaleElementUpdateEvent.php b/core/lib/Thelia/Core/Event/ProductSaleElement/ProductSaleElementUpdateEvent.php index a3eed8a2a..8a0618dc5 100644 --- a/core/lib/Thelia/Core/Event/ProductSaleElement/ProductSaleElementUpdateEvent.php +++ b/core/lib/Thelia/Core/Event/ProductSaleElement/ProductSaleElementUpdateEvent.php @@ -41,7 +41,8 @@ class ProductSaleElementUpdateEvent extends ProductSaleElementEvent protected $isnew; protected $isdefault; protected $ean_code; - protected $taxrule; + protected $tax_rule_id; + protected $from_default_currency; public function __construct(Product $product, $product_sale_element_id) { @@ -196,16 +197,27 @@ class ProductSaleElementUpdateEvent extends ProductSaleElementEvent return $this; } - public function getTaxrule() + public function getTaxRuleId() { - return $this->taxrule; + return $this->tax_rule_id; } - public function setTaxrule($taxrule) + public function setTaxRuleId($tax_rule_id) { - $this->taxrule = $taxrule; + $this->tax_rule_id = $tax_rule_id; return $this; } + public function getFromDefaultCurrency() + { + return $this->from_default_currency; + } + + public function setFromDefaultCurrency($from_default_currency) + { + $this->from_default_currency = $from_default_currency; + return $this; + } + } diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index 2d6125a37..a2ddd9433 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -668,6 +668,7 @@ class Product extends BaseI18nLoop ->set("PREVIOUS" , $previous != null ? $previous->getId() : -1) ->set("NEXT" , $next != null ? $next->getId() : -1) ->set("DEFAULT_CATEGORY" , $default_category_id) + ->set("TAX_RULE_ID" , $product->getTaxRuleId()) ; diff --git a/core/lib/Thelia/Form/ProductDefaultSaleElementUpdateForm.php b/core/lib/Thelia/Form/ProductDefaultSaleElementUpdateForm.php index 807f8ac07..ebc081faf 100644 --- a/core/lib/Thelia/Form/ProductDefaultSaleElementUpdateForm.php +++ b/core/lib/Thelia/Form/ProductDefaultSaleElementUpdateForm.php @@ -28,4 +28,4 @@ class ProductDefaultSaleElementUpdateForm extends ProductSaleElementUpdateForm { return "thelia_product_default_sale_element_update_form"; } -} +} \ No newline at end of file diff --git a/core/lib/Thelia/Form/ProductSaleElementUpdateForm.php b/core/lib/Thelia/Form/ProductSaleElementUpdateForm.php index 3d7966cab..f93a4e251 100644 --- a/core/lib/Thelia/Form/ProductSaleElementUpdateForm.php +++ b/core/lib/Thelia/Form/ProductSaleElementUpdateForm.php @@ -77,7 +77,7 @@ class ProductSaleElementUpdateForm extends BaseForm "label_attr" => array("for" => "quantity_field") )) ->add("sale_price", "number", array( - "label" => Translator::getInstance()->trans("Sale price without taxes *"), + "label" => Translator::getInstance()->trans("Sale price without taxes"), "label_attr" => array("for" => "price_with_tax_field") )) ->add("sale_price_with_tax", "number", array( @@ -96,7 +96,7 @@ class ProductSaleElementUpdateForm extends BaseForm "label" => Translator::getInstance()->trans("Is it the default product sale element ?"), "label_attr" => array("for" => "isdefault_field") )) - ->add("ean_code", "integer", array( + ->add("ean_code", "text", array( "label" => Translator::getInstance()->trans("EAN Code"), "label_attr" => array("for" => "ean_code_field") )) diff --git a/templates/admin/default/includes/product-details-tab.html b/templates/admin/default/includes/product-details-tab.html index 7adb09715..57dda78cf 100644 --- a/templates/admin/default/includes/product-details-tab.html +++ b/templates/admin/default/includes/product-details-tab.html @@ -83,7 +83,7 @@ @@ -280,7 +280,7 @@