? vat-extensible.patch
? vat_validators.info
? vat_validators.module
Index: vat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vat/vat.module,v
retrieving revision 1.1
diff -u -p -r1.1 vat.module
--- vat.module	15 Jun 2010 10:10:44 -0000	1.1
+++ vat.module	24 Jun 2010 21:09:45 -0000
@@ -17,10 +17,34 @@ function vat_field_info() {
 }
 
 /**
+ * Registers VAT validators in VAT modules based on hook_vat_info()
+ * This list gets refreshed each time the 'manage field' form is visited.
+ */
+function vat_register_validators() {
+  $vat_validators = array();
+  $vat_validators = module_invoke_all('vat_info');      
+  variable_set('available_vat_validators', $vat_validators);
+}
+
+/**
  * Implementation of hook_field_settings().
  */
 function vat_field_settings($op, $field) {
   switch ($op) {
+    case 'form':
+      vat_register_validators();      
+      foreach (variable_get('available_vat_validators', array()) as $country_code => $validator) {
+        $options[$country_code] = $validator['country'];
+      }
+      $form['enabled_vat_validators'] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('Validate VAT for these countries'),
+        '#default_value' => (!empty($field['enabled_vat_validators'])) ? $field['enabled_vat_validators'] : array(),
+        '#options' => $options,
+      );
+      return $form;
+    case 'save':
+      return array('enabled_vat_validators');
     case 'database columns':
       $columns['vat'] = array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'sortable' => TRUE);
       return $columns;
@@ -34,7 +58,7 @@ function vat_field($op, &$node, $field, 
   switch ($op) {
     case 'validate':
       foreach ($items as $delta => $item) {
-        if ($item['vat'] != '' && !vat_is_valid_vat(trim($item['vat']))) {
+        if ($item['vat'] != '' && !vat_is_valid_vat(trim($item['vat']), $field['enabled_vat_validators'])) {
           form_set_error($field['field_name'], t('%vat is not a valid vat number', array('%vat' => $item['vat'])));
         }
       }
@@ -58,42 +82,23 @@ function vat_field($op, &$node, $field, 
  * 
  * @param string $vat
  */
-function vat_is_valid_vat($vat) {
+function vat_is_valid_vat($vat, $enabled_validators = array()) {
   $matches = array();
   preg_match_all('/[A-Z0-9]/', strtoupper($vat), $matches);
   $vat = implode('', $matches[0]);
   if (!preg_match('/([A-Z]{2})([0-9A-Z]{2,12})/', $vat, $matches)) {
     return FALSE;
   }
-  // http://fiscus.fgov.be/interfaoifnl/tva_intrac/tva_eur_nl.htm
-  switch ($matches[1]) {
-    case 'NL':
-      return _vat_is_valid_nl_vat($vat);
-    case 'BE':
-      return _vat_is_valid_be_vat($vat);
-  }
-  return FALSE;
-}
 
-function _vat_is_valid_nl_vat($vat) {
-  $matches = array();
-  if (!preg_match('/NL(\d{9})B\d{2}/', $vat, $matches)) {
-    return FALSE;
-  }
-  $sum = 0;
-  for ($i = 0; $i < 8; $i++) {
-    $sum += (9 - $i) * $matches[1][$i];
-  }
-  $sum %= 11;
-  return $sum == $matches[1][8];
-}
+  // http://fiscus.fgov.be/interfaoifnl/tva_intrac/tva_eur_nl.htm
+  foreach (variable_get('available_vat_validators', array()) as $validator) {
+    if (!$validator['validator']($vat)) {
+      form_set_error('', t('%vat is not valid according to the %country VAT rules', array('%vat' => $vat, '%country' => $validator['demonym'])));
+      return $false;
+    }
+  }  
 
-function _vat_is_valid_be_vat($vat) {
-  $matches = array();
-  if (!preg_match('/BE(\d{8})(\d{2})/', $vat, $matches)) {
-    return FALSE;
-  }
-  return (97 - ($matches[1] % 97)) == $matches[2]; 
+  return TRUE;
 }
 
 /**
