# Copyright 2017 Alexandre Díaz # Copyright 2017 Dario Lodeiros # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from openerp import models, fields, api from odoo.osv.expression import get_unaccent_wrapper class ResPartner(models.Model): _inherit = 'res.partner' def _compute_reservations_count(self): hotel_reservation_obj = self.env['hotel.reservation'] for record in self: record.reservations_count = hotel_reservation_obj.search_count([ ('partner_id.id', '=', record.id) ]) def _compute_folios_count(self): hotel_folio_obj = self.env['hotel.folio'] for record in self: record.folios_count = hotel_folio_obj.search_count([ ('partner_id.id', '=', record.id) ]) reservations_count = fields.Integer('Reservations', compute='_compute_reservations_count') folios_count = fields.Integer('Folios', compute='_compute_folios_count') @api.model def name_search(self, name, args=None, operator='ilike', limit=100): result = super(ResPartner, self).name_search(name, args=None, operator='ilike', limit=100) if args is None: args = [] if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'): self.check_access_rights('read') where_query = self._where_calc(args) self._apply_ir_rules(where_query, 'read') from_clause, where_clause, where_clause_params = where_query.get_sql() where_str = where_clause and (" WHERE %s AND " % where_clause) or ' WHERE ' # search on the name of the contacts and of its company search_name = name if operator in ('ilike', 'like'): search_name = '%%%s%%' % name if operator in ('=ilike', '=like'): operator = operator[1:] unaccent = get_unaccent_wrapper(self.env.cr) query = """SELECT id FROM res_partner {where} ({phone} {operator} {percent} OR {mobile} {operator} {percent}) ORDER BY {display_name} {operator} {percent} desc, {display_name} """.format(where=where_str, operator=operator, phone=unaccent('phone'), display_name=unaccent('display_name'), mobile=unaccent('mobile'), percent=unaccent('%s'),) where_clause_params += [search_name]*3 if limit: query += ' limit %s' where_clause_params.append(limit) self.env.cr.execute(query, where_clause_params) partner_ids = [row[0] for row in self.env.cr.fetchall()] if partner_ids: result += self.browse(partner_ids).name_get() return result