[IMP] Reportings Review
[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 openerp.addons.crm import crm
23 from openerp.osv import fields, osv
24 from openerp import tools
25
26 class crm_lead_report(osv.Model):
27     """ CRM Lead Analysis """
28     _name = "crm.lead.report"
29     _auto = False
30     _description = "CRM Lead Analysis"
31     _rec_name = 'date_deadline'
32     _inherit = ["crm.tracking.mixin"]
33
34     _columns = {
35         'date_deadline': fields.date('Expected Closing', readonly=True),
36         'create_date': fields.datetime('Creation Date', readonly=True),
37         'opening_date': fields.datetime('Assignation Date', readonly=True),
38         'date_closed': fields.datetime('Close Date', readonly=True),
39         'date_last_stage_update': fields.datetime('Last Stage Update', readonly=True),
40         'nbr_cases': fields.integer("# of Cases", readonly=True),
41
42         # durations
43         'delay_open': fields.float('Delay to Assign',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to open the case"),
44         'delay_close': fields.float('Delay to Close',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
45         'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
46
47         'user_id':fields.many2one('res.users', 'User', readonly=True),
48         'team_id':fields.many2one('crm.team', 'Sales Team', oldname='section_id', readonly=True),
49         'country_id':fields.many2one('res.country', 'Country', readonly=True),
50         'company_id': fields.many2one('res.company', 'Company', readonly=True),
51         'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg"),
52         'planned_revenue': fields.float('Total Revenue',digits=(16,2),readonly=True),  # TDE FIXME master: rename into total_revenue
53         'probable_revenue': fields.float('Expected Revenue', digits=(16,2),readonly=True),  # TDE FIXME master: rename into expected_revenue
54         'stage_id': fields.many2one ('crm.stage', 'Stage', readonly=True, domain="[('team_ids', '=', team_id)]"),
55         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
56         'company_id': fields.many2one('res.company', 'Company', readonly=True),
57         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
58         'type':fields.selection([
59             ('lead','Lead'),
60             ('opportunity','Opportunity'),
61         ],'Type', help="Type is used to separate Leads and Opportunities"),
62     }
63
64     def init(self, cr):
65
66         """
67             CRM Lead Report
68             @param cr: the current row, from the database cursor
69         """
70         tools.drop_view_if_exists(cr, 'crm_lead_report')
71         cr.execute("""
72             CREATE OR REPLACE VIEW crm_lead_report AS (
73                 SELECT
74                     id,
75                     c.date_deadline,
76                     count(id) as nbr_cases,
77
78                     c.date_open as opening_date,
79                     c.date_closed as date_closed,
80
81                     c.date_last_stage_update as date_last_stage_update,
82
83                     c.user_id,
84                     c.probability,
85                     c.stage_id,
86                     c.type,
87                     c.company_id,
88                     c.priority,
89                     c.team_id,
90                     c.campaign_id,
91                     c.source_id,
92                     c.medium_id,
93                     c.partner_id,
94                     c.country_id,
95                     c.planned_revenue as planned_revenue,
96                     c.planned_revenue*(c.probability/100) as probable_revenue,
97                     c.create_date as create_date,
98                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
99                     abs(extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24)) as  delay_expected,
100                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open
101                 FROM
102                     crm_lead c
103                 WHERE c.active = 'true'
104                 GROUP BY c.id
105             )""")
106
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: