[IMP] Reportings Review
[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 openerp import tools
23 from openerp.addons.crm import crm
24 from openerp.osv import fields, osv
25
26 AVAILABLE_STATES = [
27     ('draft', 'Draft'),
28     ('open', 'Todo'),
29     ('cancel', 'Cancelled'),
30     ('done', 'Held'),
31     ('pending', 'Pending')
32 ]
33
34
35 class crm_phonecall_report(osv.osv):
36     """ Phone calls by user and team """
37
38     _name = "crm.phonecall.report"
39     _description = "Phone calls by user and team"
40     _auto = False
41
42     _columns = {
43         'user_id':fields.many2one('res.users', 'User', readonly=True),
44         'team_id':fields.many2one('crm.team', 'Sales Team', oldname='section_id', readonly=True),
45         'priority': fields.selection([('0','Low'), ('1','Normal'), ('2','High')], 'Priority'),
46         'nbr': fields.integer('# of Cases', readonly=True),  # TDE FIXME master: rename into nbr_cases
47         'state': fields.selection(AVAILABLE_STATES, 'Status', readonly=True),
48         'create_date': fields.datetime('Create Date', readonly=True, select=True),
49         'delay_close': fields.float('Delay to close', digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to close the case"),
50         'duration': fields.float('Duration', digits=(16,2),readonly=True, group_operator="avg"),
51         'delay_open': fields.float('Delay to open',digits=(16,2),readonly=True, group_operator="avg",help="Number of Days to open the case"),
52         'categ_id': fields.many2one('crm.phonecall.category', 'Category'),
53         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
54         'company_id': fields.many2one('res.company', 'Company', readonly=True),
55         'opening_date': fields.datetime('Opening Date', readonly=True, select=True),
56         'date_closed': fields.datetime('Close Date', readonly=True, select=True),
57     }
58
59     def init(self, cr):
60
61         """ Phone Calls By User And Team
62             @param cr: the current row, from the database cursor,
63         """
64         tools.drop_view_if_exists(cr, 'crm_phonecall_report')
65         cr.execute("""
66             create or replace view crm_phonecall_report as (
67                 select
68                     id,
69                     c.date_open as opening_date,
70                     c.date_closed as date_closed,
71                     c.state,
72                     c.user_id,
73                     c.team_id,
74                     c.categ_id,
75                     c.partner_id,
76                     c.duration,
77                     c.company_id,
78                     c.priority,
79                     1 as nbr,
80                     c.create_date as create_date,
81                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
82                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open
83                 from
84                     crm_phonecall c
85             )""")
86
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: