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