forked from thelia/thelia
/
Tax.php
89 lines (69 loc) · 2.62 KB
/
Tax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Thelia\Model;
use Propel\Runtime\Exception\PropelException;
use Thelia\Exception\TaxEngineException;
use Thelia\Model\Base\Tax as BaseTax;
use Thelia\Model\Tools\ModelEventDispatcherTrait;
use Thelia\TaxEngine\BaseTaxType;
class Tax extends BaseTax
{
use ModelEventDispatcherTrait;
/**
* Provides a form-and-javascript-safe version of the type, which is a fully qualified classname, with \
*/
public static function escapeTypeName($name)
{
return str_replace('\\', '-', $name);
}
public static function unescapeTypeName($name)
{
return str_replace('-', '\\', $name);
}
public function calculateTax($amount)
{
if (false === filter_var($amount, FILTER_VALIDATE_FLOAT)) {
throw new TaxEngineException('BAD AMOUNT FORMAT', TaxEngineException::BAD_AMOUNT_FORMAT);
}
$rate = $this->getRate();
if ($rate === null) {
return 0;
}
return $amount * $rate * 0.01;
}
public function getTaxRuleCountryPosition()
{
try {
$taxRuleCountryPosition = $this->getVirtualColumn(TaxRuleQuery::ALIAS_FOR_TAX_RULE_COUNTRY_POSITION);
} catch (PropelException $e) {
throw new PropelException("Virtual column `" . TaxRuleQuery::ALIAS_FOR_TAX_RULE_COUNTRY_POSITION . "` does not exist in Tax::getTaxRuleCountryPosition");
}
return $taxRuleCountryPosition;
}
public function getTypeInstance()
{
$class = $this->getType();
/* test type */
if (!class_exists($class)) {
throw new TaxEngineException('Recorded type `' . $class . '` does not exists', TaxEngineException::BAD_RECORDED_TYPE);
}
/** @var \Thelia\TaxEngine\BaseTaxType $instance */
$instance = new $class;
if (!$instance instanceof BaseTaxType) {
throw new TaxEngineException('Recorded type `' . $class . '` does not extends BaseTaxType', TaxEngineException::BAD_RECORDED_TYPE);
}
$instance->loadRequirements($this->getRequirements());
return $instance;
}
public function setRequirements($requirements)
{
return parent::setSerializedRequirements(base64_encode(json_encode($requirements)));
}
public function getRequirements()
{
$requirements = json_decode(base64_decode(parent::getSerializedRequirements()), true);
if (json_last_error() != JSON_ERROR_NONE || !is_array($requirements)) {
throw new TaxEngineException('BAD RECORDED REQUIREMENTS', TaxEngineException::BAD_RECORDED_REQUIREMENTS);
}
return $requirements;
}
}