tax in loops

This commit is contained in:
Etienne Roudeix
2013-09-09 16:27:46 +02:00
parent fa36b64f51
commit ca4df15910
9 changed files with 91 additions and 17 deletions

View File

@@ -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())

View File

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

View File

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

View File

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

View File

@@ -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

View File

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

View File

@@ -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;

View File

@@ -32,7 +32,7 @@
<h4>#TITLE</h4>
<p>#DESCRIPTION</p>
<p>Starting by #BEST_PRICE € HT (#BEST_TAXED_PRICE € TTC)</p>
<p>Starting by #BEST_PRICE € HT (TAX : #BEST_PRICE_TAX ; #BEST_TAXED_PRICE € TTC)</p>
{ifloop rel="ft"}
<h5>Features</h5>

View File

@@ -13,6 +13,8 @@ Index : {navigate to="index"}<br />
<h3>#TITLE</h3>
<p>#DESCRIPTION</p>
<p>Starting by #BEST_PRICE € HT (TAX : #BEST_PRICE_TAX ; #BEST_TAXED_PRICE € TTC)</p>
{ifloop rel="acc"}
<h4>Accessories</h4>
<ul>
@@ -64,7 +66,7 @@ Index : {navigate to="index"}<br />
#ATTRIBUTE_TITLE = #ATTRIBUTE_AVAILABILITY_TITLE<br />
{/loop}
<br />#WEIGHT g
<br /><strong>{if #IS_PROMO == 1} #PROMO_PRICE € (instead of #PRICE) {else} #PRICE € {/if}</strong>
<br /><strong>{if #IS_PROMO == 1} #PROMO_PRICE € HT // TAX : #PROMO_PRICE_TAX ; #TAXED_PROMO_PRICE € TTC (instead of #PRICE HT // TAX : #PRICE_TAX ; #TAXED_PRICE € TTC){else} #PRICE € HT // TAX : #PRICE_TAX ; #TAXED_PRICE € TTC{/if}</strong>
<br /><br />
Add
<select>