[IMP] base_vat: remove italian vat validation method from base_vat as it is now avail...
[odoo/odoo.git] / addons / base_vat / base_vat.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
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}"
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import string
24 import datetime
25 import vatnumber
26 import re
27
28 from osv import osv, fields
29 from tools.misc import ustr
30 from tools.translate import _
31
32 _ref_vat = {
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',
48 }
49
50 class res_partner(osv.osv):
51     _inherit = 'res.partner'
52
53     def _split_vat(self, vat):
54         vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
55         return vat_country, vat_number
56
57     def check_vat(self, cr, uid, ids, context=None):
58         '''
59         Check the VAT number depending of the country.
60         http://sima-pc.com/nif.php
61         '''
62         country_obj = self.pool.get('res.country')
63         for partner in self.browse(cr, uid, ids, context=context):
64             if not partner.vat:
65                 continue
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):
71                     continue
72                 #Otherwise, it means that the country code isn't valid and we return False.
73                 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):
76                 return False
77         return True
78
79     def vat_change(self, cr, uid, ids, value, context=None):
80         return {'value': {'vat_subjected': bool(value)}}
81
82     _columns = {
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.")
84     }
85
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'), ()
97
98     _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
99
100     __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
101                                     r"[ \-_]?" \
102                                     r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
103                                     r"[ \-_]?" \
104                                     r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
105
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
109
110         Verificar RFC México
111         '''
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)
115         if not m:
116             #No valid format
117             return False
118         try:
119             ano = int(m.group('ano'))
120             if ano > 30:
121                 ano = 1900 + ano
122             else:
123                 ano = 2000 + ano
124             datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
125         except ValueError:
126             return False
127
128         #Valid format and valid date
129         return True
130
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):
133         '''
134         Check Sweden VAT number.
135         '''
136         if len(vat) != 12:
137             return False
138         try:
139             int(vat)
140         except:
141             return False
142         #if int(vat[9:11]) <= 0: Fixed in OpenERP
143         if int(vat[9:11]) < 0:
144             return False
145
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)
152         if check == 10:
153             check = 0
154         if check != int(vat[9]):
155             return False
156         return True
157
158 res_partner()
159
160 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: