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(),
);
}
}

116
install/tax_faker.php Executable file
View File

@@ -0,0 +1,116 @@
<?php
use Thelia\Constraint\ConstraintFactory;
use Thelia\Constraint\ConstraintManager;
use Thelia\Constraint\Rule\AvailableForTotalAmount;
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Rule\AvailableForXArticlesManager;
use Thelia\Constraint\Rule\Operators;
use Thelia\Coupon\CouponRuleCollection;
use Thelia\Model\ProductImage;
use Thelia\Model\CategoryImage;
use Thelia\Model\FolderImage;
use Thelia\Model\ContentImage;
use Imagine\Image\Color;
use Imagine\Image\Point;
require __DIR__ . '/../core/bootstrap.php';
$thelia = new Thelia\Core\Thelia("dev", true);
$thelia->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();
}