1 | <?php |
||
10 | class VatCalculator |
||
11 | { |
||
12 | /** |
||
13 | * VAT Service check URL provided by the EU. |
||
14 | */ |
||
15 | const VAT_SERVICE_URL = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; |
||
16 | |||
17 | /** |
||
18 | * We're using the free ip2c service to lookup IP 2 country. |
||
19 | */ |
||
20 | const GEOCODE_SERVICE_URL = 'http://ip2c.org/'; |
||
21 | |||
22 | protected $soapClient; |
||
23 | |||
24 | /** |
||
25 | * All available tax rules. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $taxRules = [ |
||
30 | 'AT' => 0.20, |
||
31 | 'BE' => 0.21, |
||
32 | 'BG' => 0.20, |
||
33 | 'CY' => 0.19, |
||
34 | 'CZ' => 0.21, |
||
35 | 'DE' => 0.19, |
||
36 | 'DK' => 0.25, |
||
37 | 'EE' => 0.20, |
||
38 | 'EL' => 0.23, |
||
39 | 'ES' => 0.21, |
||
40 | 'FI' => 0.24, |
||
41 | 'FR' => 0.20, |
||
42 | 'GB' => 0.20, |
||
43 | 'GR' => 0.23, |
||
44 | 'IE' => 0.23, |
||
45 | 'IT' => 0.22, |
||
46 | 'HR' => 0.25, |
||
47 | 'HU' => 0.27, |
||
48 | 'LV' => 0.21, |
||
49 | 'LT' => 0.21, |
||
50 | 'LU' => 0.17, |
||
51 | 'MT' => 0.18, |
||
52 | 'NL' => 0.21, |
||
53 | 'NO' => 0.25, |
||
54 | 'PL' => 0.23, |
||
55 | 'PT' => 0.23, |
||
56 | 'RO' => 0.20, |
||
57 | 'SE' => 0.25, |
||
58 | 'SK' => 0.20, |
||
59 | 'SI' => 0.22, |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * @var float |
||
64 | */ |
||
65 | protected $netPrice = 0.0; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $countryCode; |
||
71 | |||
72 | /** |
||
73 | * @var Repository |
||
74 | */ |
||
75 | protected $config; |
||
76 | |||
77 | /** |
||
78 | * @var float |
||
79 | */ |
||
80 | protected $taxValue = 0; |
||
81 | |||
82 | /** |
||
83 | * @var float |
||
84 | */ |
||
85 | protected $taxRate = 0; |
||
86 | |||
87 | /** |
||
88 | * The calculate net + tax value. |
||
89 | * |
||
90 | * @var float |
||
91 | */ |
||
92 | protected $value = 0; |
||
93 | |||
94 | /** |
||
95 | * @var bool |
||
96 | */ |
||
97 | protected $company = false; |
||
98 | |||
99 | /** |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $businessCountryCode; |
||
103 | |||
104 | /** |
||
105 | * @param \Illuminate\Contracts\Config\Repository |
||
106 | */ |
||
107 | public function __construct($config = null) |
||
122 | |||
123 | /** |
||
124 | * Finds the client IP address. |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | private function getClientIP() |
||
140 | |||
141 | /** |
||
142 | * Returns the ISO 3166-1 alpha-2 two letter |
||
143 | * country code for the client IP. If the |
||
144 | * IP can't be resolved it returns false. |
||
145 | * |
||
146 | * @return bool|string |
||
147 | */ |
||
148 | public function getIPBasedCountry() |
||
163 | |||
164 | /** |
||
165 | * Determines if you need to collect VAT for the given country code. |
||
166 | * |
||
167 | * @param $countryCode |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function shouldCollectVAT($countryCode) |
||
177 | |||
178 | /** |
||
179 | * Calculate the VAT based on the net price, country code and indication if the |
||
180 | * customer is a company or not. |
||
181 | * |
||
182 | * @param int|float $netPrice The net price to use for the calculation |
||
183 | * @param null|string $countryCode The country code to use for the rate lookup |
||
184 | * @param null|bool $company |
||
185 | * |
||
186 | * @return float |
||
187 | */ |
||
188 | public function calculate($netPrice, $countryCode = null, $company = null) |
||
203 | |||
204 | /** |
||
205 | * @return float |
||
206 | */ |
||
207 | public function getNetPrice() |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getCountryCode() |
||
219 | |||
220 | /** |
||
221 | * @param mixed $countryCode |
||
222 | */ |
||
223 | public function setCountryCode($countryCode) |
||
227 | |||
228 | /** |
||
229 | * @return float |
||
230 | */ |
||
231 | public function getTaxRate() |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function isCompany() |
||
243 | |||
244 | /** |
||
245 | * @param bool $company |
||
246 | */ |
||
247 | public function setCompany($company) |
||
251 | |||
252 | /** |
||
253 | * @param string $businessCountryCode |
||
254 | */ |
||
255 | public function setBusinessCountryCode($businessCountryCode) |
||
259 | |||
260 | /** |
||
261 | * Returns the tax rate for the given country. |
||
262 | * |
||
263 | * @param string $countryCode |
||
264 | * @param bool|false $company |
||
265 | * |
||
266 | * @return float |
||
267 | */ |
||
268 | public function getTaxRateForCountry($countryCode, $company = false) |
||
280 | |||
281 | /** |
||
282 | * @return float |
||
283 | */ |
||
284 | public function getTaxValue() |
||
288 | |||
289 | /** |
||
290 | * @param $vatNumber |
||
291 | * |
||
292 | * @throws VATCheckUnavailableException |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function isValidVATNumber($vatNumber) |
||
317 | |||
318 | /** |
||
319 | * @param SoapClient $soapClient |
||
320 | */ |
||
321 | public function setSoapClient($soapClient) |
||
325 | } |
||
326 |
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: