File: Vatid.php

package info (click to toggle)
php-horde 5.2.13%2Bdebian0-1%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,652 kB
  • sloc: php: 11,153; xml: 6,751; javascript: 5,560; sh: 92; makefile: 33; sql: 1
file content (137 lines) | stat: -rw-r--r-- 4,475 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
 * @package Horde
 */
class Horde_Block_Vatid extends Horde_Core_Block
{
    /**
     */
    public function __construct($app, $params = array())
    {
        parent::__construct($app, $params);

        $this->enabled = Horde_Util::loadExtension('soap');
        $this->_name = _("EU VAT identification");
    }

    /**
     */
    protected function _title()
    {
        return _("VAT id number verification");
    }

    /**
     */
    protected function _content()
    {
        global $page_output;

        $name = strval(new Horde_Support_Randomid());

        $page_output->addScriptFile('vatid.js', 'horde');
        $page_output->addInlineScript(array(
            '$("' . $name . '").observe("submit", HordeBlockVatid.onSubmit.bindAsEventListener(HordeBlockVatid))'
        ), true);

        return '<form style="padding:2px" action="' .
            $this->_ajaxUpdateUrl() . '" id="' . $name . '">' .
            Horde_Util::formInput() .
            Horde::label('vatid', _("VAT identification number:")) .
            '<br /><input type="text" length="14" name="vatid" />' .
            '<br /><input type="submit" id="vatbutton" value="' . _("Check") .
            '" class="horde-default" /> ' .
            Horde_Themes_Image::tag('loading.gif', array(
                'alt' => _("Checking"),
                'attr' => array('style' => 'display:none')
            )) .
            '<div class="vatidResults"></div>' .
            '</form>';
    }

    /**
     */
    protected function _ajaxUpdate(Horde_Variables $vars)
    {
        $html = '';
        $vatid = str_replace(' ', '', $vars->vatid);

        if (empty($vatid) ||
            !preg_match('/^([A-Z]{2})([0-9A-Za-z\+\*\.]{2,12})$/', $vatid, $matches)) {
            return '<br />' . $this->_error(_("Invalid VAT identification number format."));
        }

        if (empty($matches)) {
            return;
        }

        try {
            $client = new SoapClient(
                'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl',
                array('exceptions' => true));
            $result = $client->checkVat(array(
                'countryCode' => $matches[1],
                'vatNumber' => $matches[2]
            ));

            if ($result->valid) {
                $html .= '<span style="color:green;font-weight:bold">'
                    . _("This VAT identification number is valid.")
                    . '</span><br />';
            } else {
                $html .= $this->_error(_("This VAT identification number is invalid.")) . '<br />';
            }

            $html .= '<em>' . _("Country") . ':</em> '
                . $result->countryCode . '<br /><em>'
                . _("VAT number") . ':</em> ' . $result->vatNumber
                . '<br /><em>' . _("Date") . ':</em> '
                . strftime($GLOBALS['prefs']->getValue('date_format'), strtotime($result->requestDate))
                . '<br />';

            if (!empty($result->name)) {
                $html .= '<em>' . _("Name") . ':</em> ' . $result->name . '<br />';
            }

            if (!empty($result->address)) {
                $html .= '<em>' . _("Address") . ':</em> ' . $result->address . '<br />';
            }
        } catch (SoapFault $e) {
            $error = $e->getMessage();

            switch (true) {
            case strpos($error, 'INVALID_INPUT'):
                $error = _("The provided country code is invalid.");
                break;

            case strpos($error, 'SERVICE_UNAVAILABLE'):
                $error = _("The service is currently not available. Try again later.");
                break;

            case strpos($error, 'MS_UNAVAILABLE'):
                $error = _("The member state service is currently not available. Try again later or with a different member state.");
                break;

            case strpos($error, 'TIMEOUT'):
                $error = _("The member state service could not be reached in time. Try again later or with a different member state.");
                break;

            case strpos($error, 'SERVER_BUSY'):
                $error = _("The service is currently too busy. Try again later.");
                break;
            }

            $html .= $this->_error($error);
        }

        return $html;
    }

    /**
     */
    private function _error($text)
    {
        return '<span style="color:red;font-weight:bold">' . $text . '</span>';
    }

}