/
FeatureFixAmountTaxType.php
87 lines (74 loc) · 2.52 KB
/
FeatureFixAmountTaxType.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
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Thelia\TaxEngine\TaxType;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
use Thelia\Model\FeatureProductQuery;
use Thelia\Model\LangQuery;
use Thelia\Model\Product;
use Thelia\TaxEngine\BaseTaxType;
use Thelia\TaxEngine\TaxTypeRequirementDefinition;
use Thelia\Type\FloatType;
use Thelia\Type\ModelValidIdType;
/**
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*/
class FeatureFixAmountTaxType extends BaseTaxType
{
public function setFeature($featureId)
{
$this->setRequirement('feature', $featureId);
return $this;
}
public function fixAmountRetriever(Product $product): float
{
$taxAmount = 0;
$featureId = $this->getRequirement('feature');
$query = FeatureProductQuery::create()
->filterByProduct($product)
->filterByFeatureId($featureId)
->findOne();
if (null !== $query) {
if (null === $query->getFeatureAvId()) {
$taxAmount = $query->getFreeTextValue(); // BC for old behavior
} else {
$locale = LangQuery::create()->findPk($this->getRequirement('lang'))->getLocale();
$taxAmount = $query->getFeatureAv()->setLocale($locale)->getTitle();
}
$testFloat = new FloatType();
if (!$testFloat->isValid($taxAmount)) {
// We cannot modify "bad" (consider uninitialized) feature value in backOffice if we throw exception
Tlog::getInstance()->error(Translator::getInstance()->trans('Feature value does not match FLOAT format'));
return 0;
}
}
return $taxAmount;
}
public function getRequirementsDefinition(): array
{
return [
new TaxTypeRequirementDefinition(
'feature',
new ModelValidIdType('Feature'),
Translator::getInstance()->trans('Feature')
),
new TaxTypeRequirementDefinition(
'lang',
new ModelValidIdType('Lang'),
Translator::getInstance()->trans('Language')
),
];
}
public function getTitle()
{
return Translator::getInstance()->trans("Constant amount found in one of the product's feature");
}
}