[FIX] Schedule jobs even if their next time has passed.
[odoo/odoo.git] / addons / report_task / report_task.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 import mx.DateTime
25
26 class  report_task_user_pipeline_open (osv.osv):
27     _name = "report.task.user.pipeline.open"
28     _description = "Tasks by user and project"
29     _auto = False
30     _columns = {
31         'user_id':fields.many2one('res.users', 'User', readonly=True),
32         'task_nbr': fields.float('Task Number', readonly=True),
33         'task_hrs': fields.float('Task Hours', readonly=True),
34         'task_progress': fields.float('Task Progress', readonly=True),
35         'company_id' : fields.many2one('res.company', 'Company'),
36         'task_state': fields.selection([('draft', 'Draft'),('open', 'Open'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done'),('no','No Task')], 'Status', readonly=True),
37     }
38
39     def init(self, cr):
40         cr.execute('''
41             create or replace view report_task_user_pipeline_open as (
42                 select
43                     min(t.id) as id,
44                     u.id as user_id,
45                     u.company_id as company_id,
46                     count(t.*) as task_nbr,
47                     sum(t.planned_hours) as task_hrs,
48                     sum(t.planned_hours * (100 - t.progress) / 100) as task_progress,
49                     case when t.state is null then 'no' else t.state end as task_state
50                 from
51                     res_users u
52                 left join 
53                     project_task t on (u.id = t.user_id)
54                 where
55                     u.active
56                 group by
57                     u.id, u.company_id, t.state
58             )
59         ''')
60 report_task_user_pipeline_open()
61
62 class  report_closed_task(osv.osv):
63     _name = "report.closed.task"
64     _description = "Closed Task Report"
65     _auto = False
66     _columns = {
67         'sequence': fields.integer('Sequence', readonly=True),
68         'name': fields.char('Task summary', size=128, readonly=True),
69         'project_id': fields.many2one('project.project', 'Project', readonly=True),
70         'user_id': fields.many2one('res.users', 'Assigned to', readonly=True),
71         'date_deadline': fields.datetime('Deadline', readonly=True),
72         'planned_hours': fields.float('Planned Hours', readonly=True),
73         'delay_hours': fields.float('Delay Hours', readonly=True),
74         'progress': fields.float('Progress (%)', readonly=True),
75         'priority' : fields.selection([('4','Very Low'), ('3','Low'), ('2','Medium'), ('1','Urgent'), ('0','Very urgent')], 'Importance', readonly=True),
76         'state': fields.selection([('draft', 'Draft'),('open', 'In Progress'),('pending', 'Pending'), ('cancelled', 'Cancelled'), ('done', 'Done')], 'Status', readonly=True),
77         'remaining_hours': fields.float('Remaining Hours', readonly=True),
78         'date_close' : fields.datetime('Date Closed', readonly=True)
79     }
80
81     def init(self, cr):
82         cr.execute('''
83             create or replace view report_closed_task as (
84                 select
85                    tsk.id as id, tsk.sequence as sequence, tsk.name as name,
86                    tsk.project_id as project_id, tsk.user_id as user_id,
87                    tsk.date_deadline as date_deadline, tsk.planned_hours as planned_hours,
88                    tsk.delay_hours as delay_hours, tsk.progress as progress,
89                    tsk.priority as priority, tsk.state as state,
90                    tsk.remaining_hours as remaining_hours, tsk.date_close as date_close
91                 from
92                     project_task tsk
93                 where
94                     (tsk.date_close <= CURRENT_DATE AND tsk.date_close > (CURRENT_DATE-15))
95             )
96         ''')
97 report_closed_task()
98
99 class report_timesheet_task_user(osv.osv):
100     _name = "report.timesheet.task.user"
101     _auto = False
102     _order = "name"
103     
104     def _get_task_hours(self, cr, uid, ids, name,args,context):
105         result = {}
106         for record in self.browse(cr, uid, ids,context):
107             last_date = mx.DateTime.strptime(record.name, '%Y-%m-%d') + mx.DateTime.RelativeDateTime(months=1) - 1
108             task_obj=self.pool.get('project.task.work')
109             task_ids = task_obj.search(cr,uid,[('user_id','=',record.user_id.id),('date','>=',record.name),('date','<=',last_date.strftime('%Y-%m-%d'))])
110             tsk_hrs = task_obj.read(cr,uid,task_ids,['hours','date','user_id'])
111             total = 0.0
112             for hrs in tsk_hrs:
113                 total += hrs['hours']
114             result[record.id] = total
115         return result
116     
117     def get_hrs_timesheet(self, cr, uid, ids, name,args,context):
118         result = {}
119         sum = 0.0
120         for record in self.browse(cr, uid, ids, context):
121             last_date = mx.DateTime.strptime(record.name, '%Y-%m-%d') + mx.DateTime.RelativeDateTime(months=1) - 1
122             obj=self.pool.get('hr_timesheet_sheet.sheet.day')
123             sheet_ids = obj.search(cr,uid,[('sheet_id.user_id','=',record.user_id.id),('name','>=',record.name),('name','<=',last_date.strftime('%Y-%m-%d'))])
124             data_days = obj.read(cr,uid,sheet_ids,['name','sheet_id.user_id','total_attendance'])
125             total = 0.0
126             for day_attendance in data_days:
127                 total += day_attendance['total_attendance']
128             result[record.id] = total
129         return result
130         
131     _columns = {
132         'name': fields.date('Month',readonly=True),
133         'user_id': fields.many2one('res.users', 'User',readonly=True),
134         'timesheet_hrs': fields.function(get_hrs_timesheet, method=True, string="Timesheet Hours"),
135         'task_hrs': fields.function(_get_task_hours, method=True, string="Task Hours"),
136       }
137     
138     
139     def init(self, cr):   
140        cr.execute(""" create or replace view report_timesheet_task_user as (
141         select  
142          ((r.id*12)+to_number(months.m_id,'99'))::integer as id,
143                months.name as name,
144                r.id as user_id
145         from res_users r,
146                 (select to_char(p.date,'YYYY-MM-01') as name,
147             to_char(p.date,'MM') as m_id
148                 from project_task_work p 
149     
150             union 
151                 select to_char(h.name,'YYYY-MM-01') as name,
152                 to_char(h.name,'MM') as m_id
153                 from hr_timesheet_sheet_sheet_day h) as months) """)
154
155 report_timesheet_task_user()
156
157 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
158