Projekt

Allgemein

Profil

GX-Bug #66613 » vat_validation.php

Anonym, 21.04.2020 11:02

 
1
<?php
2
/* --------------------------------------------------------------
3
   vat_validation.php 2019-03-19
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2019 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10

    
11

    
12
   based on: 
13
   (c) 2010 xtcModified - community made shopping  http://www.xtc-modified.org ($Id: vat_validation.php 2211 2011-09-13 12:45:15Z dokuman $)
14
   (c) 2005 xtc_validate_vatid_status.inc.php 899 2005-04-29
15
   (c) 2003 XT-Commerce - community made shopping http://www.xt-commerce.com ($Id: vat_validation.php 1283 2005-10-05 15:42:48Z mz $)
16

    
17
   Released under the GNU General Public License 
18
   ---------------------------------------------------------------------------------------*/
19

    
20
require_once(DIR_FS_CATALOG . 'gm/classes/lib/nusoap.php');
21

    
22
class vat_validation_ORIGIN {
23
	var $vat_info;
24
	
25
	public function __construct($vat_id = '', $customers_id = '', $customers_status = '', $country_id = '', $guest = false) {
26
		$this->vat_info = array ();
27
		$this->live_check = ACCOUNT_COMPANY_VAT_LIVE_CHECK;
28
		if (xtc_not_null($vat_id)) {
29
			$this->getInfo($vat_id, $customers_id, $customers_status, $country_id, $guest);
30
		} else {
31
			if ($guest) {
32
				$this->vat_info = array ('status' => DEFAULT_CUSTOMERS_STATUS_ID_GUEST);
33
			} else {
34
				$this->vat_info = array ('status' => DEFAULT_CUSTOMERS_STATUS_ID);
35
			}
36
		}
37
	}
38

    
39

    
40
	public function reset($vat_id = '', $customers_id = '', $customers_status = '', $country_id = '', $guest = false)
41
	{
42
		$this->vat_info   = array();
43
		$this->live_check = ACCOUNT_COMPANY_VAT_LIVE_CHECK;
44
		if(xtc_not_null($vat_id))
45
		{
46
			$this->getInfo($vat_id, $customers_id, $customers_status, $country_id, $guest);
47
		}
48
		else
49
		{
50
			if($guest)
51
			{
52
				$this->vat_info = array('status' => DEFAULT_CUSTOMERS_STATUS_ID_GUEST);
53
			}
54
			else
55
			{
56
				$this->vat_info = array('status' => DEFAULT_CUSTOMERS_STATUS_ID);
57
			}
58
		}
59
	}
60

    
61

    
62
	public function getVatInfo()
63
	{
64
		return $this->vat_info;
65
	}
66

    
67

    
68
	function getInfo($vat_id = '', $customers_id = '', $customers_status = '', $country_id = '', $guest = false) {
69

    
70
		if (!$guest) {
71
			if ($vat_id) {
72
				$validate_vatid = $this->validate_vatid($vat_id);
73

    
74
				switch ($validate_vatid) {
75

    
76
					case '0' :
77
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
78
							$error = true;
79
						}
80
						$status = DEFAULT_CUSTOMERS_STATUS_ID;
81
						$vat_id_status = '0';
82
						break;
83

    
84
					case '1' :
85
						if ($country_id == STORE_COUNTRY) {
86
							if (ACCOUNT_COMPANY_VAT_GROUP == 'true') {
87
								$status = DEFAULT_CUSTOMERS_VAT_STATUS_ID_LOCAL;
88
							} else {
89
								$status = DEFAULT_CUSTOMERS_STATUS_ID;
90
							}
91
						} else {
92
							if (ACCOUNT_COMPANY_VAT_GROUP == 'true') {
93
								$status = DEFAULT_CUSTOMERS_VAT_STATUS_ID;
94
							} else {
95
								$status = DEFAULT_CUSTOMERS_STATUS_ID;
96
							}
97
						}
98
						$error = false;
99
						$vat_id_status = '1';
100
						break;
101

    
102
					case '8' :
103
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
104
							$error = true;
105
						}
106
						$status = DEFAULT_CUSTOMERS_STATUS_ID;
107
						$vat_id_status = '8';
108
						break;
109

    
110
					case '9' :
111
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
112
							$error = true;
113
						}
114
						$status = DEFAULT_CUSTOMERS_STATUS_ID;
115
						$vat_id_status = '9';
116
						break;
117

    
118
					case '99' :
119
					case '98' :
120
					case '97' :
121
					case '96' :
122
					case '95' :
123
					case '94' :
124
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
125
						$error = true;
126
						}
127
						$status = DEFAULT_CUSTOMERS_STATUS_ID;
128
						$vat_id_status = $validate_vatid;
129
						break;	
130
						
131
					default :
132
						$status = DEFAULT_CUSTOMERS_STATUS_ID;
133

    
134
				}
135

    
136
			} else {
137
				if ($customers_status) {
138
					$status = $customers_status;
139
				} else {
140
					$status = DEFAULT_CUSTOMERS_STATUS_ID;
141
				}
142
				$vat_id_status = '';
143
				$error = false;
144
			}
145

    
146
		} else {
147
			if ($vat_id) {
148
				$validate_vatid = $this->validate_vatid($vat_id);
149

    
150
				switch ($validate_vatid) {
151

    
152
					case '0' :
153
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
154
							$error = true;
155
						}
156
						$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
157
						$vat_id_status = '0';
158
						break;
159

    
160
					case '1' :
161
						if ($country_id == STORE_COUNTRY) {
162
							if (ACCOUNT_COMPANY_VAT_GROUP == 'true') {
163
								$status = DEFAULT_CUSTOMERS_VAT_STATUS_ID_LOCAL;
164
							} else {
165
								$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
166
							}
167
						} else {
168
							if (ACCOUNT_COMPANY_VAT_GROUP == 'true') {
169
								$status = DEFAULT_CUSTOMERS_VAT_STATUS_ID;
170
							} else {
171
								$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
172
							}
173
						}
174
						$error = false;
175
						$vat_id_status = '1';
176
						break;
177

    
178
					case '8' :
179
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
180
							$error = true;
181
						}
182
						$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
183
						$vat_id_status = '8';
184

    
185
						break;
186

    
187
					case '9' :
188
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
189
							$error = true;
190
						}
191
						$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
192
						$vat_id_status = '9';
193
						break;
194
						
195
					case '99' :
196
					case '98' :
197
					case '97' :
198
					case '96' :
199
					case '95' :
200
					case '94' :
201
						if (ACCOUNT_VAT_BLOCK_ERROR == 'true') {
202
						$error = true;
203
						}
204
						$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
205
						$vat_id_status = $validate_vatid;
206
						break;
207

    
208
					default :
209
						$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
210

    
211
				}
212

    
213
			} else {
214
				if ($customers_status) {
215
					$status = $customers_status;
216
				} else {
217
					$status = DEFAULT_CUSTOMERS_STATUS_ID_GUEST;
218
				}
219
				$vat_id_status = '';
220
				$error = false;
221
			}
222
		}
223

    
224
		if ($customers_id) {
225
			$customers_status_query = xtc_db_query("SELECT customers_status FROM ".TABLE_CUSTOMERS." WHERE customers_id = '".(int)$customers_id."'");
226
			$customers_status_value = xtc_db_fetch_array($customers_status_query);
227

    
228
			if ($customers_status_value['customers_status'] != 0) {
229
				$status = $status;
230
			} else {
231
				$status = $customers_status_value['customers_status'];
232
			}
233
		}
234

    
235
		$this->vat_info = array ('status' => $status, 'vat_id_status' => $vat_id_status, 'error' => $error, 'validate' => $validate_vatid);
236

    
237
	}
238

    
239

    
240
	//BOF - DokuMan - 2011-08-24 - check UstID live via SOAP at http://ec.europa.eu
241
	function validate_vatid($vat_id) {
242

    
243
		$remove = array (' ', '-', '/', '\\', '.', ':', ',');
244
		// 0 = 'invalid'
245
		// 1 = 'valid'
246
		// 8 = 'unknown country'
247
		// 9 => 'unknown algorithm'
248
		//94 = 'INVALID_INPUT'       => 'The provided CountryCode is invalid or the VAT number is empty',
249
		//95 = 'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later',
250
		//96 = 'MS_UNAVAILABLE'      => 'The Member State service is unavailable, try again later or with another Member State',
251
		//97 = 'TIMEOUT'             => 'The Member State service could not be reached in time, try again later or with another Member State',
252
		//98 = 'SERVER_BUSY'         => 'The service cannot process your request. Try again later.'
253
		//99 = 'no PHP5 SOAP support'
254
		$results = array (0 => '0',
255
							1 => '1',
256
							8 => '8',
257
							9 => '9',
258
							94 => '94',
259
							95 => '95',
260
							96 => '96',
261
							97 => '97',
262
							98 => '98',
263
							99 => '99');
264

    
265
		// sonderzeichen entfernen
266
		for ($i = 0; $i < count($remove); $i ++) {
267
			$vat_id = str_replace($remove[$i], '', $vat_id);
268
		} // end for($i = 0; $i < count($remove)); $i++)
269

    
270
		// land bestimmen
271
		$country = strtolower(substr($vat_id, 0, 2));
272

    
273
		$t_result = 8; //unknown country
274

    
275
		if($this->live_check == 'true') {
276
			$country_iso_code = strtoupper($country);
277

    
278
			$t_result = $this->checkVatID_EU($vat_id, $country_iso_code);
279

    
280
		} else {
281
			$vat_id = trim(chop($vat_id));
282
			$t_result = $this->gm_validate_vatid($country, $vat_id);
283
		}
284
		
285
		return $results[$t_result];
286
	}
287

    
288
	function checkVatID_EU($vat_id, $country_id) {
289
		static $vat_validation;
290
		
291
		if($vat_validation === null)
292
		{
293
			$vat_validation = array();
294
		}
295
		
296
		$key = $vat_id . '-' . $country_id;
297
		
298
		if(array_key_exists($key, $vat_validation))
299
		{
300
			return $vat_validation[$key];
301
		}
302
		
303
		// Leerzeichen und sonderzeichen entfernen
304
		$remove = array (' ', '-', '/', '\\', '.', ':', ',');
305
		$vat_id = trim(chop($vat_id));
306
		$vat_id = str_replace($remove, '', $vat_id );
307
		$vatNumber = substr($vat_id, 2); // alles ab 2 Stellen der VAT (ohne Land)
308

    
309
		$coo_soap_client = new nusoap_client('https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', true);
310
		$coo_soap_client->soap_defencoding = 'UTF-8';
311
		$coo_soap_proxy = $coo_soap_client->getProxy();
312

    
313
		// check connection
314
		if($coo_soap_client->getError() || !is_object($coo_soap_proxy))
315
		{
316
			LogControl::get_instance()->error('Connection to https://ec.europa.eu/ could not be established.', 'widgets', 'vat_validation_errors', 'error', 'USER ERROR', 0, print_r($coo_soap_client->getError(), true));
317
		}
318
		else
319
		{
320
			$params = array('countryCode' => $country_id, 'vatNumber' => $vatNumber);
321
			
322
			$result = $coo_soap_proxy->checkVat($params);
323
			
324
			if(is_array($result) && isset($result['valid']) && $result['valid'] == 'true')
325
			{
326
				$vat_validation[$key] = 1;
327
				return 1; //valid VAT
328
			}
329
			elseif(is_array($result) && isset($result['valid']) && $result['valid'] == 'false')
330
			{
331
				$vat_validation[$key] = 0;
332
				return 0; //invalid VAT
333
			}
334
			elseif(is_array($result) && isset($result['faultstring']))
335
			{
336
				switch($result['faultstring'])
337
				{
338
					case 'INVALID_INPUT':
339
						$t_error_code = '94';
340
						break;
341
					case 'SERVICE_UNAVAILABLE':
342
						$t_error_code = '95';
343
						break;
344
					case 'MS_UNAVAILABLE':
345
						$t_error_code = '96';
346
						break;
347
					case 'TIMEOUT':
348
						$t_error_code = '97';
349
						break;
350
					case 'SERVER_BUSY':
351
						$t_error_code = '98';
352
						break;
353
					default:
354
						$t_error_code = '94';
355
				}
356
				
357
				$vat_validation[$key] = $t_error_code;
358
				return $t_error_code;				
359
			}
360
			
361
			$vat_validation[$key] = false;
362
			return false;
363
		}
364

    
365
	} // end checkVatID_EU
366
	//EOF - DokuMan - 2011-08-24 - check UstID live via SOAP at http://ec.europa.eu
367

    
368
	
369
	function gm_validate_vatid($country, $vat_id) {
370
		switch ($country) {
371
			default:
372
				return 8;
373
			break;
374
			// oesterreich
375
			case 'at' :
376
				if (strlen($vat_id) != 11 && strtoupper($vat_id[2]) != 'U') {
377
					return 0;
378
				}
379
				
380
				$number = substr(str_replace($country, '', strtolower($vat_id)), 1);
381
				
382
				if(strlen($number) == 8 && is_numeric($number)) {
383
					return 1;
384
				} else {
385
					return 0;
386
				}
387
			break;
388

    
389
			// belgien
390
			case 'be' :
391
				if (strlen($vat_id) != 12) {
392
					return 0;
393
				}
394
				
395
				$number = str_replace($country, '', strtolower($vat_id));
396
				
397
				if(strlen($number) == 10 && is_numeric($number)) {
398
					return 1;
399
				} else {
400
					return 0;
401
				}
402
			break;
403

    
404
			// bulgarien
405
			case 'bg' :
406

    
407
				$number = str_replace($country, '', strtolower($vat_id));
408
				
409
				if(strlen($vat_id) == 11) {
410
					if(strlen($number) == 9 && is_numeric($number)) {
411
						return 1;
412
					} else {
413
						return 0;
414
					}
415
				} elseif(strlen($vat_id) == 12) {
416
					if(strlen($number) == 10 && is_numeric($number)) {
417
						return 1;
418
					} else {
419
						return 0;
420
					}
421
				} else {
422
					return 0;
423
				}
424

    
425

    
426
			break;
427

    
428
			// zypern
429
			case 'cy' :
430
				if (strlen($vat_id) != 11) {
431
					return 0;
432
				}		
433
				
434
				$number = str_replace($country, '', strtolower($vat_id));		
435
				
436
				if(strlen($number) == 9) {
437
					return 1;
438
				} else {
439
					return 0;
440
				}
441
			break;
442

    
443
			// tschechische republik
444
			case 'cz' :
445
				$number = str_replace($country, '', strtolower($vat_id));		
446
				if(strlen($vat_id) == 10) {
447
					if(strlen($number) == 8 && is_numeric($number)) {
448
						return 1;
449
					} else {
450
						return 0;
451
					}
452
				} elseif(strlen($vat_id) == 11) {
453
					if(strlen($number) == 9 && is_numeric($number)) {
454
						return 1;
455
					} else {
456
						return 0;
457
					}
458
				} elseif(strlen($vat_id) == 12) {
459
					if(strlen($number) == 10 && is_numeric($number)) {
460
						return 1;
461
					} else {
462
						return 0;
463
					}
464
				} else {
465
					return 0;
466
				}
467

    
468
			break;
469

    
470
			// deutschland
471
			case 'de' :
472
				$number = str_replace($country, '', strtolower($vat_id));		
473
				if(strlen($vat_id) == 11) {
474
					if(strlen($number) == 9 && is_numeric($number)) {
475
						return 1;
476
					} else {
477
						return 0;
478
					}
479
				} else {
480
					return 0;
481
				}
482

    
483
			break;
484

    
485
			// d�nemark
486
			case 'dk' :
487
				$number = str_replace($country, '', strtolower($vat_id));		
488
				if(strlen($vat_id) == 10) {
489
					if(strlen($number) == 8 && is_numeric($number)) {
490
						return 1;
491
					} else {
492
						return 0;
493
					}
494
				} else {
495
					return 0;
496
				}
497
			break;
498

    
499
			// estland
500
			case 'ee' :
501
				$number = str_replace($country, '', strtolower($vat_id));		
502
				if(strlen($vat_id) == 11) {
503
					if(strlen($number) == 9 && is_numeric($number)) {
504
						return 1;
505
					} else {
506
						return 0;
507
					}
508
				} else {
509
					return 0;
510
				}
511
			break;
512

    
513
			// griechenland
514
			case 'el' :
515
				$number = str_replace($country, '', strtolower($vat_id));		
516
				if(strlen($vat_id) == 11) {
517
					if(strlen($number) == 9 && is_numeric($number)) {
518
						return 1;
519
					} else {
520
						return 0;
521
					}
522
				} else {
523
					return 0;
524
				}
525
			break;
526

    
527
			// spanien
528
			case 'es' :
529
				$number = str_replace($country, '', strtolower($vat_id));		
530
				if(strlen($vat_id) == 11) {
531
					if(strlen($number) == 9) {
532
						return 1;
533
					} else {
534
						return 0;
535
					}
536
				} else {
537
					return 0;
538
				}
539
			break;
540

    
541
			// finnland
542
			case 'fi' :
543
				$number = str_replace($country, '', strtolower($vat_id));		
544
				if(strlen($vat_id) == 10) {
545
					if(strlen($number) == 8 && is_numeric($number)) {
546
						return 1;
547
					} else {
548
						return 0;
549
					}
550
				} else {
551
					return 0;
552
				}
553
			break;
554

    
555
			// frankreich
556
			case 'fr' :
557
				$number = substr(str_replace($country, '', strtolower($vat_id)),2);		
558
				if(strlen($vat_id) == 13) {
559
					if(strlen($number) == 9 && is_numeric($number)) {
560
						return 1;
561
					} else {
562
						return 0;
563
					}
564
				} else {
565
					return 0;
566
				}
567
			break;
568
			
569
			// england
570
			case 'gb' :
571
				$number = str_replace($country, '', strtolower($vat_id));		
572
				if(strlen($vat_id) == 11) {
573
					if(strlen($number) == 9) {
574
						return 1;
575
					} else {
576
						return 0;
577
					}
578
				} elseif(strlen($vat_id) == 14) {
579
					if(strlen($number) == 12) {
580
						return 1;
581
					} else {
582
						return 0;
583
					}
584
				} else {
585
					return 0;
586
				}
587
			break;
588

    
589
			// ungarn
590
			case 'hu' :
591
				$number = str_replace($country, '', strtolower($vat_id));		
592
				if(strlen($vat_id) == 10) {
593
					if(strlen($number) == 8 && is_numeric($number)) {
594
						return 1;
595
					} else {
596
						return 0;
597
					}
598
				} else {
599
					return 0;
600
				}
601
			break;
602

    
603
			// irland
604
			case 'ie' :
605
				$number = str_replace($country, '', strtolower($vat_id));		
606
				if(strlen($vat_id) == 10) {
607
					if(strlen($number) == 8) {
608
						return 1;
609
					} else {
610
						return 0;
611
					}
612
				} else {
613
					return 0;
614
				}
615
			break;
616

    
617
			// italien
618
			case 'it' :
619
				$number = str_replace($country, '', strtolower($vat_id));		
620
				if(strlen($vat_id) == 13) {
621
					if(strlen($number) == 11 && is_numeric($number)) {
622
						return 1;
623
					} else {
624
						return 0;
625
					}
626
				} else {
627
					return 0;
628
				}
629
			break;
630

    
631
			// litauen
632
			case 'lt' :
633
				$number = str_replace($country, '', strtolower($vat_id));		
634
				if(strlen($vat_id) == 11) {
635
					if(strlen($number) == 9 && is_numeric($number)) {
636
						return 1;
637
					} else {
638
						return 0;
639
					}
640
				} elseif(strlen($vat_id) == 14) {
641
					if(strlen($number) == 12 && is_numeric($number)) {
642
						return 1;
643
					} else {
644
						return 0;
645
					}
646
				} else {
647
					return 0;
648
				}
649
			break;
650

    
651
			// luxemburg
652
			case 'lu' :
653
				$number = str_replace($country, '', strtolower($vat_id));		
654
				if(strlen($vat_id) == 10) {
655
					if(strlen($number) == 8 && is_numeric($number)) {
656
						return 1;
657
					} else {
658
						return 0;
659
					}
660
				} else {
661
					return 0;
662
				}
663
			break;
664
	
665
			// lettland
666
			case 'lv' :
667
				$number = str_replace($country, '', strtolower($vat_id));		
668
				if(strlen($vat_id) == 13) {
669
					if(strlen($number) == 11 && is_numeric($number)) {
670
						return 1;
671
					} else {
672
						return 0;
673
					}
674
				} else {
675
					return 0;
676
				}
677
			break;
678
	
679
			// malta
680
			case 'mt' :
681
				$number = str_replace($country, '', strtolower($vat_id));		
682
				if(strlen($vat_id) == 10) {
683
					if(strlen($number) == 8 && is_numeric($number)) {
684
						return 1;
685
					} else {
686
						return 0;
687
					}
688
				} else {
689
					return 0;
690
				}
691
	
692
			// niederlande
693
			case 'nl' :
694
				$number = str_replace($country, '', strtolower($vat_id));		
695
				if(strlen($vat_id) == 14) {
696
					if(strlen($number) == 12) {
697
						return 1;
698
					} else {
699
						return 0;
700
					}
701
				} else {
702
					return 0;
703
				}
704
			break;
705
	
706
			// polen
707
			case 'pl' :
708
				$number = str_replace($country, '', strtolower($vat_id));		
709
				if(strlen($vat_id) == 12) {
710
					if(strlen($number) == 10 && is_numeric($number)) {
711
						return 1;
712
					} else {
713
						return 0;
714
					}
715
				} else {
716
					return 0;
717
				}
718
			break;
719
	
720
			// portugal
721
			case 'pt' :
722
				$number = str_replace($country, '', strtolower($vat_id));		
723
				if(strlen($vat_id) == 11) {
724
					if(strlen($number) == 9 && is_numeric($number)) {
725
						return 1;
726
					} else {
727
						return 0;
728
					}
729
				} else {
730
					return 0;
731
				}
732
			break;
733
	
734
			// rum�nien
735
			case 'ro' :
736
				$number = str_replace($country, '', strtolower($vat_id));			
737

    
738
				if(strlen($vat_id) > 1 && strlen($vat_id) < 11) {
739
					if(is_numeric($number)) {
740
						return 1;
741
					} else {
742
						return 0;
743
					}
744
				} else {
745
					return 0;
746
				}
747
			break;
748
	
749
			// schweden
750
			case 'se' :
751
				$number = str_replace($country, '', strtolower($vat_id));		
752
				if(strlen($vat_id) == 14) {
753
					if(strlen($number) == 12 && is_numeric($number)) {
754
						return 1;
755
					} else {
756
						return 0;
757
					}
758
				} else {
759
					return 0;
760
				}
761
			break;
762
	
763
			// slowenien
764
			case 'si' :
765
				$number = str_replace($country, '', strtolower($vat_id));		
766
				if(strlen($vat_id) == 10) {
767
					if(strlen($number) == 8 && is_numeric($number)) {
768
						return 1;
769
					} else {
770
						return 0;
771
					}
772
				} else {
773
					return 0;
774
				}
775
			break;
776
	
777
			// slowakei
778
			case 'sk' :
779
				$number = str_replace($country, '', strtolower($vat_id));		
780
				if(strlen($vat_id) == 12) {
781
					if(strlen($number) == 10 && is_numeric($number)) {
782
						return 1;
783
					} else {
784
						return 0;
785
					}
786
				} else {
787
					return 0;
788
				}
789
			break;
790
	
791
		}		
792
	}
793

    
794
	/********************************************************************
795
	* landesabhaengige Hilfsfunktionen zur Berechnung                   *
796
	********************************************************************/
797

    
798
	// Canada
799
	function checkVatID_c($vat_id) {
800
		if (strlen($vat_id) != 10)
801
			return 0;
802

    
803
		// LUHN-10 code http://www.ee.unb.ca/tervo/ee4253/luhn.html
804

    
805
		$id = substr($vat_id, 1);
806
		$checksum = 0;
807
		for ($i = 9; $i > 0; $i --) {
808
			$digit = $vat_id {
809
				$i};
810
			if ($i % 2 == 1)
811
				$digit *= 2;
812
			if ($digit >= 10) {
813
				$checksum += $digit -10 + 1;
814
			} else {
815
				$checksum += $digit;
816
			}
817
		}
818
		if ($this->modulo($checksum, 10) == 0)
819
			return 1;
820

    
821
		return 0;
822
	} // Canada
823

    
824
	// belgien
825
	function checkVatID_be($vat_id) {
826
		if (strlen($vat_id) != 11)
827
			return 0;
828

    
829
		$checkvals = (int) substr($vat_id, 2, -2);
830
		$checksum = (int) substr($vat_id, -2);
831

    
832
		if (97 - $this->modulo($checkvals, 97) != $checksum)
833
			return 0;
834

    
835
		return 1;
836
	} // end belgien
837

    
838
	// daenemark
839
	function checkVatID_dk($vat_id) {
840
		if (strlen($vat_id) != 10)
841
			return 0;
842

    
843
		$weights = array (2, 7, 6, 5, 4, 3, 2, 1);
844
		$checksum = 0;
845

    
846
		for ($i = 0; $i < 8; $i ++)
847
			$checksum += (int) $vat_id[$i +2] * $weights[$i];
848
		if ($this->modulo($checksum, 11) > 0)
849
			return 0;
850

    
851
		return 1;
852
	} // end daenemark
853

    
854
	// deutschland
855
	function checkVatID_de($vat_id) {
856
		if (strlen($vat_id) != 11)
857
			return 0;
858

    
859
		$prod = 10;
860
		$checkval = 0;
861
		$checksum = (int) substr($vat_id, -1);
862

    
863
		for ($i = 2; $i < 10; $i ++) {
864
			$checkval = $this->modulo((int) $vat_id[$i] + $prod, 10);
865
			if ($checkval == 0)
866
				$checkval = 10;
867
			$prod = $this->modulo($checkval * 2, 11);
868
		} // end for($i = 2; $i < 10; $i++)
869
		$prod = $prod == 1 ? 11 : $prod;
870
		if (11 - $prod != $checksum)
871
			return 0;
872

    
873
		return 1;
874
	} // end deutschland
875

    
876
	// estland
877
	function checkVatID_ee($vat_id) {
878

    
879
		if (strlen($vat_id) != 11)
880
			return 0;
881
		if (!is_numeric(substr($vat_id, 2)))
882
			return 0;
883

    
884
		if ($this->live_check == 'true') {
885

    
886
			return $this->live($vat_id);
887

    
888
		} else {
889
			return 9; // es gibt keinen algorithmus
890
		}
891
	} // end estland
892

    
893
	// finnland
894
	function checkVatID_fi($vat_id) {
895
		if (strlen($vat_id) != 10)
896
			return 0;
897

    
898
		$weights = array (7, 9, 10, 5, 8, 4, 2);
899
		$checkval = 0;
900
		$checksum = (int) substr($vat_id, -1);
901

    
902
		for ($i = 0; $i < 8; $i ++)
903
			$checkval += (int) $vat_id[$i +2] * $weights[$i];
904

    
905
		if (11 - $this->modulo($checkval, 11) != $checksum)
906
			return 0;
907

    
908
		return 1;
909
	} // end finnland
910

    
911
	// frankreich
912
	function checkVatID_fr($vat_id) {
913
		if (strlen($vat_id) != 13)
914
			return 0;
915
		if (!is_numeric(substr($vat_id), 4))
916
			return 0;
917

    
918
		if ($this->live_check == 'true') {
919

    
920
			return $this->live($vat_id);
921

    
922
		} else {
923
			return 9; // es gibt keinen algorithmus
924
		}
925

    
926
	} // end frankreich
927

    
928
	// griechenland
929
	function checkVatID_el($vat_id) {
930
		if (strlen($vat_id) != 11)
931
			return 0;
932

    
933
		$checksum = substr($vat_id, -1);
934
		$checkval = 0;
935

    
936
		for ($i = 1; $i <= 8; $i ++)
937
			$checkval += (int) $vat_id[10 - $i] * pow(2, $i);
938
		$checkval = $this->modulo($checkval, 11) > 9 ? 0 : $this->modulo($checkval, 11);
939
		if ($checkval != $checksum)
940
			return 0;
941

    
942
		return 1;
943
	} // end griechenland
944

    
945
	// grossbrittanien
946
	function checkVatID_gb($vat_id) {
947
		if (strlen($vat_id) != 11 && strlen($vat_id) != 14)
948
			return 0;
949
		if (!is_numeric(substr($vat_id, 2)))
950
			return 0;
951

    
952
		if ($this->live_check == 'true') {
953

    
954
			return $this->live($vat_id);
955

    
956
		} else {
957
			return 9; // es gibt keinen algorithmus
958
		}
959

    
960
	} // end grossbrittanien
961

    
962
	/********************************************
963
	* irland                                    *
964
	********************************************/
965
	// irland switch
966
	function checkVatID_ie($vat_id) {
967
		if (strlen($vat_id) != 10)
968
			return 0;
969
		if (!checkVatID_ie_new($vat_id) && !checkVatID_ie_old($vat_id))
970
			return 0;
971

    
972
		return 1;
973
	} // end irland switch
974

    
975
	// irland alte methode
976
	function checkVatID_ie_old($vat_id) {
977
		// in neue form umwandeln
978
		$transform = array (substr($vat_id, 0, 2), '0', substr($vat_id, 4, 5), $vat_id[2], $vat_id[9]);
979
		$vat_id = join('', $transform);
980

    
981
		// nach neuer form pruefen
982
		return checkVatID_ie_new($vat_id);
983
	} // end irland alte methode
984

    
985
	// irland neue methode
986
	function checkVatID_ie_new($vat_id) {
987
		$checksum = strtoupper(substr($vat_id, -1));
988
		$checkval = 0;
989
		$checkchar = 'A';
990
		for ($i = 2; $i <= 8; $i ++)
991
			$checkval += (int) $vat_id[10 - $i] * $i;
992
		$checkval = $this->modulo($checkval, 23);
993
		if ($checkval == 0) {
994
			$checkchar = 'W';
995
		} else {
996
			for ($i = $checkval -1; $i > 0; $i --)
997
				$checkchar ++;
998
		}
999
		if ($checkchar != $checksum)
1000
			return false;
1001

    
1002
		return true;
1003
	} // end irland neue methode
1004
	/* end irland
1005
	********************************************/
1006

    
1007
	// italien
1008
	function checkVatID_it($vat_id) {
1009
		if (strlen($vat_id) != 13)
1010
			return 0;
1011

    
1012
		$checksum = (int) substr($vat_id, -1);
1013
		$checkval = 0;
1014
		for ($i = 0; $i <= 9; $i ++)
1015
			//echo $vat_id[11-$i];
1016
			$checkval += (int) $vat_id[11 - $i] * ($this->is_even($i) ? 2 : 1);
1017
		if ($checksum != $this->modulo($checkval, 10))
1018
			return 0;
1019

    
1020
		return 1;
1021
	} // end italien
1022

    
1023
	// lettland
1024
	function checkVatID_lv($vat_id) {
1025

    
1026
		if (strlen($vat_id) != 13)
1027
			return 0;
1028
		if (!is_numeric(substr($vat_id, 2)))
1029
			return 0;
1030

    
1031
		if ($this->live_check == 'true') {
1032

    
1033
			return $this->live($vat_id);
1034

    
1035
		} else {
1036
			return 9; // es gibt keinen algorithmus
1037
		}
1038
	} // end lettland
1039

    
1040
	// litauen
1041
	function checkVatID_lt($vat_id) {
1042

    
1043
		if ((strlen($vat_id) != 13) || (strlen($vat_id) != 11))
1044
			return 0;
1045
		if (!is_numeric(substr($vat_id, 2)))
1046
			return 0;
1047

    
1048
		if ($this->live_check == 'true') {
1049

    
1050
			return $this->live($vat_id);
1051

    
1052
		} else {
1053
			return 9; // es gibt keinen algorithmus
1054
		}
1055
	} // end litauen
1056

    
1057
	// luxemburg
1058
	function checkVatID_lu($vat_id) {
1059
		if (strlen($vat_id) != 10)
1060
			return 0;
1061

    
1062
		$checksum = (int) substr($vat_id, -2);
1063
		$checkval = (int) substr($vat_id, 2, 6);
1064
		if ($this->modulo($checkval, 89) != $checksum)
1065
			return 0;
1066

    
1067
		return 1;
1068
	} // luxemburg
1069

    
1070
	// malta
1071
	function checkVatID_mt($vat_id) {
1072

    
1073
		if (strlen($vat_id) != 10)
1074
			return 0;
1075
		if (!is_numeric(substr($vat_id, 2)))
1076
			return 0;
1077

    
1078
		if ($this->live_check == 'true') {
1079

    
1080
			return $this->live($vat_id);
1081

    
1082
		} else {
1083
			return 9; // es gibt keinen algorithmus
1084
		}
1085
	} // end malta
1086

    
1087
	// niederlande
1088
	function checkVatID_nl($vat_id) {
1089
		if (strlen($vat_id) != 14)
1090
			return 0;
1091
		if (strtoupper($vat_id[11]) != 'B')
1092
			return 0;
1093
		if ((int) $vat_id[12] == 0 || (int) $vat_id[13] == 0)
1094
			return 0;
1095

    
1096
		$checksum = (int) $vat_id[10];
1097
		$checkval = 0;
1098

    
1099
		for ($i = 2; $i <= 9; $i ++)
1100
			$checkval += (int) $vat_id[11 - $i] * $i;
1101
		$checkval = $this->modulo($checkval, 11) > 9 ? 0 : $this->modulo($checkval, 11);
1102

    
1103
		if ($checkval != $checksum)
1104
			return 0;
1105

    
1106
		return 1;
1107
	} // end niederlande
1108

    
1109
	// oesterreich
1110
	function checkVatID_at($vat_id) {
1111
		if (strlen($vat_id) != 11)
1112
			return 0;
1113
		if (strtoupper($vat_id[2]) != 'U')
1114
			return 0;
1115

    
1116
		$checksum = (int) $vat_id[10];
1117
		$checkval = 0;
1118

    
1119
		for ($i = 3; $i < 10; $i ++)
1120
			$checkval += $this->cross_summa((int) $vat_id[$i] * ($this->is_even($i) ? 2 : 1));
1121
		$checkval = substr((string) (96 - $checkval), -1);
1122

    
1123
		if ($checksum != $checkval)
1124
			return 0;
1125

    
1126
		return 1;
1127
	} // end oesterreich
1128

    
1129
	// polen
1130
	function checkVatID_pl($vat_id) {
1131
		if (strlen($vat_id) != 12)
1132
			return 0;
1133

    
1134
		$weights = array (6, 5, 7, 2, 3, 4, 5, 6, 7);
1135
		$checksum = (int) $vat_id[11];
1136
		$checkval = 0;
1137
		for ($i = 0; $i < count($weights); $i ++)
1138
			$checkval += (int) $vat_id[$i +2] * $weights[$i];
1139
		$checkval = $this->modulo($checkval, 11);
1140

    
1141
		if ($checkval != $checksum)
1142
			return 0;
1143

    
1144
		return 1;
1145
	} // end polen
1146

    
1147
	// portugal
1148
	function checkVatID_pt($vat_id) {
1149
		if (strlen($vat_id) != 11)
1150
			return 0;
1151

    
1152
		$checksum = (int) $vat_id[10];
1153
		$checkval = 0;
1154

    
1155
		for ($i = 2; $i < 10; $i ++) {
1156
			$checkval += (int) $vat_id[11 - $i] * $i;
1157
		}
1158
		$checkval = (11 - $this->modulo($checkval, 11)) > 9 ? 0 : (11 - $this->modulo($checkval, 11));
1159
		if ($checksum != $checkval)
1160
			return 0;
1161

    
1162
		return 1;
1163
	} // end portugal
1164

    
1165
	// schweden
1166
	function checkVatID_se($vat_id) {
1167
		if (strlen($vat_id) != 14)
1168
			return 0;
1169
		if ((int) substr($vat_id, -2) < 1 || (int) substr($vat_id, -2) > 94)
1170
			return 0;
1171
		$checksum = (int) $vat_id[11];
1172
		$checkval = 0;
1173

    
1174
		for ($i = 0; $i < 10; $i ++)
1175
			$checkval += $this->cross_summa((int) $vat_id[10 - $i] * ($this->is_even($i) ? 2 : 1));
1176
		if ($checksum != ($this->modulo($checkval, 10) == 0 ? 0 : 10 - $this->modulo($checkval, 10)))
1177
			return 0;
1178

    
1179
		$checkval = 0;
1180
		for ($i = 0; $i < 13; $i ++)
1181
			$checkval += (int) $vat_id[13 - $i] * ($this->is_even($i) ? 2 : 1);
1182
		if ($this->modulo($checkval, 10) > 0)
1183
			return 0;
1184

    
1185
		return 1;
1186
	} // end schweden
1187

    
1188
	// slowakische republik
1189
	function checkVatID_sk($vat_id) {
1190
		if (strlen($vat_id) != 12)
1191
			return 0;
1192
		if (!is_numeric(substr($vat_id, 2)))
1193
			return 0;
1194

    
1195
		if ($this->live_check == 'true') {
1196

    
1197
			return $this->live($vat_id);
1198

    
1199
		} else {
1200
			return 9; // es gibt keinen algorithmus
1201
		}
1202

    
1203
	} // end slowakische republik
1204

    
1205
	// slowenien
1206
	function checkVatID_si($vat_id) {
1207
		if (strlen($vat_id) != 10)
1208
			return 0;
1209
		if ((int) $vat_id[2] == 0)
1210
			return 0;
1211

    
1212
		$checksum = (int) $vat_id[9];
1213
		$checkval = 0;
1214

    
1215
		for ($i = 2; $i <= 8; $i ++)
1216
			$checkval += (int) $vat_id[10 - $i] * $i;
1217
		$checkval = $this->modulo($checkval, 11) == 10 ? 0 : 11 - $this->modulo($checkval, 11);
1218
		if ($checksum != $checkval)
1219
			return 0;
1220

    
1221
		return 1;
1222
	} // end slowenien
1223

    
1224
	// spanien
1225
	function checkVatID_es($vat_id) {
1226
		if (strlen($vat_id) != 11)
1227
			return 0;
1228

    
1229
		$allowed = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'Q');
1230
		$checkval = false;
1231

    
1232
		for ($i = 0; $i < count($allowed); $i ++) {
1233
			if (strtoupper($vat_id[2]) == $allowed[$i])
1234
				$checkval = true;
1235
		} // end for($i=0; $i<count($allowed); $i++)
1236
		if (!$checkval)
1237
			return 0;
1238

    
1239
		$checksum = (int) $vat_id[10];
1240
		$checkval = 0;
1241

    
1242
		for ($i = 2; $i <= 8; $i ++)
1243
			$checkval += $this->cross_summa((int) $vat_id[11 - $i] * ($this->is_even($i) ? 2 : 1));
1244
		if ($checksum != 10 - $this->modulo($checkval, 10))
1245
			return 0;
1246

    
1247
		return 1;
1248
	} // end spanien
1249

    
1250
	// tschechien
1251
	function checkVatID_cz($vat_id) {
1252

    
1253
		if ((strlen($vat_id) != 10) || (strlen($vat_id) != 11) || (strlen($vat_id) != 12))
1254
			return 0;
1255
		if (!is_numeric(substr($vat_id, 2)))
1256
			return 0;
1257

    
1258
		if ($this->live_check == 'true') {
1259

    
1260
			return $this->live($vat_id);
1261

    
1262
		} else {
1263
			return 9; // es gibt keinen algorithmus
1264
		}
1265
	} // end tschechien
1266

    
1267
	// ungarn
1268
	function checkVatID_hu($vat_id) {
1269

    
1270
		if (strlen($vat_id) != 10)
1271
			return 0;
1272
		if (!is_numeric(substr($vat_id, 2)))
1273
			return 0;
1274

    
1275
		if ($this->live_check == 'true') {
1276

    
1277
			return $this->live($vat_id);
1278

    
1279
		} else {
1280
			return 9; // es gibt keinen algorithmus
1281
		}
1282
	} // end ungarn
1283

    
1284
	// zypern
1285
	function checkVatID_cy($vat_id) {
1286

    
1287
		if (strlen($vat_id) != 11)
1288
			return 0;
1289

    
1290
		if ($this->live_check == 'true') {
1291

    
1292
			return $this->live($vat_id);
1293

    
1294
		} else {
1295
			return 9; // es gibt keinen algorithmus
1296
		}
1297
	} // end zypern
1298

    
1299
	/*******************************************************************/
1300

    
1301
	/********************************************************************
1302
	* mathematische Hilfsfunktionen                                     *
1303
	********************************************************************/
1304
	// modulo berechnet den rest einer division von $val durch $param
1305
	function modulo($val, $param) {
1306
		return $val - (floor($val / $param) * $param);
1307
	} // end function modulo($val, $param)
1308

    
1309
	// stellt fest, ob eine zahl gerade ist
1310
	function is_even($val) {
1311
		return ($val / 2 == floor($val / 2)) ? true : false;
1312
	} // end function is_even($val)
1313

    
1314
	// errechnet die quersumme von $val
1315
	function cross_summa($val) {
1316
		$val = (string) $val;
1317
		$sum = 0;
1318
		for ($i = 0; $i < strlen($val); $i ++)
1319
			$sum += (int) $val[$i];
1320
		return $sum;
1321
	} // end function cross_summa((string) $val)
1322
	/*******************************************************************/
1323
}
    (1-1/1)