checkVatPopup.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // Added by MMI Mathieu Moulin iProspective
  27. $hookmanager->initHooks(array('checkvatpopup'));
  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 = !empty($conf->global->VAT_INTRA_CHECK_VIES_WS_METHOD) ?$conf->global->VAT_INTRA_CHECK_VIES_WS_METHOD :'checkVat';
  34. //$WS_METHOD = 'checkVatApprox';
  35. $conf->dol_hide_topmenu = 1;
  36. $conf->dol_hide_leftmenu = 1;
  37. llxHeader('', $langs->trans("VATIntraCheckableOnEUSite"));
  38. print '<div class="vatcheckarea margintoponly marginbottomonly">';
  39. print load_fiche_titre($langs->trans("VATIntraCheckableOnEUSite"), '', 'title_setup');
  40. $vatNumber = GETPOST("vatNumber", 'alpha');
  41. if (!$vatNumber) {
  42. print '<br>';
  43. print '<span class="error">'.$langs->transnoentities("ErrorFieldRequired", $langs->trans("VATIntraShort")).'</span><br>';
  44. } else {
  45. $vatNumber = preg_replace('/\^\w/', '', $vatNumber);
  46. $vatNumber = str_replace(array(' ', '.'), '', $vatNumber);
  47. $countryCode = substr($vatNumber, 0, 2);
  48. $vatNumber = substr($vatNumber, 2);
  49. print '<b>'.$langs->trans("Country").'</b>: '.$countryCode.'<br>';
  50. print '<b>'.$langs->trans("VATIntraShort").'</b>: '.$vatNumber.'<br>';
  51. print '<br>';
  52. // Set the parameters to send to the WebService
  53. $parameters = array("countryCode" => $countryCode,
  54. "vatNumber" => $vatNumber);
  55. if ($WS_METHOD == 'checkVatApprox') {
  56. $parameters['requesterCountryCode'] = substr($mysoc->tva_intra, 0, 2);
  57. $parameters['requesterVatNumber'] = substr($mysoc->tva_intra, 2);
  58. }
  59. // Set the WebService URL
  60. dol_syslog("Create nusoap_client for URL=".$WS_DOL_URL." WSDL=".$WS_DOL_URL_WSDL);
  61. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  62. $params = getSoapParams();
  63. //ini_set('default_socket_timeout', $params['response_timeout']);
  64. //$soapclient = new SoapClient($WS_DOL_URL_WSDL,$params);
  65. $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']);
  66. $soapclient->soap_defencoding = 'utf-8';
  67. $soapclient->xml_encoding = 'utf-8';
  68. $soapclient->decode_utf8 = false;
  69. // Check for an error
  70. $err = $soapclient->getError();
  71. if ($err) {
  72. dol_syslog("Constructor error ".$WS_DOL_URL, LOG_ERR);
  73. }
  74. // Call the WebService and store its result in $result.
  75. dol_syslog("Call method ".$WS_METHOD);
  76. $result = $soapclient->call($WS_METHOD, $parameters);
  77. //var_dump($parameters);
  78. //var_dump($soapclient);
  79. //print "x".is_array($result)."i";
  80. //var_dump($result);
  81. //print $soapclient->request.'<br>';
  82. //print $soapclient->response.'<br>';
  83. $parameters = [
  84. 'WS_METHOD' => $WS_METHOD,
  85. 'parameters' => $parameters,
  86. 'result' => &$result,
  87. 'soapclient' => $soapclient,
  88. ];
  89. $reshook = $hookmanager->executeHooks('checkVatResult', $parameters);
  90. if ($reshook < 0) {
  91. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  92. }
  93. $messagetoshow = '';
  94. print '<b>'.$langs->trans("Response").'</b>:<br>';
  95. // Service indisponible
  96. if (!is_array($result) || preg_match('/SERVICE_UNAVAILABLE/i', $result['faultstring'])) {
  97. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  98. $messagetoshow = $soapclient->response;
  99. } elseif (preg_match('/TIMEOUT/i', $result['faultstring'])) {
  100. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  101. $messagetoshow = $soapclient->response;
  102. } elseif (preg_match('/SERVER_BUSY/i', $result['faultstring'])) {
  103. print '<span class="error">'.$langs->trans("ErrorServiceUnavailableTryLater").'</span><br>';
  104. $messagetoshow = $soapclient->response;
  105. } elseif ($result['faultstring']) {
  106. print '<span class="error">'.$langs->trans("Error").'</span><br>';
  107. $messagetoshow = $result['faultstring'];
  108. } elseif (preg_match('/INVALID_INPUT/i', $result['faultstring'])
  109. || ($result['requestDate'] && !$result['valid'])) {
  110. // Syntaxe ko
  111. if ($result['requestDate']) {
  112. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  113. }
  114. print $langs->trans("VATIntraSyntaxIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  115. print $langs->trans("ValueIsValid").': <span class="error">'.$langs->trans("No").'</span> (Might be a non europeen VAT)<br>';
  116. //$messagetoshow=$soapclient->response;
  117. } else {
  118. // Syntaxe ok
  119. if ($result['requestDate']) {
  120. print $langs->trans("Date").': '.$result['requestDate'].'<br>';
  121. }
  122. print $langs->trans("VATIntraSyntaxIsValid").': <span class="ok">'.$langs->trans("Yes").'</span><br>';
  123. print $langs->trans("ValueIsValid").': ';
  124. if (preg_match('/MS_UNAVAILABLE/i', $result['faultstring'])) {
  125. print '<span class="error">'.$langs->trans("ErrorVATCheckMS_UNAVAILABLE", $countryCode).'</span><br>';
  126. } else {
  127. if (!empty($result['valid']) && ($result['valid'] == 1 || $result['valid'] == 'true')) {
  128. print '<span class="ok">'.$langs->trans("Yes").'</span>';
  129. print '<br>';
  130. print $langs->trans("Name").': '.$result['name'].'<br>';
  131. print $langs->trans("Address").': '.$result['address'].'<br>';
  132. $reshook = $hookmanager->executeHooks('checkVatDisplay', $parameters);
  133. if ($reshook < 0) {
  134. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  135. }
  136. } else {
  137. print '<span class="error">'.$langs->trans("No").'</span>';
  138. print '<br>'."\n";
  139. }
  140. }
  141. }
  142. // Show log data into page
  143. // print "\n";
  144. // print '<!-- ';
  145. // var_dump($result);
  146. // print '-->';
  147. }
  148. print '<br>';
  149. print $langs->trans("VATIntraManualCheck", $langs->trans("VATIntraCheckURL"), $langs->transnoentitiesnoconv("VATIntraCheckURL")).'<br>';
  150. print '<br>';
  151. print '<div class="center"><input type="button" class="button" value="'.$langs->trans("CloseWindow").'" onclick="javascript: window.close()"></div>';
  152. if ($messagetoshow) {
  153. print '<br><br>';
  154. print "\n".'Error returned:<br>';
  155. print nl2br($messagetoshow);
  156. }
  157. print '</div>';
  158. // End of page
  159. llxFooter();
  160. $db->close();