vies.phpw 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * A PHP-GTK2 UI for a SOAP client to the EU
  4. * VAT Information Exchange System (VIES) database
  5. *
  6. * (c) 2007-2010 Frederic G. MARAND
  7. *
  8. * Licensed under the CeCILL 2.0 license
  9. * http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
  10. *
  11. * $Id: vies.phpw,v 1.2 2010-09-21 09:05:15 fgm Exp $
  12. */
  13. error_reporting(E_ALL|E_STRICT);
  14. /**
  15. * Straight from the WSDL schema
  16. */
  17. class checkVat {
  18. /**
  19. * @var string
  20. */
  21. var $countryCode;
  22. /**
  23. * @var string
  24. */
  25. var $vatNumber;
  26. function __construct($cc, $vat) {
  27. $this->countryCode = $cc;
  28. $this->vatNumber = $vat;
  29. }
  30. }
  31. class UI {
  32. /**
  33. * @var GladeXML
  34. */
  35. var $glade ;
  36. function __construct() {
  37. $this->glade = new GladeXML('vies.glade');
  38. $this->glade->signal_autoconnect_instance($this);
  39. }
  40. /**
  41. * Return a widget from the loaded Glade file
  42. *
  43. * @param GtkWidget $name
  44. */
  45. function get_widget($name) {
  46. $ret = $this->glade->get_widget($name);
  47. return $ret;
  48. }
  49. function on_quit_action() {
  50. Gtk::main_quit();
  51. }
  52. function on_pdc_about_activate() {
  53. $dl = $this->get_widget('dl_about');
  54. $dl->show();
  55. }
  56. function on_btn_clicked() {
  57. $sb = $this->get_widget('sb');
  58. $cc = $this->get_widget('cb_country');
  59. $cc = $cc->get_active_text();
  60. $cc = substr($cc, 0, 2);
  61. $text_buffer = new GtkTextBuffer();
  62. $text_buffer->set_text('');
  63. $vat = $this->get_widget('en_vat');
  64. $vat = $vat->get_text();
  65. /* var_dump($cc);
  66. var_dump($vat); */
  67. if (empty($cc) || empty($vat)) {
  68. $sb->push(0, 'You must choose a country and key in a VAT number.');
  69. yield();
  70. }
  71. else {
  72. $nb = $this->get_widget('nb');
  73. $nb->set_current_page(0);
  74. $sb->push(0, 'Invoking VIES web service. Please wait.');
  75. yield();
  76. $ret = checkVat($cc, $vat);
  77. $sb->pop(0);
  78. yield();
  79. if (!is_object($ret)) { // An error occurred
  80. $tv = $this->get_widget('tv');
  81. $text_buffer->set_text($ret);
  82. $tv->set_buffer($text_buffer);
  83. }
  84. else { // We got a result
  85. print_r($ret);
  86. $val = $this->get_widget('lbCountryCodeVal');
  87. $val->set_text($ret->countryCode);
  88. $val = $this->get_widget('lbVatNumberVal');
  89. $val->set_text($ret->vatNumber);
  90. $val = $this->get_widget('lbRequestDateVal');
  91. $val->set_text($ret->requestDate);
  92. $val = $this->get_widget('imgValid');
  93. if ($ret->valid) {
  94. $val->set_from_stock(Gtk::STOCK_YES, Gtk::ICON_SIZE_LARGE_TOOLBAR);
  95. }
  96. else {
  97. $val->set_from_stock(Gtk::STOCK_NO, Gtk::ICON_SIZE_LARGE_TOOLBAR);
  98. }
  99. $val = $this->get_widget('lbNameVal');
  100. $val->set_text(empty($ret->name) ? '(not returned)' : $ret->name);
  101. $val = $this->get_widget('lbAddressVal');
  102. $val->set_text(empty($ret->address) ? '(not returned)' : $ret->address);
  103. $nb->set_current_page(1);
  104. }
  105. yield();
  106. }
  107. }
  108. function run() {
  109. $w = $this->get_widget('w');
  110. $w->show_all();
  111. Gtk::main();
  112. }
  113. }
  114. function yield() {
  115. while (Gtk::events_pending()) {
  116. Gtk::main_iteration();
  117. }
  118. }
  119. /**
  120. * Make sure the code can proceed by checking requirements.
  121. *
  122. * - PHP version
  123. * - PHP-GTK extension
  124. * - SOAP extension
  125. *
  126. * @return void
  127. */
  128. function checkRequirements() {
  129. $ext = get_loaded_extensions();
  130. if (version_compare(PHP_VERSION, '5.0.0') < 0) {
  131. die('PHP >= 5.0 needed. Current version: '. PHP_VERSION);
  132. }
  133. if (!in_array('php-gtk', $ext)) {
  134. die('Missing PHP-GTK extension. Aborting.');
  135. }
  136. if (!in_array('soap', $ext)) {
  137. die('Missing SOAP extension. Aborting.');
  138. }
  139. }
  140. /**
  141. * Invoke the VIES service to check an EU VAT number
  142. *
  143. * @param string $cc Country Code
  144. * @param string $vat VAT number
  145. *
  146. * @return mixed
  147. */
  148. function checkVat($cc, $vat) {
  149. // 2007 URL
  150. // $wsdl = 'http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl';
  151. // 2010 URL
  152. $wsdl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
  153. $vies = new SoapClient($wsdl);
  154. /*
  155. var_dump($vies->__getFunctions());
  156. var_dump($vies->__getTypes());
  157. */
  158. $nii = new checkVat($cc, $vat);
  159. try {
  160. $ret = $vies->checkVat($nii);
  161. }
  162. catch (SoapFault $e) {
  163. $ret = $e->faultstring;
  164. $regex = '/\{ \'([A-Z_]*)\' \}/';
  165. $n = preg_match($regex, $ret, $matches);
  166. $ret = $matches[1];
  167. $faults = array
  168. (
  169. 'INVALID_INPUT' => 'The provided CountryCode is invalid or the VAT number is empty',
  170. 'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later',
  171. 'MS_UNAVAILABLE' => 'The Member State service is unavailable, try again later or with another Member State',
  172. 'TIMEOUT' => 'The Member State service could not be reached in time, try again later or with another Member State',
  173. 'SERVER_BUSY' => 'The service cannot process your request. Try again later.'
  174. );
  175. $ret = $faults[$ret];
  176. }
  177. return $ret;
  178. }
  179. checkRequirements();
  180. $ui = new UI();
  181. $ui->run();