|
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
|
|
|
|
|
11
|
|
|
class Controller extends BaseController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var VatCalculator |
|
15
|
|
|
*/ |
|
16
|
|
|
private $calculator; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(VatCalculator $calculator) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->calculator = $calculator; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Returns the tax rate for the given country code and postal code. |
|
25
|
|
|
* |
|
26
|
|
|
* @return \Illuminate\Http\Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getTaxRateForLocation($countryCode = null, $postalCode = null) |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
'tax_rate' => $this->calculator->getTaxRateForLocation($countryCode, $postalCode), |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the tax rate for the given country. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* |
|
40
|
|
|
* @return \Illuminate\Http\Response |
|
41
|
|
|
*/ |
|
42
|
|
|
public function calculateGrossPrice(Request $request) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$request->has('netPrice')) { |
|
45
|
|
|
return Response::json([ |
|
46
|
|
|
'error' => "The 'netPrice' parameter is missing", |
|
47
|
|
|
], 422); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$valid_vat_id = null; |
|
51
|
|
|
$valid_company = false; |
|
52
|
|
|
if ($request->has('vat_number')) { |
|
53
|
|
|
$valid_company = $this->validateVATID($request->get('vat_number')); |
|
54
|
|
|
$valid_company = $valid_company['is_valid']; |
|
55
|
|
|
$valid_vat_id = $valid_company; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return [ |
|
|
|
|
|
|
59
|
|
|
'gross_price' => $this->calculator->calculate($request->get('netPrice'), $request->get('country'), $request->get('postal_code'), $valid_company), |
|
60
|
|
|
'net_price' => $this->calculator->getNetPrice(), |
|
61
|
|
|
'tax_rate' => $this->calculator->getTaxRate(), |
|
62
|
|
|
'tax_value' => $this->calculator->getTaxValue(), |
|
63
|
|
|
'valid_vat_id' => $valid_vat_id, |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the tax rate for the given country. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Illuminate\Http\Response |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getCountryCode() |
|
73
|
|
|
{ |
|
74
|
|
|
return [ |
|
75
|
|
|
'country_code' => $this->calculator->getIPBasedCountry(), |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the tax rate for the given country. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $vat_id |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Http\Response |
|
87
|
|
|
*/ |
|
88
|
|
|
public function validateVATID($vat_id) |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
|
|
$isValid = $this->calculator->isValidVATNumber($vat_id); |
|
92
|
|
|
$message = ''; |
|
93
|
|
|
} catch (VATCheckUnavailableException $e) { |
|
94
|
|
|
$isValid = false; |
|
95
|
|
|
$message = $e->getMessage(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return [ |
|
99
|
|
|
'vat_id' => $vat_id, |
|
100
|
|
|
'is_valid' => $isValid, |
|
101
|
|
|
'message' => $message, |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.