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