analisis_compraventa.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # 'price_costo': fields.float('Price Costo', readonly=True),
  20. ##############################################################################
  21. from openerp import tools
  22. from openerp.osv import fields, osv
  23. class compraventa_analisis(osv.osv):
  24. _name = "compraventa.analisis"
  25. _description = "Analisis de Compra y Venta sobre Productos"
  26. _auto = False
  27. _columns = {
  28. 'id': fields.many2one('product.product', 'Id', readonly=True),
  29. 'name_template': fields.char('Descripcion', readonly=True),
  30. 'price_cost': fields.float('Precio compra', readonly=True),
  31. 'product_qty': fields.float('Qty compra', readonly=True),
  32. 'tpc': fields.float('Subtotal compra', readonly=True),
  33. 'price_unit': fields.float('Precio venta', readonly=True),
  34. 'product_uom_qty': fields.float('Qty venta', readonly=True),
  35. 'tpv': fields.float('Subtotal venta', readonly=True),
  36. 'tgan': fields.float('Ganancia/Perdida', readonly=True),
  37. }
  38. _order = 'id desc'
  39. def init(self, cr):
  40. tools.sql.drop_view_if_exists(cr, 'compraventa_analisis')
  41. cr.execute("""
  42. CREATE OR REPLACE VIEW compraventa_analisis AS (
  43. SELECT p.id as id, p.name_template as name_template, c.price_cost as price_cost, c.compras as product_qty, c.tpc as tpc, v.price_unit as price_unit,v.ventas as product_uom_qty, v.tpv as tpv, (v.tpv-c.tpc) as tgan
  44. FROM product_product p
  45. LEFT JOIN (
  46. SELECT MIN(r.product_id) AS id, SUM(r.price_unit/r.product_qty) as price_cost, SUM(r.product_qty) compras, (SUM(r.price_unit/r.product_qty)*SUM(r.product_qty)) tpc
  47. FROM purchase_order_line r left join purchase_order f on (f.id=r.order_id) where f.state='done' GROUP BY r.product_id) c ON c.id=p.id
  48. LEFT JOIN (
  49. SELECT MIN(l.product_id) AS id, SUM(l.price_unit/l.product_uom_qty) as price_unit, SUM(l.product_uom_qty) ventas, (SUM(l.price_unit/l.product_uom_qty)*SUM(l.product_uom_qty)) tpv
  50. FROM sale_order_line l left join sale_order s on (s.id=l.order_id) where l.state='done' GROUP BY l.product_id, l.price_unit) v ON v.id=p.id
  51. )
  52. """)
  53. compraventa_analisis()