@@ -136,7 +136,85 @@ function check_used_servers($field_name, $field_value, $validator)
136136 }
137137 }
138138
139+ function check_vat_id ($ field_name , $ field_value , $ validator ){
140+ global $ app , $ page ;
141+
142+ $ vatid = trim ($ field_value );
143+ if (isset ($ app ->remoting_lib ->primary_id )) {
144+ $ country = $ app ->remoting_lib ->dataRecord ['country ' ];
145+ } else {
146+ $ country = $ page ->dataRecord ['country ' ];
147+ }
148+
149+ // check if country is member of EU
150+ $ country_details = $ app ->db ->queryOneRecord ("SELECT * FROM country WHERE iso = ' " .$ country ."' " );
151+ if ($ country_details ['eu ' ] == 'y ' && $ vatid != '' ){
152+
153+ $ vatid = preg_replace ('/\s+/ ' , '' , $ vatid );
154+ $ vatid = str_replace (array ('. ' , '- ' , ', ' ), '' , $ vatid );
155+ $ cc = substr ($ vatid , 0 , 2 );
156+ $ vn = substr ($ vatid , 2 );
157+
158+ // Test if the country of the VAT-ID matches the country of the customer
159+ if ($ country != '' ){
160+ if (strtoupper ($ cc ) != $ country ){
161+ $ errmsg = $ validator ['errmsg ' ];
162+ if (isset ($ app ->tform ->wordbook [$ errmsg ])) {
163+ return $ app ->tform ->wordbook [$ errmsg ]."<br> \r\n" ;
164+ } else {
165+ return $ errmsg ."<br> \r\n" ;
166+ }
167+ }
168+ }
139169
170+ $ client = new SoapClient ("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl " );
171+
172+ if ($ client ){
173+ $ params = array ('countryCode ' => $ cc , 'vatNumber ' => $ vn );
174+ try {
175+ $ r = $ client ->checkVat ($ params );
176+ if ($ r ->valid == true ){
177+ } else {
178+ $ errmsg = $ validator ['errmsg ' ];
179+ if (isset ($ app ->tform ->wordbook [$ errmsg ])) {
180+ return $ app ->tform ->wordbook [$ errmsg ]."<br> \r\n" ;
181+ } else {
182+ return $ errmsg ."<br> \r\n" ;
183+ }
184+ }
185+
186+ // This foreach shows every single line of the returned information
187+ /*
188+ foreach($r as $k=>$prop){
189+ echo $k . ': ' . $prop;
190+ }
191+ */
192+
193+ } catch (SoapFault $ e ) {
194+ //echo 'Error, see message: '.$e->faultstring;
195+ switch ($ e ->faultstring ) {
196+ case 'INVALID_INPUT ' :
197+ $ errmsg = $ validator ['errmsg ' ];
198+ if (isset ($ app ->tform ->wordbook [$ errmsg ])) {
199+ return $ app ->tform ->wordbook [$ errmsg ]."<br> \r\n" ;
200+ } else {
201+ return $ errmsg ."<br> \r\n" ;
202+ }
203+ break ;
204+ // the following cases shouldn't be the user's fault, so we return no error
205+ case 'SERVICE_UNAVAILABLE ' :
206+ case 'MS_UNAVAILABLE ' :
207+ case 'TIMEOUT ' :
208+ case 'SERVER_BUSY ' :
209+ break ;
210+ }
211+ }
212+ } else {
213+ // Connection to host not possible, europe.eu down?
214+ // this shouldn't be the user's fault, so we return no error
215+ }
216+ }
217+ }
140218
141219
142220}
0 commit comments