2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2008-08-24 14:45:43 +00:00
##############################################################################
2010-03-26 13:27:16 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-08-24 14:45:43 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero 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-24 14:45:43 +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
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2008-08-24 14:45:43 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-26 13:27:16 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2008-08-24 14:45:43 +00:00
#
##############################################################################
from osv import fields , osv
2011-11-28 17:02:10 +00:00
import addons
2008-08-24 14:45:43 +00:00
class res_partner_contact ( osv . osv ) :
2010-03-26 13:27:16 +00:00
""" Partner Contact """
2008-08-24 14:45:43 +00:00
_name = " res.partner.contact "
2010-05-19 18:32:32 +00:00
_description = " Contact "
2010-09-29 09:52:55 +00:00
2011-12-18 11:42:39 +00:00
def _name_get_full ( self , cr , uid , ids , prop , unknow_none , context = None ) :
result = { }
for rec in self . browse ( cr , uid , ids , context = context ) :
result [ rec . id ] = rec . last_name + ' ' + ( rec . first_name or ' ' )
return result
2008-08-24 14:45:43 +00:00
_columns = {
2011-12-21 12:37:24 +00:00
' name ' : fields . function ( _name_get_full , string = ' Name ' , size = 64 , type = " char " , store = True , select = True ) ,
2011-12-18 11:42:39 +00:00
' last_name ' : fields . char ( ' Last Name ' , size = 64 , required = True ) ,
2010-08-05 10:15:50 +00:00
' first_name ' : fields . char ( ' First Name ' , size = 64 ) ,
' mobile ' : fields . char ( ' Mobile ' , size = 64 ) ,
2011-12-18 11:42:39 +00:00
' title ' : fields . many2one ( ' res.partner.title ' , ' Title ' , domain = [ ( ' domain ' , ' = ' , ' contact ' ) ] ) ,
2010-06-16 11:51:39 +00:00
' website ' : fields . char ( ' Website ' , size = 120 ) ,
' lang_id ' : fields . many2one ( ' res.lang ' , ' Language ' ) ,
2011-12-08 18:43:28 +00:00
' job_ids ' : fields . one2many ( ' res.partner.address ' , ' contact_id ' , ' Functions and Addresses ' ) ,
2010-03-26 13:27:16 +00:00
' country_id ' : fields . many2one ( ' res.country ' , ' Nationality ' ) ,
' birthdate ' : fields . date ( ' Birth Date ' ) ,
2010-11-15 13:15:55 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to False, \
2010-03-26 13:27:16 +00:00
it will allow you to hide the partner contact without removing it . " ),
2011-12-18 11:42:39 +00:00
' partner_id ' : fields . related ( ' job_ids ' , ' partner_id ' , type = ' many2one ' , \
2010-03-26 13:27:16 +00:00
relation = ' res.partner ' , string = ' Main Employer ' ) ,
2010-05-24 06:11:15 +00:00
' function ' : fields . related ( ' job_ids ' , ' function ' , type = ' char ' , \
string = ' Main Function ' ) ,
2009-02-20 00:06:54 +00:00
' email ' : fields . char ( ' E-Mail ' , size = 240 ) ,
2010-03-26 13:27:16 +00:00
' comment ' : fields . text ( ' Notes ' , translate = True ) ,
2011-11-28 17:02:10 +00:00
' photo ' : fields . binary ( ' Photo ' ) ,
2008-08-24 14:45:43 +00:00
}
2011-11-28 17:02:10 +00:00
def _get_photo ( self , cr , uid , context = None ) :
photo_path = addons . get_module_resource ( ' base_contact ' , ' images ' , ' photo.png ' )
return open ( photo_path , ' rb ' ) . read ( ) . encode ( ' base64 ' )
2008-08-24 14:45:43 +00:00
_defaults = {
2011-11-28 17:02:10 +00:00
' photo ' : _get_photo ,
2008-08-24 14:45:43 +00:00
' active ' : lambda * a : True ,
}
2010-03-26 13:27:16 +00:00
2011-12-21 22:15:04 +00:00
_order = " name "
2010-03-26 13:27:16 +00:00
2010-09-29 14:17:24 +00:00
def name_search ( self , cr , uid , name = ' ' , args = None , operator = ' ilike ' , context = None , limit = None ) :
if not args :
args = [ ]
if context is None :
context = { }
if name :
ids = self . search ( cr , uid , [ ' | ' , ( ' name ' , operator , name ) , ( ' first_name ' , operator , name ) ] + args , limit = limit , context = context )
else :
ids = self . search ( cr , uid , args , limit = limit , context = context )
return self . name_get ( cr , uid , ids , context = context )
2008-08-24 14:45:43 +00:00
2011-12-14 09:11:18 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2011-12-18 13:08:40 +00:00
result = { }
for obj in self . browse ( cr , uid , ids , context = context ) :
result [ obj . id ] = obj . name or ' / '
if obj . partner_id :
result [ obj . id ] = result [ obj . id ] + ' , ' + obj . partner_id . name
return result . items ( )
2010-03-26 13:27:16 +00:00
2011-12-18 11:42:39 +00:00
def _auto_init ( self , cr , context = None ) :
def table_exists ( view_name ) :
cr . execute ( ' SELECT count(relname) FROM pg_class WHERE relname = %s ' , ( view_name , ) )
value = cr . fetchone ( ) [ 0 ]
return bool ( value == 1 )
2008-08-24 14:45:43 +00:00
2011-12-18 11:42:39 +00:00
exists = table_exists ( self . _table )
super ( res_partner_contact , self ) . _auto_init ( cr , context )
if not exists :
cr . execute ( """
INSERT INTO
res_partner_contact
( id , name , last_name , title , active )
SELECT
id , COALESCE ( name , ' / ' ) , COALESCE ( name , ' / ' ) , title , true
FROM
res_partner_address """ )
cr . execute ( " alter table res_partner_address add contact_id int references res_partner_contact " )
cr . execute ( " update res_partner_address set contact_id=id " )
cr . execute ( " select setval( ' res_partner_contact_id_seq ' , (select max(id)+1 from res_partner_contact)) " )
res_partner_contact ( )
2011-09-16 09:54:46 +00:00
2011-11-25 10:33:20 +00:00
class res_partner_location ( osv . osv ) :
_name = ' res.partner.location '
2011-12-18 11:42:39 +00:00
_rec_name = ' street '
2008-08-24 14:45:43 +00:00
_columns = {
2011-12-18 11:42:39 +00:00
' street ' : fields . char ( ' Street ' , size = 128 ) ,
' street2 ' : fields . char ( ' Street2 ' , size = 128 ) ,
' zip ' : fields . char ( ' Zip ' , change_default = True , size = 24 ) ,
' city ' : fields . char ( ' City ' , size = 128 ) ,
' state_id ' : fields . many2one ( " res.country.state " , ' Fed. State ' , domain = " [( ' country_id ' , ' = ' ,country_id)] " ) ,
' country_id ' : fields . many2one ( ' res.country ' , ' Country ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner Name ' , ondelete = ' set null ' , select = True , help = " Keep empty for a private address, not related to partner. " ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
2011-12-17 09:51:49 +00:00
' job_ids ' : fields . one2many ( ' res.partner.address ' , ' location_id ' , ' Contacts ' ) ,
2011-12-18 11:42:39 +00:00
' partner_id ' : fields . related ( ' job_ids ' , ' partner_id ' , type = ' many2one ' , \
relation = ' res.partner ' , string = ' Main Partner ' ) ,
}
_defaults = {
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' res.partner.address ' , context = c ) ,
2008-08-24 14:45:43 +00:00
}
2011-12-09 16:55:31 +00:00
def _auto_init ( self , cr , context = None ) :
def table_exists ( view_name ) :
cr . execute ( ' SELECT count(relname) FROM pg_class WHERE relname = %s ' , ( view_name , ) )
value = cr . fetchone ( ) [ 0 ]
return bool ( value == 1 )
exists = table_exists ( self . _table )
super ( res_partner_location , self ) . _auto_init ( cr , context )
if not exists :
2011-12-18 11:42:39 +00:00
cr . execute ( """
INSERT INTO
res_partner_location
( id , street , street2 , zip , city ,
state_id , country_id , company_id )
SELECT
id , street , street2 , zip , city ,
state_id , country_id , company_id
FROM
res_partner_address """ )
cr . execute ( " alter table res_partner_address add location_id int references res_partner_location " )
cr . execute ( " update res_partner_address set location_id=id " )
cr . execute ( " select setval( ' res_partner_location_id_seq ' , (select max(id)+1 from res_partner_address)) " )
2008-08-24 14:45:43 +00:00
2010-09-29 09:52:55 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2011-12-18 11:42:39 +00:00
result = { }
for obj in self . browse ( cr , uid , ids , context = context ) :
res = [ ]
if obj . partner_id : res . append ( obj . partner_id . name_get ( ) [ 0 ] [ 1 ] )
if obj . city : res . append ( obj . city )
if obj . country_id : res . append ( obj . country_id . name_get ( ) [ 0 ] [ 1 ] )
result [ obj . id ] = ' , ' . join ( res )
2011-12-18 13:08:40 +00:00
return result . items ( )
2010-03-26 13:27:16 +00:00
2011-11-25 10:33:20 +00:00
res_partner_location ( )
2010-09-24 11:30:21 +00:00
2011-11-25 10:33:20 +00:00
class res_partner_address ( osv . osv ) :
2011-12-18 11:42:39 +00:00
_inherit = ' res.partner.address '
2010-09-29 09:52:55 +00:00
2011-12-17 09:51:49 +00:00
def _default_location_id ( self , cr , uid , context = None ) :
context = context or { }
if not context . get ( ' default_partner_id ' , False ) :
return False
ids = self . pool . get ( ' res.partner.location ' ) . search ( cr , uid , [ ( ' partner_id ' , ' = ' , context [ ' default_partner_id ' ] ) ] , context = context )
return ids and ids [ 0 ] or False
2008-08-24 14:45:43 +00:00
2011-12-18 11:42:39 +00:00
def onchange_location_id ( self , cr , uid , ids , location_id = False , context = { } ) :
if not location_id :
return { }
location = self . pool . get ( ' res.partner.location ' ) . browse ( cr , uid , location_id , context = context )
return { ' value ' : {
' street ' : location . street ,
' street2 ' : location . street2 ,
' zip ' : location . zip ,
' city ' : location . city ,
' country_id ' : location . country_id and location . country_id . id or False ,
' state_id ' : location . state_id and location . state_id . id or False ,
} }
2010-03-26 13:27:16 +00:00
2008-08-24 14:45:43 +00:00
_columns = {
2011-12-18 13:08:40 +00:00
' location_id ' : fields . many2one ( ' res.partner.location ' , ' Location ' ) ,
2011-12-17 09:51:49 +00:00
' contact_id ' : fields . many2one ( ' res.partner.contact ' , ' Contact ' ) ,
2011-12-08 18:43:28 +00:00
2011-12-18 11:42:39 +00:00
# fields from location
' street ' : fields . related ( ' location_id ' , ' street ' , string = ' Street ' , type = " char " , store = True , size = 128 ) ,
' street2 ' : fields . related ( ' location_id ' , ' street2 ' , string = ' Street2 ' , type = " char " , store = True , size = 128 ) ,
' zip ' : fields . related ( ' location_id ' , ' zip ' , string = ' Zip ' , type = " char " , store = True , change_default = True , size = 24 ) ,
' city ' : fields . related ( ' location_id ' , ' city ' , string = ' City ' , type = " char " , store = True , size = 128 ) ,
' state_id ' : fields . related ( ' location_id ' , ' state_id ' , relation = " res.country.state " , string = ' Fed. State ' , type = " many2one " , store = True , domain = " [( ' country_id ' , ' = ' ,country_id)] " ) ,
2011-12-17 09:51:49 +00:00
' country_id ' : fields . related ( ' location_id ' , ' country_id ' , type = ' many2one ' , string = ' Country ' , store = True , relation = ' res.country ' ) ,
' phone ' : fields . char ( ' Phone ' , size = 64 ) ,
' fax ' : fields . char ( ' Fax ' , size = 64 ) ,
' email ' : fields . char ( ' E-Mail ' , size = 240 ) ,
2011-12-18 11:42:39 +00:00
# fields from contact
' mobile ' : fields . related ( ' contact_id ' , ' mobile ' , type = ' char ' , size = 64 , string = ' Mobile ' ) ,
' name ' : fields . related ( ' contact_id ' , ' name ' , type = ' char ' , size = 64 , string = " Contact Name " , store = True ) ,
' title ' : fields . related ( ' contact_id ' , ' title ' , type = ' many2one ' , relation = ' res.partner.title ' , string = " Title " , store = True ) ,
2008-08-24 14:45:43 +00:00
}
2011-12-18 13:08:40 +00:00
def create ( self , cr , uid , data , context = { } ) :
if not data . get ( ' location_id ' , False ) :
loc_id = self . pool . get ( ' res.partner.location ' ) . create ( cr , uid , {
' street ' : data . get ( ' street ' , ' ' ) ,
' street2 ' : data . get ( ' street2 ' , ' ' ) ,
' zip ' : data . get ( ' zip ' , ' ' ) ,
' city ' : data . get ( ' city ' , ' ' ) ,
' country_id ' : data . get ( ' country_id ' , False ) ,
' state_id ' : data . get ( ' state_id ' , False )
} , context = context )
data [ ' location_id ' ] = loc_id
result = super ( res_partner_address , self ) . create ( cr , uid , data , context = context )
return result
2008-08-24 14:45:43 +00:00
2011-12-14 09:11:18 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2011-12-18 11:42:39 +00:00
result = { }
for rec in self . browse ( cr , uid , ids , context = context ) :
res = [ ]
2011-12-18 13:08:40 +00:00
if rec . partner_id :
res . append ( rec . partner_id . name_get ( ) [ 0 ] [ 1 ] )
if rec . contact_id and rec . contact_id . name :
res . append ( rec . contact_id . name )
if rec . location_id :
if rec . location_id . city : res . append ( rec . location_id . city )
if rec . location_id . country_id : res . append ( rec . location_id . country_id . name_get ( ) [ 0 ] [ 1 ] )
2011-12-18 11:42:39 +00:00
result [ rec . id ] = ' , ' . join ( res )
2011-12-18 13:08:40 +00:00
return result . items ( )
2011-12-14 09:11:18 +00:00
2008-08-24 14:45:43 +00:00
_defaults = {
2011-12-17 09:51:49 +00:00
' location_id ' : _default_location_id
2008-08-24 14:45:43 +00:00
}
2011-11-25 10:33:20 +00:00
2011-12-17 09:51:49 +00:00
def default_get ( self , cr , uid , fields = [ ] , context = None ) :
context = context or { }
if ' default_type ' in context :
del context [ ' default_type ' ]
return super ( res_partner_address , self ) . default_get ( cr , uid , fields , context )
2011-11-25 10:33:20 +00:00
res_partner_address ( )
2008-08-24 14:45:43 +00:00