[IMP] base_vat: Validate the VAT Number using the python.vatnumber Library => remove...
[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 import string
23 from osv import osv, fields
24 from tools.translate import _
25 import vatnumber     #User should install "python-vatnumber" package from the Synaptic Package Manager to check valid VAT NUMBER
26
27 class res_partner(osv.osv):
28     _inherit = 'res.partner'
29
30     def _split_vat(self, vat):
31         vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
32         return vat_country, vat_number
33
34     def check_vat(self, cr, uid, ids, context=None):
35         '''
36         Check the VAT number depending of the country.
37         http://sima-pc.com/nif.php
38         '''
39         for partner in self.browse(cr, uid, ids, context=context):
40             if not partner.vat:
41                 continue
42             vat_country, vat_number = self._split_vat(partner.vat)
43             if not hasattr(vatnumber, 'check_vat_' + vat_country):
44                 if vat_country.upper() in vatnumber.countries():
45                     continue
46                 return False
47             check = getattr(vatnumber, 'check_vat_' + vat_country)
48             if not check(vat_number):
49                 return False
50         return True
51
52     def vat_change(self, cr, uid, ids, value, context=None):
53         return {'value': {'vat_subjected': bool(value)}}
54
55     _columns = {
56         '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.")
57     }
58
59     def _construct_constraint_msg(self, cr, uid, ids, context=None):
60         def default_vat_check(cn, vn):
61             return cn[0] in string.ascii_lowercase and cn[1] in string.ascii_lowercase
62         vat_country, vat_number = self._split_vat(self.browse(cr, uid, ids)[0].vat)
63         if default_vat_check(vat_country, vat_number):
64             vat_no = vat_country in vatnumber.countries() and vatnumber.countries() or 'Country Code + Vat Number'
65             return _('The Vat does not seems to be correct. You should have entered something like this %s'), (vat_no)
66         return _('The VAT is invalid, It should begin with the country code'), ()
67
68     _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
69     
70 res_partner()
71
72 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: