product.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. from openerp import fields, models, api
  3. class ProductTemplate(models.Model):
  4. _inherit = "product.template"
  5. template_trending_index_sale = fields.Integer('Trending Index Sale', default=0)
  6. template_trending_index_purchase = fields.Integer('Trending Index Purchase', default=0)
  7. @api.model
  8. def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
  9. if ['id', '!=', False] in domain:
  10. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  11. 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))
  12. product_list = []
  13. template_dict = {}
  14. template_list = []
  15. for rec in self.env.cr.fetchall():
  16. product_id = self.env['product.product'].search([('id', '=', rec[0])])
  17. if product_id and product_id.product_tmpl_id:
  18. template_dict[product_id.product_tmpl_id.id] = template_dict.get(product_id.product_tmpl_id.id, 0)+ rec[1]
  19. for rec in template_dict.items():
  20. template = self.search([('id', '=', rec[0])])
  21. template and template.write({'template_trending_index_sale': rec[1]})
  22. template_list.append(rec[0])
  23. order='template_trending_index_sale desc'
  24. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  25. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  26. if id_domain:
  27. id_domain = id_domain[0]
  28. domain.remove(id_domain)
  29. id_domain[2] = list(set(id_domain[2]) & set(list(set(template_list))))
  30. domain.extend([id_domain])
  31. else:
  32. domain.extend([['id', 'in', list(set(template_list))]])
  33. if ['id', '!=', -1] in domain:
  34. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  35. 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))
  36. product_list = []
  37. template_dict = {}
  38. template_list = []
  39. for rec in self.env.cr.fetchall():
  40. product_id = self.env['product.product'].search([('id', '=', rec[0])])
  41. if product_id and product_id.product_tmpl_id:
  42. template_dict[product_id.product_tmpl_id.id] = template_dict.get(product_id.product_tmpl_id.id, 0)+ rec[1]
  43. for rec in template_dict.items():
  44. template = self.search([('id', '=', rec[0])])
  45. template and template.write({'template_trending_index_purchase': rec[1]})
  46. template_list.append(rec[0])
  47. order='template_trending_index_purchase desc'
  48. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  49. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  50. if id_domain:
  51. id_domain = id_domain[0]
  52. domain.remove(id_domain)
  53. id_domain[2] = list(set(id_domain[2]) & set(list(set(template_list))))
  54. domain.extend([id_domain])
  55. else:
  56. domain.extend([['id', 'in', list(set(template_list))]])
  57. return super(ProductTemplate, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)
  58. class ProductProduct(models.Model):
  59. _inherit = "product.product"
  60. product_trending_index_sale = fields.Integer('Trending Index Sale', default=0)
  61. product_trending_index_purchase = fields.Integer('Trending Index Purchase', default=0)
  62. @api.model
  63. def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
  64. if ['id', '!=', False] in domain:
  65. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  66. 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))
  67. product_list = []
  68. for rec in self.env.cr.fetchall():
  69. product = self.search([('id', '=', rec[0])])
  70. product and product.write({'product_trending_index_sale': rec[1]})
  71. product_list.append(rec[0])
  72. order='product_trending_index_sale desc'
  73. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  74. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  75. if id_domain:
  76. id_domain = id_domain[0]
  77. domain.remove(id_domain)
  78. id_domain[2] = list(set(id_domain[2]) & set(list(set(product_list))))
  79. domain.extend([id_domain])
  80. else:
  81. domain.extend([['id', 'in', list(set(product_list))]])
  82. if ['id', '!=', -1] in domain:
  83. top_most = self.env['ir.config_parameter'].sudo().get_param('top.most.trending.product')
  84. 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))
  85. product_list = []
  86. for rec in self.env.cr.fetchall():
  87. product = self.search([('id', '=', rec[0])])
  88. product and product.write({'product_trending_index_purchase': rec[1]})
  89. product_list.append(rec[0])
  90. order='product_trending_index_purchase desc'
  91. domain.extend([['company_id', '=', self.env.user.company_id.id]])
  92. id_domain = [x for x in domain if x[0] == 'id' and x[1] == 'in']
  93. if id_domain:
  94. id_domain = id_domain[0]
  95. domain.remove(id_domain)
  96. id_domain[2] = list(set(id_domain[2]) & set(list(set(product_list))))
  97. domain.extend([id_domain])
  98. else:
  99. domain.extend([['id', 'in', list(set(product_list))]])
  100. return super(ProductProduct, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)