Launchpad automatic translations update.
[odoo/odoo.git] / addons / base_vat / base_vat.py
index 339c152..51c1663 100644 (file)
@@ -23,11 +23,12 @@ import logging
 import string
 import datetime
 import re
+_logger = logging.getLogger(__name__)
 
 try:
     import vatnumber
 except ImportError:
-    logging.getLogger('base_vat').warning("VAT validation partially unavailable because the `vatnumber` Python library cannot be found. "
+    _logger.warning("VAT validation partially unavailable because the `vatnumber` Python library cannot be found. "
                                           "Install it to support more countries, for example with `easy_install vatnumber`.")
     vatnumber = None
 
@@ -36,8 +37,8 @@ from tools.misc import ustr
 from tools.translate import _
 
 _ref_vat = {
-    'be': 'BE0477472701',
     'at': 'ATU12345675',
+    'be': 'BE0477472701',
     'bg': 'BG1234567892',
     'ch': 'CHE-123.456.788 TVA or CH TVA 123456', #Swiss by Yannick Vaucher @ Camptocamp
     'cy': 'CY12345678F',
@@ -104,6 +105,12 @@ class res_partner(osv.osv):
             # country code or empty VAT number), so we fall back to the simple check.
             return self.simple_vat_check(cr, uid, country_code, vat_number, context=context)
 
+    def button_check_vat(self, cr, uid, ids, context=None):
+        if not self.check_vat(cr, uid, ids, context=context):
+            msg = self._construct_constraint_msg(cr, uid, ids, context=context)
+            raise osv.except_osv(_('Error!'), msg)
+        return True
+
     def check_vat(self, cr, uid, ids, context=None):
         user_company = self.pool.get('res.users').browse(cr, uid, uid).company_id
         if user_company.vat_check_vies:
@@ -112,7 +119,6 @@ class res_partner(osv.osv):
         else:
             # quick and partial off-line checksum validation
             check_func = self.simple_vat_check
-
         for partner in self.browse(cr, uid, ids, context=context):
             if not partner.vat:
                 continue
@@ -143,13 +149,8 @@ class res_partner(osv.osv):
     _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
 
 
-    # Mexican VAT verification, contributed by <moylop260@hotmail.com>
-    # and Panos Christeas <p_christ@hol.gr>
-    __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
-                                    r"[ \-_]?" \
-                                    r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
-                                    r"[ \-_]?" \
-                                    r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
+    __check_vat_ch_re1 = re.compile(r'(MWST|TVA|IVA)[0-9]{6}$')
+    __check_vat_ch_re2 = re.compile(r'E([0-9]{9}|-[0-9]{3}\.[0-9]{3}\.[0-9]{3})(MWST|TVA|IVA)$')
 
     def check_vat_ch(self, vat):
         '''
@@ -160,9 +161,10 @@ class res_partner(osv.osv):
         # Old format is "TVA 123456" we will admit the user has to enter ch before the number
         # Format will becomes such as "CHE-999.999.99C TVA"
         # Both old and new format will be accepted till end of 2013
-        # Accepted format are:
-        #     CHTVA######
+        # Accepted format are: (spaces are ignored)
         #     CH TVA ######
+        #     CH IVA ######
+        #     CH MWST #######
         #
         #     CHE#########MWST
         #     CHE#########TVA
@@ -171,33 +173,26 @@ class res_partner(osv.osv):
         #     CHE-###.###.### TVA
         #     CHE-###.###.### IVA
         #     
-
-        vat = vat.replace('-','').replace('.','')
-        if len(vat) in (13,14):
-            if not vat[0] == 'E':
-                return False
-            if vat[10:] not in ('TVA','IVA','MWST'):
-                return False
-            num = vat[1:10]
-        elif len(vat) in (9,10):
-            if vat[:-6] not in ('TVA','IVA','MWST'):
-                return False
-            num = vat[-6:]
-        else:
-            return False
-        # For new TVA numbers, do a mod11 check
-        if len(num) == 9:
-            def is_mod11(num):
-                factor = (5,4,3,2,7,6,5,4)
-                csum = sum([int(num[i]) * factor[i] for i in range(8)])
-                check = 11 - (csum % 11)
-                return check == int(num[8])
-
-            if not is_mod11(num):
-                return False
-        return True
+        if self.__check_vat_ch_re1.match(vat):
+            return True
+        match = self.__check_vat_ch_re2.match(vat) 
+        if match:
+            # For new TVA numbers, do a mod11 check
+            num = filter(lambda s: s.isdigit(), match.group(1))        # get the digits only
+            factor = (5,4,3,2,7,6,5,4)
+            csum = sum([int(num[i]) * factor[i] for i in range(8)])
+            check = 11 - (csum % 11)
+            return check == int(num[8])
+        return False
 
 
+    # Mexican VAT verification, contributed by <moylop260@hotmail.com>
+    # and Panos Christeas <p_christ@hol.gr>
+    __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
+                                    r"[ \-_]?" \
+                                    r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
+                                    r"[ \-_]?" \
+                                    r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
     def check_vat_mx(self, vat):
         ''' Mexican VAT verification