tax engine retriever

This commit is contained in:
Etienne Roudeix
2013-09-13 13:59:16 +02:00
parent e7368fa9f0
commit ecc419fbdf
6 changed files with 155 additions and 23 deletions

View File

@@ -73,11 +73,16 @@ class Calculator
return $this;
}
public function getTaxAmount($untaxedPrice)
public function getTaxAmountFromUntaxedPrice($untaxedPrice)
{
return $this->getTaxedPrice($untaxedPrice) - $untaxedPrice;
}
public function getTaxAmountFromTaxedPrice($taxedPrice)
{
return $taxedPrice - $this->getUntaxedPrice($taxedPrice);
}
public function getTaxedPrice($untaxedPrice)
{
if(null === $this->taxRulesCollection) {
@@ -111,4 +116,72 @@ class Calculator
return $taxedPrice;
}
public function getUntaxedPrice($taxedPrice)
{
if(null === $this->taxRulesCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
if(false === filter_var($taxedPrice, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
$taxRule = $this->taxRulesCollection->getLast();
$untaxedPrice = $taxedPrice;
$currentPosition = (int)$taxRule->getTaxRuleCountryPosition();
$currentFixTax = 0;
$currentTaxFactor = 0;
do {
$position = (int)$taxRule->getTaxRuleCountryPosition();
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements( $taxRule->getRequirements() );
if($currentPosition !== $position) {
$untaxedPrice -= $currentFixTax;
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);
$currentFixTax = 0;
$currentTaxFactor = 0;
$currentPosition = $position;
}
$currentFixTax += $taxType->fixAmountRetriever();
$currentTaxFactor += $taxType->pricePercentRetriever();
} while($taxRule = $this->taxRulesCollection->getPrevious());
$untaxedPrice -= $currentFixTax;
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);
/*do {
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements( $taxRule->getRequirements() );
$untaxedPrice -= $taxType->fixAmountRetriever();
} while($taxRule = $this->taxRulesCollection->getPrevious());
$taxRule = $this->taxRulesCollection->getLast();
$currentTaxFactor = 0;
do {
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements( $taxRule->getRequirements() );
$currentTaxFactor += $taxType->pricePercentRetriever($untaxedPrice);
$toto = true;
} while($taxRule = $this->taxRulesCollection->getPrevious());
$untaxedPrice = $untaxedPrice / (1+$currentTaxFactor);*/
return $untaxedPrice;
}
}

View File

@@ -36,6 +36,10 @@ abstract class BaseTaxType
public abstract function calculate($untaxedPrice);
public abstract function pricePercentRetriever();
public abstract function fixAmountRetriever();
public abstract function getRequirementsList();
public function loadRequirements($requirementsValues)

View File

@@ -37,6 +37,16 @@ class featureSlicePercentTaxType extends BaseTaxType
}
public function pricePercentRetriever()
{
}
public function fixAmountRetriever()
{
}
public function getRequirementsList()
{
return array(

View File

@@ -36,6 +36,16 @@ class FixAmountTaxType extends BaseTaxType
return $this->getRequirement("amount");
}
public function pricePercentRetriever()
{
return 0;
}
public function fixAmountRetriever()
{
return $this->getRequirement("amount");
}
public function getRequirementsList()
{
return array(

View File

@@ -36,6 +36,16 @@ class PricePercentTaxType extends BaseTaxType
return $untaxedPrice * $this->getRequirement("percent") * 0.01;
}
public function pricePercentRetriever()
{
return ($this->getRequirement("percent") * 0.01);
}
public function fixAmountRetriever()
{
return 0;
}
public function getRequirementsList()
{
return array(
@@ -43,3 +53,5 @@ class PricePercentTaxType extends BaseTaxType
);
}
}
//600 / (1 + 0,10 + 0,10) =/= 600 / (1 + 0,10 ) + 600 / (1 + 0,10 )