[FIX] correct various date issues in reporting
[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 section """
37
38     _name = "crm.phonecall.report"
39     _description = "Phone calls by user and section"
40     _auto = False
41     
42     _columns = {
43         'user_id':fields.many2one('res.users', 'User', readonly=True),
44         'section_id':fields.many2one('crm.case.section', 'Section', readonly=True),
45         'priority': fields.selection([('0','Low'), ('1','Normal'), ('2','High')], 'Priority'),
46         'nbr': fields.integer('# of Cases', readonly=True),
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.case.categ', 'Category', \
53                         domain="[('section_id','=',section_id),\
54                         ('object_id.model', '=', 'crm.phonecall')]"),
55         'partner_id': fields.many2one('res.partner', 'Partner' , readonly=True),
56         'company_id': fields.many2one('res.company', 'Company', readonly=True),
57         'opening_date': fields.date('Opening Date', readonly=True, select=True),
58         'date_closed': fields.date('Close Date', readonly=True, select=True),
59     }
60
61     def init(self, cr):
62
63         """ Phone Calls By User And Section
64             @param cr: the current row, from the database cursor,
65         """
66         tools.drop_view_if_exists(cr, 'crm_phonecall_report')
67         cr.execute("""
68             create or replace view crm_phonecall_report as (
69                 select
70                     id,
71                     date(c.date_open) as opening_date,
72                     date(c.date_closed) as date_closed,
73                     c.state,
74                     c.user_id,
75                     c.section_id,
76                     c.categ_id,
77                     c.partner_id,
78                     c.duration,
79                     c.company_id,
80                     c.priority,
81                     1 as nbr,
82                     c.create_date as create_date,
83                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
84                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open
85                 from
86                     crm_phonecall c
87             )""")
88
89 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: