Launchpad automatic translations update.
[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-2010 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 from osv import fields, osv
23
24 class res_partner_contact(osv.osv):
25     """ Partner Contact """
26
27     _name = "res.partner.contact"
28     _description = "Contact"
29
30     def _main_job(self, cr, uid, ids, fields, arg, context=None):
31         """
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
36             @fields: Get Fields
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)
40
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)))
44
45         for contact in self.browse(cr, uid, ids, context=context):
46             if contact.job_ids:
47                 res[contact.id] = all_job_names.get(contact.job_ids[0].id, False)
48
49         return res
50
51     _columns = {
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'),
72
73     }
74     _defaults = {
75         'active' : lambda *a: True,
76     }
77
78     _order = "name,first_name"
79
80     def name_get(self, cr, user, ids, context=None):
81
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
89         """
90
91         if not len(ids):
92             return []
93         res = []
94         for contact in self.browse(cr, user, ids, context=context):
95             _contact = ""
96             if contact.title:
97                 _contact += "%s "%(contact.title.name)
98             _contact += contact.name or ""
99             if contact.name and contact.first_name:
100                 _contact += " "
101             _contact += contact.first_name or ""
102             res.append((contact.id, _contact))
103         return res
104     
105     def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=None):
106         if not args:
107             args = []
108         if context is None:
109             context = {}
110         if name:
111             ids = self.search(cr, uid, ['|',('name', operator, name),('first_name', operator, name)] + args, limit=limit, context=context)
112         else:
113             ids = self.search(cr, uid, args, limit=limit, context=context)
114         return self.name_get(cr, uid, ids, context=context)
115     
116 res_partner_contact()
117
118
119 class res_partner_address(osv.osv):
120
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):
123         """
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
129         """
130
131         if not len(ids):
132             return []
133         res = []
134         if context is None: 
135             context = {}
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]))
139             else:
140                 addr = str('')
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 '/'))
144         return res
145
146     _name = 'res.partner.address'
147     _inherit = 'res.partner.address'
148     _description ='Partner Address'
149
150     _columns = {
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'),
154     }
155 res_partner_address()
156
157 class res_partner_job(osv.osv):
158     def name_get(self, cr, uid, ids, context=None):
159         """
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
165         """
166         if context is None:
167             context = {}
168
169         if not ids:
170             return []
171         res = []
172
173         jobs = self.browse(cr, uid, ids, context=context)
174
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))
177
178         for r in jobs:
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))
182
183         return res
184
185     _name = 'res.partner.job'
186     _description ='Contact Partner Function'
187     _order = 'sequence_contact'
188
189     _columns = {
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"),
210     }
211
212     _defaults = {
213         'sequence_contact' : lambda *a: 0,
214         'state': lambda *a: 'current',
215     }
216     
217     def onchange_name(self, cr, uid, ids, address_id='', name='', context=None):    
218         return {'value': {'address_id': address_id}, 'domain':{'partner_id':'name'}}     
219     
220     def onchange_partner(self, cr, uid, _, partner_id, context=None):
221         """
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
228         """
229         return {'value': {'address_id': False}}
230
231     def onchange_address(self, cr, uid, _, address_id, context=None):
232         """
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
239         """
240         partner_id = False
241         if address_id:
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}}
246     
247 res_partner_job()
248
249
250 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
251