1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
29 ('cancel', 'Cancelled'),
49 class crm_lead_report(osv.osv):
50 """ CRM Lead Analysis """
51 _name = "crm.lead.report"
53 _description = "CRM Lead Analysis"
54 _rec_name = 'deadline_day'
57 # grouping fields based on Deadline Date
58 'deadline_year': fields.char('Ex. Closing Year', size=10, readonly=True, help="Expected closing year"),
59 'deadline_month':fields.selection(MONTHS, 'Exp. Closing Month', readonly=True, help="Expected closing month"),
60 'deadline_day': fields.char('Exp. Closing Day', size=10, readonly=True, help="Expected closing day"),
62 # grouping fields based on Create Date
63 'creation_year': fields.char('Creation Year', size=10, readonly=True, help="Creation year"),
64 'creation_month': fields.selection(MONTHS, 'Creation Month', readonly=True, help="Creation month"),
65 'creation_day': fields.char('Creation Day', size=10, readonly=True, help="Creation day"),
68 'create_date': fields.datetime('Create Date', readonly=True),
69 'opening_date': fields.date('Opening Date', readonly=True),
70 'date_closed': fields.date('Close Date', readonly=True),
73 'delay_open': fields.float('Delay to Open',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to open the case"),
74 'delay_close': fields.float('Delay to Close',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
75 'delay_expected': fields.float('Overpassed Deadline',digits=(16,2),readonly=True, group_operator="avg"),
77 'user_id':fields.many2one('res.users', 'User', readonly=True),
78 'country_id':fields.many2one('res.country', 'Country', readonly=True),
79 'section_id':fields.many2one('crm.case.section', 'Sales Team', readonly=True),
80 'channel_id':fields.many2one('crm.case.channel', 'Channel', readonly=True),
81 'type_id':fields.many2one('crm.case.resource.type', 'Campaign', readonly=True),
82 'state': fields.selection(AVAILABLE_STATES, 'Status', size=16, readonly=True),
83 'company_id': fields.many2one('res.company', 'Company', readonly=True),
84 'email': fields.integer('# Emails', size=128, readonly=True),
85 'probability': fields.float('Probability',digits=(16,2),readonly=True, group_operator="avg"),
86 'planned_revenue': fields.float('Planned Revenue',digits=(16,2),readonly=True),
87 'probable_revenue': fields.float('Probable Revenue', digits=(16,2),readonly=True),
88 'categ_id': fields.many2one('crm.case.categ', 'Category',\
89 domain="['|',('section_id','=',False),('section_id','=',section_id)]" , readonly=True),
90 'stage_id': fields.many2one ('crm.case.stage', 'Stage', readonly=True, domain="[('section_ids', '=', section_id)]"),
91 'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
92 'nbr': fields.integer('# of Cases', readonly=True),
93 'company_id': fields.many2one('res.company', 'Company', readonly=True),
94 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
95 'type':fields.selection([
97 ('opportunity','Opportunity'),
98 ],'Type', help="Type is used to separate Leads and Opportunities"),
108 @param cr: the current row, from the database cursor
110 tools.drop_view_if_exists(cr, 'crm_lead_report')
112 CREATE OR REPLACE VIEW crm_lead_report AS (
116 to_char(c.date_deadline, 'YYYY') as deadline_year,
117 to_char(c.date_deadline, 'MM') as deadline_month,
118 to_char(c.date_deadline, 'YYYY-MM-DD') as deadline_day,
120 to_char(c.create_date, 'YYYY') as creation_year,
121 to_char(c.create_date, 'MM') as creation_month,
122 to_char(c.create_date, 'YYYY-MM-DD') as creation_day,
124 to_char(c.date_open, 'YYYY-MM-DD') as opening_date,
125 to_char(c.date_closed, 'YYYY-mm-dd') as date_closed,
141 c.planned_revenue*(c.probability/100) as probable_revenue,
143 (SELECT count(id) FROM mail_message WHERE model='crm.lead' AND res_id=c.id AND email_from is not null) AS email,
144 date_trunc('day',c.create_date) as create_date,
145 extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as delay_close,
146 abs(extract('epoch' from (c.date_deadline - c.date_closed))/(3600*24)) as delay_expected,
147 extract('epoch' from (c.date_open-c.create_date))/(3600*24) as delay_open
150 WHERE c.active = 'true'
155 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: