report_gral.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #
  20. ##############################################################################
  21. from openerp import tools
  22. from openerp.osv import fields, osv
  23. class sale_reportgral(osv.osv):
  24. _name = "sale.reportgral"
  25. _description = "Listado General de Ventas"
  26. _auto = False
  27. _rec_name = 'date'
  28. _columns = {
  29. 'date': fields.datetime('Fecha de Orden', readonly=True), # TDE FIXME master: rename into date_order
  30. 'date_confirm': fields.date('Fecha Confirmada', readonly=True),
  31. 'product_id': fields.many2one('product.product', 'Producto', readonly=True),
  32. 'product_uom': fields.many2one('product.uom', 'Unidad de Medida', readonly=True),
  33. 'product_uom_qty': fields.float('# de Cant.', readonly=True),
  34. 'product_brand_id':fields.many2one('product.brand', 'Marca', readonly=True),
  35. 'partner_id': fields.many2one('res.partner', 'Cliente', readonly=True),
  36. 'warehouse_id': fields.many2one('stock.warehouse', 'Sucursal', readonly=True),
  37. 'city_id': fields.many2one('res.country.state', 'City', readonly=True),
  38. 'city': fields.char('Ciudad', readonly=True),
  39. 'company_id': fields.many2one('res.company', 'Compania', readonly=True),
  40. 'user_id': fields.many2one('res.users', 'Vendedor', readonly=True),
  41. 'price_unit': fields.float('Precio Unit', readonly=True),
  42. 'price_total': fields.float('Precio Total', readonly=True),
  43. 'delay': fields.float('Retraso', digits=(16,2), readonly=True),
  44. 'product_categ_id': fields.many2one('product.category', 'Categoria', readonly=True),
  45. 'nbr': fields.integer('# de Lineas', readonly=True), # TDE FIXME master: rename into nbr_lines
  46. 'state': fields.selection([
  47. ('draft', 'Borrador'),
  48. ('waiting_date', 'Lista de espera'),
  49. ('manual', 'Manual En Progreso'),
  50. ('progress', 'En Progreso'),
  51. ('invoice_except', 'Excepción factura'),
  52. ('done', 'Realizado'),
  53. ('cancel', 'Cancelado')
  54. ], 'Order Status', readonly=True),
  55. 'pricelist_id': fields.many2one('product.pricelist', 'Lista de Precio', readonly=True),
  56. 'analytic_account_id': fields.many2one('account.analytic.account', 'Cuenta Analtica', readonly=True),
  57. 'section_id': fields.many2one('crm.case.section', 'Seccion'),
  58. }
  59. _order = 'date desc'
  60. def _select(self):
  61. select_str1 = """
  62. SELECT min(l.id) as id,
  63. l.product_id as product_id,
  64. t.uom_id as product_uom,
  65. l.price_unit as price_unit,
  66. sum(l.product_uom_qty / u.factor * u2.factor) as product_uom_qty,
  67. sum(l.product_uom_qty * l.price_unit * (100.0-l.discount) / 100.0) as price_total,
  68. count(*) as nbr,
  69. s.date_order as date,
  70. s.date_confirm as date_confirm,
  71. s.partner_id as partner_id,
  72. q.id as city_id,
  73. h.name as city,
  74. s.user_id as user_id,
  75. s.company_id as company_id,
  76. s.warehouse_id as warehouse_id,
  77. extract(epoch from avg(date_trunc('day',s.date_confirm)-date_trunc('day',s.create_date)))/(24*60*60)::decimal(16,2) as delay,
  78. s.state,
  79. t.categ_id as product_categ_id,
  80. t.product_brand_id as product_brand_id,
  81. s.pricelist_id as pricelist_id,
  82. s.project_id as analytic_account_id,
  83. s.section_id as section_id
  84. """
  85. return select_str1
  86. def _from(self):
  87. from_str1 = """
  88. sale_order_line l
  89. join sale_order s on (l.order_id=s.id)
  90. left join product_product p on (l.product_id=p.id)
  91. left join product_template t on (p.product_tmpl_id=t.id)
  92. left join product_brand r on (t.product_brand_id=r.id)
  93. left join res_partner q on (q.id=s.partner_id)
  94. left join res_country_state h on (h.id=q.state_id)
  95. left join stock_warehouse j on (s.warehouse_id=j.id)
  96. left join product_uom u on (u.id=l.product_uom)
  97. left join product_uom u2 on (u2.id=t.uom_id)
  98. """
  99. return from_str1
  100. def _group_by(self):
  101. group_by_str1 = """
  102. GROUP BY l.product_id,
  103. l.order_id,
  104. t.uom_id,
  105. t.categ_id,
  106. l.price_unit,
  107. s.date_order,
  108. q.id,
  109. h.name,
  110. s.date_confirm,
  111. s.partner_id,
  112. s.user_id,
  113. s.company_id,
  114. s.warehouse_id,
  115. s.state,
  116. s.pricelist_id,
  117. t.product_brand_id,
  118. s.project_id,
  119. s.section_id
  120. """
  121. return group_by_str1
  122. def init(self, cr):
  123. # self._table = sale_report
  124. tools.drop_view_if_exists(cr, self._table)
  125. cr.execute("""CREATE or REPLACE VIEW %s as (
  126. %s
  127. FROM ( %s )
  128. %s
  129. )""" % (self._table, self._select(), self._from(), self._group_by()))
  130. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: