Minor modifs
[odoo/odoo.git] / addons / base_contact / base_contact.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import netsvc
24 from osv import fields, osv
25
26 class res_partner_contact(osv.osv):
27     _name = "res.partner.contact"
28     _description = "res.partner.contact"
29
30     def _title_get(self,cr, user, context={}):
31         obj = self.pool.get('res.partner.title')
32         ids = obj.search(cr, user, [])
33         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
34         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
35         return res
36
37     _columns = {
38         'name': fields.char('Last Name', size=30,required=True),
39         'first_name': fields.char('First Name', size=30),
40         'mobile':fields.char('Mobile',size=30),
41         'title': fields.selection(_title_get, 'Title'),
42         'website':fields.char('Website',size=120),
43         'lang_id':fields.many2one('res.lang','Language'),
44         'job_ids':fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
45         'country_id':fields.many2one('res.country','Nationality'),
46         'birthdate':fields.date('Birth Date'),
47         'active' : fields.boolean('Active'),
48         'partner_id':fields.related('job_ids','address_id','partner_id',type='many2one', relation='res.partner', string='Main Employer'),
49         'function_id':fields.related('job_ids','function_id',type='many2one', relation='res.partner.function', string='Main Job'),
50         'email': fields.char('E-Mail', size=240),
51     }
52     _defaults = {
53         'active' : lambda *a: True,
54     }
55     def name_get(self, cr, user, ids, context={}):
56         #will return name and first_name.......
57         if not len(ids):
58             return []
59         res = []
60         for r in self.read(cr, user, ids, ['name','first_name','title']):
61             addr = r['title'] and str(r['title'])+" " or ''
62             addr += r.get('name', '')
63             if r['name'] and r['first_name']:
64                 addr += ' '
65             addr += (r.get('first_name', '') or '')
66             res.append((r['id'], addr))
67         return res
68 res_partner_contact()
69
70 class res_partner_address(osv.osv):
71
72     #overriding of the name_get defined in base in order to remove the old contact name
73     def name_get(self, cr, user, ids, context={}):
74         if not len(ids):
75             return []
76         res = []
77         for r in self.read(cr, user, ids, ['zip','city','partner_id', 'street']):
78             if context.get('contact_display', 'contact')=='partner':
79                 res.append((r['id'], r['partner_id'][1]))
80             else:
81                 addr = str('')
82                 addr += "%s %s %s" % ( r.get('street', '') or '', r.get('zip', '') or '', r.get('city', '') or '' )
83                 res.append((r['id'], addr.strip() or '/'))
84         return res
85
86     _name = 'res.partner.address'
87     _inherit='res.partner.address'
88     _description ='Partner Address'
89     _columns = {
90         'job_ids':fields.one2many('res.partner.job', 'address_id', 'Contacts'),
91     }
92 res_partner_address()
93
94 class res_partner_job(osv.osv):
95
96     def name_get(self, cr, uid, ids, context={}):
97         if not len(ids):
98             return []
99         res = []
100         for r in self.browse(cr, uid, ids):
101             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, [r.contact_id.id])[0][1] +", "+ r.function_id.name))
102         return res
103
104     def search(self, cr, user, args, offset=0, limit=None, order=None,
105             context=None, count=False):
106         for arg in args:
107             if arg[0]=='address_id':
108                 self._order = 'sequence_partner'
109             if arg[0]=='contact_id':
110                 self._order = 'sequence_contact'
111         return super(res_partner_job,self).search(cr, user, args, offset, limit, order, context, count)
112
113     _name = 'res.partner.job'
114     _description ='Contact Partner Function'
115     _order = 'sequence_contact'
116     _columns = {
117         'name': fields.related('address_id','partner_id', type='many2one', relation='res.partner', string='Partner'),
118         'address_id':fields.many2one('res.partner.address','Address'),
119         'contact_id':fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
120         'function_id': fields.many2one('res.partner.function','Partner Function'),
121         'sequence_contact':fields.integer('Contact Seq.',help='Order of importance of this address in the list of addresses of the linked contact'),
122         'sequence_partner':fields.integer('Partner Seq.',help='Order of importance of this job title in the list of job title of the linked partner'),
123         'email': fields.char('E-Mail', size=240),
124         'phone': fields.char('Phone', size=64),
125         'fax': fields.char('Fax', size=64),
126         'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
127         'other': fields.char('Other', size=64, help='Additional phone field'),
128         'date_start' : fields.date('Date Start'),
129         'date_stop' : fields.date('Date Stop'),
130         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True),
131     }
132
133     _defaults = {
134         'sequence_contact' : lambda *a: 0,
135         'state' : lambda *a: 'current', 
136     }
137 res_partner_job()
138
139
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
141