[IMP] crm:-reverted the useability change in the state field of Phonecall
[odoo/odoo.git] / addons / crm / report / crm_phonecall_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 from crm import crm
25
26 AVAILABLE_STATES = [
27     ('draft','Draft'),
28     ('open','Open'),
29     ('cancel', 'Cancelled'),
30     ('done', 'Closed'),
31     ('pending','Pending')
32 ]
33
34
35 class crm_phonecall_report(osv.osv):
36     """ Phone calls by user and section """
37
38     _name = "crm.phonecall.report"
39     _description = "Phone calls by user and section"
40     _auto = False
41     
42     def _get_data(self, cr, uid, ids, field_name, arg, context={}):
43
44         """ @param cr: the current row, from the database cursor,
45             @param uid: the current user’s ID for security checks,
46             @param ids: List of case and section Data’s IDs
47             @param context: A standard dictionary for contextual values """
48
49         res = {}
50         state_perc = 0.0
51         avg_ans = 0.0
52
53         for case in self.browse(cr, uid, ids, context):
54             if field_name != 'avg_answers':
55                 state = field_name[5:]
56                 cr.execute("select count(*) from crm_lead where \
57                     section_id =%s and state='%s'"%(case.section_id.id, state))
58                 state_cases = cr.fetchone()[0]
59                 perc_state = (state_cases / float(case.nbr)) * 100
60
61                 res[case.id] = perc_state
62             else:
63                 model_name = self._name.split('report.')
64                 if len(model_name) < 2:
65                     res[case.id] = 0.0
66                 else:
67                     model_name = model_name[1]
68
69                     cr.execute("select count(*) from crm_case_log l, ir_model m \
70                          where l.model_id=m.id and m.model = '%s'" , model_name)
71                     logs = cr.fetchone()[0]
72
73                     avg_ans = logs / case.nbr
74                     res[case.id] = avg_ans
75
76         return res
77
78     _columns = {
79         'name': fields.char('Year', size=64, required=False, readonly=True),
80         'user_id':fields.many2one('res.users', 'User', readonly=True),
81         'section_id':fields.many2one('crm.case.section', 'Section', readonly=True),
82         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
83         'nbr': fields.integer('# of Cases', readonly=True),
84         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
85         'avg_answers': fields.function(_get_data, string='Avg. Answers', method=True, type="integer"),
86         'perc_done': fields.function(_get_data, string='%Done', method=True, type="float"),
87         'perc_cancel': fields.function(_get_data, string='%Cancel', method=True, type="float"),
88         'month':fields.selection([('01', 'January'), ('02', 'February'), \
89                                   ('03', 'March'), ('04', 'April'),\
90                                   ('05', 'May'), ('06', 'June'), \
91                                   ('07', 'July'), ('08', 'August'),\
92                                   ('09', 'September'), ('10', 'October'),\
93                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
94         'create_date': fields.datetime('Create Date', readonly=True),
95         'day': fields.char('Day', size=128, readonly=True), 
96         'delay_close': fields.float('Delay to close', digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
97         'duration': fields.float('Duration', digits=(16,2),readonly=True, group_operator="avg"),
98         'delay_open': fields.float('Delay to open',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to open the case"),
99         'categ_id': fields.many2one('crm.case.categ', 'Category', \
100                         domain="[('section_id','=',section_id),\
101                         ('object_id.model', '=', 'crm.phonecall')]"),
102         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
103         'stage_id': fields.many2one ('crm.case.stage', 'Stage', \
104                          domain="[('section_id','=',section_id),\
105                         ('object_id.model', '=', 'crm.phonecall')]", readonly=True),
106         'company_id': fields.many2one('res.company', 'Company', readonly=True),
107         'opening_date': fields.date('Opening Date', readonly=True),
108         'creation_date': fields.date('Creation Date', readonly=True),
109         'date_closed': fields.date('Close Date', readonly=True),
110     }
111
112     def init(self, cr):
113
114         """ Phone Calls By User And Section
115             @param cr: the current row, from the database cursor,
116         """
117         tools.drop_view_if_exists(cr, 'crm_phonecall_report')
118         cr.execute("""
119             create or replace view crm_phonecall_report as (
120                 select
121                     id,
122                     to_char(c.date, 'YYYY') as name,
123                     to_char(c.date, 'MM') as month,
124                     to_char(c.date, 'YYYY-MM-DD') as day,
125                     to_char(c.create_date, 'YYYY-MM-DD') as creation_date,
126                     to_char(c.date_open, 'YYYY-MM-DD') as opening_date,
127                     to_char(c.date_closed, 'YYYY-mm-dd') as date_closed,
128                     c.state,
129                     c.user_id,
130                     c.section_id,
131                     c.categ_id,
132                     c.partner_id,
133                     c.stage_id,
134                     c.duration,
135                     c.company_id,
136                     c.priority,
137                     1 as nbr,
138                     0 as avg_answers,
139                     0.0 as perc_done,
140                     0.0 as perc_cancel,
141                     date_trunc('day',c.create_date) as create_date,
142                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
143                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open
144                 from
145                     crm_phonecall c
146             )""")
147
148 crm_phonecall_report()
149
150 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: