[IMP] project & its related module with removing of mx.datetime dependancy
[odoo/odoo.git] / addons / project_issue / report / project_issue_report.py
1
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #    
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 import tools
25 from crm import crm
26
27 AVAILABLE_STATES = [
28     ('draft','Draft'),
29     ('open','Open'),
30     ('cancel', 'Cancelled'),
31     ('done', 'Closed'),
32     ('pending','Pending')
33 ]
34 class project_issue_report(osv.osv):
35     _name = "project.issue.report"
36     _auto = False
37
38     def _get_data(self, cr, uid, ids, field_name, arg, context=None):
39         if context is None:
40             context = {}
41         """ @param cr: the current row, from the database cursor,
42             @param uid: the current users ID for security checks,
43             @param ids: List of case and section Datas IDs
44             @param context: A standard dictionary for contextual values """
45
46         res = {}
47         avg_ans = 0.0
48
49         for case in self.browse(cr, uid, ids, context):
50             if field_name != 'avg_answers':
51                 state = field_name[5:]
52                 cr.execute("select count(*) from crm_opportunity where \
53                     section_id =%s and state='%s'"%(case.section_id.id, state))
54                 state_cases = cr.fetchone()[0]
55                 perc_state = (state_cases / float(case.nbr)) * 100
56
57                 res[case.id] = perc_state
58             else:
59                 model_name = self._name.split('report.')
60                 if len(model_name) < 2:
61                     res[case.id] = 0.0
62                 else:
63                     model_name = model_name[1]
64
65                     cr.execute("select count(*) from crm_case_log l, ir_model m \
66                          where l.model_id=m.id and m.model = '%s'" , model_name)
67                     logs = cr.fetchone()[0]
68
69                     avg_ans = logs / case.nbr
70                     res[case.id] = avg_ans
71
72         return res
73
74     _columns = {
75         'name': fields.char('Year', size=64, required=False, readonly=True),
76         'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
77         'user_id2':fields.many2one('res.users', 'Assignee', readonly=True),
78         'section_id':fields.many2one('crm.case.section', 'Section', readonly=True),
79         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
80         'avg_answers': fields.function(_get_data, string='Avg. Answers', method=True, type="integer"),
81         'perc_done': fields.function(_get_data, string='%Done', method=True, type="float"),
82         'perc_cancel': fields.function(_get_data, string='%Cancel', method=True, type="float"),
83         'month':fields.selection([('01', 'January'), ('02', 'February'), \
84                                   ('03', 'March'), ('04', 'April'),\
85                                   ('05', 'May'), ('06', 'June'), \
86                                   ('07', 'July'), ('08', 'August'),\
87                                   ('09', 'September'), ('10', 'October'),\
88                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
89         'company_id': fields.many2one('res.company', 'Company', readonly=True),
90         'day': fields.char('Day', size=128, readonly=True),
91         'opening_date': fields.date('Date of Opening', readonly=True),
92         'creation_date': fields.date('Creation Date', readonly=True),
93         'date_closed': fields.date('Date of Closing', readonly=True),
94         'categ_id': fields.many2one('crm.case.categ', 'Category', domain="[('section_id','=',section_id),('object_id.model', '=', 'project.issue')]"),
95         'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('object_id.model', '=', 'project.issue')]"),
96         'nbr': fields.integer('# of Issues', readonly=True),
97         'working_hours_open': fields.float('Avg. Working Hours to Open', readonly=True),
98         'working_hours_close': fields.float('Avg. Working Hours to Close', readonly=True),
99         'delay_open': fields.float('Avg. Delay to Open', digits=(16,2), readonly=True, group_operator="avg",
100                                        help="Number of Days to close the project issue"),
101         'delay_close': fields.float('Avg. Delay to Close', digits=(16,2), readonly=True, group_operator="avg",
102                                        help="Number of Days to close the project issue"),
103         'company_id' : fields.many2one('res.company', 'Company'),
104         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
105         'project_id':fields.many2one('project.project', 'Project',readonly=True),
106         'type_id': fields.many2one('crm.case.resource.type', 'Version', domain="[('object_id.model', '=', 'project.issue')]"),
107         'user_id2losed': fields.date('Close Date', readonly=True),
108         'assigned_to' : fields.many2one('res.users', 'Assigned to',readonly=True),
109         'partner_id': fields.many2one('res.partner','Partner',domain="[('object_id.model', '=', 'project.issue')]"),
110         'canal_id': fields.many2one('res.partner.canal', 'Channel',readonly=True),
111         'task_id': fields.many2one('project.task', 'Task',domain="[('object_id.model', '=', 'project.issue')]" ),
112         'email': fields.integer('# Emails', size=128, readonly=True),
113     }
114
115     def init(self, cr):
116         tools.drop_view_if_exists(cr, 'project_issue_report')
117         cr.execute("""
118             CREATE OR REPLACE VIEW project_issue_report AS (
119                 SELECT
120                     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                     to_char(c.date_open, 'YYYY-MM-DD') as opening_date,
125                     to_char(c.create_date, 'YYYY-MM-DD') as creation_date,
126                     c.state,
127                     c.user_id,
128                     c.working_hours_open,
129                     c.working_hours_close,
130                     c.section_id,
131                     c.categ_id,
132                     c.stage_id,
133                     to_char(c.date_closed, 'YYYY-mm-dd') as date_closed,
134                     c.company_id as company_id,
135                     c.priority as priority,
136                     c.project_id as project_id,
137                     c.type_id as type_id,
138                     1 as nbr,
139                     c.assigned_to,
140                     c.partner_id,
141                     c.canal_id,
142                     c.task_id,
143                     date_trunc('day',c.create_date) as create_date,
144                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open,
145                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
146                     (SELECT count(id) FROM mailgate_message WHERE model='project.issue' AND res_id=c.id) AS email
147                 FROM
148                     project_issue c
149                 WHERE c.categ_id IN (select res_id from ir_model_data WHERE model = 'crm.case.categ' and name='bug_categ')
150             )""")
151
152 project_issue_report()
153
154 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: