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