1 | <?php |
||
9 | class VatCalculator |
||
10 | { |
||
11 | /** |
||
12 | * VAT Service check URL provided by the EU. |
||
13 | */ |
||
14 | const VAT_SERVICE_URL = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; |
||
15 | |||
16 | /** |
||
17 | * We're using the free ip2c service to lookup IP 2 country. |
||
18 | */ |
||
19 | const GEOCODE_SERVICE_URL = 'http://ip2c.org/'; |
||
20 | |||
21 | protected $soapClient; |
||
22 | |||
23 | /** |
||
24 | * All available tax rules. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $taxRules = [ |
||
29 | 'AT' => 0.20, |
||
30 | 'BE' => 0.21, |
||
31 | 'BG' => 0.20, |
||
32 | 'CY' => 0.19, |
||
33 | 'CZ' => 0.21, |
||
34 | 'DE' => 0.19, |
||
35 | 'DK' => 0.25, |
||
36 | 'EE' => 0.20, |
||
37 | 'EL' => 0.23, |
||
38 | 'ES' => 0.21, |
||
39 | 'FI' => 0.24, |
||
40 | 'FR' => 0.20, |
||
41 | 'GB' => 0.20, |
||
42 | 'GR' => 0.23, |
||
43 | 'IE' => 0.23, |
||
44 | 'IT' => 0.22, |
||
45 | 'HR' => 0.25, |
||
46 | 'HU' => 0.27, |
||
47 | 'LV' => 0.21, |
||
48 | 'LT' => 0.21, |
||
49 | 'LU' => 0.17, |
||
50 | 'MT' => 0.18, |
||
51 | 'NL' => 0.21, |
||
52 | 'NO' => 0.25, |
||
53 | 'PL' => 0.23, |
||
54 | 'PT' => 0.23, |
||
55 | 'RO' => 0.20, |
||
56 | 'SE' => 0.25, |
||
57 | 'SK' => 0.20, |
||
58 | 'SI' => 0.22, |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var float |
||
63 | */ |
||
64 | protected $netPrice = 0.0; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $countryCode; |
||
70 | |||
71 | /** |
||
72 | * @var Repository |
||
73 | */ |
||
74 | protected $config; |
||
75 | |||
76 | /** |
||
77 | * @var float |
||
78 | */ |
||
79 | protected $taxValue = 0; |
||
80 | |||
81 | /** |
||
82 | * @var float |
||
83 | */ |
||
84 | protected $taxRate = 0; |
||
85 | |||
86 | /** |
||
87 | * The calculate net + tax value. |
||
88 | * |
||
89 | * @var float |
||
90 | */ |
||
91 | protected $value = 0; |
||
92 | |||
93 | /** |
||
94 | * @var bool |
||
95 | */ |
||
96 | protected $company = false; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $businessCountryCode; |
||
102 | |||
103 | /** |
||
104 | * @param \Illuminate\Contracts\Config\Repository |
||
105 | */ |
||
106 | public function __construct($config = null) |
||
117 | |||
118 | /** |
||
119 | * Finds the client IP address. |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | private function getClientIP() |
||
135 | |||
136 | /** |
||
137 | * Returns the ISO 3166-1 alpha-2 two letter |
||
138 | * country code for the client IP. If the |
||
139 | * IP can't be resolved it returns false. |
||
140 | * |
||
141 | * @return bool|string |
||
142 | */ |
||
143 | public function getIPBasedCountry() |
||
158 | |||
159 | /** |
||
160 | * Calculate the VAT based on the net price, country code and indication if the |
||
161 | * customer is a company or not. |
||
162 | * |
||
163 | * @param int|float $netPrice The net price to use for the calculation |
||
164 | * @param null|string $countryCode The country code to use for the rate lookup |
||
165 | * @param null|bool $company |
||
166 | * |
||
167 | * @return float |
||
168 | */ |
||
169 | public function calculate($netPrice, $countryCode = null, $company = null) |
||
184 | |||
185 | /** |
||
186 | * @return float |
||
187 | */ |
||
188 | public function getNetPrice() |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getCountryCode() |
||
200 | |||
201 | /** |
||
202 | * @param mixed $countryCode |
||
203 | */ |
||
204 | public function setCountryCode($countryCode) |
||
208 | |||
209 | /** |
||
210 | * @return float |
||
211 | */ |
||
212 | public function getTaxRate() |
||
216 | |||
217 | /** |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function isCompany() |
||
224 | |||
225 | /** |
||
226 | * @param bool $company |
||
227 | */ |
||
228 | public function setCompany($company) |
||
232 | |||
233 | /** |
||
234 | * @param string $businessCountryCode |
||
235 | */ |
||
236 | public function setBusinessCountryCode($businessCountryCode) |
||
240 | |||
241 | /** |
||
242 | * Returns the tax rate for the given country. |
||
243 | * |
||
244 | * @param string $countryCode |
||
245 | * @param bool|false $company |
||
246 | * |
||
247 | * @return float |
||
248 | */ |
||
249 | public function getTaxRateForCountry($countryCode, $company = false) |
||
261 | |||
262 | /** |
||
263 | * @return float |
||
264 | */ |
||
265 | public function getTaxValue() |
||
269 | |||
270 | /** |
||
271 | * @param $vatNumber |
||
272 | * |
||
273 | * @throws VATCheckUnavailableException |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function isValidVATNumber($vatNumber) |
||
298 | |||
299 | /** |
||
300 | * @param SoapClient $soapClient |
||
301 | */ |
||
302 | public function setSoapClient($soapClient) |
||
306 | } |
||
307 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: