From 12e1474515200ed1657075cab6f8f61cf01b3e2c Mon Sep 17 00:00:00 2001
From: Falko Timme <ft@falkotimme.com>
Date: Sat, 13 Dec 2014 16:16:55 -0500
Subject: [PATCH] - Added VAT ID check (for EU VAT IDs) to client/reseller form.

---
 interface/lib/classes/validate_client.inc.php |   78 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/interface/lib/classes/validate_client.inc.php b/interface/lib/classes/validate_client.inc.php
index 0f90a5b..198701b 100644
--- a/interface/lib/classes/validate_client.inc.php
+++ b/interface/lib/classes/validate_client.inc.php
@@ -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
+			}
+		}
+	}
 
 
 }

--
Gitblit v1.9.1