Till Brehm
2015-01-05 9ca1e3b8acc34e15ff4bdbdd4b61402da06b4227
interface/lib/classes/validate_client.inc.php
@@ -53,7 +53,7 @@
            }
         }
      } else {
         $num_rec = $app->db->queryOneRecord("SELECT count(*) as number FROM sys_user WHERE username = '".$app->db->quote($field_value)."' AND client_id != ".$client_id);
         $num_rec = $app->db->queryOneRecord("SELECT count(*) as number FROM sys_user WHERE username = '".$app->db->quote($field_value)."' AND client_id != ".$app->functions->intval($client_id));
         if($num_rec["number"] > 0) {
            $errmsg = $validator['errmsg'];
            if(isset($app->tform->wordbook[$errmsg])) {
@@ -136,7 +136,85 @@
      }
   }
   function check_vat_id ($field_name, $field_value, $validator){
      global $app, $page;
      $vatid = trim($field_value);
      if(isset($app->remoting_lib->primary_id)) {
         $country = $app->remoting_lib->dataRecord['country'];
      } else {
         $country = $page->dataRecord['country'];
      }
      // check if country is member of EU
      $country_details = $app->db->queryOneRecord("SELECT * FROM country WHERE iso = '".$country."'");
      if($country_details['eu'] == 'y' && $vatid != ''){
         $vatid = preg_replace('/\s+/', '', $vatid);
         $vatid = str_replace(array('.', '-', ','), '', $vatid);
         $cc = substr($vatid, 0, 2);
         $vn = substr($vatid, 2);
         // Test if the country of the VAT-ID matches the country of the customer
         if($country != ''){
            if(strtoupper($cc) != $country){
               $errmsg = $validator['errmsg'];
               if(isset($app->tform->wordbook[$errmsg])) {
                  return $app->tform->wordbook[$errmsg]."<br>\r\n";
               } else {
                  return $errmsg."<br>\r\n";
               }
            }
         }
         $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
         if($client){
            $params = array('countryCode' => $cc, 'vatNumber' => $vn);
            try{
               $r = $client->checkVat($params);
               if($r->valid == true){
               } else {
                  $errmsg = $validator['errmsg'];
                     if(isset($app->tform->wordbook[$errmsg])) {
                        return $app->tform->wordbook[$errmsg]."<br>\r\n";
                     } else {
                        return $errmsg."<br>\r\n";
                     }
               }
               // This foreach shows every single line of the returned information
               /*
               foreach($r as $k=>$prop){
                  echo $k . ': ' . $prop;
               }
               */
            } catch(SoapFault $e) {
               //echo 'Error, see message: '.$e->faultstring;
               switch ($e->faultstring) {
                  case 'INVALID_INPUT':
                     $errmsg = $validator['errmsg'];
                     if(isset($app->tform->wordbook[$errmsg])) {
                        return $app->tform->wordbook[$errmsg]."<br>\r\n";
                     } else {
                        return $errmsg."<br>\r\n";
                     }
                     break;
                  // the following cases shouldn't be the user's fault, so we return no error
                  case 'SERVICE_UNAVAILABLE':
                  case 'MS_UNAVAILABLE':
                  case 'TIMEOUT':
                  case 'SERVER_BUSY':
                     break;
               }
            }
         } else {
            // Connection to host not possible, europe.eu down?
            // this shouldn't be the user's fault, so we return no error
         }
      }
   }
}