1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
24 class res_partner_contact(osv.osv):
25 """ Partner Contact """
27 _name = "res.partner.contact"
28 _description = "Contact"
30 def _main_job(self, cr, uid, ids, fields, arg, context=None):
32 @param self: The object pointer
33 @param cr: the current row, from the database cursor,
34 @param uid: the current user’s ID for security checks,
35 @param ids: List of partner contact’s IDs
37 @param context: A standard dictionary for contextual values
38 @param arg: list of tuples of form [(‘name_of_the_field’, ‘operator’, value), ...]. """
39 res = dict.fromkeys(ids, False)
41 res_partner_job_obj = self.pool.get('res.partner.job')
42 all_job_ids = res_partner_job_obj.search(cr, uid, [])
43 all_job_names = dict(zip(all_job_ids, res_partner_job_obj.name_get(cr, uid, all_job_ids, context=context)))
45 for contact in self.browse(cr, uid, ids, context=context):
47 res[contact.id] = all_job_names.get(contact.job_ids[0].id, False)
52 'name': fields.char('Last Name', size=64, required=True),
53 'first_name': fields.char('First Name', size=64),
54 'mobile': fields.char('Mobile', size=64),
55 'title': fields.many2one('res.partner.title','Title'),
56 'website': fields.char('Website', size=120),
57 'lang_id': fields.many2one('res.lang', 'Language'),
58 'job_ids': fields.one2many('res.partner.job', 'contact_id', 'Functions and Addresses'),
59 'country_id': fields.many2one('res.country','Nationality'),
60 'birthdate': fields.date('Birth Date'),
61 'active': fields.boolean('Active', help="If the active field is set to False,\
62 it will allow you to hide the partner contact without removing it."),
63 'partner_id': fields.related('job_ids', 'address_id', 'partner_id', type='many2one',\
64 relation='res.partner', string='Main Employer'),
65 'function': fields.related('job_ids', 'function', type='char', \
66 string='Main Function'),
67 'job_id': fields.function(_main_job, method=True, type='many2one',\
68 relation='res.partner.job', string='Main Job'),
69 'email': fields.char('E-Mail', size=240),
70 'comment': fields.text('Notes', translate=True),
71 'photo': fields.binary('Image'),
75 'active' : lambda *a: True,
78 _order = "name,first_name"
80 def name_get(self, cr, user, ids, context=None):
82 """ will return name and first_name.......
83 @param self: The object pointer
84 @param cr: the current row, from the database cursor,
85 @param user: the current user’s ID for security checks,
86 @param ids: List of create menu’s IDs
87 @return: name and first_name
88 @param context: A standard dictionary for contextual values
94 for contact in self.browse(cr, user, ids, context=context):
97 _contact += "%s "%(contact.title.name)
98 _contact += contact.name or ""
99 if contact.name and contact.first_name:
101 _contact += contact.first_name or ""
102 res.append((contact.id, _contact))
105 def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=None):
111 ids = self.search(cr, uid, ['|',('name', operator, name),('first_name', operator, name)] + args, limit=limit, context=context)
113 ids = self.search(cr, uid, args, limit=limit, context=context)
114 return self.name_get(cr, uid, ids, context=context)
116 res_partner_contact()
119 class res_partner_address(osv.osv):
121 #overriding of the name_get defined in base in order to remove the old contact name
122 def name_get(self, cr, user, ids, context=None):
124 @param self: The object pointer
125 @param cr: the current row, from the database cursor,
126 @param user: the current user,
127 @param ids: List of partner address’s IDs
128 @param context: A standard dictionary for contextual values
136 for r in self.read(cr, user, ids, ['zip', 'city', 'partner_id', 'street']):
137 if context.get('contact_display', 'contact')=='partner' and r['partner_id']:
138 res.append((r['id'], r['partner_id'][1]))
140 addr = "%s %s %s" % (r.get('street', '') or '', r.get('zip', '') \
141 or '', r.get('city', '') or '')
142 res.append((r['id'], addr.strip() or '/'))
145 _name = 'res.partner.address'
146 _inherit = 'res.partner.address'
147 _description ='Partner Address'
150 'job_id': fields.related('job_ids','contact_id','job_id',type='many2one',\
151 relation='res.partner.job', string='Main Job'),
152 'job_ids': fields.one2many('res.partner.job', 'address_id', 'Contacts'),
154 res_partner_address()
156 class res_partner_job(osv.osv):
157 def name_get(self, cr, uid, ids, context=None):
159 @param self: The object pointer
160 @param cr: the current row, from the database cursor,
161 @param user: the current user,
162 @param ids: List of partner address’s IDs
163 @param context: A standard dictionary for contextual values
172 jobs = self.browse(cr, uid, ids, context=context)
174 contact_ids = [rec.contact_id.id for rec in jobs]
175 contact_names = dict(self.pool.get('res.partner.contact').name_get(cr, uid, contact_ids, context=context))
178 function_name = r.function
179 funct = function_name and (", " + function_name) or ""
180 res.append((r.id, contact_names.get(r.contact_id.id, '') + funct))
184 _name = 'res.partner.job'
185 _description ='Contact Partner Function'
186 _order = 'sequence_contact'
189 'name': fields.related('address_id', 'partner_id', type='many2one',\
190 relation='res.partner', string='Partner', help="You may\
191 enter Address first,Partner will be linked automatically if any."),
192 'address_id': fields.many2one('res.partner.address', 'Address', \
193 help='Address which is linked to the Partner'), # TO Correct: domain=[('partner_id', '=', name)]
194 'contact_id': fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
195 'function': fields.char('Partner Function', size=64, help="Function of this contact with this partner"),
196 'sequence_contact': fields.integer('Contact Seq.',help='Order of\
197 importance of this address in the list of addresses of the linked contact'),
198 'sequence_partner': fields.integer('Partner Seq.',help='Order of importance\
199 of this job title in the list of job title of the linked partner'),
200 'email': fields.char('E-Mail', size=240, help="Job E-Mail"),
201 'phone': fields.char('Phone', size=64, help="Job Phone no."),
202 'fax': fields.char('Fax', size=64, help="Job FAX no."),
203 'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
204 'other': fields.char('Other', size=64, help='Additional phone field'),
205 'date_start': fields.date('Date Start',help="Start date of job(Joining Date)"),
206 'date_stop': fields.date('Date Stop', help="Last date of job"),
207 'state': fields.selection([('past', 'Past'),('current', 'Current')], \
208 'State', required=True, help="Status of Address"),
212 'sequence_contact' : lambda *a: 0,
213 'state': lambda *a: 'current',
216 def onchange_name(self, cr, uid, ids, partner_id='', address_id='', context=None):
219 partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
220 addresses = [a.id for a in partner.address if a.type == 'contact'] or [a.id for a in partner.address]
221 address_id = addresses and addresses[0] or False
223 domain = partner_id and [('partner_id', '=', partner_id)] or []
225 return {'value': {'address_id': address_id}, 'domain': {'address_id': domain}}
227 def onchange_address(self, cr, uid, _, name, address_id, context=None):
229 @@param self: The object pointer
230 @param cr: the current row, from the database cursor,
231 @param uid: the current user,
232 @param _: List of IDs,
233 @address_id : ID of the Address selected,
234 @param context: A standard dictionary for contextual values
238 address = self.pool.get('res.partner.address').browse(cr, uid, address_id, context=context)
239 partner_id = address.partner_id.id
241 domain = name and [('partner_id', '=', name)] or []
243 return {'value': {'name': partner_id}, 'domain': {'address_id': domain}}
248 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: