tax engine

This commit is contained in:
Etienne Roudeix
2013-09-09 14:24:50 +02:00
parent a178835f6b
commit 0ddf88e46e
9 changed files with 117 additions and 21 deletions

View File

@@ -2,8 +2,10 @@
namespace Thelia\Model;
use Propel\Runtime\Exception\PropelException;
use Thelia\Model\Base\Product as BaseProduct;
use Thelia\Tools\URL;
use Thelia\TaxEngine\Calculator;
class Product extends BaseProduct
{
@@ -11,4 +13,21 @@ class Product extends BaseProduct
{
return URL::getInstance()->retrieve('product', $this->getId(), $locale)->toString();
}
public function getRealLowestPrice($virtualColumnName = 'real_lowest_price')
{
try {
$amount = $this->getVirtualColumn($virtualColumnName);
} catch(PropelException $e) {
throw new PropelException("Virtual column `$virtualColumnName` does not exist in Product::getRealLowestPrice");
}
return $amount;
}
public function getTaxedPrice(Country $country)
{
$taxCalculator = new Calculator();
return $taxCalculator->load($this, $country)->getTaxedPrice();
}
}

View File

@@ -2,8 +2,45 @@
namespace Thelia\Model;
use Thelia\Exception\TaxEngineException;
use Thelia\Model\Base\Tax as BaseTax;
class Tax extends BaseTax {
class Tax extends BaseTax
{
public function calculateTax($amount)
{
if(false === filter_var($amount, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
$rate = $this->getRate();
if($rate === null) {
return 0;
}
return $amount * $rate * 0.01;
}
public function getTaxRuleCountryPosition()
{
try {
$taxRuleCountryPosition = $this->getVirtualColumn(TaxRuleQuery::ALIAS_FOR_TAX_RULE_COUNTRY_POSITION);
} catch(PropelException $e) {
throw new PropelException("Virtual column `" . TaxRuleQuery::ALIAS_FOR_TAX_RULE_COUNTRY_POSITION . "` does not exist in Tax::getTaxRuleCountryPosition");
}
return $taxRuleCountryPosition;
}
public function getTaxRuleRateSum()
{
try {
$taxRuleRateSum = $this->getVirtualColumn(TaxRuleQuery::ALIAS_FOR_TAX_RATE_SUM);
} catch(PropelException $e) {
throw new PropelException("Virtual column `" . TaxRuleQuery::ALIAS_FOR_TAX_RATE_SUM . "` does not exist in Tax::getTaxRuleRateSum");
}
return $taxRuleRateSum;
}
}

View File

@@ -4,7 +4,8 @@ namespace Thelia\Model;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Model\Base\TaxRuleQuery as BaseTaxRuleQuery;
use Thelia\Model\Map\TaxRuleCountryTableMap;
use Thelia\Model\Map\TaxTableMap;
/**
* Skeleton subclass for performing query and update operations on the 'tax_rule' table.
@@ -18,14 +19,26 @@ use Thelia\Model\Base\TaxRuleQuery as BaseTaxRuleQuery;
*/
class TaxRuleQuery extends BaseTaxRuleQuery
{
public function getTaxCalculatorCollection(Product $product, Country $country)
{
$search = TaxRuleCountryQuery::create()
->filterByCountry($country, Criteria::EQUAL)
->filterByTaxRuleId($product->getTaxRuleId())
->orderByPosition()
->find();
const ALIAS_FOR_TAX_RULE_COUNTRY_POSITION = 'taxRuleCountryPosition';
const ALIAS_FOR_TAX_RATE_SUM = 'taxRateSum';
return $search;
public function getTaxCalculatorGroupedCollection(Product $product, Country $country)
{
$search = TaxQuery::create()
->filterByTaxRuleCountry(
TaxRuleCountryQuery::create()
->filterByCountry($country, Criteria::EQUAL)
->filterByTaxRuleId($product->getTaxRuleId())
->groupByPosition()
->orderByPosition()
->find()
)
->withColumn(TaxRuleCountryTableMap::POSITION, self::ALIAS_FOR_TAX_RULE_COUNTRY_POSITION)
->withColumn('ROUND(SUM(' . TaxTableMap::RATE . '), 2)', self::ALIAS_FOR_TAX_RATE_SUM)
;
//var_dump($search->toString());
return $search->find();
}
} // TaxRuleQuery