[REF] Base_contact : search() of Address corrected
[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 import netsvc
23 from osv import fields, osv
24
25 class res_partner_contact(osv.osv):
26     """ Partner Contact """
27
28     _name = "res.partner.contact"
29     _description = "res.partner.contact"
30
31     def _title_get(self,cr, user, context={}):
32         """
33             @param self: The object pointer
34             @param cr: the current row, from the database cursor,
35             @param user: the current user,
36             @param context: A standard dictionary for contextual values
37         """
38
39         obj = self.pool.get('res.partner.title')
40         ids = obj.search(cr, user, [])
41         res = obj.read(cr, user, ids, ['shortcut', 'name','domain'], context)
42         res = [(r['shortcut'], r['name']) for r in res if r['domain']=='contact']
43         return res
44
45     def _main_job(self, cr, uid, ids, fields, arg, context=None):
46         """
47             @param self: The object pointer
48             @param cr: the current row, from the database cursor,
49             @param uid: the current user’s ID for security checks,
50             @param ids: List of partner contact’s IDs
51             @fields: Get Fields
52             @param context: A standard dictionary for contextual values
53             @param arg: list of tuples of form [(‘name_of_the_field’, ‘operator’, value), ...]. """
54
55
56         res = dict.fromkeys(ids, False)
57         for contact in self.browse(cr, uid, ids, context):
58             if contact.job_ids:
59                 res[contact.id] = contact.job_ids[0].name_get()[0]
60         return res
61
62     _columns = {
63         'name': fields.char('Last Name', size=30,required=True),
64         'first_name': fields.char('First Name', size=30),
65         'mobile': fields.char('Mobile',size=30),
66         'title': fields.selection(_title_get, 'Title'),
67         'website': fields.char('Website',size=120),
68         'lang_id': fields.many2one('res.lang','Language'),
69         'job_ids': fields.one2many('res.partner.job','contact_id','Functions and Addresses'),
70         'country_id': fields.many2one('res.country','Nationality'),
71         'birthdate': fields.date('Birth Date'),
72         'active': fields.boolean('Active', help="If the active field is set to true,\
73                  it will allow you to hide the partner contact without removing it."),
74         'partner_id': fields.related('job_ids','address_id','partner_id',type='many2one',\
75                          relation='res.partner', string='Main Employer'),
76         'function_id': fields.related('job_ids','function_id',type='many2one', \
77                             relation='res.partner.function', string='Main Function'),
78         'job_id': fields.function(_main_job, method=True, type='many2one',\
79                                  relation='res.partner.job', string='Main Job'),
80         'email': fields.char('E-Mail', size=240),
81         'comment': fields.text('Notes', translate=True),
82         'photo': fields.binary('Image'),
83
84     }
85     _defaults = {
86         'active' : lambda *a: True,
87     }
88
89     _order = "name,first_name"
90
91     def name_get(self, cr, user, ids, context={}):
92
93         """ will return name and first_name.......
94             @param self: The object pointer
95             @param cr: the current row, from the database cursor,
96             @param user: the current user’s ID for security checks,
97             @param ids: List of create menu’s IDs
98             @return: name and first_name
99             @param context: A standard dictionary for contextual values
100         """
101
102         if not len(ids):
103             return []
104         res = []
105         for r in self.read(cr, user, ids, ['name','first_name','title']):
106             addr = r['title'] and str(r['title'])+" " or ''
107             addr += r.get('name', '')
108             if r['name'] and r['first_name']:
109                 addr += ' '
110             addr += (r.get('first_name', '') or '')
111             res.append((r['id'], addr))
112         return res
113
114 res_partner_contact()
115
116
117 class res_partner_address(osv.osv):
118
119     def search(self, cr, user, args, offset=0, limit=None, order=None,
120             context=None, count=False):
121         """ search parnter address
122             @param self: The object pointer
123             @param cr: the current row, from the database cursor,
124             @param user: the current user
125             @param args: list of tuples of form [(‘name_of_the_field’, ‘operator’, value), ...].
126             @param offset: The Number of Results to Pass
127             @param limit: The Number of Results to Return
128             @param context: A standard dictionary for contextual values
129         """
130         if context is None:
131             context = {}
132         if context.get('address_partner_id',False):
133             args.append(('partner_id', '=', context['address_partner_id']))
134         return super(res_partner_address, self).search(cr, user, args, offset, limit, order, context, count)
135
136     #overriding of the name_get defined in base in order to remove the old contact name
137     def name_get(self, cr, user, ids, context={}):
138         """
139             @param self: The object pointer
140             @param cr: the current row, from the database cursor,
141             @param user: the current user,
142             @param ids: List of partner address’s IDs
143             @param context: A standard dictionary for contextual values
144         """
145
146         if not len(ids):
147             return []
148         res = []
149         for r in self.read(cr, user, ids, ['zip', 'city', 'partner_id', 'street']):
150             if context.get('contact_display', 'contact')=='partner' and r['partner_id']:
151                 res.append((r['id'], r['partner_id'][1]))
152             else:
153                 addr = str('')
154                 addr += "%s %s %s" % (r.get('street', '') or '', r.get('zip', '') \
155                                     or '', r.get('city', '') or '')
156                 res.append((r['id'], addr.strip() or '/'))
157         return res
158
159     _name = 'res.partner.address'
160     _inherit = 'res.partner.address'
161     _description ='Partner Address'
162
163     _columns = {
164         'job_id': fields.related('job_ids','contact_id','job_id',type='many2one',\
165                          relation='res.partner.job', string='Main Job'),
166         'job_ids': fields.one2many('res.partner.job', 'address_id', 'Contacts'),
167     }
168 res_partner_address()
169
170 class res_partner_job(osv.osv):
171
172     def name_get(self, cr, uid, ids, context={}):
173         """
174             @param self: The object pointer
175             @param cr: the current row, from the database cursor,
176             @param user: the current user,
177             @param ids: List of partner address’s IDs
178             @param context: A standard dictionary for contextual values
179         """
180
181         if not len(ids):
182             return []
183         res = []
184         for r in self.browse(cr, uid, ids):
185             funct = r.function_id and (", " + r.function_id.name) or ""
186             res.append((r.id, self.pool.get('res.partner.contact').name_get(cr, uid, \
187                                     [r.contact_id.id])[0][1] + funct))
188         return res
189
190     def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
191         """ search parnter job
192             @param self: The object pointer
193             @param cr: the current row, from the database cursor,
194             @param user: the current user
195             @param args: list of tuples of form [(‘name_of_the_field’, ‘operator’, value), ...].
196             @param offset: The Number of Results to Pass
197             @param limit: The Number of Results to Return
198             @param context: A standard dictionary for contextual values
199         """
200
201         job_ids = []
202         for arg in args:
203             if arg[0] == 'address_id':
204                 self._order = 'sequence_partner'
205             elif arg[0] == 'contact_id':
206                 self._order = 'sequence_contact'
207
208                 contact_obj = self.pool.get('res.partner.contact')
209                 if arg[2] and not count:
210                     search_arg = ['|', ('first_name', 'ilike', arg[2]), ('name', 'ilike', arg[2])]
211                     contact_ids = contact_obj.search(cr, user, search_arg, offset=offset, limit=limit, order=order, context=context, count=count)
212                     if not contact_ids:
213                          continue
214                     contacts = contact_obj.browse(cr, user, contact_ids, context=context)
215                     for contact in contacts:
216                         job_ids.extend([item.id for item in contact.job_ids])
217
218         res = super(res_partner_job,self).search(cr, user, args, offset=offset,\
219                     limit=limit, order=order, context=context, count=count)
220         if job_ids:
221             res = list(set(res + job_ids))
222
223         return res
224
225     _name = 'res.partner.job'
226     _description ='Contact Partner Function'
227     _order = 'sequence_contact'
228
229     _columns = {
230         'name': fields.related('address_id','partner_id', type='many2one',\
231                      relation='res.partner', string='Partner', help="You may\
232                      enter Address first,Partner will be linked automatically if any."),
233         'address_id': fields.many2one('res.partner.address','Address', \
234                         help='Address which is linked to the Partner'),
235         'contact_id': fields.many2one('res.partner.contact','Contact', required=True, ondelete='cascade'),
236         'function_id': fields.many2one('res.partner.function','Partner Function', \
237                             help="Function of this contact with this partner"),
238         'sequence_contact': fields.integer('Contact Seq.',help='Order of\
239                      importance of this address in the list of addresses of the linked contact'),
240         'sequence_partner': fields.integer('Partner Seq.',help='Order of importance\
241                  of this job title in the list of job title of the linked partner'),
242         'email': fields.char('E-Mail', size=240, help="Job E-Mail"),
243         'phone': fields.char('Phone', size=64, help="Job Phone no."),
244         'fax': fields.char('Fax', size=64, help="Job FAX no."),
245         'extension': fields.char('Extension', size=64, help='Internal/External extension phone number'),
246         'other': fields.char('Other', size=64, help='Additional phone field'),
247         'date_start': fields.date('Date Start',help="Start date of job(Joining Date)"),
248         'date_stop': fields.date('Date Stop', help="Last date of job"),
249         'state': fields.selection([('past', 'Past'),('current', 'Current')], \
250                                 'State', required=True, help="Status of Address"),
251     }
252
253     _defaults = {
254         'sequence_contact' : lambda *a: 0,
255         'state': lambda *a: 'current',
256     }
257
258 res_partner_job()
259
260
261 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
262