my_report.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # #
  2. # You should have received a copy of the GNU Affero General Public License #
  3. # along with this program. If not, see <http://www.gnu.org/licenses/>. #
  4. # #
  5. #################################################################################
  6. from openerp.osv import fields,osv
  7. from openerp import tools
  8. class my_report_model(osv.osv):
  9. _name = "my.report.model"
  10. _description = "Listado de productos mas vendidos"
  11. _auto = False
  12. _columns = {
  13. 'product_id': fields.many2one('product.product', string='Producto', required=True, readonly=True),
  14. 'nombre': fields.char('Nombre', size=128, readonly=True),
  15. 'suma': fields.float('Cantidad', readonly=True),
  16. 'lastdate': fields.date('Fecha Ult.Venta', readonly=True)
  17. }
  18. _order = 'suma ASC'
  19. def init(self, cr):
  20. tools.sql.drop_view_if_exists(cr, 'my_report_model')
  21. cr.execute("""
  22. CREATE OR REPLACE VIEW my_report_model AS (
  23. SELECT
  24. a.id AS id,
  25. c.product_id AS product_id,
  26. c.nombre AS nombre,
  27. c.pos AS suma,
  28. c.lastdate
  29. FROM product_template a
  30. LEFT JOIN(select product_id AS product_id,name AS nombre,SUM(quantity) AS pos , MAX(create_date) AS lastdate from account_invoice_line group by product_id,name) c on c.product_id=a.id
  31. WHERE c.pos>0 and a.sale_ok = True
  32. )
  33. """)
  34. my_report_model()