bugfix
[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-2008 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
27 class res_partner_contact(osv.osv):
28     _name = "res.partner.contact"
29     _description = "res.partner.contact"
30
31     def _title_get(self,cr, user, context={}):
32         obj = self.pool.get('res.partner.title')
33         ids = obj.search(cr, user, [])
34         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
35         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
36         return res
37
38     _columns = {
39         'name': fields.char('Last Name', size=30,required=True),
40         'first_name': fields.char('First Name', size=30),
41         'mobile':fields.char('Mobile',size=30),
42         'title': fields.selection(_title_get, 'Title'),
43         'website':fields.char('Website',size=120),
44         'lang_id':fields.many2one('res.lang','Language'),
45         'job_ids':fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
46         'country_id':fields.many2one('res.country','Nationality'),
47         'birthdate':fields.date('Birth Date'),
48         'active' : fields.boolean('Active'),
49         'partner_id':fields.related('job_ids','address_id','partner_id',type='many2one', relation='res.partner', string='Main Employer'),
50         'function_id':fields.related('job_ids','function_id',type='many2one', relation='res.partner.function', string='Main Job'),
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', ''), r.get('zip', ''), r.get('city', '') )
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 Job Title'
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','Job Title'),
121         'sequence_contact':fields.integer('Sequence',help='Order of importance of this address in the list of addresses of the linked contact'),
122         'sequence_partner':fields.integer('Sequence',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         'date_start' : fields.date('Date Start'),
126         'date_stop' : fields.date('Date Stop'),
127         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True),
128     }
129
130     _defaults = {
131         'sequence_contact' : lambda *a: 0,
132         'state' : lambda *a: 'current', 
133     }
134 res_partner_job()
135
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
138