improvement
[odoo/odoo.git] / addons / base_contact / base_contact.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import netsvc
30 from osv import fields, osv
31
32
33 class res_partner_contact(osv.osv):
34     _name = "res.partner.contact"
35     _description = "res.partner.contact"
36
37     def _title_get(self,cr, user, context={}):
38         obj = self.pool.get('res.partner.title')
39         ids = obj.search(cr, user, [])
40         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
41         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
42         return res
43
44     _columns = {
45         'name': fields.char('Last Name', size=30,required=True),
46         'first_name': fields.char('First Name', size=30),
47         'mobile':fields.char('Mobile',size=30),
48         'title': fields.selection(_title_get, 'Title'),
49         'website':fields.char('Website',size=120),
50         'lang_id':fields.many2one('res.lang','Language'),
51         'job_ids':fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
52         'country_id':fields.many2one('res.country','Nationality'),
53         'birthdate':fields.date('Birth Date'),
54         'active' : fields.boolean('Active'),
55     }
56     _defaults = {
57         'active' : lambda *a: True,
58     }
59     def name_get(self, cr, user, ids, context={}):
60         #will return name and first_name.......
61         if not len(ids):
62             return []
63         res = []
64         for r in self.read(cr, user, ids, ['name','first_name','title']):
65             addr = r['title'] and str(r['title'])+" " or ''
66             addr +=str(r['name'] or '')
67             if r['name'] and r['first_name']:
68                 addr += ' '
69             addr += str(r['first_name'] or '')
70             res.append((r['id'], addr))
71         return res
72 res_partner_contact()
73
74 class res_partner_address(osv.osv):
75
76     #overriding of the name_get defined in base in order to remove the old contact name
77     def name_get(self, cr, user, ids, context={}):
78         if not len(ids):
79             return []
80         res = []
81         for r in self.read(cr, user, ids, ['zip','city','partner_id', 'street']):
82             if context.get('contact_display', 'contact')=='partner':
83                 res.append((r['id'], r['partner_id'][1]))
84             else:
85                 addr = str('')
86                 addr += str(r['street'] or '') + ' ' + str(r['zip'] or '') + ' ' + str(r['city'] or '')
87                 res.append((r['id'], addr.strip() or '/'))
88         return res
89
90     _name = 'res.partner.address'
91     _inherit='res.partner.address'
92     _description ='Partner Address'
93     _columns = {
94         'job_ids':fields.one2many('res.partner.job', 'address_id', 'Functions and Addresses'),
95         'email': fields.related('job_ids', 'email', type='char', string='Default Email'),
96     }
97 res_partner_address()
98
99 class res_partner_job(osv.osv):
100
101     def _get_partner_id(self, cr, uid, ids, *a):
102         res={}
103         for id in self.browse(cr, uid, ids):
104             res[id.id] = id.address_id.partner_id and id.address_id.partner_id.id or False
105         return res
106
107     def name_get(self, cr, uid, ids, context={}):
108         if not len(ids):
109             return []
110         res = []
111         for r in self.browse(cr, uid, ids):
112             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, [r.contact_id.id])[0][1] +", "+ r.function_id.name))
113         return res
114
115     def search(self, cr, user, args, offset=0, limit=None, order=None,
116             context=None, count=False):
117         for arg in args:
118             if arg[0]=='address_id':
119                 self._order = 'sequence_partner'
120             if arg[0]=='contact_id':
121                 self._order = 'sequence_contact'
122         return super(res_partner_job,self).search(cr, user, args, offset, limit, order, context, count)
123
124     _name = 'res.partner.job'
125     _description ='Contact Function'
126     _order = 'sequence_contact'
127     _columns = {
128         'name': fields.function(_get_partner_id, method=True, type='many2one', relation='res.partner', string='Partner'),
129         'address_id':fields.many2one('res.partner.address','Address', required=True),
130         'contact_id':fields.many2one('res.partner.contact','Contact', required=True),
131         'function_id': fields.many2one('res.partner.function','Function', required=True),
132         'sequence_contact':fields.integer('Sequence (Contact)',help='order of importance of this address in the list of addresses of the linked contact'),
133         'sequence_partner':fields.integer('Sequence (Partner)',help='order of importance of this function in the list of functions of the linked partner'),
134         'email': fields.char('E-Mail', size=240),
135         'phone': fields.char('Phone', size=64),
136         'date_start' : fields.date('Date Start'),
137         'date_stop' : fields.date('Date Stop'),
138         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True),
139     }
140
141     _defaults = {
142         'sequence_contact' : lambda *a: 0,
143         'state' : lambda *a: 'current', 
144     }
145 res_partner_job()
146
147
148 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
149