[MERGE] base_vat: contribution for Croatia VAT numbers, courtesy of Milan Tribuson
[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-2011 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
27 try:
28     import vatnumber
29 except ImportError:
30     logging.getLogger('base_vat').warning("VAT validation partially unavailable because the `vatnumber` Python library cannot be found. "
31                                           "Install it to support more countries, for example with `easy_install vatnumber`.")
32     vatnumber = None
33
34 from osv import osv, fields
35 from tools.misc import ustr
36 from tools.translate import _
37
38 _ref_vat = {
39     'be': 'BE0477472701', 'at': 'ATU12345675',
40     'bg': 'BG1234567892', 'cy': 'CY12345678F',
41     'cz': 'CZ12345679', 'de': 'DE123456788',
42     'dk': 'DK12345674', 'ee': 'EE123456780',
43     'es': 'ESA12345674', 'fi': 'FI12345671',
44     'fr': 'FR32123456789', 'gb': 'GB123456782',
45     'gr': 'GR12345670', 'hu': 'HU12345676',
46     'ie': 'IE1234567T', 'it': 'IT12345670017',
47     'lt': 'LT123456715', 'lu': 'LU12345613',
48     'lv': 'LV41234567891', 'mt': 'MT12345634',
49     'nl': 'NL123456782B90', 'pl': 'PL1234567883',
50     'pt': 'PT123456789', 'ro': 'RO1234567897',
51     'se': 'SE123456789701', 'si': 'SI12345679',
52     'sk': 'SK0012345675', 'el': 'EL12345670',
53     'mx': 'MXABC123456T1B', 'no': 'NO123456785',
54     'hr': 'HR01234567896', # Croatia, contributed by Milan Tribuson 
55 }
56
57 class res_partner(osv.osv):
58     _inherit = 'res.partner'
59
60     def _split_vat(self, vat):
61         vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
62         return vat_country, vat_number
63
64     def check_vat(self, cr, uid, ids, context=None):
65         '''
66         Check the VAT number depending of the country.
67         http://sima-pc.com/nif.php
68         '''
69         country_obj = self.pool.get('res.country')
70         for partner in self.browse(cr, uid, ids, context=context):
71             if not partner.vat:
72                 continue
73             vat_country, vat_number = self._split_vat(partner.vat)
74             check_func_name = 'check_vat_' + vat_country
75             check_func = getattr(self, check_func_name, None) or \
76                             getattr(vatnumber, check_func_name, None)
77             if not check_func:
78                 # No VAT validation available, default to check that the country code
79                 # exists.
80                 if country_obj.search(cr, uid, [('code', 'ilike', vat_country)], context=context):
81                     continue
82                 # Country code not found, considered invalid
83                 return False
84             return check_func(vat_number)
85         return True
86
87     def vat_change(self, cr, uid, ids, value, context=None):
88         return {'value': {'vat_subjected': bool(value)}}
89
90     _columns = {
91         '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.")
92     }
93
94     def _construct_constraint_msg(self, cr, uid, ids, context=None):
95         def default_vat_check(cn, vn):
96             # by default, a VAT number is valid if:
97             #  it starts with 2 letters
98             #  has more than 3 characters
99             return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
100         vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
101         vat_no = "'CC##' (CC=Country Code, ##=VAT Number)"
102         if default_vat_check(vat_country, vat_number):
103             vat_no = _ref_vat[vat_country] if vat_country in _ref_vat else vat_no
104         return '\n' + _('This VAT number does not seem to be valid.\nNote: the expected format is %s') % vat_no
105
106     _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
107
108
109     # Mexican VAT verification, contributed by <moylop260@hotmail.com>
110     # and Panos Christeas <p_christ@hol.gr>
111     __check_vat_mx_re = re.compile(r"(?P<primeras>[A-Za-z\xd1\xf1&]{3,4})" \
112                                     r"[ \-_]?" \
113                                     r"(?P<ano>[0-9]{2})(?P<mes>[01][0-9])(?P<dia>[0-3][0-9])" \
114                                     r"[ \-_]?" \
115                                     r"(?P<code>[A-Za-z0-9&\xd1\xf1]{3})$")
116     def check_vat_mx(self, vat):
117         ''' Mexican VAT verification
118
119         Verificar RFC México
120         '''
121         # we convert to 8-bit encoding, to help the regex parse only bytes
122         vat = ustr(vat).encode('iso8859-1')
123         m = self.__check_vat_mx_re.match(vat)
124         if not m:
125             #No valid format
126             return False
127         try:
128             ano = int(m.group('ano'))
129             if ano > 30:
130                 ano = 1900 + ano
131             else:
132                 ano = 2000 + ano
133             datetime.date(ano, int(m.group('mes')), int(m.group('dia')))
134         except ValueError:
135             return False
136
137         #Valid format and valid date
138         return True
139
140
141     # Norway VAT validation, contributed by Rolv Råen (adEgo) <rora@adego.no>
142     def check_vat_no(self, vat):
143         '''
144         Check Norway VAT number.See http://www.brreg.no/english/coordination/number.html
145         '''
146         if len(vat) != 9:
147             return False
148         try:
149             int(vat)
150         except ValueError:
151             return False
152
153         sum = (3 * int(vat[0])) + (2 * int(vat[1])) + \
154             (7 * int(vat[2])) + (6 * int(vat[3])) + \
155             (5 * int(vat[4])) + (4 * int(vat[5])) + \
156             (3 * int(vat[6])) + (2 * int(vat[7]))
157
158         check = 11 -(sum % 11)
159         if check == 11:
160             check = 0
161         if check == 10:
162             # 10 is not a valid check digit for an organization number
163             return False
164         return check == int(vat[8])
165
166 res_partner()
167
168 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: