vies.phpw 4.3 KB

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