tax engine

This commit is contained in:
Etienne Roudeix
2013-09-11 16:56:16 +02:00
parent e8a4e56324
commit 25756ef9dd
6 changed files with 35 additions and 30 deletions

View File

@@ -30,9 +30,15 @@ class TaxEngineException extends \RuntimeException
const BAD_RECORDED_TYPE = 101; const BAD_RECORDED_TYPE = 101;
const BAD_RECORDED_REQUIREMENTS = 102; const BAD_RECORDED_REQUIREMENTS = 102;
const TAX_TYPE_BAD_ABSTRACT_METHOD = 201;
const TAX_TYPE_REQUIREMENT_NOT_FOUND = 202;
const TAX_TYPE_BAD_REQUIREMENT_VALUE = 203;
const UNDEFINED_PRODUCT = 501; const UNDEFINED_PRODUCT = 501;
const UNDEFINED_COUNTRY = 502; const UNDEFINED_COUNTRY = 502;
const UNDEFINED_TAX_RULES_COLLECTION = 503; const UNDEFINED_TAX_RULES_COLLECTION = 503;
const UNDEFINED_REQUIREMENTS = 504;
const UNDEFINED_REQUIREMENT_VALUE = 505;
const BAD_AMOUNT_FORMAT = 601; const BAD_AMOUNT_FORMAT = 601;

View File

@@ -2,7 +2,6 @@
namespace Thelia\Model; namespace Thelia\Model;
use Symfony\Component\Form\Exception\Exception;
use Thelia\Exception\TaxEngineException; use Thelia\Exception\TaxEngineException;
use Thelia\Model\Base\Tax as BaseTax; use Thelia\Model\Base\Tax as BaseTax;
use Thelia\TaxEngine\TaxType\BaseTaxType; use Thelia\TaxEngine\TaxType\BaseTaxType;
@@ -49,7 +48,7 @@ class Tax extends BaseTax
if(!$instance instanceof BaseTaxType) { if(!$instance instanceof BaseTaxType) {
throw new TaxEngineException('Recorded type does not extends BaseTaxType', TaxEngineException::BAD_RECORDED_TYPE); throw new TaxEngineException('Recorded type does not extends BaseTaxType', TaxEngineException::BAD_RECORDED_TYPE);
} }
return $instance; return $instance;
} }

View File

@@ -21,7 +21,7 @@ class TaxRuleQuery extends BaseTaxRuleQuery
{ {
const ALIAS_FOR_TAX_RULE_COUNTRY_POSITION = 'taxRuleCountryPosition'; const ALIAS_FOR_TAX_RULE_COUNTRY_POSITION = 'taxRuleCountryPosition';
public function getTaxCalculatorGroupedCollection(Product $product, Country $country) public function getTaxCalculatorCollection(Product $product, Country $country)
{ {
$search = TaxQuery::create() $search = TaxQuery::create()
->filterByTaxRuleCountry( ->filterByTaxRuleCountry(

View File

@@ -34,6 +34,9 @@ use Thelia\Model\TaxRuleQuery;
*/ */
class Calculator class Calculator
{ {
/**
* @var TaxRuleQuery
*/
protected $taxRuleQuery = null; protected $taxRuleQuery = null;
protected $taxRulesCollection = null; protected $taxRulesCollection = null;
@@ -62,7 +65,7 @@ class Calculator
$this->product = $product; $this->product = $product;
$this->country = $country; $this->country = $country;
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorGroupedCollection($product, $country); $this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($product, $country);
return $this; return $this;
} }

View File

@@ -22,6 +22,7 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\TaxEngine\TaxType; namespace Thelia\TaxEngine\TaxType;
use Thelia\Exception\TaxEngineException;
use Thelia\Type\TypeInterface; use Thelia\Type\TypeInterface;
/** /**
@@ -42,24 +43,20 @@ abstract class BaseTaxType
$this->requirements = $this->getRequirementsList(); $this->requirements = $this->getRequirementsList();
if(!is_array($this->requirements)) { if(!is_array($this->requirements)) {
//@todo throw sg throw new TaxEngineException('getRequirementsList must return an array', TaxEngineException::TAX_TYPE_BAD_ABSTRACT_METHOD);
exit('err_1');
} }
foreach($this->requirements as $requirement => $requirementType) { foreach($this->requirements as $requirement => $requirementType) {
if(!$requirementType instanceof TypeInterface) { if(!$requirementType instanceof TypeInterface) {
//@todo throw sg throw new TaxEngineException('getRequirementsList must return an array of TypeInterface', TaxEngineException::TAX_TYPE_BAD_ABSTRACT_METHOD);
exit('err_2');
} }
if(!array_key_exists($requirement, $requirementsValues)) { if(!array_key_exists($requirement, $requirementsValues)) {
//@todo throw sg throw new TaxEngineException('Cannot load requirements : requirement value for `' . $requirement . '` not found', TaxEngineException::TAX_TYPE_REQUIREMENT_NOT_FOUND);
exit('err_3');
} }
if(!$requirementType->isValid($requirementsValues[$requirement])) { if(!$requirementType->isValid($requirementsValues[$requirement])) {
//@todo throw sg throw new TaxEngineException('Requirement value for `' . $requirement . '` does not match required type', TaxEngineException::TAX_TYPE_BAD_REQUIREMENT_VALUE);
exit('err_4');
} }
$this->requirements[$requirement] = $requirementsValues[$requirement]; $this->requirements[$requirement] = $requirementsValues[$requirement];
@@ -69,13 +66,11 @@ abstract class BaseTaxType
public function getRequirement($key) public function getRequirement($key)
{ {
if($this->requirements === null) { if($this->requirements === null) {
//@todo throw sg throw new TaxEngineException('Requirements are empty in BaseTaxType::getRequirement', TaxEngineException::UNDEFINED_REQUIREMENTS);
exit('err_5');
} }
if(!array_key_exists($key, $this->requirements)) { if(!array_key_exists($key, $this->requirements)) {
//@todo throw sg throw new TaxEngineException('Requirement value for `' . $key . '` does not exists in BaseTaxType::$requirements', TaxEngineException::UNDEFINED_REQUIREMENT_VALUE);
exit('err_6');
} }
return $this->requirements[$key]; return $this->requirements[$key];

View File

@@ -83,9 +83,9 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
$calculator = new Calculator(); $calculator = new Calculator();
$taxRuleQuery = $this->getMock('\Thelia\Model\TaxRuleQuery', array('getTaxCalculatorGroupedCollection')); $taxRuleQuery = $this->getMock('\Thelia\Model\TaxRuleQuery', array('getTaxCalculatorCollection'));
$taxRuleQuery->expects($this->once()) $taxRuleQuery->expects($this->once())
->method('getTaxCalculatorGroupedCollection') ->method('getTaxCalculatorCollection')
->with($productQuery, $countryQuery) ->with($productQuery, $countryQuery)
->will($this->returnValue('foo')); ->will($this->returnValue('foo'));
@@ -104,7 +104,7 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
); );
$this->assertEquals( $this->assertEquals(
'foo', 'foo',
$this->getProperty('taxRulesGroupedCollection')->getValue($calculator) $this->getProperty('taxRulesCollection')->getValue($calculator)
); );
} }
@@ -124,35 +124,37 @@ class CalculatorTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGetTaxAmountBadAmount() public function testGetTaxAmountBadAmount()
{ {
$taxRulesGroupedCollection = new ObjectCollection(); $taxRulesCollection = new ObjectCollection();
$calculator = new Calculator(); $calculator = new Calculator();
$rewritingUrlQuery = $this->getProperty('taxRulesGroupedCollection'); $rewritingUrlQuery = $this->getProperty('taxRulesCollection');
$rewritingUrlQuery->setValue($calculator, $taxRulesGroupedCollection); $rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
$calculator->getTaxAmount('foo'); $calculator->getTaxAmount('foo');
} }
public function testGetTaxAmountAndGetTaxedPrice() public function testGetTaxAmountAndGetTaxedPrice()
{ {
$taxRulesGroupedCollection = new ObjectCollection(); $taxRulesCollection = new ObjectCollection();
$taxRulesGroupedCollection->setModel('\Thelia\Model\Tax'); $taxRulesCollection->setModel('\Thelia\Model\Tax');
$tax = new Tax(); $tax = new Tax();
$tax->setVirtualColumn('taxRateSum', 10); $tax->setType('PricePercentTaxType')
->setRequirements(array('percent' => 10));
$taxRulesGroupedCollection->append($tax); $taxRulesCollection->append($tax);
$tax = new Tax(); $tax = new Tax();
$tax->setVirtualColumn('taxRateSum', 8); $tax->setType('PricePercentTaxType')
->setRequirements(array('percent' => 8));
$taxRulesGroupedCollection->append($tax); $taxRulesCollection->append($tax);
$calculator = new Calculator(); $calculator = new Calculator();
$rewritingUrlQuery = $this->getProperty('taxRulesGroupedCollection'); $rewritingUrlQuery = $this->getProperty('taxRulesCollection');
$rewritingUrlQuery->setValue($calculator, $taxRulesGroupedCollection); $rewritingUrlQuery->setValue($calculator, $taxRulesCollection);
$taxAmount = $calculator->getTaxAmount(500); $taxAmount = $calculator->getTaxAmount(500);
$taxedPrice = $calculator->getTaxedPrice(500); $taxedPrice = $calculator->getTaxedPrice(500);