022b6897ac7c00f72e90040125bc4c3906bd0e35
[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 +=str(r['name'] or '')
63             if r['name'] and r['first_name']:
64                 addr += ' '
65             addr += str(r['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 += str(r['street'] or '') + ' ' + str(r['zip'] or '') + ' ' + str(r['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         'email': fields.related('job_ids', 'email', type='char', string='Default Email'),
92     }
93 res_partner_address()
94
95 class res_partner_job(osv.osv):
96
97     def name_get(self, cr, uid, ids, context={}):
98         if not len(ids):
99             return []
100         res = []
101         for r in self.browse(cr, uid, ids):
102             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, [r.contact_id.id])[0][1] +", "+ r.function_id.name))
103         return res
104
105     def search(self, cr, user, args, offset=0, limit=None, order=None,
106             context=None, count=False):
107         for arg in args:
108             if arg[0]=='address_id':
109                 self._order = 'sequence_partner'
110             if arg[0]=='contact_id':
111                 self._order = 'sequence_contact'
112         return super(res_partner_job,self).search(cr, user, args, offset, limit, order, context, count)
113
114     _name = 'res.partner.job'
115     _description ='Contact Job Title'
116     _order = 'sequence_contact'
117     _columns = {
118         'name': fields.related('address_id','partner_id', type='many2one', relation='res.partner', string='Partner'),
119         'address_id':fields.many2one('res.partner.address','Address', required=True),
120         'contact_id':fields.many2one('res.partner.contact','Contact', required=True),
121         'function_id': fields.many2one('res.partner.function','Job Title'),
122         'sequence_contact':fields.integer('Sequence (Contact)',help='order of importance of this address in the list of addresses of the linked contact'),
123         'sequence_partner':fields.integer('Sequence (Partner)',help='order of importance of this job title in the list of job title of the linked partner'),
124         'email': fields.char('E-Mail', size=240),
125         'phone': fields.char('Phone', size=64),
126         'date_start' : fields.date('Date Start'),
127         'date_stop' : fields.date('Date Stop'),
128         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True),
129     }
130
131     _defaults = {
132         'sequence_contact' : lambda *a: 0,
133         'state' : lambda *a: 'current', 
134     }
135 res_partner_job()
136
137
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
139