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