checkVatPopup.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
  23. require "../../main.inc.php";
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  25. require_once NUSOAP_PATH.'/nusoap.php';
  26. $langs->load("companies");
  27. //http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
  28. $WS_DOL_URL = 'https://ec.europa.eu/taxation_customs/vies/services/checkVatService';
  29. //$WS_DOL_URL_WSDL=$WS_DOL_URL.'?wsdl';
  30. $WS_DOL_URL_WSDL = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
  31. $WS_METHOD = 'checkVat';
  32. $conf->dol_hide_topmenu = 1;
  33. $conf->dol_hide_leftmenu = 1;
  34. llxHeader('', $langs->trans("VATIntraCheckableOnEUSite"));
  35. print '<div class="vatcheckarea margintoponly marginbottomonly">';
  36. print load_fiche_titre($langs->trans("VATIntraCheckableOnEUSite"), '', 'title_setup');
  37. $vatNumber = GETPOST("vatNumber", 'alpha');
  38. if (!$vatNumber) {
  39. print '<br>';
  40. print '<span class="error">'.$langs->transnoentities("ErrorFieldRequired", $langs->trans("VATIntraShort")).'</span><br>';
  41. } else {
  42. $vatNumber = preg_replace('/\^\w/', '', $vatNumber);
  43. $vatNumber = str_replace(array(' ', '.'), '', $vatNumber);
  44. $countryCode = substr($vatNumber, 0, 2);
  45. $vatNumber = substr($vatNumber, 2);
  46. print '<b>'.$langs->trans("Country").'</b>: '.$countryCode.'<br>';
  47. print '<b>'.$langs->trans("VATIntraShort").'</b>: '.$vatNumber.'<br>';
  48. print '<br>';
  49. // Set the parameters to send to the WebService
  50. $parameters = array("countryCode" => $countryCode,
  51. "vatNumber" => $vatNumber);
  52. // Set the WebService URL
  53. dol_syslog("Create nusoap_client for URL=".$WS_DOL_URL." WSDL=".$WS_DOL_URL_WSDL);
  54. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  55. $params = getSoapParams();
  56. //ini_set('default_socket_timeout', $params['response_timeout']);
  57. //$soapclient = new SoapClient($WS_DOL_URL_WSDL,$params);
  58. $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']);
  59. $soapclient->soap_defencoding = 'utf-8';
  60. $soapclient->xml_encoding = 'utf-8';
  61. $soapclient->decode_utf8 = false;
  62. // Check for an error
  63. $err = $soapclient->getError();
  64. if ($err) {
  65. dol_syslog("Constructor error ".$WS_DOL_URL, LOG_ERR);
  66. }
  67. // Call the WebService and store its result in $result.
  68. dol_syslog("Call method ".$WS_METHOD);
  69. $result = $soapclient->call($WS_METHOD, $parameters);
  70. //var_dump($parameters);
  71. //var_dump($soapclient);
  72. //print "x".is_array($result)."i";
  73. //var_dump($result);
  74. //print $soapclient->request.'<br>';
  75. //print $soapclient->response.'<br>';
  76. $messagetoshow = '';
  77. print '<b>'.$langs->trans("Response").'</b>:<br>';
  78. // Service indisponible
  79. if (!is_array($result) || preg_match('/SERVICE_UNAVAILABLE/i', $result['faultstring'])) {
  80. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  81. $messagetoshow = $soapclient->response;
  82. } elseif (preg_match('/TIMEOUT/i', $result['faultstring'])) {
  83. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  84. $messagetoshow = $soapclient->response;
  85. } elseif (preg_match('/SERVER_BUSY/i', $result['faultstring'])) {
  86. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  87. $messagetoshow = $soapclient->response;
  88. } elseif ($result['faultstring']) {
  89. print '<span class="error">'.$langs->trans("Error").'</span><br>';
  90. $messagetoshow = $result['faultstring'];
  91. } elseif (preg_match('/INVALID_INPUT/i', $result['faultstring'])
  92. || ($result['requestDate'] && !$result['valid'])) {
  93. // Syntaxe ko
  94. if ($result['requestDate']) {
  95. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  96. }
  97. print $langs->trans("VATIntraSyntaxIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  98. print $langs->trans("ValueIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  99. //$messagetoshow=$soapclient->response;
  100. } else {
  101. // Syntaxe ok
  102. if ($result['requestDate']) {
  103. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  104. }
  105. print $langs->trans("VATIntraSyntaxIsValid").': <span class="ok">'.$langs->trans("Yes").'</span><br>';
  106. print $langs->trans("ValueIsValid").': ';
  107. if (preg_match('/MS_UNAVAILABLE/i', $result['faultstring'])) {
  108. print '<span class="error">'.$langs->trans("ErrorVATCheckMS_UNAVAILABLE", $countryCode).'</span><br>';
  109. } else {
  110. if (!empty($result['valid']) && ($result['valid'] == 1 || $result['valid'] == 'true')) {
  111. print '<span class="ok">'.$langs->trans("Yes").'</span>';
  112. print '<br>';
  113. print $langs->trans("Name").': '.$result['name'].'<br>';
  114. print $langs->trans("Address").': '.$result['address'].'<br>';
  115. } else {
  116. print '<span class="error">'.$langs->trans("No").'</span>';
  117. print '<br>'."\n";
  118. }
  119. }
  120. }
  121. // Show log data into page
  122. // print "\n";
  123. // print '<!-- ';
  124. // var_dump($result);
  125. // print '-->';
  126. }
  127. print '<br>';
  128. print $langs->trans("VATIntraManualCheck", $langs->trans("VATIntraCheckURL"), $langs->transnoentitiesnoconv("VATIntraCheckURL")).'<br>';
  129. print '<br>';
  130. print '<div class="center"><input type="button" class="button" value="'.$langs->trans("CloseWindow").'" onclick="javascript: window.close()"></div>';
  131. if ($messagetoshow) {
  132. print '<br><br>';
  133. print "\n".'Error returned:<br>';
  134. print nl2br($messagetoshow);
  135. }
  136. print '</div>';
  137. // End of page
  138. llxFooter();
  139. $db->close();