1
# -*- coding: utf-8 -*-
2
#########################################################################
4
# Copyright (C) 2012 Factor Libre SL #
6
#This program is free software: you can redistribute it and/or modify #
7
#it under the terms of the GNU General Public License as published by #
8
#the Free Software Foundation, either version 3 of the License, or #
9
#(at your option) any later version. #
11
#This program is distributed in the hope that it will be useful, #
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of #
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14
#GNU General Public License for more details. #
16
#You should have received a copy of the GNU General Public License #
17
#along with this program. If not, see <http://www.gnu.org/licenses/>. #
18
#########################################################################
21
from osv import fields
22
from tools.translate import _
26
class validate_vies_vat(osv.osv_memory):
27
_name = 'validate.vies.vat'
29
def _split_vat(self, vat):
30
vat_country, vat_number = vat[:2].lower(), vat[2:].replace(' ', '')
31
return vat_country, vat_number
33
def check_customer_vat(self, cr, uid, vat_country, vat_number):
34
partner_obj = self.pool.get('res.partner')
35
if not hasattr(partner_obj, 'check_vat_' + vat_country):
37
check = getattr(partner_obj, 'check_vat_' + vat_country)
38
if not check(vat_number):
42
def validate_vies(self, cr, uid, ids, context=None):
46
partner_obj = self.pool.get('res.partner')
47
for partner in partner_obj.browse(cr, uid, context.get('active_ids'), context=context):
48
values['valid_vies_vat'] = False
50
vat = self._split_vat(partner.vat)
51
if self.check_customer_vat(cr, uid, vat[0], vat[1]):
52
if vatnumber.check_vies(partner.vat):
53
values['valid_vies_vat'] = True
55
raise osv.except_osv(_("Error"), _("Client Vat Number not have a valid format"))
56
return {'type': 'ir.actions.act_window_close'}
57
partner_obj.write(cr, uid, [partner.id], values)
58
return {'type': 'ir.actions.act_window_close'}