fix tests

This commit is contained in:
Etienne Roudeix
2013-10-07 14:36:20 +02:00
parent fe3aea4ab7
commit b84e863033
14 changed files with 306 additions and 46 deletions

View File

@@ -76,7 +76,7 @@ class Calculator
return $this;
}
public function loadTaxRule(TaxRule $taxRule, Country $country)
public function loadTaxRule(TaxRule $taxRule, Country $country, Product $product)
{
$this->product = null;
$this->country = null;
@@ -88,8 +88,12 @@ class Calculator
if($country->getId() === null) {
throw new TaxEngineException('Country id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_COUNTRY);
}
if($product->getId() === null) {
throw new TaxEngineException('Product id is empty in Calculator::load', TaxEngineException::UNDEFINED_PRODUCT);
}
$this->country = $country;
$this->product = $product;
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
@@ -117,7 +121,11 @@ class Calculator
public function getTaxedPrice($untaxedPrice, &$taxCollection = null, $askedLocale = null)
{
if(null === $this->taxRulesCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
if(null === $this->product) {
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
}
if(false === filter_var($untaxedPrice, FILTER_VALIDATE_FLOAT)) {
@@ -143,7 +151,7 @@ class Calculator
$currentPosition = $position;
}
$taxAmount = round($taxType->calculate($taxedPrice), 2);
$taxAmount = round($taxType->calculate($this->product, $taxedPrice), 2);
$currentTax += $taxAmount;
if(null !== $taxCollection) {
@@ -167,12 +175,20 @@ class Calculator
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
if(null === $this->product) {
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
}
if(false === filter_var($taxedPrice, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
$taxRule = $this->taxRulesCollection->getLast();
if(null === $taxRule) {
throw new TaxEngineException('Tax rules collection got no tax ', TaxEngineException::NO_TAX_IN_TAX_RULES_COLLECTION);
}
$untaxedPrice = $taxedPrice;
$currentPosition = (int)$taxRule->getTaxRuleCountryPosition();
$currentFixTax = 0;
@@ -192,7 +208,7 @@ class Calculator
$currentPosition = $position;
}
$currentFixTax += $taxType->fixAmountRetriever();
$currentFixTax += $taxType->fixAmountRetriever($this->product);
$currentTaxFactor += $taxType->pricePercentRetriever();

View File

@@ -35,14 +35,17 @@ abstract class BaseTaxType
{
protected $requirements = null;
public abstract function calculate($untaxedPrice);
public abstract function pricePercentRetriever();
public abstract function fixAmountRetriever(Product $product);
public abstract function getRequirementsList();
public function calculate(Product $product, $untaxedPrice)
{
return $untaxedPrice * $this->pricePercentRetriever() + $this->fixAmountRetriever($product);
}
public function loadRequirements($requirementsValues)
{
$this->requirements = $this->getRequirementsList();

View File

@@ -26,6 +26,7 @@ use Thelia\Exception\TaxEngineException;
use Thelia\Model\FeatureProductQuery;
use Thelia\Model\Product;
use Thelia\Type\FloatType;
use Thelia\Type\ModelValidIdType;
/**
*
@@ -34,11 +35,6 @@ use Thelia\Type\FloatType;
*/
class FeatureFixAmountTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
return $this->getRequirement("amount");
}
public function pricePercentRetriever()
{
return 0;
@@ -46,7 +42,7 @@ class FeatureFixAmountTaxType extends BaseTaxType
public function fixAmountRetriever(Product $product)
{
$featureId = $this->getRequirement("featureId");
$featureId = $this->getRequirement("feature");
$query = FeatureProductQuery::create()
->filterByProduct($product)
@@ -66,7 +62,7 @@ class FeatureFixAmountTaxType extends BaseTaxType
public function getRequirementsList()
{
return array(
'featureId' => new ModelType('Feature'),
'feature' => new ModelValidIdType('Feature'),
);
}
}

View File

@@ -23,7 +23,7 @@
namespace Thelia\TaxEngine\TaxType;
use Thelia\Type\FloatToFloatArrayType;
use Thelia\Type\ModelType;
use Thelia\Type\ModelValidIdType;
/**
*
@@ -32,11 +32,6 @@ use Thelia\Type\ModelType;
*/
class featureSlicePercentTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
}
public function pricePercentRetriever()
{
@@ -50,7 +45,7 @@ class featureSlicePercentTaxType extends BaseTaxType
public function getRequirementsList()
{
return array(
'featureId' => new ModelType('Currency'),
'featureId' => new ModelValidIdType('Currency'),
'slices' => new FloatToFloatArrayType(),
);
}

View File

@@ -31,11 +31,6 @@ use Thelia\Type\FloatType;
*/
class FixAmountTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
return $this->getRequirement("amount");
}
public function pricePercentRetriever()
{
return 0;

View File

@@ -31,11 +31,6 @@ use Thelia\Type\FloatType;
*/
class PricePercentTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
return $untaxedPrice * $this->getRequirement("percent") * 0.01;
}
public function pricePercentRetriever()
{
return ($this->getRequirement("percent") * 0.01);