[IMP] crm data
[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_names, 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
48         for report in self.browse(cr, uid, ids, context):
49             res[report.id]= {
50                 'avg_answers': 0.0,
51                 'perc_done' : 0.0,
52                 'perc_cancel': 0.0
53             }
54             #TODO: Calculate
55
56         return res
57
58     _columns = {
59         'name': fields.char('Year', size=64, required=False, readonly=True),
60         'user_id':fields.many2one('res.users', 'Responsible', readonly=True),
61         'section_id':fields.many2one('crm.case.section', 'Section', readonly=True),
62         'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True),
63         'avg_answers': fields.function(_get_data, group_operator='avg', string='Avg. Answers', multi='avg_answers', method=True, type="integer"),
64         'perc_done': fields.function(_get_data, group_operator='avg', string='%Done', multi='perc_done', method=True, type="float"),
65         'perc_cancel': fields.function(_get_data, group_operator='avg', string='%Cancel', multi='perc_cancel', method=True, type="float"),
66         'month':fields.selection([('01', 'January'), ('02', 'February'), \
67                                   ('03', 'March'), ('04', 'April'),\
68                                   ('05', 'May'), ('06', 'June'), \
69                                   ('07', 'July'), ('08', 'August'),\
70                                   ('09', 'September'), ('10', 'October'),\
71                                   ('11', 'November'), ('12', 'December')], 'Month', readonly=True),
72         'company_id': fields.many2one('res.company', 'Company', readonly=True),
73         'day': fields.char('Day', size=128, readonly=True),
74         'opening_date': fields.date('Date of Opening', readonly=True),
75         'creation_date': fields.date('Creation Date', readonly=True),
76         'date_closed': fields.date('Date of Closing', readonly=True),
77         'categ_id': fields.many2one('crm.case.categ', 'Category', domain="[('section_id','=',section_id),('object_id.model', '=', 'project.issue')]"),
78         'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('object_id.model', '=', 'project.issue')]"),
79         'nbr': fields.integer('# of Issues', readonly=True),
80         'working_hours_open': fields.float('Avg. Working Hours to Open', readonly=True),
81         'working_hours_close': fields.float('Avg. Working Hours to Close', readonly=True),
82         'delay_open': fields.float('Avg. Delay to Open', digits=(16,2), readonly=True, group_operator="avg",
83                                        help="Number of Days to close the project issue"),
84         'delay_close': fields.float('Avg. Delay to Close', digits=(16,2), readonly=True, group_operator="avg",
85                                        help="Number of Days to close the project issue"),
86         'company_id' : fields.many2one('res.company', 'Company'),
87         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
88         'project_id':fields.many2one('project.project', 'Project',readonly=True),
89         'version_id': fields.many2one('project.issue.version', 'Version'),
90         'assigned_to' : fields.many2one('res.users', 'Assigned to',readonly=True),
91         'partner_id': fields.many2one('res.partner','Partner',domain="[('object_id.model', '=', 'project.issue')]"),
92         'canal_id': fields.many2one('res.partner.canal', 'Channel',readonly=True),
93         'task_id': fields.many2one('project.task', 'Task',domain="[('object_id.model', '=', 'project.issue')]" ),
94         'email': fields.integer('# Emails', size=128, readonly=True),
95     }
96
97     def init(self, cr):
98         tools.drop_view_if_exists(cr, 'project_issue_report')
99         cr.execute("""
100             CREATE OR REPLACE VIEW project_issue_report AS (
101                 SELECT
102                     c.id as id,
103                     to_char(c.create_date, 'YYYY') as name,
104                     to_char(c.create_date, 'MM') as month,
105                     to_char(c.create_date, 'YYYY-MM-DD') as day,
106                     to_char(c.date_open, 'YYYY-MM-DD') as opening_date,
107                     to_char(c.create_date, 'YYYY-MM-DD') as creation_date,
108                     c.state,
109                     c.user_id,
110                     c.working_hours_open,
111                     c.working_hours_close,
112                     c.section_id,
113                     c.categ_id,
114                     c.stage_id,
115                     to_char(c.date_closed, 'YYYY-mm-dd') as date_closed,
116                     c.company_id as company_id,
117                     c.priority as priority,
118                     c.project_id as project_id,
119                     c.version_id as version_id,
120                     1 as nbr,
121                     c.assigned_to,
122                     c.partner_id,
123                     c.canal_id,
124                     c.task_id,
125                     date_trunc('day',c.create_date) as create_date,
126                     extract('epoch' from (c.date_open-c.create_date))/(3600*24) as  delay_open,
127                     extract('epoch' from (c.date_closed-c.create_date))/(3600*24) as  delay_close,
128                     (SELECT count(id) FROM mailgate_message WHERE model='project.issue' AND res_id=c.id) AS email
129                 FROM
130                     project_issue c
131                 WHERE c.categ_id IN (select res_id from ir_model_data WHERE model = 'crm.case.categ' and name='bug_categ')
132             )""")
133
134 project_issue_report()
135
136 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: