This commit is contained in:
Manuel Raynaud
2014-01-10 11:41:55 +01:00
parent ab0fbb6e27
commit 9d0121f321
54 changed files with 220 additions and 290 deletions

View File

@@ -61,10 +61,10 @@ class Calculator
$this->country = null;
$this->taxRulesCollection = null;
if($product->getId() === null) {
if ($product->getId() === null) {
throw new TaxEngineException('Product id is empty in Calculator::load', TaxEngineException::UNDEFINED_PRODUCT);
}
if($country->getId() === null) {
if ($country->getId() === null) {
throw new TaxEngineException('Country id is empty in Calculator::load', TaxEngineException::UNDEFINED_COUNTRY);
}
@@ -82,13 +82,13 @@ class Calculator
$this->country = null;
$this->taxRulesCollection = null;
if($taxRule->getId() === null) {
if ($taxRule->getId() === null) {
throw new TaxEngineException('TaxRule id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_TAX_RULE);
}
if($country->getId() === null) {
if ($country->getId() === null) {
throw new TaxEngineException('Country id is empty in Calculator::loadTaxRule', TaxEngineException::UNDEFINED_COUNTRY);
}
if($product->getId() === null) {
if ($product->getId() === null) {
throw new TaxEngineException('Product id is empty in Calculator::load', TaxEngineException::UNDEFINED_PRODUCT);
}
@@ -120,15 +120,15 @@ class Calculator
*/
public function getTaxedPrice($untaxedPrice, &$taxCollection = null, $askedLocale = null)
{
if(null === $this->taxRulesCollection) {
if (null === $this->taxRulesCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
if(null === $this->product) {
if (null === $this->product) {
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
}
if(false === filter_var($untaxedPrice, FILTER_VALIDATE_FLOAT)) {
if (false === filter_var($untaxedPrice, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
@@ -136,16 +136,16 @@ class Calculator
$currentPosition = 1;
$currentTax = 0;
if(null !== $taxCollection) {
if (null !== $taxCollection) {
$taxCollection = new OrderProductTaxCollection();
}
foreach($this->taxRulesCollection as $taxRule) {
$position = (int)$taxRule->getTaxRuleCountryPosition();
foreach ($this->taxRulesCollection as $taxRule) {
$position = (int) $taxRule->getTaxRuleCountryPosition();
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements( $taxRule->getRequirements() );
if($currentPosition !== $position) {
if ($currentPosition !== $position) {
$taxedPrice += $currentTax;
$currentTax = 0;
$currentPosition = $position;
@@ -154,7 +154,7 @@ class Calculator
$taxAmount = round($taxType->calculate($this->product, $taxedPrice), 2);
$currentTax += $taxAmount;
if(null !== $taxCollection) {
if (null !== $taxCollection) {
$taxI18n = I18n::forceI18nRetrieving($askedLocale, 'Tax', $taxRule->getId());
$orderProductTax = new OrderProductTax();
$orderProductTax->setTitle($taxI18n->getTitle());
@@ -171,36 +171,36 @@ class Calculator
public function getUntaxedPrice($taxedPrice)
{
if(null === $this->taxRulesCollection) {
if (null === $this->taxRulesCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
if(null === $this->product) {
if (null === $this->product) {
throw new TaxEngineException('Product is empty in Calculator::getTaxedPrice', TaxEngineException::UNDEFINED_PRODUCT);
}
if(false === filter_var($taxedPrice, FILTER_VALIDATE_FLOAT)) {
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) {
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();
$currentPosition = (int) $taxRule->getTaxRuleCountryPosition();
$currentFixTax = 0;
$currentTaxFactor = 0;
do {
$position = (int)$taxRule->getTaxRuleCountryPosition();
$position = (int) $taxRule->getTaxRuleCountryPosition();
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements( $taxRule->getRequirements() );
if($currentPosition !== $position) {
if ($currentPosition !== $position) {
$untaxedPrice -= $currentFixTax;
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);
$currentFixTax = 0;
@@ -211,8 +211,7 @@ class Calculator
$currentFixTax += $taxType->fixAmountRetriever($this->product);
$currentTaxFactor += $taxType->pricePercentRetriever();
} while($taxRule = $this->taxRulesCollection->getPrevious());
} while ($taxRule = $this->taxRulesCollection->getPrevious());
$untaxedPrice -= $currentFixTax;
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);
@@ -224,7 +223,7 @@ class Calculator
$untaxedPrice -= $taxType->fixAmountRetriever();
} while($taxRule = $this->taxRulesCollection->getPrevious());
} while ($taxRule = $this->taxRulesCollection->getPrevious());
$taxRule = $this->taxRulesCollection->getLast();
@@ -238,7 +237,7 @@ class Calculator
$toto = true;
} while($taxRule = $this->taxRulesCollection->getPrevious());
} while ($taxRule = $this->taxRulesCollection->getPrevious());
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);*/

View File

@@ -42,13 +42,13 @@ class TaxEngine
*/
protected $session = null;
static public function getInstance(Session $session = null)
public static function getInstance(Session $session = null)
{
if(null === self::$instance) {
if (null === self::$instance) {
self::$instance = new TaxEngine();
}
if(null !== self::$instance) {
if (null !== self::$instance) {
self::$instance->setSession($session);
}
@@ -85,7 +85,7 @@ class TaxEngine
$fileName = $directoryContent->getFilename();
$className = substr($fileName, 0, (1+strlen($directoryContent->getExtension())) * -1);
if($className == "BaseTaxType") {
if ($className == "BaseTaxType") {
continue;
}
@@ -101,14 +101,14 @@ class TaxEngine
* Then look at the current customer default address country
* Else look at the default website country
*
* @param bool $force result is static cached ; even if a below parameter change between 2 calls, we need to keep coherent results. but you can force it.
* @param bool $force result is static cached ; even if a below parameter change between 2 calls, we need to keep coherent results. but you can force it.
* @return null|TaxEngine
*/
public function getDeliveryCountry($force = false)
{
if(false === $force || null === self::$taxCountry) {
if (false === $force || null === self::$taxCountry) {
/* is there a logged in customer ? */
if(null !== $customer = $this->session->getCustomerUser()) {
if (null !== $customer = $this->session->getCustomerUser()) {
if (null !== $this->session->getOrder()
&& null !== $this->session->getOrder()->chosenDeliveryAddress
&& null !== $currentDeliveryAddress = AddressQuery::create()->findPk($this->session->getOrder()->chosenDeliveryAddress)) {

View File

@@ -35,13 +35,13 @@ abstract class BaseTaxType
{
protected $requirements = null;
public abstract function pricePercentRetriever();
abstract public function pricePercentRetriever();
public abstract function fixAmountRetriever(Product $product);
abstract public function fixAmountRetriever(Product $product);
public abstract function getRequirementsList();
abstract public function getRequirementsList();
public abstract function getTitle();
abstract public function getTitle();
public function calculate(Product $product, $untaxedPrice)
{
@@ -52,20 +52,20 @@ abstract class BaseTaxType
{
$this->requirements = $this->getRequirementsList();
if(!is_array($this->requirements)) {
if (!is_array($this->requirements)) {
throw new TaxEngineException('getRequirementsList must return an array', TaxEngineException::TAX_TYPE_BAD_ABSTRACT_METHOD);
}
foreach($this->requirements as $requirement => $requirementType) {
if(!$requirementType instanceof TypeInterface) {
foreach ($this->requirements as $requirement => $requirementType) {
if (!$requirementType instanceof TypeInterface) {
throw new TaxEngineException('getRequirementsList must return an array of TypeInterface', TaxEngineException::TAX_TYPE_BAD_ABSTRACT_METHOD);
}
if(!array_key_exists($requirement, $requirementsValues)) {
if (!array_key_exists($requirement, $requirementsValues)) {
throw new TaxEngineException('Cannot load requirements : requirement value for `' . $requirement . '` not found', TaxEngineException::TAX_TYPE_REQUIREMENT_NOT_FOUND);
}
if(!$requirementType->isValid($requirementsValues[$requirement])) {
if (!$requirementType->isValid($requirementsValues[$requirement])) {
throw new TaxEngineException('Requirement value for `' . $requirement . '` does not match required type', TaxEngineException::TAX_TYPE_BAD_REQUIREMENT_VALUE);
}
@@ -75,11 +75,11 @@ abstract class BaseTaxType
public function getRequirement($key)
{
if($this->requirements === null) {
if ($this->requirements === null) {
throw new TaxEngineException('Requirements are empty in BaseTaxType::getRequirement', TaxEngineException::UNDEFINED_REQUIREMENTS);
}
if(!array_key_exists($key, $this->requirements)) {
if (!array_key_exists($key, $this->requirements)) {
throw new TaxEngineException('Requirement value for `' . $key . '` does not exists in BaseTaxType::$requirements', TaxEngineException::UNDEFINED_REQUIREMENT_VALUE);
}

View File

@@ -52,7 +52,7 @@ class FeatureFixAmountTaxType extends BaseTaxType
$taxAmount = $query->getFreeTextValue();
$testInt = new FloatType();
if(!$testInt->isValid($taxAmount)) {
if (!$testInt->isValid($taxAmount)) {
throw new TaxEngineException('Feature value does not match FLOAT format', TaxEngineException::FEATURE_BAD_EXPECTED_VALUE);
}