Changed licencing terms
[odoo/odoo.git] / addons / base_contact / base_contact.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import netsvc
23 from osv import fields, osv
24
25 class res_partner_contact(osv.osv):
26     _name = "res.partner.contact"
27     _description = "res.partner.contact"
28
29     def _title_get(self,cr, user, context={}):
30         obj = self.pool.get('res.partner.title')
31         ids = obj.search(cr, user, [])
32         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
33         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
34         return res
35
36     _columns = {
37         'name': fields.char('Last Name', size=30,required=True),
38         'first_name': fields.char('First Name', size=30),
39         'mobile':fields.char('Mobile',size=30),
40         'title': fields.selection(_title_get, 'Title'),
41         'website':fields.char('Website',size=120),
42         'lang_id':fields.many2one('res.lang','Language'),
43         'job_ids':fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
44         'country_id':fields.many2one('res.country','Nationality'),
45         'birthdate':fields.date('Birth Date'),
46         'active' : fields.boolean('Active'),
47         'partner_id':fields.related('job_ids','address_id','partner_id',type='many2one', relation='res.partner', string='Main Employer'),
48         'function_id':fields.related('job_ids','function_id',type='many2one', relation='res.partner.function', string='Main Function'),
49         'job_id':fields.related('job_ids',type='many2one', relation='res.partner.job', string='Main Job'),
50         'email': fields.char('E-Mail', size=240),
51         'comment' : fields.text('Notes', translate=True),
52         'photo' : fields.binary('Image'),
53         
54     }
55     _defaults = {
56         'active' : lambda *a: True,
57     }
58     
59     _order = "name,first_name"
60         
61     def name_get(self, cr, user, ids, context={}):
62         #will return name and first_name.......
63         if not len(ids):
64             return []
65         res = []
66         for r in self.read(cr, user, ids, ['name','first_name','title']):
67             addr = r['title'] and str(r['title'])+" " or ''
68             addr += r.get('name', '')
69             if r['name'] and r['first_name']:
70                 addr += ' '
71             addr += (r.get('first_name', '') or '')
72             res.append((r['id'], addr))
73         return res
74 res_partner_contact()
75
76 class res_partner_address(osv.osv):
77
78     #overriding of the name_get defined in base in order to remove the old contact name
79     def name_get(self, cr, user, ids, context={}):
80         if not len(ids):
81             return []
82         res = []
83         for r in self.read(cr, user, ids, ['zip','city','partner_id', 'street']):
84             if context.get('contact_display', 'contact')=='partner' and r['partner_id']:
85                 res.append((r['id'], r['partner_id'][1]))
86             else:
87                 addr = str('')
88                 addr += "%s %s %s" % ( r.get('street', '') or '', r.get('zip', '') or '', r.get('city', '') or '' )
89                 res.append((r['id'], addr.strip() or '/'))
90         return res
91
92     _name = 'res.partner.address'
93     _inherit='res.partner.address'
94     _description ='Partner Address'
95     _columns = {
96         'job_id':fields.related('job_ids','contact_id','job_id',type='many2one', relation='res.partner.job', string='Main Job'),
97         'job_ids':fields.one2many('res.partner.job', 'address_id', 'Contacts'),
98     }
99 res_partner_address()
100
101 class res_partner_job(osv.osv):
102
103     def name_get(self, cr, uid, ids, context={}):
104         if not len(ids):
105             return []
106         res = []
107         for r in self.browse(cr, uid, ids):
108             funct = r.function_id and (", " + r.function_id.name) or ""
109             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, [r.contact_id.id])[0][1] + funct ))
110         return res
111
112     def search(self, cr, user, args, offset=0, limit=None, order=None,
113             context=None, count=False):
114         for arg in args:
115             if arg[0]=='address_id':
116                 self._order = 'sequence_partner'
117             if arg[0]=='contact_id':
118                 self._order = 'sequence_contact'
119         return super(res_partner_job,self).search(cr, user, args, offset, limit, order, context, count)
120
121     _name = 'res.partner.job'
122     _description ='Contact Partner Function'
123     _order = 'sequence_contact'
124     _columns = {
125         'name': fields.related('address_id','partner_id', type='many2one', relation='res.partner', string='Partner', help="You may enter Address first,Partner will be linked automatically if any."),
126         'address_id':fields.many2one('res.partner.address','Address', help='Address which is linked to the Partner'),
127         'contact_id':fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
128         'function_id': fields.many2one('res.partner.function','Partner Function', help="Function of this contact with this partner"),
129         'sequence_contact':fields.integer('Contact Seq.',help='Order of importance of this address in the list of addresses of the linked contact'),
130         'sequence_partner':fields.integer('Partner Seq.',help='Order of importance of this job title in the list of job title of the linked partner'),
131         'email': fields.char('E-Mail', size=240, help="Job E-Mail"),
132         'phone': fields.char('Phone', size=64, help="Job Phone no."),
133         'fax': fields.char('Fax', size=64, help="Job FAX no."),
134         'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
135         'other': fields.char('Other', size=64, help='Additional phone field'),
136         'date_start' : fields.date('Date Start',help="Start date of job(Joining Date)"),
137         'date_stop' : fields.date('Date Stop', help="Last date of job"),
138         'state' : fields.selection([('past', 'Past'),('current', 'Current')], 'State', required=True, help="Status of Address"),
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