[FIX] document_page: allow other users than administrator to create menu to a static...
[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 openerp.osv import fields,osv
25 from openerp 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_hrs_timesheet(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             obj = self.pool.get('hr_timesheet_sheet.sheet.day')
37             sheet_ids = obj.search(cr, uid, [('sheet_id.user_id','=',record.user_id.id),('name','>=',record.name),('name','<=',last_date.strftime('%Y-%m-%d'))])
38             data_days = obj.read(cr, uid, sheet_ids, ['name','sheet_id.user_id','total_attendance'])
39             total = 0.0
40             for day_attendance in data_days:
41                 total += day_attendance['total_attendance']
42             result[record.id] = total
43         return result
44
45     _columns = {
46         'name': fields.char('Date',size=64),
47         'year': fields.char('Year',size=64,required=False, 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         'user_id': fields.many2one('res.users', 'User',readonly=True),
51         'timesheet_hrs': fields.function(get_hrs_timesheet, string="Timesheet Hours"),
52         'task_hrs' : fields.float('Task Hours'),
53     }
54
55     def init(self, cr):
56         tools.drop_view_if_exists(cr, 'report_timesheet_task_user')
57         cr.execute(""" create or replace view report_timesheet_task_user as (
58         select
59          ((r.id*12)+to_number(months.m_id,'999999'))::integer as id,
60                months.name as name,
61                r.id as user_id,
62                to_char(to_date(months.name, 'YYYY/MM/DD'),'YYYY') as year,
63                to_char(to_date(months.name, 'YYYY/MM/DD'),'MM') as month,
64                (select sum(hours) from project_task_work where user_id = r.id and date between to_date(months.name, 'YYYY/MM/DD') and (to_date(months.name, 'YYYY/MM/DD') + interval '1 month' -
65             interval '1 day') ) as task_hrs
66         from res_users r,
67                 (select to_char(p.date,'YYYY-MM-01') as name,
68             to_char(p.date,'YYYYMM') as m_id
69                 from project_task_work p
70
71             union
72                 select to_char(h.name,'YYYY-MM-01') as name,
73                 to_char(h.name,'YYYYMM') as m_id
74                 from hr_timesheet_sheet_sheet_day h) as months
75
76             group by
77                 r.id,months.m_id,months.name,
78                 to_char(to_date(months.name, 'YYYY/MM/DD'),'YYYY') ,
79                 to_char(to_date(months.name, 'YYYY/MM/DD'),'MM')
80               ) """)
81
82 report_timesheet_task_user()
83
84 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: