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 ##############################################################################
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
27 class res_partner(osv.osv):
28 _inherit = 'res.partner'
30 def _split_vat(self, vat):
31 vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
32 return vat_country, vat_number
34 def check_vat(self, cr, uid, ids, context=None):
36 Check the VAT number depending of the country.
37 http://sima-pc.com/nif.php
39 for partner in self.browse(cr, uid, ids, context=context):
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():
47 check = getattr(vatnumber, 'check_vat_' + vat_country)
48 if not check(vat_number):
52 def vat_change(self, cr, uid, ids, value, context=None):
53 return {'value': {'vat_subjected': bool(value)}}
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.")
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'), ()
68 _constraints = [(check_vat, _construct_constraint_msg, ["vat"])]
72 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: