res_partner.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. from openerp import fields, models, api
  3. class ResPartner(models.Model):
  4. _inherit = "res.partner"
  5. @api.model
  6. def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
  7. if ['id', '!=', False] in domain:
  8. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  9. self.env.cr.execute('select product_id, count(product_id) as cnt from sale_order_line group by product_id order by cnt desc limit %s' % (top_most))
  10. partner_list = []
  11. for rec in self.env.cr.fetchall():
  12. sol_ids = self.env['sale.order.line'].search([('product_id', '=', rec[0]), ('order_id.state', 'not in', ['draft', 'sent', 'cancel'])])
  13. partner_list.extend(list(set([x.order_id.partner_id.id for x in sol_ids])))
  14. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  15. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  16. if id_domain:
  17. id_domain = id_domain[0]
  18. domain.remove(id_domain)
  19. id_domain[2] = list(set(id_domain[2]) & set(list(set(partner_list))))
  20. domain.extend([id_domain])
  21. else:
  22. domain.extend([['id', 'in', list(set(partner_list))]])
  23. if ['id', '!=', -1] in domain:
  24. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  25. self.env.cr.execute('select product_id, count(product_id) as cnt from purchase_order_line group by product_id order by cnt desc limit %s' % (top_most))
  26. product_list = []
  27. partner_list = []
  28. for rec in self.env.cr.fetchall():
  29. pol_ids = self.env['purchase.order.line'].search([('product_id', '=', rec[0]), ('order_id.state', 'not in', ['draft', 'sent', 'cancel'])])
  30. partner_list.extend(list(set([x.order_id.partner_id.id for x in pol_ids])))
  31. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  32. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  33. if id_domain:
  34. id_domain = id_domain[0]
  35. domain.remove(id_domain)
  36. id_domain[2] = list(set(id_domain[2]) & set(list(set(partner_list))))
  37. domain.extend([id_domain])
  38. else:
  39. domain.extend([['id', 'in', list(set(partner_list))]])
  40. return super(ResPartner, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)