1: <?php
2: 3: 4:
5: class Horde_Block_Vatid extends Horde_Core_Block
6: {
7: 8:
9: public function __construct($app, $params = array())
10: {
11: parent::__construct($app, $params);
12:
13: $this->enabled = class_exists('SOAP_Client');
14: $this->_name = _("EU VAT identification");
15: }
16:
17: 18:
19: protected function _title()
20: {
21: return _("VAT id number verification");
22: }
23:
24: 25:
26: protected function _content()
27: {
28: $name = strval(new Horde_Support_Randomid());
29:
30: Horde::addScriptFile('vatid.js', 'horde');
31: Horde::addInlineScript(
32: '$("' . $name . '").observe("submit", HordeBlockVatid.onSubmit.bindAsEventListener(HordeBlockVatid))',
33: 'dom'
34: );
35:
36: return '<form style="padding:2px" action="' .
37: $this->_ajaxUpdateUrl() . '" id="' . $name . '">' .
38: Horde_Util::formInput() .
39: Horde::label('vatid', _("VAT identification number:")) .
40: '<br /><input type="text" length="14" name="vatid" />' .
41: '<br /><input type="submit" id="vatbutton" value="' . _("Check") .
42: '" class="button" /> ' .
43: Horde::img('loading.gif', _("Checking"), array('style' => 'display:none')) .
44: '<div class="vatidResults"></div>' .
45: '</form>';
46: }
47:
48: 49:
50: protected function _ajaxUpdate($vars)
51: {
52: $html = '';
53: $vatid = str_replace(' ', '', $vars->vatid);
54:
55: if (empty($vatid) ||
56: !preg_match('/^([A-Z]{2})([0-9A-Za-z\+\*\.]{2,12})$/', $vatid, $matches)) {
57: return '<br />' . $this->_error(_("Invalid VAT identification number format."));
58: }
59:
60: if (empty($matches)) {
61: return;
62: }
63:
64: $client = new SOAP_Client('http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl', true, false, array(), Horde::getTempDir());
65: $params = array(
66: 'countryCode' => $matches[1],
67: 'vatNumber' => $matches[2]
68: );
69: $result = $client->call('checkVat', $params);
70: if ($result instanceof SOAP_Fault) {
71: $error = $result->getMessage();
72:
73: switch (true) {
74: case strpos($error, 'INVALID_INPUT'):
75: $error = _("The provided country code is invalid.");
76: break;
77:
78: case strpos($error, 'SERVICE_UNAVAILABLE'):
79: $error = _("The service is currently not available. Try again later.");
80: break;
81:
82: case strpos($error, 'MS_UNAVAILABLE'):
83: $error = _("The member state service is currently not available. Try again later or with a different member state.");
84: break;
85:
86: case strpos($error, 'TIMEOUT'):
87: $error = _("The member state service could not be reached in time. Try again later or with a different member state.");
88: break;
89:
90: case strpos($error, 'SERVER_BUSY'):
91: $error = _("The service is currently too busy. Try again later.");
92: break;
93: }
94:
95: $html .= $this->_error($error);
96: } else {
97: if ($result['valid']) {
98: $html .= '<span style="color:green;font-weight:bold">'
99: . _("This VAT identification number is valid.")
100: . '</span><br />';
101: } else {
102: $html .= $this->_error(_("This VAT identification number is invalid.")) . '<br />';
103: }
104:
105: $html .= '<em>' . _("Country") . ':</em> '
106: . $result['countryCode'] . '<br /><em>'
107: . _("VAT number") . ':</em> ' . $result['vatNumber']
108: . '<br /><em>' . _("Date") . ':</em> '
109: . strftime($GLOBALS['prefs']->getValue('date_format'), strtotime($result['requestDate']))
110: . '<br />';
111:
112: if (!empty($result['name'])) {
113: $html .= '<em>' . _("Name") . ':</em> ' . $result['name'] . '<br />';
114: }
115:
116: if (!empty($result['address'])) {
117: $html .= '<em>' . _("Address") . ':</em> ' . $result['address'] . '<br />';
118: }
119: }
120:
121: return $html;
122: }
123:
124: 125:
126: private function _error($text)
127: {
128: return '<span style="color:red;font-weight:bold">' . $text . '</span>';
129: }
130:
131: }
132: