[IMP]:project:Improved SQL report(Task).
[odoo/odoo.git] / addons / project / report / project_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
25 class report_project_task_user(osv.osv):
26     _name = "report.project.task.user"
27     _description = "Tasks by user and project"
28     _auto = False
29     _columns = {
30         'name': fields.char('Task Summary', size=128, readonly=True),
31         'day': fields.char('Day', size=128, readonly=True),
32         'year': fields.char('Year',size=64,required=False, readonly=True),
33         'user_id':fields.many2one('res.users', 'Assigned To', readonly=True),
34         'date_start': fields.date('Starting Date',readonly=True),
35         'no_of_days': fields.integer('#Days', size=128, readonly=True),
36         'description': fields.text('Description',readonly=True),
37         'date_end': fields.date('Ending Date',readonly=True),
38         'date_deadline': fields.date('Deadline',readonly=True),
39         'project_id':fields.many2one('project.project', 'Project', readonly=True),
40         'hours_planned': fields.float('Planned Hours', readonly=True),
41         'hours_effective': fields.float('Effective Hours', readonly=True),
42         'hours_delay': fields.float('Avg. Plan.-Eff.', readonly=True),
43         'closing_days': fields.float('Avg Closing Delay', digits=(16,2), readonly=True, group_operator="avg",
44                                        help="Number of Days to close the task"),
45
46         'nbr': fields.integer('#Number of tasks', readonly=True),
47         'priority' : fields.selection([('4','Very Low'),
48                                        ('3','Low'),
49                                        ('2','Medium'),
50                                        ('1','Urgent'),
51                                        ('0','Very urgent')],
52                                        'Importance',readonly=True),
53         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
54                           ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
55         'state': fields.selection([
56                ('draft', 'Draft'),
57                ('open', 'In Progress'),
58                ('pending', 'Pending'),
59                ('cancelled', 'Cancelled'),
60                ('done', 'Done')],
61             'State', readonly=True),
62         'company_id': fields.many2one('res.company', 'Company',readonly=True),
63         'partner_id': fields.many2one('res.partner', 'Partner',readonly=True),
64         'type': fields.many2one('project.task.type', 'Stage',readonly=True),
65
66     }
67     _order = 'name desc, project_id'
68     def init(self, cr):
69         tools.sql.drop_view_if_exists(cr, 'report_project_task_user')
70         cr.execute("""
71             create or replace view report_project_task_user as (
72                 select
73                     min(t.id) as id,
74                     to_char(date_start, 'YYYY') as year,
75                     to_char(date_start, 'MM') as month,
76                     to_char(date_start, 'YYYY-MM-DD') as day,
77                     count(distinct t.id) as nbr,
78                     date_trunc('day',t.date_start) as date_start,
79                     date_trunc('day',t.date_end) as date_end,
80                     to_date(to_char(t.date_deadline, 'dd-MM-YYYY'),'dd-MM-YYYY') as date_deadline,
81                     sum(cast(to_char(date_trunc('day',t.date_end) - date_trunc('day',t.date_start),'DD') as int)) as no_of_days,
82                     t.user_id,
83                     t.project_id,
84                     t.state,
85                     t.priority,
86                     t.name as name,
87                     t.company_id,
88                     t.partner_id,
89                     t.type,
90                     sum(planned_hours) as hours_planned,
91                     avg(extract('epoch' from (t.date_end-t.create_date)))/(3600*24)  as closing_days,
92                     sum(w.hours) as hours_effective,
93                     ((sum(planned_hours)-sum(w.hours))/count(distinct t.id))::decimal(16,2) as hours_delay
94                 from project_task t
95                     left join project_task_work w on (t.id=w.task_id)
96                 group by
97                     to_char(date_start, 'YYYY'),
98                     to_char(date_start, 'MM'),
99                     to_char(date_start, 'YYYY-MM-DD'),
100                     t.priority,
101                     t.user_id,
102                     t.state,
103                     date_trunc('day',t.date_end),
104                     to_date(to_char(t.date_deadline, 'dd-MM-YYYY'),'dd-MM-YYYY'),
105                     t.company_id,
106                     t.partner_id,
107                     t.type,
108                     t.name,
109                     t.project_id,
110                     t.date_start
111             )
112         """)
113 report_project_task_user()
114
115 #This class is generated for project deshboard purpose
116 class project_vs_remaining_hours(osv.osv):
117     _name = "project.vs.remaining.hours"
118     _description = " Project vs Remaining hours"
119     _auto = False
120     _columns = {
121         'project': fields.char('Project', size=128, required=True),
122         'remaining_hours': fields.float('Remaining Hours', readonly=True),
123         'state': fields.selection([('draft','Draft'),('open','Open'), ('pending','Pending'),('cancelled', 'Cancelled'),('close','Close'),('template', 'Template')], 'State', required=True,readonly=True)
124     }
125     _order = 'project desc'
126     def init(self, cr):
127         tools.sql.drop_view_if_exists(cr, 'project_vs_remaining_hours')
128         cr.execute("""
129             create or replace view project_vs_remaining_hours as (
130                 select
131                       min(pt.id) as id,
132                       aaa.user_id as uid,
133                       aaa.name as project,
134                       aaa.state,
135                       sum(pt.remaining_hours) as remaining_hours
136                  from project_project as pp,
137                        account_analytic_account as aaa,
138                        project_task as pt
139                  where aaa.id=pp.category_id and pt.project_id=pp.id and pp.category_id=aaa.id
140                  group by aaa.user_id,aaa.state,aaa.name
141                  UNION All
142                  select
143                       min(pt.id) as id,
144                       pur.uid as uid,
145                       aaa.name as project,
146                       aaa.state,
147                       sum(pt.remaining_hours) as remaining_hours
148                  from project_project as pp,
149                       project_user_rel as pur,
150                       account_analytic_account as aaa,
151                       project_task as pt
152                  where pur.project_id=pp.id and pt.project_id=pp.id and pp.category_id=aaa.id
153                  group by pur.uid,aaa.state,aaa.name
154             )
155         """)
156 project_vs_remaining_hours()
157
158 class task_by_days(osv.osv):
159     _name = "task.by.days"
160     _description = "Task By Days"
161     _auto = False
162     _columns = {
163         'day': fields.char('Day', size=128, required=True),
164         'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'State', readonly=True, required=True),
165         'total_task': fields.float('Total tasks', readonly=True),
166         'project_id':fields.many2one('project.project','Project')
167      }
168     _order = 'day desc'
169     def init(self, cr):
170         tools.sql.drop_view_if_exists(cr, 'task_by_days')
171         cr.execute("""
172             create or replace view task_by_days as (
173                 select
174                     min(pt.id) as id,
175                     to_char(pt.create_date, 'YYYY-MM-DD') as day,
176                     count(*) as total_task,
177                     pt.state as state,
178                     pt.project_id
179                 from
180                     project_task as pt
181                 group by
182                     to_char(pt.create_date, 'YYYY-MM-DD'),pt.state,pt.project_id
183             )
184         """)
185 task_by_days()
186
187 class task_by_days_vs_planned_hours(osv.osv):
188     _name = "task.by.days.vs.planned.hours"
189     _description = "Task By Days vs Planned Hours"
190     _auto = False
191     _columns = {
192         'day': fields.char('Day', size=128, required=True),
193         'planned_hour': fields.float('Planned Hours', readonly=True),
194         'project_id':fields.many2one('project.project','Project')
195      }
196     _order = 'day desc'
197     def init(self, cr):
198         tools.sql.drop_view_if_exists(cr, 'task_by_days_vs_planned_hours')
199         cr.execute("""
200             create or replace view task_by_days_vs_planned_hours as (
201                 select
202                     min(pt.id) as id,
203                     to_char(pt.create_date, 'YYYY-MM-DD') as day,
204                     sum(planned_hours) as planned_hour,
205                     pt.project_id
206                 from
207                     project_task as pt
208                 group by
209                     to_char(pt.create_date, 'YYYY-MM-DD'),pt.project_id
210             )
211         """)
212 task_by_days_vs_planned_hours()
213 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
214