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]))
141 addr += "%s %s %s" % (r.get('street', '') or '', r.get('zip', '') \
142 or '', r.get('city', '') or '')
143 res.append((r['id'], addr.strip() or '/'))
146 _name = 'res.partner.address'
147 _inherit = 'res.partner.address'
148 _description ='Partner Address'
151 'job_id': fields.related('job_ids','contact_id','job_id',type='many2one',\
152 relation='res.partner.job', string='Main Job'),
153 'job_ids': fields.one2many('res.partner.job', 'address_id', 'Contacts'),
155 res_partner_address()
157 class res_partner_job(osv.osv):
158 def name_get(self, cr, uid, ids, context=None):
160 @param self: The object pointer
161 @param cr: the current row, from the database cursor,
162 @param user: the current user,
163 @param ids: List of partner address’s IDs
164 @param context: A standard dictionary for contextual values
173 jobs = self.browse(cr, uid, ids, context=context)
175 contact_ids = [rec.contact_id.id for rec in jobs]
176 contact_names = dict(self.pool.get('res.partner.contact').name_get(cr, uid, contact_ids, context=context))
179 function_name = r.function
180 funct = function_name and (", " + function_name) or ""
181 res.append((r.id, contact_names.get(r.contact_id.id, '') + funct))
185 _name = 'res.partner.job'
186 _description ='Contact Partner Function'
187 _order = 'sequence_contact'
190 'name': fields.related('address_id', 'partner_id', type='many2one',\
191 relation='res.partner', string='Partner', help="You may\
192 enter Address first,Partner will be linked automatically if any."),
193 'address_id': fields.many2one('res.partner.address', 'Address', \
194 help='Address which is linked to the Partner'), # TO Correct: domain=[('partner_id', '=', name)]
195 'contact_id': fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
196 'function': fields.char('Partner Function', size=64, help="Function of this contact with this partner"),
197 'sequence_contact': fields.integer('Contact Seq.',help='Order of\
198 importance of this address in the list of addresses of the linked contact'),
199 'sequence_partner': fields.integer('Partner Seq.',help='Order of importance\
200 of this job title in the list of job title of the linked partner'),
201 'email': fields.char('E-Mail', size=240, help="Job E-Mail"),
202 'phone': fields.char('Phone', size=64, help="Job Phone no."),
203 'fax': fields.char('Fax', size=64, help="Job FAX no."),
204 'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
205 'other': fields.char('Other', size=64, help='Additional phone field'),
206 'date_start': fields.date('Date Start',help="Start date of job(Joining Date)"),
207 'date_stop': fields.date('Date Stop', help="Last date of job"),
208 'state': fields.selection([('past', 'Past'),('current', 'Current')], \
209 'State', required=True, help="Status of Address"),
213 'sequence_contact' : lambda *a: 0,
214 'state': lambda *a: 'current',
217 def onchange_name(self, cr, uid, ids, address_id='', name='', context=None):
218 return {'value': {'address_id': address_id}, 'domain':{'partner_id':'name'}}
220 def onchange_partner(self, cr, uid, _, partner_id, context=None):
222 @param self: The object pointer
223 @param cr: the current row, from the database cursor,
224 @param uid: the current user,
225 @param _: List of IDs,
226 @partner_id : ID of the Partner selected,
227 @param context: A standard dictionary for contextual values
229 return {'value': {'address_id': False}}
231 def onchange_address(self, cr, uid, _, address_id, context=None):
233 @@param self: The object pointer
234 @param cr: the current row, from the database cursor,
235 @param uid: the current user,
236 @param _: List of IDs,
237 @address_id : ID of the Address selected,
238 @param context: A standard dictionary for contextual values
242 address = self.pool.get('res.partner.address')\
243 .browse(cr, uid, address_id, context=context)
244 partner_id = address.partner_id.id
245 return {'value': {'name': partner_id}}
250 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: