[FIX] web_tip: don't display tip on element whose width or height is 0
[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-2012 OpenERP SA (<http://openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import logging
23 import string
24 import datetime
25 import re
26 _logger = logging.getLogger(__name__)
27
28 try:
29     import vatnumber
30 except ImportError:
31     _logger.warning("VAT validation partially unavailable because the `vatnumber` Python library cannot be found. "
32                                           "Install it to support more countries, for example with `easy_install vatnumber`.")
33     vatnumber = None
34
35 from openerp.osv import fields, osv
36 from openerp.tools.misc import ustr
37 from openerp.tools.translate import _
38
39 _ref_vat = {
40     'at': 'ATU12345675',
41     'be': 'BE0477472701',
42     'bg': 'BG1234567892',
43     'ch': 'CHE-123.456.788 TVA or CH TVA 123456', #Swiss by Yannick Vaucher @ Camptocamp
44     'cy': 'CY12345678F',
45     'cz': 'CZ12345679',
46     'de': 'DE123456788',
47     'dk': 'DK12345674',
48     'ee': 'EE123456780',
49     'el': 'EL12345670',
50     'es': 'ESA12345674',
51     'fi': 'FI12345671',
52     'fr': 'FR32123456789',
53     'gb': 'GB123456782',
54     'gr': 'GR12345670',
55     'hu': 'HU12345676',
56     'hr': 'HR01234567896', # Croatia, contributed by Milan Tribuson 
57     'ie': 'IE1234567FA',
58     'it': 'IT12345670017',
59     'lt': 'LT123456715',
60     'lu': 'LU12345613',
61     'lv': 'LV41234567891',
62     'mt': 'MT12345634',
63     'mx': 'MXABC123456T1B',
64     'nl': 'NL123456782B90',
65     'no': 'NO123456785',
66     'pe': 'PER10254824220 or PED10254824220',
67     'pl': 'PL1234567883',
68     'pt': 'PT123456789',
69     'ro': 'RO1234567897',
70     'se': 'SE123456789701',
71     'si': 'SI12345679',
72     'sk': 'SK0012345675',
73 }
74
75 class res_partner(osv.osv):
76     _inherit = 'res.partner'
77
78     def _split_vat(self, vat):
79         vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
80         return vat_country, vat_number
81
82     def simple_vat_check(self, cr, uid, country_code, vat_number, context=None):
83         '''
84         Check the VAT number depending of the country.
85         http://sima-pc.com/nif.php
86         '''
87         if not ustr(country_code).encode('utf-8').isalpha():
88             return False
89         check_func_name = 'check_vat_' + country_code
90         check_func = getattr(self, check_func_name, None) or \
91                         getattr(vatnumber, check_func_name, None)
92         if not check_func:
93             # No VAT validation available, default to check that the country code exists
94             res_country = self.pool.get('res.country')
95             return bool(res_country.search(cr, uid, [('code', '=ilike', country_code)], context=context))
96         return check_func(vat_number)
97
98     def vies_vat_check(self, cr, uid, country_code, vat_number, context=None):
99         try:
100             # Validate against  VAT Information Exchange System (VIES)
101             # see also http://ec.europa.eu/taxation_customs/vies/
102             return vatnumber.check_vies(country_code.upper()+vat_number)
103         except Exception:
104             # see http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
105             # Fault code may contain INVALID_INPUT, SERVICE_UNAVAILABLE, MS_UNAVAILABLE,
106             # TIMEOUT or SERVER_BUSY. There is no way we can validate the input
107             # with VIES if any of these arise, including the first one (it means invalid
108             # country code or empty VAT number), so we fall back to the simple check.
109             return self.simple_vat_check(cr, uid, country_code, vat_number, context=context)
110
111     def button_check_vat(self, cr, uid, ids, context=None):
112         if not self.check_vat(cr, uid, ids, context=context):
113             msg = self._construct_constraint_msg(cr, uid, ids, context=context)
114             raise osv.except_osv(_('Error!'), msg)
115         return True
116
117     def check_vat(self, cr, uid, ids, context=None):
118         user_company = self.pool.get('res.users').browse(cr, uid, uid).company_id
119         if user_company.vat_check_vies:
120             # force full VIES online check
121             check_func = self.vies_vat_check
122         else:
123             # quick and partial off-line checksum validation
124             check_func = self.simple_vat_check
125         for partner in self.browse(cr, uid, ids, context=context):
126             if not partner.vat:
127                 continue
128             vat_country, vat_number = self._split_vat(partner.vat)
129             if not check_func(cr, uid, vat_country, vat_number, context=context):
130                 _logger.info(_("Importing VAT Number [%s] is not valid !" % vat_number))
131                 return False
132         return True
133
134     def vat_change(self, cr, uid, ids, value, context=None):
135         return {'value': {'vat_subjected': bool(value)}}
136
137     def _commercial_fields(self, cr, uid, context=None):
138         return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['vat_subjected']
139
140     def _construct_constraint_msg(self, cr, uid, ids, context=None):
141         def default_vat_check(cn, vn):
142             # by default, a VAT number is valid if:
143             #  it starts with 2 letters
144             #  has more than 3 characters
145             return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
146         vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
147         vat_no = "'CC##' (CC=Country Code, ##=VAT Number)"
148         error_partner = self.browse(cr, uid, ids, context=context)
149         if default_vat_check(vat_country, vat_number):
150             vat_no = _ref_vat[vat_country] if vat_country in _ref_vat else vat_no
151             if self.pool['res.users'].browse(cr, uid, uid).company_id.vat_check_vies:
152                 return '\n' + _('The VAT number [%s] for partner [%s] either failed the VIES VAT validation check or did not respect the expected format %s.') % (error_partner[0].vat, error_partner[0].name, vat_no)
153         return '\n' + _('The VAT number [%s] for partner [%s] does not seem to be valid. \nNote: the expected format is %s') % (error_partner[0].vat, error_partner[0].name, vat_no)
154
155     _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
156
157
158     __check_vat_ch_re1 = re.compile(r'(MWST|TVA|IVA)[0-9]{6}$')
159     __check_vat_ch_re2 = re.compile(r'E([0-9]{9}|-[0-9]{3}\.[0-9]{3}\.[0-9]{3})(MWST|TVA|IVA)$')
160
161     def check_vat_ch(self, vat):
162         '''
163         Check Switzerland VAT number.
164         '''
165         # VAT number in Switzerland will change between 2011 and 2013 
166         # http://www.estv.admin.ch/mwst/themen/00154/00589/01107/index.html?lang=fr
167         # Old format is "TVA 123456" we will admit the user has to enter ch before the number
168         # Format will becomes such as "CHE-999.999.99C TVA"
169         # Both old and new format will be accepted till end of 2013
170         # Accepted format are: (spaces are ignored)
171         #     CH TVA ######
172         #     CH IVA ######
173         #     CH MWST #######
174         #
175         #     CHE#########MWST
176         #     CHE#########TVA
177         #     CHE#########IVA
178         #     CHE-###.###.### MWST
179         #     CHE-###.###.### TVA
180         #     CHE-###.###.### IVA
181         #     
182         if self.__check_vat_ch_re1.match(vat):
183             return True
184         match = self.__check_vat_ch_re2.match(vat) 
185         if match:
186             # For new TVA numbers, do a mod11 check
187             num = filter(lambda s: s.isdigit(), match.group(1))        # get the digits only
188             factor = (5,4,3,2,7,6,5,4)
189             csum = sum([int(num[i]) * factor[i] for i in range(8)])
190             check = (11 - (csum % 11)) % 11
191             return check == int(num[8])
192         return False
193
194     def _ie_check_char(self, vat):
195         vat = vat.zfill(8)
196         extra = 0
197         if vat[7] not in ' W':
198             if vat[7].isalpha():
199                 extra = 9 * (ord(vat[7]) - 64)
200             else:
201                 # invalid
202                 return -1
203         checksum = extra + sum((8-i) * int(x) for i, x in enumerate(vat[:7]))
204         return 'WABCDEFGHIJKLMNOPQRSTUV'[checksum % 23]
205
206     def check_vat_ie(self, vat):
207         """ Temporary Ireland VAT validation to support the new format
208         introduced in January 2013 in Ireland, until upstream is fixed.
209         TODO: remove when fixed upstream"""
210         if len(vat) not in (8, 9) or not vat[2:7].isdigit():
211             return False
212         if len(vat) == 8:
213             # Normalize pre-2013 numbers: final space or 'W' not significant
214             vat += ' '
215         if vat[:7].isdigit():
216             return vat[7] == self._ie_check_char(vat[:7] + vat[8])
217         elif vat[1] in (string.ascii_uppercase + '+*'):
218             # Deprecated format
219             # See http://www.revenue.ie/en/online/third-party-reporting/reporting-payment-details/faqs.html#section3
220             return vat[7] == self._ie_check_char(vat[2:7] + vat[0] + vat[8])
221         return False
222
223     # Mexican VAT verification, contributed by Vauxoo
224     # and Panos Christeas <p_christ@hol.gr>
225     __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
226                                     r"[ \-_]?" \
227                                     r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
228                                     r"[ \-_]?" \
229                                     r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
230     def check_vat_mx(self, vat):
231         ''' Mexican VAT verification
232
233         Verificar RFC México
234         '''
235         # we convert to 8-bit encoding, to help the regex parse only bytes
236         vat = ustr(vat).encode('iso8859-1')
237         m = self.__check_vat_mx_re.match(vat)
238         if not m:
239             #No valid format
240             return False
241         try:
242             ano = int(m.group('ano'))
243             if ano > 30:
244                 ano = 1900 + ano
245             else:
246                 ano = 2000 + ano
247             datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
248         except ValueError:
249             return False
250
251         #Valid format and valid date
252         return True
253
254
255     # Norway VAT validation, contributed by Rolv Råen (adEgo) <rora@adego.no>
256     def check_vat_no(self, vat):
257         '''
258         Check Norway VAT number.See http://www.brreg.no/english/coordination/number.html
259         '''
260         if len(vat) != 9:
261             return False
262         try:
263             int(vat)
264         except ValueError:
265             return False
266
267         sum = (3 * int(vat[0])) + (2 * int(vat[1])) + \
268             (7 * int(vat[2])) + (6 * int(vat[3])) + \
269             (5 * int(vat[4])) + (4 * int(vat[5])) + \
270             (3 * int(vat[6])) + (2 * int(vat[7]))
271
272         check = 11 -(sum % 11)
273         if check == 11:
274             check = 0
275         if check == 10:
276             # 10 is not a valid check digit for an organization number
277             return False
278         return check == int(vat[8])
279
280     # Peruvian VAT validation, contributed by Vauxoo
281     def check_vat_pe(self, vat):
282
283         vat_type,vat = vat and len(vat)>=2 and (vat[0], vat[1:]) or (False, False)
284
285         if vat_type and vat_type.upper() == 'D':
286             #DNI
287             return True
288         elif vat_type and vat_type.upper() == 'R':
289             #verify RUC
290             factor = '5432765432'
291             sum = 0
292             dig_check = False
293             if len(vat) != 11:
294                 return False
295             try:
296                 int(vat)
297             except ValueError:
298                 return False 
299                          
300             for f in range(0,10):
301                 sum += int(factor[f]) * int(vat[f])
302                 
303             subtraction = 11 - (sum % 11)
304             if subtraction == 10:
305                 dig_check = 0
306             elif subtraction == 11:
307                 dig_check = 1
308             else:
309                 dig_check = subtraction
310             
311             return int(vat[10]) == dig_check
312         else:
313             return False
314
315 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: