tax engine

This commit is contained in:
Etienne Roudeix
2013-09-11 19:01:23 +02:00
parent 642376a6b3
commit fa5c1aaefc
4 changed files with 183 additions and 10 deletions

View File

@@ -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()

View File

@@ -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)

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 FixAmountTaxType extends BaseTaxType
{
public function calculate($untaxedPrice)
{
return $this->getRequirement("amount");
}
public function getRequirementsList()
{
return array(
'amount' => new FloatType(),
);
}
}