From ca4df159107495a985ebbb6d73c2dd6c74a0db45 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 9 Sep 2013 16:27:46 +0200 Subject: [PATCH] tax in loops --- .../lib/Thelia/Core/Template/Loop/Product.php | 13 ++++--- ...aleElement.php => ProductSaleElements.php} | 18 ++++++++-- core/lib/Thelia/Model/Product.php | 2 +- core/lib/Thelia/Model/ProductSaleElements.php | 35 ++++++++++++++++++- core/lib/Thelia/Model/TaxRuleQuery.php | 2 -- core/lib/Thelia/TaxEngine/Calculator.php | 16 ++++++--- core/lib/Thelia/Tools/URL.php | 16 +++++++++ templates/default/category.html | 2 +- templates/default/product.html | 4 ++- 9 files changed, 91 insertions(+), 17 deletions(-) rename core/lib/Thelia/Core/Template/Loop/{ProductSaleElement.php => ProductSaleElements.php} (87%) diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index f52b314a9..b7a7c13fe 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -510,6 +510,12 @@ class Product extends BaseI18nLoop foreach ($products as $product) { $loopResultRow = new LoopResultRow($loopResult, $product, $this->versionable, $this->timestampable, $this->countable); + $price = $product->getRealLowestPrice(); + $taxedPrice = $product->getTaxedPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + + $loopResultRow->set("ID", $product->getId()) ->set("REF",$product->getRef()) ->set("IS_TRANSLATED",$product->getVirtualColumn('IS_TRANSLATED')) @@ -519,10 +525,9 @@ class Product extends BaseI18nLoop ->set("DESCRIPTION", $product->getVirtualColumn('i18n_DESCRIPTION')) ->set("POSTSCRIPTUM", $product->getVirtualColumn('i18n_POSTSCRIPTUM')) ->set("URL", $product->getUrl($locale)) - ->set("BEST_PRICE", $product->getRealLowestPrice()) - ->set("BEST_TAXED_PRICE", $product->getTaxedPrice( - CountryQuery::create()->findOneById(64) // @TODO : make it magic - )) + ->set("BEST_PRICE", $price) + ->set("BEST_PRICE_TAX", $taxedPrice - $price) + ->set("BEST_TAXED_PRICE", $taxedPrice) ->set("IS_PROMO", $product->getVirtualColumn('main_product_is_promo')) ->set("IS_NEW", $product->getVirtualColumn('main_product_is_new')) ->set("POSITION", $product->getPosition()) diff --git a/core/lib/Thelia/Core/Template/Loop/ProductSaleElement.php b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php similarity index 87% rename from core/lib/Thelia/Core/Template/Loop/ProductSaleElement.php rename to core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php index 7a1a0b8c8..ec4c73230 100755 --- a/core/lib/Thelia/Core/Template/Loop/ProductSaleElement.php +++ b/core/lib/Thelia/Core/Template/Loop/ProductSaleElements.php @@ -35,6 +35,7 @@ use Thelia\Log\Tlog; use Thelia\Model\Base\ProductSaleElementsQuery; use Thelia\Model\ConfigQuery; +use Thelia\Model\CountryQuery; use Thelia\Type\TypeCollection; use Thelia\Type; @@ -124,6 +125,15 @@ class ProductSaleElements extends BaseLoop foreach ($PSEValues as $PSEValue) { $loopResultRow = new LoopResultRow($loopResult, $PSEValue, $this->versionable, $this->timestampable, $this->countable); + $price = $PSEValue->getPrice(); + $taxedPrice = $PSEValue->getTaxedPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + $promoPrice = $PSEValue->getPromoPrice(); + $taxedPromoPrice = $PSEValue->getTaxedPromoPrice( + CountryQuery::create()->findOneById(64) // @TODO : make it magic + ); + $loopResultRow->set("ID", $PSEValue->getId()) ->set("QUANTITY", $PSEValue->getQuantity()) ->set("IS_PROMO", $PSEValue->getPromo() === 1 ? 1 : 0) @@ -131,8 +141,12 @@ class ProductSaleElements extends BaseLoop ->set("WEIGHT", $PSEValue->getWeight()) ->set("CURRENCY", $PSEValue->getVirtualColumn('price_CURRENCY_ID')) - ->set("PRICE", $PSEValue->getVirtualColumn('price_PRICE')) - ->set("PROMO_PRICE", $PSEValue->getVirtualColumn('price_PROMO_PRICE')); + ->set("PRICE", $price) + ->set("PRICE_TAX", $taxedPrice - $price) + ->set("TAXED_PRICE", $taxedPrice) + ->set("PROMO_PRICE", $promoPrice) + ->set("PROMO_PRICE_TAX", $taxedPromoPrice - $promoPrice) + ->set("TAXED_PROMO_PRICE", $taxedPromoPrice); $loopResult->addRow($loopResultRow); } diff --git a/core/lib/Thelia/Model/Product.php b/core/lib/Thelia/Model/Product.php index 66e2a80ad..06c4640d0 100755 --- a/core/lib/Thelia/Model/Product.php +++ b/core/lib/Thelia/Model/Product.php @@ -28,6 +28,6 @@ class Product extends BaseProduct public function getTaxedPrice(Country $country) { $taxCalculator = new Calculator(); - return $taxCalculator->load($this, $country)->getTaxedPrice(); + return $taxCalculator->load($this, $country)->getTaxedPrice($this->getRealLowestPrice()); } } diff --git a/core/lib/Thelia/Model/ProductSaleElements.php b/core/lib/Thelia/Model/ProductSaleElements.php index cec9c49a7..184e37d0a 100755 --- a/core/lib/Thelia/Model/ProductSaleElements.php +++ b/core/lib/Thelia/Model/ProductSaleElements.php @@ -3,8 +3,41 @@ namespace Thelia\Model; use Thelia\Model\Base\ProductSaleElements as BaseProductSaleElements; +use Thelia\TaxEngine\Calculator; - class ProductSaleElements extends BaseProductSaleElements +class ProductSaleElements extends BaseProductSaleElements { + public function getPrice($virtualColumnName = 'price_PRICE') + { + try { + $amount = $this->getVirtualColumn($virtualColumnName); + } catch(PropelException $e) { + throw new PropelException("Virtual column `$virtualColumnName` does not exist in ProductSaleElements::getPrice"); + } + return $amount; + } + + public function getPromoPrice($virtualColumnName = 'price_PROMO_PRICE') + { + try { + $amount = $this->getVirtualColumn($virtualColumnName); + } catch(PropelException $e) { + throw new PropelException("Virtual column `$virtualColumnName` does not exist in ProductSaleElements::getPromoPrice"); + } + + return $amount; + } + + public function getTaxedPrice(Country $country) + { + $taxCalculator = new Calculator(); + return $taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPrice()); + } + + public function getTaxedPromoPrice(Country $country) + { + $taxCalculator = new Calculator(); + return $taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()); + } } diff --git a/core/lib/Thelia/Model/TaxRuleQuery.php b/core/lib/Thelia/Model/TaxRuleQuery.php index b14e0f55d..f9c6cf1e5 100755 --- a/core/lib/Thelia/Model/TaxRuleQuery.php +++ b/core/lib/Thelia/Model/TaxRuleQuery.php @@ -37,8 +37,6 @@ class TaxRuleQuery extends BaseTaxRuleQuery ->withColumn('ROUND(SUM(' . TaxTableMap::RATE . '), 2)', self::ALIAS_FOR_TAX_RATE_SUM) ; - //var_dump($search->toString()); - return $search->find(); } } // TaxRuleQuery diff --git a/core/lib/Thelia/TaxEngine/Calculator.php b/core/lib/Thelia/TaxEngine/Calculator.php index b0dcbb2fa..ec6e3c78d 100755 --- a/core/lib/Thelia/TaxEngine/Calculator.php +++ b/core/lib/Thelia/TaxEngine/Calculator.php @@ -67,14 +67,16 @@ class Calculator return $this; } - public function getTaxAmount() + public function getTaxAmount($amount) { + if(false === filter_var($amount, FILTER_VALIDATE_FLOAT)) { + throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT); + } + if(null === $this->taxRulesGroupedCollection) { throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION); } - $amount = $this->product->getRealLowestPrice(); - $taxRateAmount = 0; foreach($this->taxRulesGroupedCollection as $taxRule) { $taxRateAmount += $taxRule->getTaxRuleRateSum(); @@ -83,8 +85,12 @@ class Calculator return $amount * $taxRateAmount * 0.01; } - public function getTaxedPrice() + public function getTaxedPrice($amount) { - return $this->product->getRealLowestPrice() + $this->getTaxAmount(); + if(false === filter_var($amount, FILTER_VALIDATE_FLOAT)) { + throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT); + } + + return $amount + $this->getTaxAmount($amount); } } diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index 161175bbf..e43591ffc 100755 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -189,6 +189,14 @@ class URL { if(ConfigQuery::isRewritingEnable()) { $this->retriever->loadViewUrl($view, $viewLocale, $viewId); + } else { + $allParametersWithoutView = array(); + $allParametersWithoutView['locale'] = $viewLocale; + if(null !== $viewId) { + $allParametersWithoutView[$view . '_id'] = $viewId; + } + $this->retriever->rewrittenUrl = null; + $this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView); } return $this->retriever; @@ -220,6 +228,14 @@ class URL } $this->retriever->loadSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters); + } else { + $allParametersWithoutView = $viewOtherParameters; + $allParametersWithoutView['locale'] = $viewLocale; + if(null !== $viewId) { + $allParametersWithoutView[$view . '_id'] = $viewId; + } + $this->retriever->rewrittenUrl = null; + $this->retriever->url = URL::getInstance()->viewUrl($view, $allParametersWithoutView); } return $this->retriever; diff --git a/templates/default/category.html b/templates/default/category.html index 081c28ca5..a888605ff 100755 --- a/templates/default/category.html +++ b/templates/default/category.html @@ -32,7 +32,7 @@

#TITLE

#DESCRIPTION

-

Starting by #BEST_PRICE € HT (#BEST_TAXED_PRICE € TTC)

+

Starting by #BEST_PRICE € HT (TAX : #BEST_PRICE_TAX ; #BEST_TAXED_PRICE € TTC)

{ifloop rel="ft"}
Features
diff --git a/templates/default/product.html b/templates/default/product.html index 4f82b5fb8..4280fc88c 100755 --- a/templates/default/product.html +++ b/templates/default/product.html @@ -13,6 +13,8 @@ Index : {navigate to="index"}

#TITLE

#DESCRIPTION

+

Starting by #BEST_PRICE € HT (TAX : #BEST_PRICE_TAX ; #BEST_TAXED_PRICE € TTC)

+ {ifloop rel="acc"}

Accessories