[FIX] correctly type date field in crm_lead_report
[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.osv import fields, osv
23 from openerp import tools
24 from openerp.addons.crm import crm
25
26 MONTHS = [
27     ('01', 'January'),
28     ('02', 'February'),
29     ('03', 'March'),
30     ('04', 'April'),
31     ('05', 'May'),
32     ('06', 'June'),
33     ('07', 'July'),
34     ('08', 'August'),
35     ('09', 'September'),
36     ('10', 'October'),
37     ('11', 'November'),
38     ('12', 'December')
39 ]
40
41 class crm_lead_report(osv.osv):
42     """ CRM Lead Analysis """
43     _name = "crm.lead.report"
44     _auto = False
45     _description = "CRM Lead Analysis"
46     _rec_name = 'date_deadline'
47     _inherit = ["crm.tracking.mixin"]
48
49     _columns = {
50         'date_deadline': fields.date('Exp. Closing', readonly=True, help="Expected Closing"),
51         'create_date': fields.datetime('Creation Date', readonly=True),
52         'opening_date': fields.date('Assignation Date', readonly=True),
53         'date_closed': fields.date('Close Date', readonly=True),
54         'date_last_stage_update': fields.datetime('Last Stage Update', readonly=True),
55
56         # durations
57         'delay_open': fields.float('Delay to Assign',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to open the case"),
58         'delay_close': fields.float('Delay to Close',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
59         'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
60
61         'user_id':fields.many2one('res.users', 'User', readonly=True),
62        'section_id':fields.many2one('crm.case.section', 'Sales Team', readonly=True),
63         'country_id':fields.many2one('res.country', 'Country', readonly=True),
64         'company_id': fields.many2one('res.company', 'Company', readonly=True),
65         'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg"),
66         'planned_revenue': fields.float('Planned Revenue',digits=(16,2),readonly=True),
67         'probable_revenue': fields.float('Probable Revenue', digits=(16,2),readonly=True),
68         'stage_id': fields.many2one ('crm.case.stage', 'Stage', readonly=True, domain="[('section_ids', '=', section_id)]"),
69         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
70         'company_id': fields.many2one('res.company', 'Company', readonly=True),
71         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
72         'type':fields.selection([
73             ('lead','Lead'),
74             ('opportunity','Opportunity'),
75         ],'Type', help="Type is used to separate Leads and Opportunities"),
76     }
77
78     def init(self, cr):
79
80         """
81             CRM Lead Report
82             @param cr: the current row, from the database cursor
83         """
84         tools.drop_view_if_exists(cr, 'crm_lead_report')
85         cr.execute("""
86             CREATE OR REPLACE VIEW crm_lead_report AS (
87                 SELECT
88                     id,
89                     c.date_deadline,
90
91                     date(c.date_open) as opening_date,
92                     date(c.date_closed) as date_closed,
93
94                     date_trunc('day',c.date_last_stage_update) as date_last_stage_update,
95
96                     c.user_id,
97                     c.probability,
98                     c.stage_id,
99                     c.type,
100                     c.company_id,
101                     c.priority,
102                     c.section_id,
103                     c.campaign_id,
104                     c.source_id,
105                     c.medium_id,
106                     c.partner_id,
107                     c.country_id,
108                     c.planned_revenue,
109                     c.planned_revenue*(c.probability/100) as probable_revenue,
110                     date_trunc('day',c.create_date) as create_date,
111                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
112                     abs(extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24)) as  delay_expected,
113                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open
114                 FROM
115                     crm_lead c
116                 WHERE c.active = 'true'
117             )""")
118
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: