|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\VatCalculator\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Routing\Controller as BaseController; |
|
7
|
|
|
use Illuminate\Support\Facades\Response; |
|
8
|
|
|
use Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException; |
|
9
|
|
|
use Mpociot\VatCalculator\VatCalculator; |
|
10
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
|
11
|
|
|
|
|
12
|
|
|
class Controller extends BaseController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var VatCalculator |
|
16
|
|
|
*/ |
|
17
|
|
|
private $calculator; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(ConfigRepository $configRepository) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->calculator = new \Mpociot\VatCalculator\VatCalculator($configRepository); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns the tax rate for the given country code and postal code. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Illuminate\Http\Response |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getTaxRateForLocation($countryCode = null, $postalCode = null) |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'tax_rate' => $this->calculator->getTaxRateForLocation($countryCode, $postalCode), |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the tax rate for the given country. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Illuminate\Http\Response |
|
42
|
|
|
*/ |
|
43
|
|
|
public function calculateGrossPrice(Request $request) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$request->has('netPrice')) { |
|
46
|
|
|
return Response::json([ |
|
47
|
|
|
'error' => "The 'netPrice' parameter is missing", |
|
48
|
|
|
], 422); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$valid_vat_id = null; |
|
52
|
|
|
$valid_company = false; |
|
53
|
|
|
if ($request->has('vat_number')) { |
|
54
|
|
|
$valid_company = $this->validateVATID($request->get('vat_number')); |
|
55
|
|
|
$valid_company = $valid_company['is_valid']; |
|
56
|
|
|
$valid_vat_id = $valid_company; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'gross_price' => $this->calculator->calculate($request->get('netPrice'), $request->get('country'), $request->get('postal_code'), $valid_company), |
|
61
|
|
|
'net_price' => $this->calculator->getNetPrice(), |
|
62
|
|
|
'tax_rate' => $this->calculator->getTaxRate(), |
|
63
|
|
|
'tax_value' => $this->calculator->getTaxValue(), |
|
64
|
|
|
'valid_vat_id' => $valid_vat_id, |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the tax rate for the given country. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Http\Response |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getCountryCode() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'country_code' => $this->calculator->getIPBasedCountry(), |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the tax rate for the given country. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $vat_id |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Illuminate\Http\Response |
|
88
|
|
|
*/ |
|
89
|
|
|
public function validateVATID($vat_id) |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
|
|
$isValid = $this->calculator->isValidVATNumber($vat_id); |
|
93
|
|
|
$message = ''; |
|
94
|
|
|
} catch (VATCheckUnavailableException $e) { |
|
95
|
|
|
$isValid = false; |
|
96
|
|
|
$message = $e->getMessage(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return [ |
|
100
|
|
|
'vat_id' => $vat_id, |
|
101
|
|
|
'is_valid' => $isValid, |
|
102
|
|
|
'message' => $message, |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|