diff --git a/addons/account_report_company/account_report_company.py b/addons/account_report_company/account_report_company.py index fe5d237adf2..be77be8d0b6 100644 --- a/addons/account_report_company/account_report_company.py +++ b/addons/account_report_company/account_report_company.py @@ -43,6 +43,11 @@ class res_partner(osv.Model): 'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers, select=1), } + def _get_display_name(self, unaccent): + # use stored display name for better performances + return unaccent('res_partner.display_name') + + class account_invoice(osv.Model): _inherit = 'account.invoice' diff --git a/openerp/addons/base/res/res_partner.py b/openerp/addons/base/res/res_partner.py index 4f2dcaf4b85..414a513f918 100644 --- a/openerp/addons/base/res/res_partner.py +++ b/openerp/addons/base/res/res_partner.py @@ -622,6 +622,20 @@ class res_partner(osv.osv, format_address): return super(res_partner, self)._search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count, access_rights_uid=access_rights_uid) + def _get_display_name(self, unaccent): + # TODO: simplify this in trunk with `display_name`, once it is stored + # Perf note: a CTE expression (WITH ...) seems to have an even higher cost + # than this query with duplicated CASE expressions. The bulk of + # the cost is the ORDER BY, and it is inevitable if we want + # relevant results for the next step, otherwise we'd return + # a random selection of `limit` results. + + return """CASE WHEN company.id IS NULL OR res_partner.is_company + THEN {partner_name} + ELSE {company_name} || ', ' || {partner_name} + END""".format(partner_name=unaccent('res_partner.name'), + company_name=unaccent('company.name')) + def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100): if not args: args = [] @@ -642,18 +656,7 @@ class res_partner(osv.osv, format_address): unaccent = get_unaccent_wrapper(cr) - # TODO: simplify this in trunk with `display_name`, once it is stored - # Perf note: a CTE expression (WITH ...) seems to have an even higher cost - # than this query with duplicated CASE expressions. The bulk of - # the cost is the ORDER BY, and it is inevitable if we want - # relevant results for the next step, otherwise we'd return - # a random selection of `limit` results. - - display_name = """CASE WHEN company.id IS NULL OR res_partner.is_company - THEN {partner_name} - ELSE {company_name} || ', ' || {partner_name} - END""".format(partner_name=unaccent('res_partner.name'), - company_name=unaccent('company.name')) + display_name = self._get_display_name(unaccent) query = """SELECT res_partner.id FROM res_partner