stock.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. from openerp import fields, models, api
  3. class StockQuant(models.Model):
  4. _inherit = "stock.quant"
  5. @api.model
  6. def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  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. product_list = []
  11. for rec in self.env.cr.fetchall():
  12. product = self.env['product.product'].sudo().search([('id', '=', rec[0])])
  13. product_list.append(rec[0])
  14. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  15. id_domain = [x for x in domain if x[0] == 'product_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(product_list))))
  20. domain.extend([id_domain])
  21. else:
  22. domain.extend([['product_id', 'in', list(set(product_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. for rec in self.env.cr.fetchall():
  28. product = self.search([('product_id', '=', rec[0])])
  29. product_list.append(rec[0])
  30. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  31. id_domain = [x for x in domain if x[0] == 'product_id' and x[1] == 'in']
  32. if id_domain:
  33. id_domain = id_domain[0]
  34. domain.remove(id_domain)
  35. id_domain[2] = list(set(id_domain[2]) & set(list(set(product_list))))
  36. domain.extend([id_domain])
  37. else:
  38. domain.extend([['product_id', 'in', list(set(product_list))]])
  39. return super(StockQuant, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)