X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=addons%2Fbase_vat%2Fbase_vat.py;h=51c1663d3662ea70b4070ca520de8977996fb01f;hb=117bf937b9b9489718a46ad5d6a967110ee5c3f7;hp=e02a4c8d5b9824c7cde893967647e30147aab25b;hpb=a30138475f4f2a5d2f9ccf356ee448c0945f87b0;p=odoo%2Fodoo.git diff --git a/addons/base_vat/base_vat.py b/addons/base_vat/base_vat.py index e02a4c8..51c1663 100644 --- a/addons/base_vat/base_vat.py +++ b/addons/base_vat/base_vat.py @@ -2,8 +2,7 @@ ############################################################################## # # OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). All Rights Reserved -# Copyright (C) 2008-2009 B2CK, Cedric Krier, Bertrand Chenal (the methods "check_vat_[a-z]{2}" +# Copyright (C) 2004-2011 OpenERP SA () # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,31 +19,56 @@ # ############################################################################## +import logging import string import datetime -import vatnumber import re +_logger = logging.getLogger(__name__) + +try: + import vatnumber +except ImportError: + _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 from osv import osv, fields from tools.misc import ustr from tools.translate import _ _ref_vat = { - 'be': 'BE0477472701', 'at': 'ATU12345675', - 'bg': 'BG1234567892', 'cy': 'CY12345678F', - 'cz': 'CZ12345679', 'de': 'DE123456788', - 'dk': 'DK12345674', 'ee': 'EE123456780', - 'es': 'ESA12345674', 'fi': 'FI12345671', - 'fr': 'FR32123456789', 'gb': 'GB123456782', - 'gr': 'GR12345670', 'hu': 'HU12345676', - 'ie': 'IE1234567T', 'it': 'IT12345670017', - 'lt': 'LT123456715', 'lu': 'LU12345613', - 'lv': 'LV41234567891', 'mt': 'MT12345634', - 'nl': 'NL123456782B90', 'pl': 'PL1234567883', - 'pt': 'PT123456789', 'ro': 'RO1234567897', - 'se': 'SE123456789701', 'si': 'SI12345679', - 'sk': 'SK0012345675', 'el': 'EL12345670', - 'mx': 'MXABCD831230T1B', + 'at': 'ATU12345675', + 'be': 'BE0477472701', + 'bg': 'BG1234567892', + 'ch': 'CHE-123.456.788 TVA or CH TVA 123456', #Swiss by Yannick Vaucher @ Camptocamp + 'cy': 'CY12345678F', + 'cz': 'CZ12345679', + 'de': 'DE123456788', + 'dk': 'DK12345674', + 'ee': 'EE123456780', + 'el': 'EL12345670', + 'es': 'ESA12345674', + 'fi': 'FI12345671', + 'fr': 'FR32123456789', + 'gb': 'GB123456782', + 'gr': 'GR12345670', + 'hu': 'HU12345676', + 'hr': 'HR01234567896', # Croatia, contributed by Milan Tribuson + 'ie': 'IE1234567T', + 'it': 'IT12345670017', + 'lt': 'LT123456715', + 'lu': 'LU12345613', + 'lv': 'LV41234567891', + 'mt': 'MT12345634', + 'mx': 'MXABC123456T1B', + 'nl': 'NL123456782B90', + 'no': 'NO123456785', + 'pl': 'PL1234567883', + 'pt': 'PT123456789', + 'ro': 'RO1234567897', + 'se': 'SE123456789701', + 'si': 'SI12345679', + 'sk': 'SK0012345675', } class res_partner(osv.osv): @@ -54,25 +78,52 @@ class res_partner(osv.osv): vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '') return vat_country, vat_number - def check_vat(self, cr, uid, ids, context=None): + def simple_vat_check(self, cr, uid, country_code, vat_number, context=None): ''' Check the VAT number depending of the country. http://sima-pc.com/nif.php ''' - country_obj = self.pool.get('res.country') + check_func_name = 'check_vat_' + country_code + check_func = getattr(self, check_func_name, None) or \ + getattr(vatnumber, check_func_name, None) + if not check_func: + # No VAT validation available, default to check that the country code exists + res_country = self.pool.get('res.country') + return bool(res_country.search(cr, uid, [('code', '=ilike', country_code)], context=context)) + return check_func(vat_number) + + def vies_vat_check(self, cr, uid, country_code, vat_number, context=None): + try: + # Validate against VAT Information Exchange System (VIES) + # see also http://ec.europa.eu/taxation_customs/vies/ + return vatnumber.check_vies(country_code.upper()+vat_number) + except Exception: + # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl + # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE, + # TIMEOUT or SERVER_BUSY. There is no way we can validate the input + # with VIES if any of these arise, including the first one (it means invalid + # 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: + # force full VIES online check + check_func = self.vies_vat_check + 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 vat_country, vat_number = self._split_vat(partner.vat) - if not hasattr(self, 'check_vat_' + vat_country) and not hasattr(vatnumber, 'check_vat_' + vat_country): - #We didn't find the validation method for the country code. If that country code can be found in openerp, this means that it is a valid country code - #and we simply didn't have implemented that function. In that case we continue. - if country_obj.search(cr, uid, [('code', 'ilike', vat_country)], context=context): - continue - #Otherwise, it means that the country code isn't valid and we return False. - return False - check = getattr(self, 'check_vat_' + vat_country, False) or getattr(vatnumber, 'check_vat_' + vat_country, False) - if not check(vat_number): + if not check_func(cr, uid, vat_country, vat_number, context=context): return False return True @@ -90,20 +141,58 @@ class res_partner(osv.osv): # has more than 3 characters return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat) + vat_no = "'CC##' (CC=Country Code, ##=VAT Number)" if default_vat_check(vat_country, vat_number): - vat_no = vat_country in _ref_vat and _ref_vat[vat_country] or 'Country Code + Vat Number' - return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no) - return _('The VAT is invalid, It should begin with the country code'), () + vat_no = _ref_vat[vat_country] if vat_country in _ref_vat else vat_no + return '\n' + _('This VAT number does not seem to be valid.\nNote: the expected format is %s') % vat_no _constraints = [(check_vat, _construct_constraint_msg, ["vat"])] + + __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): + ''' + Check Switzerland VAT number. + ''' + # VAT number in Switzerland will change between 2011 and 2013 + # http://www.estv.admin.ch/mwst/themen/00154/00589/01107/index.html?lang=fr + # 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: (spaces are ignored) + # CH TVA ###### + # CH IVA ###### + # CH MWST ####### + # + # CHE#########MWST + # CHE#########TVA + # CHE#########IVA + # CHE-###.###.### MWST + # CHE-###.###.### TVA + # CHE-###.###.### IVA + # + 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 + # and Panos Christeas __check_vat_mx_re = re.compile(r"(?P[A-Za-z\xd1\xf1&]{3,4})" \ r"[ \-_]?" \ r"(?P[0-9]{2})(?P[01][0-9])(?P[0-3][0-9])" \ r"[ \-_]?" \ r"(?P[A-Za-z0-9&\xd1\xf1]{3})$") - - # Mexican VAT verification is not define in vatnumber library, so we need to define it here def check_vat_mx(self, vat): ''' Mexican VAT verification @@ -128,66 +217,32 @@ class res_partner(osv.osv): #Valid format and valid date return True - # Sweden VAT number check fails is some cases from vatnumber library..so override this check method here - def check_vat_se(self, vat): + + # Norway VAT validation, contributed by Rolv RÃ¥en (adEgo) + def check_vat_no(self, vat): ''' - Check Sweden VAT number. + Check Norway VAT number.See http://www.brreg.no/english/coordination/number.html ''' - if len(vat) != 12: + if len(vat) != 9: return False try: int(vat) - except: - return False - #if int(vat[9:11]) <= 0: Fixed in OpenERP - if int(vat[9:11]) < 0: + except ValueError: return False - sum = vatnumber.mult_add(2, int(vat[0])) + int(vat[1]) + \ - vatnumber.mult_add(2, int(vat[2])) + int(vat[3]) + \ - vatnumber.mult_add(2, int(vat[4])) + int(vat[5]) + \ - vatnumber.mult_add(2, int(vat[6])) + int(vat[7]) + \ - vatnumber.mult_add(2, int(vat[8])) - check = 10 - (sum % 10) - if check == 10: - check = 0 - if check != int(vat[9]): - return False - return True + sum = (3 * int(vat[0])) + (2 * int(vat[1])) + \ + (7 * int(vat[2])) + (6 * int(vat[3])) + \ + (5 * int(vat[4])) + (4 * int(vat[5])) + \ + (3 * int(vat[6])) + (2 * int(vat[7])) - # Italy VAT number check fails is some cases from vatnumber library..so override this check method here - def check_vat_it(self, vat): - ''' - Check Italy VAT number. - ''' - if len(vat) != 11: - return False - try: - int(vat) - except: - return False - if int(vat[0:7]) <= 0: - return False - if int(vat[7:10]) <= 0: - return False - if int(vat[7:10]) > 100 and int(vat[7:10]) < 120: - return False -# Fixed in OpenERP -# if int(vat[7:10]) > 121: -# return False - - sum = int(vat[0]) + vatnumber.mult_add(2, int(vat[1])) + int(vat[2]) + \ - vatnumber.mult_add(2, int(vat[3])) + int(vat[4]) + \ - vatnumber.mult_add(2, int(vat[5])) + int(vat[6]) + \ - vatnumber.mult_add(2, int(vat[7])) + int(vat[8]) + \ - vatnumber.mult_add(2, int(vat[9])) - check = 10 - (sum % 10) - if check == 10: + check = 11 -(sum % 11) + if check == 11: check = 0 - if check != int(vat[10]): + if check == 10: + # 10 is not a valid check digit for an organization number return False - return True + return check == int(vat[8]) res_partner() -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: \ No newline at end of file +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: