tax engine

This commit is contained in:
Etienne Roudeix
2013-09-11 16:21:51 +02:00
parent 551c132b76
commit e8a4e56324
17 changed files with 561 additions and 107 deletions

View File

@@ -36,7 +36,7 @@ class Calculator
{
protected $taxRuleQuery = null;
protected $taxRulesGroupedCollection = null;
protected $taxRulesCollection = null;
protected $product = null;
protected $country = null;
@@ -50,7 +50,7 @@ class Calculator
{
$this->product = null;
$this->country = null;
$this->taxRulesGroupedCollection = null;
$this->taxRulesCollection = null;
if($product->getId() === null) {
throw new TaxEngineException('Product id is empty in Calculator::load', TaxEngineException::UNDEFINED_PRODUCT);
@@ -62,14 +62,14 @@ class Calculator
$this->product = $product;
$this->country = $country;
$this->taxRulesGroupedCollection = $this->taxRuleQuery->getTaxCalculatorGroupedCollection($product, $country);
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorGroupedCollection($product, $country);
return $this;
}
public function getTaxAmount($untaxedPrice)
{
if(null === $this->taxRulesGroupedCollection) {
if(null === $this->taxRulesCollection) {
throw new TaxEngineException('Tax rules collection is empty in Calculator::getTaxAmount', TaxEngineException::UNDEFINED_TAX_RULES_COLLECTION);
}
@@ -78,9 +78,13 @@ class Calculator
}
$totalTaxAmount = 0;
foreach($this->taxRulesGroupedCollection as $taxRule) {
$rateSum = $taxRule->getTaxRuleRateSum();
$taxAmount = $untaxedPrice * $rateSum * 0.01;
foreach($this->taxRulesCollection as $taxRule) {
$taxType = $taxRule->getTypeInstance();
$taxType->loadRequirements($taxRule->getRequirements());
$taxAmount = $taxType->calculate($untaxedPrice);
$totalTaxAmount += $taxAmount;
$untaxedPrice += $taxAmount;
}

View File

@@ -0,0 +1,83 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\TaxEngine\TaxType;
use Thelia\Type\TypeInterface;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
abstract class BaseTaxType
{
protected $requirements = null;
public abstract function calculate($untaxedPrice);
public abstract function getRequirementsList();
public function loadRequirements($requirementsValues)
{
$this->requirements = $this->getRequirementsList();
if(!is_array($this->requirements)) {
//@todo throw sg
exit('err_1');
}
foreach($this->requirements as $requirement => $requirementType) {
if(!$requirementType instanceof TypeInterface) {
//@todo throw sg
exit('err_2');
}
if(!array_key_exists($requirement, $requirementsValues)) {
//@todo throw sg
exit('err_3');
}
if(!$requirementType->isValid($requirementsValues[$requirement])) {
//@todo throw sg
exit('err_4');
}
$this->requirements[$requirement] = $requirementsValues[$requirement];
}
}
public function getRequirement($key)
{
if($this->requirements === null) {
//@todo throw sg
exit('err_5');
}
if(!array_key_exists($key, $this->requirements)) {
//@todo throw sg
exit('err_6');
}
return $this->requirements[$key];
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\TaxEngine\TaxType;
use Thelia\Type\FloatToFloatArrayType;
use Thelia\Type\ModelType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class featureSlicePercentTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
}
public function getRequirementsList()
{
return array(
'featureId' => new ModelType('Currency'),
'slices' => new FloatToFloatArrayType(),
);
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\TaxEngine\TaxType;
use Thelia\Type\FloatType;
/**
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*
*/
class PricePercentTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
return $untaxedPrice * $this->getRequirement("percent") * 0.01;
}
public function getRequirementsList()
{
return array(
'percent' => new FloatType(),
);
}
}