1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Context\Setup; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Sylius\Component\Addressing\Model\ZoneInterface; |
16
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
17
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
18
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Mateusz Zalewski <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class TaxContext implements Context |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $taxRateFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $taxCategoryFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $taxRateRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var RepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $taxCategoryRepository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var RepositoryInterface |
47
|
|
|
*/ |
48
|
|
|
private $zoneRepository; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param FactoryInterface $taxRateFactory |
52
|
|
|
* @param FactoryInterface $taxCategoryFactory |
53
|
|
|
* @param RepositoryInterface $taxRateRepository |
54
|
|
|
* @param RepositoryInterface $taxCategoryRepository |
55
|
|
|
* @param RepositoryInterface $zoneRepository |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
FactoryInterface $taxRateFactory, |
59
|
|
|
FactoryInterface $taxCategoryFactory, |
60
|
|
|
RepositoryInterface $taxRateRepository, |
61
|
|
|
RepositoryInterface $taxCategoryRepository, |
62
|
|
|
RepositoryInterface $zoneRepository |
63
|
|
|
) { |
64
|
|
|
$this->taxRateFactory = $taxRateFactory; |
65
|
|
|
$this->taxCategoryFactory = $taxCategoryFactory; |
66
|
|
|
$this->taxRateRepository = $taxRateRepository; |
67
|
|
|
$this->taxCategoryRepository = $taxCategoryRepository; |
68
|
|
|
$this->zoneRepository = $zoneRepository; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @Transform /^"([^"]+)" tax category$/ |
73
|
|
|
* @Transform /^tax category "([^"]+)"$/ |
74
|
|
|
*/ |
75
|
|
|
public function getTaxCategoryByName($taxCategoryName) |
76
|
|
|
{ |
77
|
|
|
$taxCategory = $this->taxCategoryRepository->findOneBy(['name' => $taxCategoryName]); |
78
|
|
|
if (null === $taxCategory) { |
79
|
|
|
throw new \InvalidArgumentException('Tax category with name "'.$taxCategoryName.'" does not exist'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $taxCategory; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @Given store has :taxRateName tax rate of :taxRateAmount% for :taxCategoryName within :zone zone |
87
|
|
|
* @Given /^store has "([^"]+)" tax rate of ([^"]+)% for "([^"]+)" for (the rest of the world)$/ |
88
|
|
|
*/ |
89
|
|
|
public function storeHasTaxRateWithinZone($taxRateName, $taxRateAmount, $taxCategoryName, ZoneInterface $zone) |
90
|
|
|
{ |
91
|
|
|
$taxCategory = $this->getOrCreateTaxCategory($taxCategoryName); |
92
|
|
|
|
93
|
|
|
$taxRate = $this->taxRateFactory->createNew(); |
94
|
|
|
$taxRate->setName($taxRateName); |
95
|
|
|
$taxRate->setCode($this->getCodeFromNameAndZoneCode($taxRateName, $zone->getCode())); |
96
|
|
|
$taxRate->setZone($zone); |
97
|
|
|
$taxRate->setAmount($this->getAmountFromString($taxRateAmount)); |
98
|
|
|
$taxRate->setCategory($taxCategory); |
99
|
|
|
$taxRate->setCalculator('default'); |
100
|
|
|
|
101
|
|
|
$this->taxRateRepository->add($taxRate); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $taxCategoryName |
106
|
|
|
* |
107
|
|
|
* @return TaxCategoryInterface |
108
|
|
|
*/ |
109
|
|
|
private function getOrCreateTaxCategory($taxCategoryName) |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
return $this->getTaxCategoryByName($taxCategoryName); |
113
|
|
|
} catch (\InvalidArgumentException $exception) { |
114
|
|
|
return $this->createTaxCategory($taxCategoryName); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $taxCategoryName |
120
|
|
|
* |
121
|
|
|
* @return TaxCategoryInterface |
122
|
|
|
*/ |
123
|
|
|
private function createTaxCategory($taxCategoryName) |
124
|
|
|
{ |
125
|
|
|
$taxCategory = $this->taxCategoryFactory->createNew(); |
126
|
|
|
$taxCategory->setName($taxCategoryName); |
127
|
|
|
$taxCategory->setCode($this->getCodeFromName($taxCategoryName)); |
128
|
|
|
|
129
|
|
|
$this->taxCategoryRepository->add($taxCategory); |
130
|
|
|
|
131
|
|
|
return $taxCategory; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $taxRateAmount |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
private function getAmountFromString($taxRateAmount) |
140
|
|
|
{ |
141
|
|
|
return ((int) $taxRateAmount) / 100; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $taxRateName |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function getCodeFromName($taxRateName) |
150
|
|
|
{ |
151
|
|
|
return str_replace(' ', '_', strtolower($taxRateName)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $taxRateName |
156
|
|
|
* @param string $zoneCode |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function getCodeFromNameAndZoneCode($taxRateName, $zoneCode) |
161
|
|
|
{ |
162
|
|
|
return $this->getCodeFromName($taxRateName).'_'.strtolower($zoneCode); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|