2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-08-22 13:33:44 +00:00
##############################################################################
#
2010-05-11 06:59:53 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
2010-05-11 06:59:53 +00:00
# Copyright (C) 2008-2009 B2CK, Cedric Krier, Bertrand Chenal (the methods "check_vat_[a-z]{2}"
2008-08-22 13:33:44 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2008-08-22 13:33:44 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2008-08-22 13:33:44 +00:00
#
2008-11-03 19:18:56 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-08-22 13:33:44 +00:00
#
2008-11-03 19:18:56 +00:00
##############################################################################
2009-04-15 08:57:38 +00:00
import string
2010-05-12 05:59:51 +00:00
from osv import osv , fields
2010-05-11 12:50:02 +00:00
from tools . translate import _
2011-05-11 09:18:38 +00:00
import vatnumber #User should install "python-vatnumber" package from the Synaptic Package Manager to check valid VAT NUMBER
2008-08-22 13:33:44 +00:00
class res_partner ( osv . osv ) :
_inherit = ' res.partner '
2008-09-02 13:26:51 +00:00
2010-05-11 12:50:02 +00:00
def _split_vat ( self , vat ) :
vat_country , vat_number = vat [ : 2 ] . lower ( ) , vat [ 2 : ] . replace ( ' ' , ' ' )
return vat_country , vat_number
2010-11-19 13:48:01 +00:00
def check_vat ( self , cr , uid , ids , context = None ) :
2008-08-22 13:33:44 +00:00
'''
Check the VAT number depending of the country .
http : / / sima - pc . com / nif . php
'''
2010-11-19 13:48:01 +00:00
for partner in self . browse ( cr , uid , ids , context = context ) :
2008-08-22 13:33:44 +00:00
if not partner . vat :
2010-05-11 12:57:51 +00:00
continue
2010-05-11 12:50:02 +00:00
vat_country , vat_number = self . _split_vat ( partner . vat )
2011-05-11 09:18:38 +00:00
if not hasattr ( vatnumber , ' check_vat_ ' + vat_country ) :
if vat_country . upper ( ) in vatnumber . countries ( ) :
2011-04-29 07:06:23 +00:00
continue
2010-05-11 06:59:53 +00:00
return False
2011-05-11 09:18:38 +00:00
check = getattr ( vatnumber , ' check_vat_ ' + vat_country )
2008-08-26 14:32:01 +00:00
if not check ( vat_number ) :
2008-08-22 13:33:44 +00:00
return False
return True
2010-05-12 05:59:51 +00:00
def vat_change ( self , cr , uid , ids , value , context = None ) :
2008-09-02 13:26:51 +00:00
return { ' value ' : { ' vat_subjected ' : bool ( value ) } }
_columns = {
2009-01-22 12:04:01 +00:00
' 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. " )
2008-09-02 13:26:51 +00:00
}
2010-05-11 12:50:02 +00:00
2010-12-07 07:18:00 +00:00
def _construct_constraint_msg ( self , cr , uid , ids , context = None ) :
2010-05-11 12:50:02 +00:00
def default_vat_check ( cn , vn ) :
return cn [ 0 ] in string . ascii_lowercase and cn [ 1 ] in string . ascii_lowercase
vat_country , vat_number = self . _split_vat ( self . browse ( cr , uid , ids ) [ 0 ] . vat )
if default_vat_check ( vat_country , vat_number ) :
2011-05-11 09:18:38 +00:00
vat_no = vat_country in vatnumber . countries ( ) and vatnumber . countries ( ) or ' Country Code + Vat Number '
2010-05-12 05:59:51 +00:00
return _ ( ' The Vat does not seems to be correct. You should have entered something like this %s ' ) , ( vat_no )
return _ ( ' The VAT is invalid, It should begin with the country code ' ) , ( )
2010-05-11 12:50:02 +00:00
_constraints = [ ( check_vat , _construct_constraint_msg , [ " vat " ] ) ]
2011-05-11 09:18:38 +00:00
2008-08-22 13:33:44 +00:00
res_partner ( )
2011-01-17 20:44:55 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: