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