[IMP]: crm: Major changes in crm
[odoo/odoo.git] / addons / crm / report / crm_lead_report.py
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
22 from osv import fields,osv
23 import tools
24
25 AVAILABLE_STATES = [
26     ('draft','Draft'),
27     ('open','Open'),
28     ('cancel', 'Cancelled'),
29     ('done', 'Closed'),
30     ('pending','Pending')
31 ]
32
33 class crm_lead_report(osv.osv):
34     """ CRM Lead Report """
35     _name = "crm.lead.report"
36     _auto = False
37 #    _inherit = "crm.case.report"
38     _description = "CRM Lead Report"
39
40     _columns = {
41         'delay_close': fields.float('Delay to close',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
42         'categ_id': fields.many2one('crm.case.categ', 'Category',\
43                          domain="[('section_id','=',section_id),\
44                         ('object_id.model', '=', 'crm.lead')]" , readonly=True),
45         'stage_id': fields.many2one ('crm.case.stage', 'Stage', \
46                          domain="[('section_id','=',section_id),\
47                         ('object_id.model', '=', 'crm.lead')]", readonly=True),
48         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
49         'company_id': fields.many2one('res.company', 'Company', readonly=True),
50     }
51     def init(self, cr):
52
53         """
54             CRM Lead Report
55             @param cr: the current row, from the database cursor
56         """
57
58         tools.drop_view_if_exists(cr, 'crm_lead_report')
59         cr.execute("""
60             create or replace view crm_lead_report as (
61                 select
62                     min(c.id) as id,
63                     to_char(c.create_date, 'YYYY') as name,
64                     to_char(c.create_date, 'MM') as month,
65                     to_char(c.create_date, 'YYYY-MM-DD') as day,
66                     c.state as state,
67                     c.user_id,
68                     c.stage_id,
69                     c.company_id,
70                     c.section_id,
71                     c.categ_id,
72                     c.partner_id,
73                     count(*) as nbr,
74                     0 as avg_answers,
75                     0.0 as perc_done,
76                     0.0 as perc_cancel,
77                     date_trunc('day',c.create_date) as create_date,
78                     avg(extract('epoch' from (c.date_closed-c.create_date)))/(3600*24) as  delay_close
79                 from
80                     crm_lead c
81                 group by to_char(c.create_date, 'YYYY'), to_char(c.create_date, 'MM'),\
82                      c.state, c.user_id,c.section_id,c.stage_id,categ_id,c.partner_id,c.company_id
83                      ,c.create_date,to_char(c.create_date, 'YYYY-MM-DD')
84             )""")
85
86 crm_lead_report()
87
88 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: