~savoirfairelinux-openerp/partner-contact-management/view_improvment

« back to all changes in this revision

Viewing changes to firstname_display_name_trigger/res_partner.py

[ADD] module firstname_display_name_trigger, Link module if partner_lastname and account_report_company are installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Yannick Vaucher
 
5
#    Copyright 2013 Camptocamp SA
 
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
from openerp.osv import orm, fields
 
22
 
 
23
 
 
24
class ResPartner(orm.Model):
 
25
    _inherit = 'res.partner'
 
26
 
 
27
    def _display_name_compute(self, cr, uid, ids, name, args, context=None):
 
28
        return dict(self.name_get(cr, uid, ids, context=context))
 
29
 
 
30
    def name_get(self, cr, uid, ids, context=None):
 
31
        """ By pass of name_get to use directly firstname and lastname
 
32
        as we cannot ensure name as already been computed when calling this
 
33
        method for display_name"""
 
34
        if context is None:
 
35
            context = {}
 
36
        if isinstance(ids, (int, long)):
 
37
            ids = [ids]
 
38
        res = []
 
39
        for record in self.browse(cr, uid, ids, context=context):
 
40
            name = '%s %s'%(record.lastname if record.lastname else u"",
 
41
                            record.firstname if record.firstname else u"")
 
42
            if record.parent_id and not record.is_company:
 
43
                name =  "%s, %s" % (record.parent_id.name, name)
 
44
            if context.get('show_address'):
 
45
                name = name + "\n" + self._display_address(cr, uid, record, without_company=True, context=context)
 
46
                name = name.replace('\n\n','\n')
 
47
                name = name.replace('\n\n','\n')
 
48
            if context.get('show_email') and record.email:
 
49
                name = "%s <%s>" % (name, record.email)
 
50
            res.append((record.id, name))
 
51
        return res
 
52
 
 
53
 
 
54
    _display_name_store_triggers = {
 
55
        'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)]),
 
56
                        ['parent_id', 'is_company', 'name', 'firstname', 'lastname'], 10)
 
57
        }
 
58
 
 
59
    # indirection to avoid passing a copy of the overridable method when declaring the function field
 
60
    _display_name = lambda self, *args, **kwargs: self._display_name_compute(*args, **kwargs)
 
61
 
 
62
    _columns = {
 
63
        # extra field to allow ORDER BY to match visible names
 
64
        'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers),
 
65
        }