123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * A PHP-GTK2 UI for a SOAP client to the EU
- * VAT Information Exchange System (VIES) database
- *
- * (c) 2007-2010 Frederic G. MARAND
- *
- * Licensed under the CeCILL 2.0 license
- * http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
- *
- * $Id: vies.phpw,v 1.2 2010-09-21 09:05:15 fgm Exp $
- */
- error_reporting(E_ALL|E_STRICT);
- /**
- * Straight from the WSDL schema
- */
- class checkVat {
- /**
- * @var string
- */
- var $countryCode;
- /**
- * @var string
- */
- var $vatNumber;
- function __construct($cc, $vat) {
- $this->countryCode = $cc;
- $this->vatNumber = $vat;
- }
- }
- class UI {
- /**
- * @var GladeXML
- */
- var $glade ;
- function __construct() {
- $this->glade = new GladeXML('vies.glade');
- $this->glade->signal_autoconnect_instance($this);
- }
- /**
- * Return a widget from the loaded Glade file
- *
- * @param GtkWidget $name
- */
- function get_widget($name) {
- $ret = $this->glade->get_widget($name);
- return $ret;
- }
- function on_quit_action() {
- Gtk::main_quit();
- }
- function on_pdc_about_activate() {
- $dl = $this->get_widget('dl_about');
- $dl->show();
- }
- function on_btn_clicked() {
- $sb = $this->get_widget('sb');
- $cc = $this->get_widget('cb_country');
- $cc = $cc->get_active_text();
- $cc = substr($cc, 0, 2);
- $text_buffer = new GtkTextBuffer();
- $text_buffer->set_text('');
- $vat = $this->get_widget('en_vat');
- $vat = $vat->get_text();
- /* var_dump($cc);
- var_dump($vat); */
- if (empty($cc) || empty($vat)) {
- $sb->push(0, 'You must choose a country and key in a VAT number.');
- yield();
- }
- else {
- $nb = $this->get_widget('nb');
- $nb->set_current_page(0);
- $sb->push(0, 'Invoking VIES web service. Please wait.');
- yield();
- $ret = checkVat($cc, $vat);
- $sb->pop(0);
- yield();
- if (!is_object($ret)) { // An error occurred
- $tv = $this->get_widget('tv');
- $text_buffer->set_text($ret);
- $tv->set_buffer($text_buffer);
- }
- else { // We got a result
- print_r($ret);
- $val = $this->get_widget('lbCountryCodeVal');
- $val->set_text($ret->countryCode);
- $val = $this->get_widget('lbVatNumberVal');
- $val->set_text($ret->vatNumber);
- $val = $this->get_widget('lbRequestDateVal');
- $val->set_text($ret->requestDate);
- $val = $this->get_widget('imgValid');
- if ($ret->valid) {
- $val->set_from_stock(Gtk::STOCK_YES, Gtk::ICON_SIZE_LARGE_TOOLBAR);
- }
- else {
- $val->set_from_stock(Gtk::STOCK_NO, Gtk::ICON_SIZE_LARGE_TOOLBAR);
- }
- $val = $this->get_widget('lbNameVal');
- $val->set_text(empty($ret->name) ? '(not returned)' : $ret->name);
- $val = $this->get_widget('lbAddressVal');
- $val->set_text(empty($ret->address) ? '(not returned)' : $ret->address);
- $nb->set_current_page(1);
- }
- yield();
- }
- }
- function run() {
- $w = $this->get_widget('w');
- $w->show_all();
- Gtk::main();
- }
- }
- function yield() {
- while (Gtk::events_pending()) {
- Gtk::main_iteration();
- }
- }
- /**
- * Make sure the code can proceed by checking requirements.
- *
- * - PHP version
- * - PHP-GTK extension
- * - SOAP extension
- *
- * @return void
- */
- function checkRequirements() {
- $ext = get_loaded_extensions();
- if (version_compare(PHP_VERSION, '5.0.0') < 0) {
- die('PHP >= 5.0 needed. Current version: '. PHP_VERSION);
- }
- if (!in_array('php-gtk', $ext)) {
- die('Missing PHP-GTK extension. Aborting.');
- }
- if (!in_array('soap', $ext)) {
- die('Missing SOAP extension. Aborting.');
- }
- }
- /**
- * Invoke the VIES service to check an EU VAT number
- *
- * @param string $cc Country Code
- * @param string $vat VAT number
- *
- * @return mixed
- */
- function checkVat($cc, $vat) {
- // 2007 URL
- // $wsdl = 'http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl';
- // 2010 URL
- $wsdl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
- $vies = new SoapClient($wsdl);
- /*
- var_dump($vies->__getFunctions());
- var_dump($vies->__getTypes());
- */
- $nii = new checkVat($cc, $vat);
- try {
- $ret = $vies->checkVat($nii);
- }
- catch (SoapFault $e) {
- $ret = $e->faultstring;
- $regex = '/\{ \'([A-Z_]*)\' \}/';
- $n = preg_match($regex, $ret, $matches);
- $ret = $matches[1];
- $faults = array
- (
- 'INVALID_INPUT' => 'The provided CountryCode is invalid or the VAT number is empty',
- 'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later',
- 'MS_UNAVAILABLE' => 'The Member State service is unavailable, try again later or with another Member State',
- 'TIMEOUT' => 'The Member State service could not be reached in time, try again later or with another Member State',
- 'SERVER_BUSY' => 'The service cannot process your request. Try again later.'
- );
- $ret = $faults[$ret];
- }
- return $ret;
- }
- checkRequirements();
- $ui = new UI();
- $ui->run();
|