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_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_COUNTRY = 502;
const UNDEFINED_TAX_RULES_COLLECTION = 503;
const UNDEFINED_REQUIREMENTS = 504;
const UNDEFINED_REQUIREMENT_VALUE = 505;
const BAD_AMOUNT_FORMAT = 601;

View File

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

View File

@@ -21,7 +21,7 @@ class TaxRuleQuery extends BaseTaxRuleQuery
{
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()
->filterByTaxRuleCountry(

View File

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

View File

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

View File

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