diff --git a/core/lib/Thelia/Model/Tax.php b/core/lib/Thelia/Model/Tax.php index 738f16508..7b9b22b6f 100755 --- a/core/lib/Thelia/Model/Tax.php +++ b/core/lib/Thelia/Model/Tax.php @@ -40,13 +40,13 @@ class Tax extends BaseTax /* test type */ if(!class_exists($class)) { - throw new TaxEngineException('Recorded type does not exists', TaxEngineException::BAD_RECORDED_TYPE); + throw new TaxEngineException('Recorded type `' . $class . '` does not exists', TaxEngineException::BAD_RECORDED_TYPE); } $instance = new $class; if(!$instance instanceof BaseTaxType) { - throw new TaxEngineException('Recorded type does not extends BaseTaxType', TaxEngineException::BAD_RECORDED_TYPE); + throw new TaxEngineException('Recorded type `' . $class . '` does not extends BaseTaxType', TaxEngineException::BAD_RECORDED_TYPE); } return $instance; @@ -54,7 +54,7 @@ class Tax extends BaseTax public function setRequirements($requirements) { - parent::setSerializedRequirements(base64_encode(json_encode($requirements))); + return parent::setSerializedRequirements(base64_encode(json_encode($requirements))); } public function getRequirements() diff --git a/core/lib/Thelia/TaxEngine/Calculator.php b/core/lib/Thelia/TaxEngine/Calculator.php index 2708e88c6..f49498263 100755 --- a/core/lib/Thelia/TaxEngine/Calculator.php +++ b/core/lib/Thelia/TaxEngine/Calculator.php @@ -39,6 +39,9 @@ class Calculator */ protected $taxRuleQuery = null; + /** + * @var null|\Propel\Runtime\Collection\ObjectCollection + */ protected $taxRulesCollection = null; protected $product = null; @@ -80,19 +83,28 @@ class Calculator throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT); } - $totalTaxAmount = 0; + $taxedPrice = $untaxedPrice; + $currentPosition = 1; + $currentTax = 0; + foreach($this->taxRulesCollection as $taxRule) { + $position = (int)$taxRule->getTaxRuleCountryPosition(); + $taxType = $taxRule->getTypeInstance(); + $taxType->loadRequirements( $taxRule->getRequirements() ); - $taxType->loadRequirements($taxRule->getRequirements()); + if($currentPosition !== $position) { + $taxedPrice += $currentTax; + $currentTax = 0; + $currentPosition = $position; + } - $taxAmount = $taxType->calculate($untaxedPrice); - - $totalTaxAmount += $taxAmount; - $untaxedPrice += $taxAmount; + $currentTax += $taxType->calculate($taxedPrice); } - return $totalTaxAmount; + $taxedPrice += $currentTax; + + return $taxedPrice; } public function getTaxedPrice($untaxedPrice) diff --git a/core/lib/Thelia/TaxEngine/TaxType/FixAmountTaxType.php b/core/lib/Thelia/TaxEngine/TaxType/FixAmountTaxType.php new file mode 100755 index 000000000..c533d0ec3 --- /dev/null +++ b/core/lib/Thelia/TaxEngine/TaxType/FixAmountTaxType.php @@ -0,0 +1,45 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\TaxEngine\TaxType; + +use Thelia\Type\FloatType; + +/** + * + * @author Etienne Roudeix + * + */ +class FixAmountTaxType extends BaseTaxType +{ + public function calculate($untaxedPrice) + { + return $this->getRequirement("amount"); + } + + public function getRequirementsList() + { + return array( + 'amount' => new FloatType(), + ); + } +} diff --git a/install/tax_faker.php b/install/tax_faker.php new file mode 100755 index 000000000..5d534e2ef --- /dev/null +++ b/install/tax_faker.php @@ -0,0 +1,116 @@ +boot(); + +$faker = Faker\Factory::create(); + +$con = \Propel\Runtime\Propel::getConnection( + Thelia\Model\Map\ProductTableMap::DATABASE_NAME +); +$con->beginTransaction(); + +$currency = \Thelia\Model\CurrencyQuery::create()->filterByCode('EUR')->findOne(); + +try { + $stmt = $con->prepare("SET foreign_key_checks = 0"); + $stmt->execute(); + + \Thelia\Model\TaxQuery::create() + ->find() + ->delete(); + + \Thelia\Model\Base\TaxRuleQuery::create() + ->find() + ->delete(); + + \Thelia\Model\Base\TaxRuleCountryQuery::create() + ->find() + ->delete(); + + $stmt = $con->prepare("SET foreign_key_checks = 1"); + $stmt->execute(); + + /* 10% tax */ + $tax10p = new \Thelia\Model\Tax(); + $tax10p->setType('PricePercentTaxType') + ->setRequirements(array('percent' => 10)) + ->save(); + + /* 8% tax */ + $tax8p = new \Thelia\Model\Tax(); + $tax8p->setType('PricePercentTaxType') + ->setRequirements(array('percent' => 8)) + ->save(); + + /* fix 5 tax */ + $tax5 = new \Thelia\Model\Tax(); + $tax5->setType('FixAmountTaxType') + ->setRequirements(array('amount' => 5)) + ->save(); + + /* 1% tax */ + $tax1p = new \Thelia\Model\Tax(); + $tax1p->setType('PricePercentTaxType') + ->setRequirements(array('percent' => 1)) + ->save(); + + /* tax rule */ + $taxRule = new \Thelia\Model\TaxRule(); + $taxRule->save(); + + /* add 4 taxes to the rule for France (64) */ + $taxRuleCountry = new \Thelia\Model\TaxRuleCountry(); + $taxRuleCountry->setTaxRule($taxRule) + ->setCountryId(64) + ->setTax($tax10p) + ->setPosition(1) + ->save(); + + $taxRuleCountry = new \Thelia\Model\TaxRuleCountry(); + $taxRuleCountry->setTaxRule($taxRule) + ->setCountryId(64) + ->setTax($tax8p) + ->setPosition(1) + ->save(); + + $taxRuleCountry = new \Thelia\Model\TaxRuleCountry(); + $taxRuleCountry->setTaxRule($taxRule) + ->setCountryId(64) + ->setTax($tax5) + ->setPosition(2) + ->save(); + + $taxRuleCountry = new \Thelia\Model\TaxRuleCountry(); + $taxRuleCountry->setTaxRule($taxRule) + ->setCountryId(64) + ->setTax($tax1p) + ->setPosition(3) + ->save(); + + foreach(\Thelia\Model\ProductQuery::create()->find() as $productActiveRecord) { + $productActiveRecord->setTaxRule($taxRule) + ->save(); + } + + $con->commit(); + +} catch (Exception $e) { + echo "error : ".$e->getMessage()."\n"; + $con->rollBack(); +}