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