[IMP]
[odoo/odoo.git] / addons / project_timesheet / report / task_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 from datetime import datetime
22 from dateutil.relativedelta import relativedelta
23
24 from osv import fields,osv
25 import tools
26
27 class report_timesheet_task_user(osv.osv):
28     _name = "report.timesheet.task.user"
29     _auto = False
30     _order = "name"
31
32     def _get_task_hours(self, cr, uid, ids, name,args,context):
33         result = {}
34         for record in self.browse(cr, uid, ids,context):
35             last_date = datetime.strptime(record.name, '%Y-%m-%d') + relativedelta(months=1) - relativedelta(days=1)
36             task_obj = self.pool.get('project.task.work')
37             task_ids = task_obj.search(cr, uid, [('user_id','=',record.user_id.id),('date','>=',record.name),('date','<=',last_date.strftime('%Y-%m-%d'))])
38             tsk_hrs = task_obj.read(cr, uid, task_ids, ['hours','date','user_id'])
39             total = 0.0
40             for hrs in tsk_hrs:
41                 total += hrs['hours']
42             result[record.id] = total
43         return result
44
45     def get_hrs_timesheet(self, cr, uid, ids, name,args,context):
46         result = {}
47         sum = 0.0
48         for record in self.browse(cr, uid, ids, context):
49             last_date = datetime.strptime(record.name, '%Y-%m-%d') + relativedelta(months=1) - relativedelta(days=1)
50             obj = self.pool.get('hr_timesheet_sheet.sheet.day')
51             sheet_ids = obj.search(cr, uid, [('sheet_id.user_id','=',record.user_id.id),('name','>=',record.name),('name','<=',last_date.strftime('%Y-%m-%d'))])
52             data_days = obj.read(cr, uid, sheet_ids, ['name','sheet_id.user_id','total_attendance'])
53             total = 0.0
54             for day_attendance in data_days:
55                 total += day_attendance['total_attendance']
56             result[record.id] = total
57         return result
58
59     _columns = {
60         'name': fields.char('Date',size=64),
61         'year': fields.char('Year',size=64,required=False, readonly=True),
62         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'), ('05','May'), ('06','June'),
63                                   ('07','July'), ('08','August'), ('09','September'), ('10','October'), ('11','November'), ('12','December')],'Month',readonly=True),
64         'user_id': fields.many2one('res.users', 'User',readonly=True),
65         'timesheet_hrs': fields.function(get_hrs_timesheet, string="Timesheet Hours"),
66         'task_hrs': fields.function(_get_task_hours, string="Task Hours"),
67     }
68
69     def init(self, cr):
70         tools.drop_view_if_exists(cr, 'report_timesheet_task_user')
71         cr.execute(""" create or replace view report_timesheet_task_user as (
72         select
73          ((r.id*12)+to_number(months.m_id,'99'))::integer as id,
74                months.name as name,
75                r.id as user_id,
76                to_char(to_date(months.name, 'YYYY/MM/DD'),'YYYY') as year,
77                to_char(to_date(months.name, 'YYYY/MM/DD'),'MM') as month
78         from res_users r,
79                 (select to_char(p.date,'YYYY-MM-01') as name,
80             to_char(p.date,'MM') as m_id
81                 from project_task_work p
82
83             union
84                 select to_char(h.name,'YYYY-MM-01') as name,
85                 to_char(h.name,'MM') as m_id
86                 from hr_timesheet_sheet_sheet_day h) as months
87
88             group by
89                 r.id,months.m_id,months.name,
90                 to_char(to_date(months.name, 'YYYY/MM/DD'),'YYYY') ,
91                 to_char(to_date(months.name, 'YYYY/MM/DD'),'MM')
92               ) """)
93
94 report_timesheet_task_user()
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: