checkVatPopup.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/societe/checkvat/checkVatPopup.php
  19. * \ingroup societe
  20. * \brief Popup screen to validate VAT
  21. */
  22. if (! defined('NOTOKENRENEWAL')) {
  23. define('NOTOKENRENEWAL', '1');
  24. } // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  25. require "../../main.inc.php";
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  27. require_once NUSOAP_PATH.'/nusoap.php';
  28. $langs->load("companies");
  29. //http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
  30. $WS_DOL_URL = 'https://ec.europa.eu/taxation_customs/vies/services/checkVatService';
  31. //$WS_DOL_URL_WSDL=$WS_DOL_URL.'?wsdl';
  32. $WS_DOL_URL_WSDL = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
  33. $WS_METHOD = 'checkVat';
  34. $conf->dol_hide_topmenu = 1;
  35. $conf->dol_hide_leftmenu = 1;
  36. llxHeader('', $langs->trans("VATIntraCheckableOnEUSite"));
  37. print '<div class="vatcheckarea margintoponly marginbottomonly">';
  38. print load_fiche_titre($langs->trans("VATIntraCheckableOnEUSite"), '', 'title_setup');
  39. $vatNumber = GETPOST("vatNumber", 'alpha');
  40. if (!$vatNumber) {
  41. print '<br>';
  42. print '<span class="error">'.$langs->transnoentities("ErrorFieldRequired", $langs->trans("VATIntraShort")).'</span><br>';
  43. } else {
  44. $vatNumber = preg_replace('/\^\w/', '', $vatNumber);
  45. $vatNumber = str_replace(array(' ', '.'), '', $vatNumber);
  46. $countryCode = substr($vatNumber, 0, 2);
  47. $vatNumber = substr($vatNumber, 2);
  48. print '<b>'.$langs->trans("Country").'</b>: '.$countryCode.'<br>';
  49. print '<b>'.$langs->trans("VATIntraShort").'</b>: '.$vatNumber.'<br>';
  50. print '<br>';
  51. // Set the parameters to send to the WebService
  52. $parameters = array("countryCode" => $countryCode,
  53. "vatNumber" => $vatNumber);
  54. // Set the WebService URL
  55. dol_syslog("Create nusoap_client for URL=".$WS_DOL_URL." WSDL=".$WS_DOL_URL_WSDL);
  56. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  57. $params = getSoapParams();
  58. //ini_set('default_socket_timeout', $params['response_timeout']);
  59. //$soapclient = new SoapClient($WS_DOL_URL_WSDL,$params);
  60. $soapclient = new nusoap_client($WS_DOL_URL_WSDL, true, $params['proxy_host'], $params['proxy_port'], $params['proxy_login'], $params['proxy_password'], $params['connection_timeout'], $params['response_timeout']);
  61. $soapclient->soap_defencoding = 'utf-8';
  62. $soapclient->xml_encoding = 'utf-8';
  63. $soapclient->decode_utf8 = false;
  64. // Check for an error
  65. $err = $soapclient->getError();
  66. if ($err) {
  67. dol_syslog("Constructor error ".$WS_DOL_URL, LOG_ERR);
  68. }
  69. // Call the WebService and store its result in $result.
  70. dol_syslog("Call method ".$WS_METHOD);
  71. $result = $soapclient->call($WS_METHOD, $parameters);
  72. //var_dump($parameters);
  73. //var_dump($soapclient);
  74. //print "x".is_array($result)."i";
  75. //var_dump($result);
  76. //print $soapclient->request.'<br>';
  77. //print $soapclient->response.'<br>';
  78. $messagetoshow = '';
  79. print '<b>'.$langs->trans("Response").'</b>:<br>';
  80. // Service indisponible
  81. if (!is_array($result) || preg_match('/SERVICE_UNAVAILABLE/i', $result['faultstring'])) {
  82. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  83. $messagetoshow = $soapclient->response;
  84. } elseif (preg_match('/TIMEOUT/i', $result['faultstring'])) {
  85. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  86. $messagetoshow = $soapclient->response;
  87. } elseif (preg_match('/SERVER_BUSY/i', $result['faultstring'])) {
  88. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  89. $messagetoshow = $soapclient->response;
  90. } elseif ($result['faultstring']) {
  91. print '<span class="error">'.$langs->trans("Error").'</span><br>';
  92. $messagetoshow = $result['faultstring'];
  93. } elseif (preg_match('/INVALID_INPUT/i', $result['faultstring'])
  94. || ($result['requestDate'] && !$result['valid'])) {
  95. // Syntaxe ko
  96. if ($result['requestDate']) {
  97. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  98. }
  99. print $langs->trans("VATIntraSyntaxIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  100. print $langs->trans("ValueIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  101. //$messagetoshow=$soapclient->response;
  102. } else {
  103. // Syntaxe ok
  104. if ($result['requestDate']) {
  105. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  106. }
  107. print $langs->trans("VATIntraSyntaxIsValid").': <span class="ok">'.$langs->trans("Yes").'</span><br>';
  108. print $langs->trans("ValueIsValid").': ';
  109. if (preg_match('/MS_UNAVAILABLE/i', $result['faultstring'])) {
  110. print '<span class="error">'.$langs->trans("ErrorVATCheckMS_UNAVAILABLE", $countryCode).'</span><br>';
  111. } else {
  112. if (!empty($result['valid']) && ($result['valid'] == 1 || $result['valid'] == 'true')) {
  113. print '<span class="ok">'.$langs->trans("Yes").'</span>';
  114. print '<br>';
  115. print $langs->trans("Name").': '.$result['name'].'<br>';
  116. print $langs->trans("Address").': '.$result['address'].'<br>';
  117. } else {
  118. print '<span class="error">'.$langs->trans("No").'</span>';
  119. print '<br>'."\n";
  120. }
  121. }
  122. }
  123. // Show log data into page
  124. // print "\n";
  125. // print '<!-- ';
  126. // var_dump($result);
  127. // print '-->';
  128. }
  129. print '<br>';
  130. print $langs->trans("VATIntraManualCheck", $langs->trans("VATIntraCheckURL"), $langs->transnoentitiesnoconv("VATIntraCheckURL")).'<br>';
  131. print '<br>';
  132. print '<div class="center"><input type="button" class="button" value="'.$langs->trans("CloseWindow").'" onclick="window.close()"></div>';
  133. if ($messagetoshow) {
  134. print '<br><br>';
  135. print "\n".'Error returned:<br>';
  136. print nl2br($messagetoshow);
  137. }
  138. print '</div>';
  139. // End of page
  140. llxFooter();
  141. $db->close();