2009-10-20 10:52:23 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-05-14 09:57:41 +00:00
#
2008-11-18 14:39:15 +00:00
# OpenERP, Open Source Management Solution
2009-10-14 12:32:15 +00:00
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 12:32:15 +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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +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 12:32:15 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 12:32:15 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-05-14 09:57:41 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
2012-03-05 07:26:18 +00:00
##############################################################################
2006-12-07 13:41:40 +00:00
2012-03-06 06:49:43 +00:00
import os
2006-12-07 13:41:40 +00:00
import math
2012-03-07 15:12:33 +00:00
from osv import osv , fields
2006-12-07 13:41:40 +00:00
import tools
2009-03-06 18:06:50 +00:00
from tools . translate import _
2012-02-29 04:16:10 +00:00
import logging
2012-03-06 06:49:43 +00:00
import pooler
2006-12-07 13:41:40 +00:00
class res_payterm ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_description = ' Payment term '
_name = ' res.payterm '
2010-12-10 22:42:58 +00:00
_order = ' name '
2008-07-22 14:24:36 +00:00
_columns = {
2009-01-26 17:40:29 +00:00
' name ' : fields . char ( ' Payment Term (short name) ' , size = 64 ) ,
2008-07-22 14:24:36 +00:00
}
2012-03-06 06:49:43 +00:00
2006-12-07 13:41:40 +00:00
class res_partner_category ( osv . osv ) :
2011-12-21 14:54:43 +00:00
2009-01-23 14:17:31 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
2011-12-21 14:54:43 +00:00
""" Return the categories ' display name, including their direct
parent by default .
: param dict context : the ` ` partner_category_display ` ` key can be
used to select the short version of the
category name ( without the direct parent ) ,
when set to ` ` ' short ' ` ` . The default is
2012-03-06 06:49:43 +00:00
the long version . """
2011-12-15 14:56:04 +00:00
if context is None :
context = { }
2011-12-21 14:54:43 +00:00
if context . get ( ' partner_category_display ' ) == ' short ' :
return super ( res_partner_category , self ) . name_get ( cr , uid , ids , context = context )
2012-06-20 09:58:12 +00:00
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2009-01-23 14:17:31 +00:00
reads = self . read ( cr , uid , ids , [ ' name ' , ' parent_id ' ] , context = context )
2008-07-22 14:24:36 +00:00
res = [ ]
for record in reads :
name = record [ ' name ' ]
if record [ ' parent_id ' ] :
name = record [ ' parent_id ' ] [ 1 ] + ' / ' + name
res . append ( ( record [ ' id ' ] , name ) )
return res
2010-12-22 18:39:26 +00:00
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
if not args :
args = [ ]
if not context :
context = { }
if name :
# Be sure name_search is symetric to name_get
name = name . split ( ' / ' ) [ - 1 ]
ids = self . search ( cr , uid , [ ( ' 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 )
2009-01-23 14:17:31 +00:00
def _name_get_fnc ( self , cr , uid , ids , prop , unknow_none , context = None ) :
res = self . name_get ( cr , uid , ids , context = context )
2008-07-22 14:24:36 +00:00
return dict ( res )
2009-01-23 14:17:31 +00:00
2008-07-22 14:24:36 +00:00
_description = ' Partner Categories '
_name = ' res.partner.category '
_columns = {
2008-09-08 22:52:36 +00:00
' name ' : fields . char ( ' Category Name ' , required = True , size = 64 , translate = True ) ,
2010-12-11 00:04:50 +00:00
' parent_id ' : fields . many2one ( ' res.partner.category ' , ' Parent Category ' , select = True , ondelete = ' cascade ' ) ,
2012-01-04 13:30:27 +00:00
' complete_name ' : fields . function ( _name_get_fnc , type = " char " , string = ' Full Name ' ) ,
2009-01-26 17:40:29 +00:00
' child_ids ' : fields . one2many ( ' res.partner.category ' , ' parent_id ' , ' Child Categories ' ) ,
' active ' : fields . boolean ( ' Active ' , help = " The active field allows you to hide the category without removing it. " ) ,
2010-12-11 00:04:50 +00:00
' parent_left ' : fields . integer ( ' Left parent ' , select = True ) ,
' parent_right ' : fields . integer ( ' Right parent ' , select = True ) ,
2012-01-12 10:50:43 +00:00
' partner_ids ' : fields . many2many ( ' res.partner ' , ' res_partner_category_rel ' , ' category_id ' , ' partner_id ' , ' Partners ' ) ,
2008-07-22 14:24:36 +00:00
}
_constraints = [
2010-12-09 10:57:33 +00:00
( osv . osv . _check_recursion , ' Error ! You can not create recursive categories. ' , [ ' parent_id ' ] )
2008-07-22 14:24:36 +00:00
]
_defaults = {
' active ' : lambda * a : 1 ,
}
2010-12-11 00:04:50 +00:00
_parent_store = True
_parent_order = ' name '
_order = ' parent_left '
2012-03-06 06:49:43 +00:00
2006-12-07 13:41:40 +00:00
class res_partner_title ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' res.partner.title '
_columns = {
' name ' : fields . char ( ' Title ' , required = True , size = 46 , translate = True ) ,
2012-04-23 10:23:13 +00:00
' shortcut ' : fields . char ( ' Abbreviation ' , required = True , size = 16 , translate = True ) ,
2008-07-22 14:24:36 +00:00
' domain ' : fields . selection ( [ ( ' partner ' , ' Partner ' ) , ( ' contact ' , ' Contact ' ) ] , ' Domain ' , required = True , size = 24 )
}
_order = ' name '
2012-03-06 06:49:43 +00:00
2011-11-07 15:19:49 +00:00
def _lang_get ( self , cr , uid , context = None ) :
2012-03-06 06:49:43 +00:00
lang_pool = self . pool . get ( ' res.lang ' )
ids = lang_pool . search ( cr , uid , [ ] , context = context )
res = lang_pool . read ( cr , uid , ids , [ ' code ' , ' name ' ] , context )
2009-05-12 14:01:27 +00:00
return [ ( r [ ' code ' ] , r [ ' name ' ] ) for r in res ] + [ ( ' ' , ' ' ) ]
2012-03-08 12:27:12 +00:00
POSTAL_ADDRESS_FIELDS = ( ' street ' , ' street2 ' , ' zip ' , ' city ' , ' state_id ' , ' country_id ' )
ADDRESS_FIELDS = POSTAL_ADDRESS_FIELDS + ( ' email ' , ' phone ' , ' fax ' , ' mobile ' , ' website ' , ' ref ' , ' lang ' )
2012-03-07 15:36:46 +00:00
2006-12-07 13:41:40 +00:00
class res_partner ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_description = ' Partner '
_name = " res.partner "
2012-03-29 06:29:24 +00:00
def _address_display ( self , cr , uid , ids , name , args , context = None ) :
res = { }
2012-03-29 11:43:31 +00:00
for partner in self . browse ( cr , uid , ids , context = context ) :
res [ partner . id ] = self . _display_address ( cr , uid , partner , context = context )
2012-03-29 06:29:24 +00:00
return res
2008-07-22 14:24:36 +00:00
_order = " name "
_columns = {
2012-03-19 10:36:02 +00:00
' name ' : fields . char ( ' Name ' , size = 128 , required = True , select = True ) ,
2008-07-22 14:24:36 +00:00
' date ' : fields . date ( ' Date ' , select = 1 ) ,
2012-02-22 13:34:51 +00:00
' title ' : fields . many2one ( ' res.partner.title ' , ' Title ' ) ,
2012-05-31 07:11:34 +00:00
' parent_id ' : fields . many2one ( ' res.partner ' , ' Company ' ) ,
2012-02-23 11:41:12 +00:00
' child_ids ' : fields . one2many ( ' res.partner ' , ' parent_id ' , ' Contacts ' ) ,
2011-01-10 17:03:28 +00:00
' ref ' : fields . char ( ' Reference ' , size = 64 , select = 1 ) ,
2011-04-19 09:03:43 +00:00
' lang ' : fields . selection ( _lang_get , ' Language ' , help = " If the selected language is loaded in the system, all documents related to this partner will be printed in this language. If not, it will be english. " ) ,
2012-06-18 16:01:48 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' Salesperson ' , help = ' The internal user that is in charge of communicating with this partner if any. ' ) ,
2009-01-19 21:07:14 +00:00
' vat ' : fields . char ( ' VAT ' , size = 32 , help = " Value Added Tax number. Check the box if the partner is subjected to the VAT. Used by the VAT legal statement. " ) ,
2008-07-22 14:24:36 +00:00
' bank_ids ' : fields . one2many ( ' res.partner.bank ' , ' partner_id ' , ' Banks ' ) ,
2012-03-28 06:00:43 +00:00
' website ' : fields . char ( ' Website ' , size = 64 , help = " Website of Partner or Company " ) ,
2008-07-22 14:24:36 +00:00
' comment ' : fields . text ( ' Notes ' ) ,
2012-03-07 15:12:33 +00:00
' address ' : fields . one2many ( ' res.partner.address ' , ' partner_id ' , ' Contacts ' ) , # should be removed in version 7, but kept until then for backward compatibility
2012-05-31 07:07:16 +00:00
' category_id ' : fields . many2many ( ' res.partner.category ' , ' res_partner_category_rel ' , ' partner_id ' , ' category_id ' , ' Tags ' ) ,
2008-07-22 14:24:36 +00:00
' credit_limit ' : fields . float ( string = ' Credit Limit ' ) ,
' ean13 ' : fields . char ( ' EAN13 ' , size = 13 ) ,
2012-02-23 11:41:12 +00:00
' active ' : fields . boolean ( ' Active ' ) ,
2008-11-18 14:39:15 +00:00
' customer ' : fields . boolean ( ' Customer ' , help = " Check this box if the partner is a customer. " ) ,
' supplier ' : fields . boolean ( ' Supplier ' , help = " Check this box if the partner is a supplier. If it ' s not checked, purchase people will not see it when encoding a purchase order. " ) ,
2010-05-05 14:13:30 +00:00
' employee ' : fields . boolean ( ' Employee ' , help = " Check this box if the partner is an Employee. " ) ,
2012-02-20 09:17:05 +00:00
' function ' : fields . char ( ' Function ' , size = 128 ) ,
2012-03-06 06:49:43 +00:00
' type ' : fields . selection ( [ ( ' default ' , ' Default ' ) , ( ' invoice ' , ' Invoice ' ) ,
( ' delivery ' , ' Delivery ' ) , ( ' contact ' , ' Contact ' ) ,
2012-03-07 15:12:33 +00:00
( ' other ' , ' Other ' ) ] ,
' Address Type ' , help = " Used to select automatically the right address according to the context in sales and purchases documents. " ) ,
2012-02-20 09:17:05 +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 ) ,
2012-05-07 09:21:13 +00:00
' state_id ' : fields . many2one ( " res.country.state " , ' State ' , domain = " [( ' country_id ' , ' = ' ,country_id)] " ) ,
2012-02-20 09:17:05 +00:00
' country_id ' : fields . many2one ( ' res.country ' , ' Country ' ) ,
2012-03-08 09:17:45 +00:00
' country ' : fields . related ( ' country_id ' , type = ' many2one ' , relation = ' res.country ' , string = ' Country ' ) , # for backward compatibility
2012-02-20 09:17:05 +00:00
' email ' : fields . char ( ' E-Mail ' , size = 240 ) ,
' phone ' : fields . char ( ' Phone ' , size = 64 ) ,
' fax ' : fields . char ( ' Fax ' , size = 64 ) ,
' mobile ' : fields . char ( ' Mobile ' , size = 64 ) ,
' birthdate ' : fields . char ( ' Birthdate ' , size = 64 ) ,
2012-03-07 15:19:42 +00:00
' is_company ' : fields . boolean ( ' Company ' , help = " Check if the contact is a company, otherwise it is a person " ) ,
2012-03-28 05:30:40 +00:00
' use_parent_address ' : fields . boolean ( ' Use Company Address ' , help = " Select this if you want to set company ' s address information for this contact " ) ,
2012-02-22 13:34:51 +00:00
' photo ' : fields . binary ( ' Photo ' ) ,
2012-02-23 11:41:12 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
' color ' : fields . integer ( ' Color Index ' ) ,
2012-03-29 11:43:31 +00:00
' contact_address ' : fields . function ( _address_display , type = ' char ' , string = ' Complete Address ' ) ,
2008-07-22 14:24:36 +00:00
}
2012-03-07 15:28:27 +00:00
2011-11-07 15:19:49 +00:00
def _default_category ( self , cr , uid , context = None ) :
if context is None :
context = { }
2012-03-07 15:28:27 +00:00
if context . get ( ' category_id ' ) :
return [ context [ ' category_id ' ] ]
2012-03-06 06:49:43 +00:00
return False
2008-11-18 12:54:32 +00:00
2012-03-05 07:26:18 +00:00
def _get_photo ( self , cr , uid , is_company , context = None ) :
2012-03-07 15:19:42 +00:00
if is_company :
path = os . path . join ( tools . config [ ' root_path ' ] , ' addons ' , ' base ' , ' res ' , ' company_icon.png ' )
else :
path = os . path . join ( tools . config [ ' root_path ' ] , ' addons ' , ' base ' , ' res ' , ' photo.png ' )
return open ( path , ' rb ' ) . read ( ) . encode ( ' base64 ' )
2012-03-05 07:26:18 +00:00
2008-07-22 14:24:36 +00:00
_defaults = {
2012-02-23 11:41:12 +00:00
' active ' : True ,
' customer ' : True ,
2008-11-18 12:54:32 +00:00
' category_id ' : _default_category ,
2009-12-23 17:18:19 +00:00
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' res.partner ' , context = c ) ,
2011-09-16 12:47:25 +00:00
' color ' : 0 ,
2012-03-07 15:19:42 +00:00
' is_company ' : False ,
2012-02-20 09:17:05 +00:00
' type ' : ' default ' ,
2012-03-07 15:19:42 +00:00
' use_parent_address ' : True ,
2012-03-07 15:57:04 +00:00
' photo ' : lambda self , cr , uid , context : self . _get_photo ( cr , uid , False , context ) ,
2008-07-22 14:24:36 +00:00
}
2012-03-05 07:26:18 +00:00
2011-11-07 15:19:49 +00:00
def copy ( self , cr , uid , id , default = None , context = None ) :
if default is None :
default = { }
2011-11-21 13:33:46 +00:00
name = self . read ( cr , uid , [ id ] , [ ' name ' ] , context ) [ 0 ] [ ' name ' ]
2012-06-04 18:30:55 +00:00
default . update ( { ' name ' : _ ( ' %s (copy) ' ) % ( name ) } )
2008-07-22 14:24:36 +00:00
return super ( res_partner , self ) . copy ( cr , uid , id , default , context )
2008-09-18 11:28:57 +00:00
2012-03-07 15:19:42 +00:00
def onchange_type ( self , cr , uid , ids , is_company , context = None ) :
value = { ' title ' : False ,
' photo ' : self . _get_photo ( cr , uid , is_company , context ) }
if is_company :
value [ ' parent_id ' ] = False
domain = { ' title ' : [ ( ' domain ' , ' = ' , ' partner ' ) ] }
else :
domain = { ' title ' : [ ( ' domain ' , ' = ' , ' contact ' ) ] }
return { ' value ' : value , ' domain ' : domain }
2012-03-06 06:49:43 +00:00
2012-02-23 14:54:26 +00:00
def onchange_address ( self , cr , uid , ids , use_parent_address , parent_id , context = None ) :
2012-03-19 10:35:11 +00:00
def value_or_id ( val ) :
""" return val or val.id if val is a browse record """
return val if isinstance ( val , ( bool , int , long , float , basestring ) ) else val . id
2012-02-23 14:54:26 +00:00
if use_parent_address and parent_id :
2012-02-23 11:41:12 +00:00
parent = self . browse ( cr , uid , parent_id , context = context )
2012-03-08 12:27:12 +00:00
return { ' value ' : dict ( ( key , value_or_id ( parent [ key ] ) ) for key in ADDRESS_FIELDS ) }
return { }
2010-05-18 15:34:22 +00:00
2010-12-09 10:57:33 +00:00
def _check_ean_key ( self , cr , uid , ids , context = None ) :
2008-07-22 14:24:36 +00:00
for partner_o in pooler . get_pool ( cr . dbname ) . get ( ' res.partner ' ) . read ( cr , uid , ids , [ ' ean13 ' , ] ) :
thisean = partner_o [ ' ean13 ' ]
if thisean and thisean != ' ' :
if len ( thisean ) != 13 :
return False
sum = 0
for i in range ( 12 ) :
if not ( i % 2 ) :
sum + = int ( thisean [ i ] )
else :
sum + = 3 * int ( thisean [ i ] )
if math . ceil ( sum / 10.0 ) * 10 - sum != int ( thisean [ 12 ] ) :
return False
return True
2012-03-06 06:49:43 +00:00
2012-03-07 15:19:42 +00:00
# _constraints = [(_check_ean_key, 'Error: Invalid ean code', ['ean13'])]
2012-03-06 06:49:43 +00:00
2012-02-28 08:24:56 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2012-03-08 14:03:42 +00:00
# Update parent and siblings or children records
2012-02-28 08:54:38 +00:00
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
2012-03-19 13:07:22 +00:00
if vals . get ( ' is_company ' ) == False :
2012-03-29 06:29:24 +00:00
vals . update ( { ' child_ids ' : [ ( 5 , ) ] } )
2012-03-08 14:03:42 +00:00
for partner in self . browse ( cr , uid , ids , context = context ) :
update_ids = [ ]
if partner . is_company :
domain_children = [ ( ' parent_id ' , ' = ' , partner . id ) , ( ' use_parent_address ' , ' = ' , True ) ]
update_ids = self . search ( cr , uid , domain_children , context = context )
2012-03-27 09:13:54 +00:00
elif partner . parent_id :
if vals . get ( ' use_parent_address ' ) == True :
domain_siblings = [ ( ' parent_id ' , ' = ' , partner . parent_id . id ) , ( ' use_parent_address ' , ' = ' , True ) ]
update_ids = [ partner . parent_id . id ] + self . search ( cr , uid , domain_siblings , context = context )
if ' use_parent_address ' not in vals and partner . use_parent_address :
domain_siblings = [ ( ' parent_id ' , ' = ' , partner . parent_id . id ) , ( ' use_parent_address ' , ' = ' , True ) ]
update_ids = [ partner . parent_id . id ] + self . search ( cr , uid , domain_siblings , context = context )
2012-03-08 14:03:42 +00:00
self . update_address ( cr , uid , update_ids , vals , context )
return super ( res_partner , self ) . write ( cr , uid , ids , vals , context = context )
2012-03-06 06:49:43 +00:00
2012-02-29 09:13:45 +00:00
def create ( self , cr , uid , vals , context = None ) :
2012-03-13 06:13:38 +00:00
if context is None :
context = { }
2012-03-08 14:03:42 +00:00
# Update parent and siblings records
if vals . get ( ' parent_id ' ) and vals . get ( ' use_parent_address ' ) :
domain_siblings = [ ( ' parent_id ' , ' = ' , vals [ ' parent_id ' ] ) , ( ' use_parent_address ' , ' = ' , True ) ]
update_ids = [ vals [ ' parent_id ' ] ] + self . search ( cr , uid , domain_siblings , context = context )
self . update_address ( cr , uid , update_ids , vals , context )
2012-03-13 06:13:38 +00:00
if ' photo ' not in vals :
vals [ ' photo ' ] = self . _get_photo ( cr , uid , vals . get ( ' is_company ' , False ) or context . get ( ' default_is_company ' ) , context )
2012-03-08 14:03:42 +00:00
return super ( res_partner , self ) . create ( cr , uid , vals , context = context )
2012-03-06 06:49:43 +00:00
2012-03-08 14:03:42 +00:00
def update_address ( self , cr , uid , ids , vals , context = None ) :
addr_vals = dict ( ( key , vals [ key ] ) for key in POSTAL_ADDRESS_FIELDS if vals . get ( key ) )
return super ( res_partner , self ) . write ( cr , uid , ids , addr_vals , context )
2008-07-22 14:24:36 +00:00
2011-11-07 15:19:49 +00:00
def name_get ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
2012-06-20 09:58:12 +00:00
if isinstance ( ids , ( int , long ) ) :
ids = [ ids ]
if not ids :
2008-07-22 14:24:36 +00:00
return [ ]
2011-11-21 13:33:46 +00:00
if context . get ( ' show_ref ' ) :
2008-07-22 14:24:36 +00:00
rec_name = ' ref '
else :
rec_name = ' name '
2012-03-02 12:29:17 +00:00
reads = self . read ( cr , uid , ids , [ rec_name , ' parent_id ' ] , context = context )
res = [ ]
for record in reads :
2012-03-06 06:49:43 +00:00
name = record . get ( ' name ' , ' / ' )
2012-03-02 12:29:17 +00:00
if record [ ' parent_id ' ] :
2012-03-07 13:22:43 +00:00
name = " %s ( %s ) " % ( name , record [ ' parent_id ' ] [ 1 ] )
2012-03-02 12:29:17 +00:00
res . append ( ( record [ ' id ' ] , name ) )
2008-07-22 14:24:36 +00:00
return res
2008-09-18 11:28:57 +00:00
2009-12-09 11:42:41 +00:00
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 100 ) :
2008-07-22 14:24:36 +00:00
if not args :
2011-10-11 16:34:35 +00:00
args = [ ]
if name and operator in ( ' = ' , ' ilike ' , ' =ilike ' , ' like ' ) :
2012-03-20 10:02:51 +00:00
# search on the name of the contacts and of its company
2012-03-20 15:47:57 +00:00
name2 = operator == ' = ' and name or ' % ' + name + ' % '
2012-04-17 10:18:10 +00:00
limit_str = ' '
query_args = [ name2 ]
if limit :
limit_str = ' limit %s '
query_args + = [ limit ]
2012-03-29 06:29:24 +00:00
cr . execute ( ''' SELECT partner.id FROM res_partner partner
LEFT JOIN res_partner company ON partner . parent_id = company . id
2012-03-20 10:02:51 +00:00
WHERE partner . name | | ' ( ' | | COALESCE ( company . name , ' ' ) | | ' ) '
2012-04-17 10:18:10 +00:00
''' + operator + ''' % s ''' + limit_str, query_args)
2012-03-20 12:48:15 +00:00
ids = map ( lambda x : x [ 0 ] , cr . fetchall ( ) )
2012-03-20 11:32:30 +00:00
if args :
ids = self . search ( cr , uid , [ ( ' id ' , ' in ' , ids ) ] + args , limit = limit , context = context )
2011-10-11 16:34:35 +00:00
if ids :
return self . name_get ( cr , uid , ids , context )
return super ( res_partner , self ) . name_search ( cr , uid , name , args , operator = operator , context = context , limit = limit )
2008-07-22 14:24:36 +00:00
def _email_send ( self , cr , uid , ids , email_from , subject , body , on_error = None ) :
partners = self . browse ( cr , uid , ids )
for partner in partners :
2012-02-21 13:08:57 +00:00
if partner . email :
tools . email_send ( email_from , [ partner . email ] , subject , body , on_error )
2008-07-22 14:24:36 +00:00
return True
def email_send ( self , cr , uid , ids , email_from , subject , body , on_error = ' ' ) :
while len ( ids ) :
self . pool . get ( ' ir.cron ' ) . create ( cr , uid , {
' name ' : ' Send Partner Emails ' ,
' user_id ' : uid ,
' model ' : ' res.partner ' ,
' function ' : ' _email_send ' ,
' args ' : repr ( [ ids [ : 16 ] , email_from , subject , body , on_error ] )
} )
ids = ids [ 16 : ]
return True
2008-09-18 11:28:57 +00:00
2011-11-07 15:19:49 +00:00
def address_get ( self , cr , uid , ids , adr_pref = None ) :
if adr_pref is None :
adr_pref = [ ' default ' ]
2012-03-06 06:49:43 +00:00
result = { }
2012-02-23 13:53:47 +00:00
# retrieve addresses from the partner itself and its children
res = [ ]
2012-03-29 11:43:31 +00:00
# need to fix the ids ,It get False value in list like ids[False]
2012-02-27 07:05:28 +00:00
if ids and ids [ 0 ] != False :
for p in self . browse ( cr , uid , ids ) :
res . append ( ( p . type , p . id ) )
res . extend ( ( c . type , c . id ) for c in p . child_ids )
2012-03-29 11:43:31 +00:00
address_dict = dict ( reversed ( res ) )
2008-09-18 11:28:57 +00:00
# get the id of the (first) default address if there is one,
2008-07-22 14:24:36 +00:00
# otherwise get the id of the first address in the list
2012-03-06 06:49:43 +00:00
default_address = False
2008-07-22 14:24:36 +00:00
if res :
2012-03-29 11:43:31 +00:00
default_address = address_dict . get ( ' default ' , res [ 0 ] [ 1 ] )
2012-03-06 06:49:43 +00:00
for adr in adr_pref :
2012-03-29 11:43:31 +00:00
result [ adr ] = address_dict . get ( adr , default_address )
2008-07-22 14:24:36 +00:00
return result
2008-09-18 11:28:57 +00:00
2008-07-22 14:24:36 +00:00
def gen_next_ref ( self , cr , uid , ids ) :
if len ( ids ) != 1 :
return True
2008-09-18 11:28:57 +00:00
2008-07-22 14:24:36 +00:00
# compute the next number ref
cr . execute ( " select ref from res_partner where ref is not null order by char_length(ref) desc, ref desc limit 1 " )
res = cr . dictfetchall ( )
ref = res and res [ 0 ] [ ' ref ' ] or ' 0 '
try :
nextref = int ( ref ) + 1
2008-12-16 10:18:25 +00:00
except :
2008-07-22 14:24:36 +00:00
raise osv . except_osv ( _ ( ' Warning ' ) , _ ( " Couldn ' t generate the next id because some partners have an alphabetic id ! " ) )
# update the current partner
2008-12-09 12:37:22 +00:00
cr . execute ( " update res_partner set ref= %s where id= %s " , ( nextref , ids [ 0 ] ) )
2008-07-22 14:24:36 +00:00
return True
2008-08-25 20:15:55 +00:00
def view_header_get ( self , cr , uid , view_id , view_type , context ) :
res = super ( res_partner , self ) . view_header_get ( cr , uid , view_id , view_type , context )
if res : return res
if ( not context . get ( ' category_id ' , False ) ) :
return False
return _ ( ' Partners: ' ) + self . pool . get ( ' res.partner.category ' ) . browse ( cr , uid , context [ ' category_id ' ] , context ) . name
2012-03-06 06:49:43 +00:00
2009-12-17 19:42:50 +00:00
def main_partner ( self , cr , uid ) :
''' Return the id of the main partner
'''
model_data = self . pool . get ( ' ir.model.data ' )
2012-03-06 06:49:43 +00:00
return model_data . browse ( cr , uid ,
model_data . search ( cr , uid , [ ( ' module ' , ' = ' , ' base ' ) ,
( ' name ' , ' = ' , ' main_partner ' ) ] ) [ 0 ] ,
) . res_id
2012-03-30 04:57:52 +00:00
def _display_address ( self , cr , uid , address , context = None ) :
2012-03-23 06:40:37 +00:00
2012-02-27 10:05:30 +00:00
'''
The purpose of this function is to build and return an address formatted accordingly to the
standards of the country where it belongs .
: param address : browse record of the res . partner . address to format
: returns : the address formatted in a display that fit its country habits ( or the default ones
if not country is specified )
: rtype : string
'''
2012-03-06 06:49:43 +00:00
2012-03-06 06:09:34 +00:00
# get the information that will be injected into the display format
2012-02-27 10:05:30 +00:00
# get the address format
address_format = address . country_id and address . country_id . address_format or \
2012-03-12 09:33:32 +00:00
' %(company_name)s \n %(street)s \n %(street2)s \n %(city)s , %(state_code)s %(zip)s '
2012-02-27 10:05:30 +00:00
args = {
' state_code ' : address . state_id and address . state_id . code or ' ' ,
' state_name ' : address . state_id and address . state_id . name or ' ' ,
' country_code ' : address . country_id and address . country_id . code or ' ' ,
' country_name ' : address . country_id and address . country_id . name or ' ' ,
2012-03-05 06:34:33 +00:00
' company_name ' : address . parent_id and address . parent_id . name or ' ' ,
2012-02-27 10:05:30 +00:00
}
address_field = [ ' title ' , ' street ' , ' street2 ' , ' zip ' , ' city ' ]
for field in address_field :
args [ field ] = getattr ( address , field ) or ' '
return address_format % args
2012-03-06 06:49:43 +00:00
2012-02-23 11:41:12 +00:00
2006-12-07 13:41:40 +00:00
2012-03-19 10:50:06 +00:00
# res.partner.address is deprecated; it is still there for backward compability only and will be removed in next version
2012-02-24 13:39:08 +00:00
class res_partner_address ( osv . osv ) :
_table = " res_partner "
_name = ' res.partner.address '
_order = ' type, name '
_columns = {
2012-03-08 09:17:45 +00:00
' parent_id ' : fields . many2one ( ' res.partner ' , ' Company ' , ondelete = ' set null ' , select = True ) ,
' partner_id ' : fields . related ( ' parent_id ' , type = ' many2one ' , relation = ' res.partner ' , string = ' Partner ' ) , # for backward compatibility
2012-02-24 13:39:08 +00:00
' type ' : fields . selection ( [ ( ' default ' , ' Default ' ) , ( ' invoice ' , ' Invoice ' ) , ( ' delivery ' , ' Delivery ' ) , ( ' contact ' , ' Contact ' ) , ( ' other ' , ' Other ' ) ] , ' Address Type ' , help = " Used to select automatically the right address according to the context in sales and purchases documents. " ) ,
' function ' : fields . char ( ' Function ' , size = 128 ) ,
' title ' : fields . many2one ( ' res.partner.title ' , ' Title ' ) ,
' name ' : fields . char ( ' Contact Name ' , size = 64 , select = 1 ) ,
' 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 ' ) ,
' email ' : fields . char ( ' E-Mail ' , size = 240 ) ,
' phone ' : fields . char ( ' Phone ' , size = 64 ) ,
' fax ' : fields . char ( ' Fax ' , size = 64 ) ,
' mobile ' : fields . char ( ' Mobile ' , size = 64 ) ,
' birthdate ' : fields . char ( ' Birthdate ' , size = 64 ) ,
' is_customer_add ' : fields . related ( ' partner_id ' , ' customer ' , type = ' boolean ' , string = ' Customer ' ) ,
' is_supplier_add ' : fields . related ( ' partner_id ' , ' supplier ' , type = ' boolean ' , string = ' Supplier ' ) ,
' active ' : fields . boolean ( ' Active ' , help = " Uncheck the active field to hide the contact. " ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , select = 1 ) ,
' color ' : fields . integer ( ' Color Index ' ) ,
}
2012-03-06 06:49:43 +00:00
2012-03-16 12:48:17 +00:00
_defaults = {
' active ' : True ,
' company_id ' : lambda s , cr , uid , c : s . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' res.partner ' , context = c ) ,
' color ' : 0 ,
' type ' : ' default ' ,
}
2012-02-29 04:16:10 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2012-03-08 09:17:45 +00:00
logging . getLogger ( ' res.partner ' ) . warning ( " Deprecated use of res.partner.address " )
2012-03-16 12:48:17 +00:00
if ' partner_id ' in vals :
vals [ ' parent_id ' ] = vals . get ( ' partner_id ' )
del ( vals [ ' partner_id ' ] )
return self . pool . get ( ' res.partner ' ) . write ( cr , uid , ids , vals , context = context )
2012-03-06 06:49:43 +00:00
2012-02-29 04:16:10 +00:00
def create ( self , cr , uid , vals , context = None ) :
2012-03-08 09:17:45 +00:00
logging . getLogger ( ' res.partner ' ) . warning ( " Deprecated use of res.partner.address " )
2012-03-16 12:48:17 +00:00
if ' partner_id ' in vals :
vals [ ' parent_id ' ] = vals . get ( ' partner_id ' )
del ( vals [ ' partner_id ' ] )
return self . pool . get ( ' res.partner ' ) . create ( cr , uid , vals , context = context )
2012-03-07 15:19:42 +00:00
2008-07-23 15:01:27 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: