Inital commit

This commit is contained in:
2020-11-19 15:36:28 +01:00
parent 71f32f83d3
commit 66ce4ee218
18077 changed files with 2166122 additions and 35184 deletions

View File

@@ -98,7 +98,6 @@ abstract class BaseTaxType
}
foreach ($requirements as $requirement) {
$requirementName = $requirement->getName();
if (! array_key_exists($requirementName, $requirementsValues)) {

View File

@@ -16,6 +16,7 @@ use Thelia\Exception\TaxEngineException;
use Thelia\Model\Country;
use Thelia\Model\OrderProductTax;
use Thelia\Model\Product;
use Thelia\Model\State;
use Thelia\Model\TaxRule;
use Thelia\Model\TaxRuleQuery;
use Thelia\Tools\I18n;
@@ -39,16 +40,20 @@ class Calculator
protected $product = null;
protected $country = null;
protected $state = null;
public function __construct()
{
$this->taxRuleQuery = new TaxRuleQuery();
}
public function load(Product $product, Country $country)
public function load(Product $product, Country $country, State $state = null)
{
$this->product = null;
$this->country = null;
$this->state = null;
$this->taxRulesCollection = null;
if ($product->getId() === null) {
@@ -60,13 +65,14 @@ class Calculator
$this->product = $product;
$this->country = $country;
$this->state = $state;
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($product->getTaxRule(), $country);
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($product->getTaxRule(), $country, $state);
return $this;
}
public function loadTaxRule(TaxRule $taxRule, Country $country, Product $product)
public function loadTaxRule(TaxRule $taxRule, Country $country, Product $product, State $state = null)
{
$this->product = null;
$this->country = null;
@@ -84,13 +90,14 @@ class Calculator
$this->country = $country;
$this->product = $product;
$this->state = $state;
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country, $state);
return $this;
}
public function loadTaxRuleWithoutProduct(TaxRule $taxRule, Country $country)
public function loadTaxRuleWithoutProduct(TaxRule $taxRule, Country $country, State $state = null)
{
$this->product = null;
$this->country = null;
@@ -105,8 +112,9 @@ class Calculator
$this->country = $country;
$this->product = new Product();
$this->state = $state;
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country);
$this->taxRulesCollection = $this->taxRuleQuery->getTaxCalculatorCollection($taxRule, $country, $state);
return $this;
}
@@ -161,7 +169,7 @@ class Calculator
$currentPosition = $position;
}
$taxAmount = round($taxType->calculate($this->product, $taxedPrice), 2);
$taxAmount = $taxType->calculate($this->product, $taxedPrice);
$currentTax += $taxAmount;
if (null !== $taxCollection) {
@@ -219,7 +227,6 @@ class Calculator
$currentFixTax += $taxType->fixAmountRetriever($this->product);
$currentTaxFactor += $taxType->pricePercentRetriever();
} while ($taxRule = $this->taxRulesCollection->getPrevious());
$untaxedPrice -= $currentFixTax;

View File

@@ -12,10 +12,13 @@
namespace Thelia\TaxEngine;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Model\AddressQuery;
use Thelia\Model\Country;
use Thelia\Model\CountryQuery;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Model\Customer;
use Thelia\Model\State;
/**
* Class TaxEngine
@@ -26,18 +29,17 @@ use Thelia\Core\HttpFoundation\Request;
class TaxEngine
{
protected $taxCountry = null;
protected $taxState = null;
protected $typeList = null;
protected $taxTypesDirectories = array();
/**
* @var Session $session
*/
protected $session = null;
/** @var RequestStack */
protected $requestStack;
public function __construct(Request $request)
public function __construct(RequestStack $requestStack)
{
$this->session = $request->getSession();
$this->requestStack = $requestStack;
// Intialize the defaults Tax Types
$this->taxTypesDirectories['Thelia\\TaxEngine\\TaxType'] = __DIR__ . DS . "TaxType";
@@ -69,20 +71,19 @@ class TaxEngine
public function getTaxTypeList()
{
if ($this->typeList === null) {
$this->typeList = array();
foreach ($this->taxTypesDirectories as $namespace => $directory) {
try {
$directoryIterator = new \DirectoryIterator($directory);
foreach ($directoryIterator as $fileinfo) {
if ($fileinfo->isFile()) {
$fileName = $fileinfo->getFilename();
$className = substr($fileName, 0, (1+strlen($fileinfo->getExtension())) * -1);
$extension = $fileinfo->getExtension();
if (strtolower($extension) !== 'php') {
continue;
}
$className = $fileinfo->getBaseName('.php');
try {
$fullyQualifiedClassName = "$namespace\\$className";
@@ -112,29 +113,60 @@ class TaxEngine
* Then look at the current customer default address country
* Else look at the default website country
* @return null|TaxEngine
* @return null|Country
*/
public function getDeliveryCountry()
{
if (null === $this->taxCountry) {
/* is there a logged in customer ? */
if (null !== $customer = $this->session->getCustomerUser()) {
if (null !== $this->session->getOrder()
&& null !== $this->session->getOrder()->getChoosenDeliveryAddress()
&& null !== $currentDeliveryAddress = AddressQuery::create()->findPk($this->session->getOrder()->getChoosenDeliveryAddress())) {
/** @var Customer $customer */
if (null !== $customer = $this->getSession()->getCustomerUser()) {
if (null !== $this->getSession()->getOrder()
&& null !== $this->getSession()->getOrder()->getChoosenDeliveryAddress()
&& null !== $currentDeliveryAddress = AddressQuery::create()->findPk($this->getSession()->getOrder()->getChoosenDeliveryAddress())) {
$this->taxCountry = $currentDeliveryAddress->getCountry();
$this->taxState = $currentDeliveryAddress->getState();
} else {
$customerDefaultAddress = $customer->getDefaultAddress();
$this->taxCountry = $customerDefaultAddress->getCountry();
$this->taxState = $customerDefaultAddress->getState();
}
}
if (null == $this->taxCountry) {
$this->taxCountry = CountryQuery::create()->findOneByByDefault(1);
$this->taxState = null;
}
}
return $this->taxCountry;
}
/**
* Find Tax State Id
*
* First look for a picked delivery address state
* Then look at the current customer default address state
* Else null
* @return null|State
* @since 2.3.0-alpha1
*/
public function getDeliveryState()
{
if (null === $this->taxCountry) {
/* is there a logged in customer ? */
$this->getDeliveryCountry();
}
return $this->taxState;
}
/**
* @return Session
*/
protected function getSession()
{
return $this->requestStack->getCurrentRequest()->getSession();
}
}

View File

@@ -55,7 +55,6 @@ class FeatureFixAmountTaxType extends BaseTaxType
TaxEngineException::FEATURE_BAD_EXPECTED_VALUE
);
}
}
return $taxAmount;
@@ -64,7 +63,11 @@ class FeatureFixAmountTaxType extends BaseTaxType
public function getRequirementsDefinition()
{
return array(
new TaxTypeRequirementDefinition('feature', new ModelValidIdType('Feature'))
new TaxTypeRequirementDefinition(
'feature',
new ModelValidIdType('Feature'),
Translator::getInstance()->trans("Feature")
)
);
}

View File

@@ -39,7 +39,11 @@ class FixAmountTaxType extends BaseTaxType
public function getRequirementsDefinition()
{
return array(
new TaxTypeRequirementDefinition('amount', new FloatType())
new TaxTypeRequirementDefinition(
'amount',
new FloatType(),
Translator::getInstance()->trans("Amount")
)
);
}

View File

@@ -39,7 +39,11 @@ class PricePercentTaxType extends BaseTaxType
public function getRequirementsDefinition()
{
return array(
new TaxTypeRequirementDefinition('percent', new FloatType())
new TaxTypeRequirementDefinition(
'percent',
new FloatType(),
Translator::getInstance()->trans("Percent")
)
);
}

View File

@@ -31,16 +31,22 @@ class TaxTypeRequirementDefinition
*/
protected $type;
/**
* @var string The translated requirement title
*/
protected $title;
/**
* Create a new Tax type requirement
*
* @param string $name the name of the requirement
* @param TypeInterface $type the type of the data
*/
public function __construct($name, TypeInterface $type)
public function __construct($name, TypeInterface $type, $title = null)
{
$this->name = $name;
$this->type = $type;
$this->title = $title ?: $name;
}
public function getName()
@@ -53,6 +59,11 @@ class TaxTypeRequirementDefinition
return $this->type;
}
public function getTitle()
{
return $this->title;
}
public function isValueValid($value)
{
return $this->type->isValid($value);