base_vat: fix check_vat_mx, wrt. encoding, small letters and year range

As of this commit, tests pass all the 242662 numbers provided by moylop260

bzr revid: p_christ@hol.gr-20110429083625-80dd16bs4dlbvt1w
This commit is contained in:
P. Christeas
2011-04-29 11:36:25 +03:00
parent 8d984744de
commit b6dce5a11b
+12 -4
View File
@@ -23,6 +23,7 @@ import string
from osv import osv, fields
from tools.translate import _
from tools.misc import ustr
import re
import datetime
@@ -1068,23 +1069,30 @@ class res_partner(osv.osv):
return False
return True
__check_vat_mx_re = re.compile(r"(?P<primeras>[A-Z&ñÑ]{3,4})" \
__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][1-9])(?P<dia>[0-3][0-9])" \
r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
r"[ \-_]?" \
r"(?P<code>[A-Z0-9&ñÑ\xd1\xf1]{3})$")
r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
def check_vat_mx(self, vat):
''' Mexican VAT verification
Verificar RFC México
'''
# we convert to 8-bit encoding, to help the regex parse only bytes
vat = ustr(vat).encode('iso8859-1')
m = self.__check_vat_mx_re.match(vat)
if not m:
#No valid format
return False
try:
datetime.date(int(m.group('ano')), int(m.group('mes')), int(m.group('dia')))
ano = int(m.group('ano'))
if ano > 30:
ano = 1900 + ano
else:
ano = 2000 + ano
datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
except ValueError:
return False