1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 # Copyright (C) 2008-2009 B2CK, Cedric Krier, Bertrand Chenal (the methods "check_vat_[a-z]{2}"
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
28 from osv import osv, fields
29 from tools.misc import ustr
30 from tools.translate import _
33 'be': 'BE0477472701', 'at': 'ATU12345675',
34 'bg': 'BG1234567892', 'cy': 'CY12345678F',
35 'cz': 'CZ12345679', 'de': 'DE123456788',
36 'dk': 'DK12345674', 'ee': 'EE123456780',
37 'es': 'ESA12345674', 'fi': 'FI12345671',
38 'fr': 'FR32123456789', 'gb': 'GB123456782',
39 'gr': 'GR12345670', 'hu': 'HU12345676',
40 'ie': 'IE1234567T', 'it': 'IT12345670017',
41 'lt': 'LT123456715', 'lu': 'LU12345613',
42 'lv': 'LV41234567891', 'mt': 'MT12345634',
43 'nl': 'NL123456782B90', 'pl': 'PL1234567883',
44 'pt': 'PT123456789', 'ro': 'RO1234567897',
45 'se': 'SE123456789701', 'si': 'SI12345679',
46 'sk': 'SK0012345675', 'el': 'EL12345670',
47 'mx': 'MXABCD831230T1B',
50 class res_partner(osv.osv):
51 _inherit = 'res.partner'
53 def _split_vat(self, vat):
54 vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
55 return vat_country, vat_number
57 def check_vat(self, cr, uid, ids, context=None):
59 Check the VAT number depending of the country.
60 http://sima-pc.com/nif.php
62 country_obj = self.pool.get('res.country')
63 for partner in self.browse(cr, uid, ids, context=context):
66 vat_country, vat_number = self._split_vat(partner.vat)
67 if not hasattr(self, 'check_vat_' + vat_country) and not hasattr(vatnumber, 'check_vat_' + vat_country):
68 #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
69 #and we simply didn't have implemented that function. In that case we continue.
70 if country_obj.search(cr, uid, [('code', 'ilike', vat_country)], context=context):
72 #Otherwise, it means that the country code isn't valid and we return False.
74 check = getattr(self, 'check_vat_' + vat_country, False) or getattr(vatnumber, 'check_vat_' + vat_country, False)
75 if not check(vat_number):
79 def vat_change(self, cr, uid, ids, value, context=None):
80 return {'value': {'vat_subjected': bool(value)}}
83 'vat_subjected': fields.boolean('VAT Legal Statement', help="Check this box if the partner is subjected to the VAT. It will be used for the VAT legal statement.")
86 def _construct_constraint_msg(self, cr, uid, ids, context=None):
87 def default_vat_check(cn, vn):
88 # by default, a VAT number is valid if:
89 # it starts with 2 letters
90 # has more than 3 characters
91 return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
92 vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
93 if default_vat_check(vat_country, vat_number):
94 vat_no = vat_country in _ref_vat and _ref_vat[vat_country] or 'Country Code + Vat Number'
95 return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no)
96 return _('The VAT is invalid, It should begin with the country code'), ()
98 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
100 __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
102 r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
104 r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
106 # Mexican VAT verification is not define in vatnumber library, so we need to define it here
107 def check_vat_mx(self, vat):
108 ''' Mexican VAT verification
110 Verificar RFC México
112 # we convert to 8-bit encoding, to help the regex parse only bytes
113 vat = ustr(vat).encode('iso8859-1')
114 m = self.__check_vat_mx_re.match(vat)
119 ano = int(m.group('ano'))
124 datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
128 #Valid format and valid date
131 # Sweden VAT number check fails is some cases from vatnumber library..so override this check method here
132 def check_vat_se(self, vat):
134 Check Sweden VAT number.
142 #if int(vat[9:11]) <= 0: Fixed in OpenERP
143 if int(vat[9:11]) < 0:
146 sum = vatnumber.mult_add(2, int(vat[0])) + int(vat[1]) + \
147 vatnumber.mult_add(2, int(vat[2])) + int(vat[3]) + \
148 vatnumber.mult_add(2, int(vat[4])) + int(vat[5]) + \
149 vatnumber.mult_add(2, int(vat[6])) + int(vat[7]) + \
150 vatnumber.mult_add(2, int(vat[8]))
151 check = 10 - (sum % 10)
154 if check != int(vat[9]):
158 # Italy VAT number check fails is some cases from vatnumber library..so override this check method here
159 def check_vat_it(self, vat):
161 Check Italy VAT number.
169 if int(vat[0:7]) <= 0:
171 if int(vat[7:10]) <= 0:
173 if int(vat[7:10]) > 100 and int(vat[7:10]) < 120:
176 # if int(vat[7:10]) > 121:
179 sum = int(vat[0]) + vatnumber.mult_add(2, int(vat[1])) + int(vat[2]) + \
180 vatnumber.mult_add(2, int(vat[3])) + int(vat[4]) + \
181 vatnumber.mult_add(2, int(vat[5])) + int(vat[6]) + \
182 vatnumber.mult_add(2, int(vat[7])) + int(vat[8]) + \
183 vatnumber.mult_add(2, int(vat[9]))
184 check = 10 - (sum % 10)
187 if check != int(vat[10]):
193 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: