[IMP] hr*: remove group_extended
[odoo/odoo.git] / addons / hr_timesheet / report / users_timesheet.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 import datetime
23 from report.interface import report_rml
24 from report.interface import toxml
25 import time
26 import pooler
27 from tools.translate import _
28 from report import report_sxw
29 from tools import ustr
30
31
32 def lengthmonth(year, month):
33     if month == 2 and ((year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))):
34         return 29
35     return [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
36
37 def emp_create_xml(cr, id, som, eom, emp):
38     # Computing the attendence by analytical account
39     cr.execute(
40         "select line.date, (unit_amount / unit.factor) as amount "\
41         "from account_analytic_line as line, hr_analytic_timesheet as hr, "\
42         "product_uom as unit "\
43         "where hr.line_id=line.id "\
44         "and product_uom_id = unit.id "\
45         "and line.user_id=%s and line.date >= %s and line.date < %s "
46         "order by line.date",
47         (id, som.strftime('%Y-%m-%d'), eom.strftime('%Y-%m-%d')))
48
49     # Sum by day
50     month = {}
51     for presence in cr.dictfetchall():
52         day = int(presence['date'][-2:])
53         month[day] = month.get(day, 0.0) + presence['amount']
54
55     xml = '''
56     <time-element date="%s">
57         <amount>%.2f</amount>
58     </time-element>
59     '''
60     time_xml = ([xml % (day, amount) for day, amount in month.iteritems()])
61
62     # Computing the xml
63     xml = '''
64     <employee id="%d" name="%s">
65     %s
66     </employee>
67     ''' % (id, toxml(emp), '\n'.join(time_xml))
68     return xml
69
70 class report_custom(report_rml):
71
72     def get_month_name(self, cr, uid, month, context=None):
73         _months = {1:_("January"), 2:_("February"), 3:_("March"), 4:_("April"), 5:_("May"), 6:_("June"), 7:_("July"), 8:_("August"), 9:_("September"), 10:_("October"), 11:_("November"), 12:_("December")}
74         return _months[month]
75
76     def get_weekday_name(self, cr, uid, weekday, context=None):
77         _weekdays = {1:_('Mon'), 2:_('Tue'), 3:_('Wed'), 4:_('Thu'), 5:_('Fri'), 6:_('Sat'), 7:_('Sun')}
78         return _weekdays[weekday]
79
80     def create_xml(self, cr, uid, ids, data, context):
81
82         # Computing the dates (start of month: som, and end of month: eom)
83         som = datetime.date(data['form']['year'], data['form']['month'], 1)
84         eom = som + datetime.timedelta(lengthmonth(som.year, som.month))
85         date_xml = ['<date month="%s" year="%d" />' % (self.get_month_name(cr, uid, som.month, context=context), som.year), '<days>']
86         date_xml += ['<day number="%d" name="%s" weekday="%d" />' % (x, self.get_weekday_name(cr, uid, som.replace(day=x).weekday()+1, context=context), som.replace(day=x).weekday()+1) for x in range(1, lengthmonth(som.year, som.month)+1)]
87         date_xml.append('</days>')
88         date_xml.append('<cols>2.5cm%s,2cm</cols>\n' % (',0.7cm' * lengthmonth(som.year, som.month)))
89
90         emp_xml=''
91         emp_obj = pooler.get_pool(cr.dbname).get('hr.employee')        
92         for id in data['form']['employee_ids']:
93             user = emp_obj.browse(cr, uid, id).user_id.id
94             empl_name = emp_obj.browse(cr, uid, id).name
95             if user:
96                 emp_xml += emp_create_xml(cr, user, som, eom, empl_name)
97         # Computing the xml
98         #Without this, report don't show non-ascii characters (TO CHECK)
99         date_xml = '\n'.join(date_xml)
100         rpt_obj = pooler.get_pool(cr.dbname).get('hr.employee')
101         rml_obj=report_sxw.rml_parse(cr, uid, rpt_obj._name,context)
102         header_xml = '''
103         <header>
104         <date>%s</date>
105         <company>%s</company>
106         </header>
107         '''  % (str(rml_obj.formatLang(time.strftime("%Y-%m-%d"),date=True))+' ' + str(time.strftime("%H:%M")),pooler.get_pool(cr.dbname).get('res.users').browse(cr,uid,uid).company_id.name)
108
109         xml='''<?xml version="1.0" encoding="UTF-8" ?>
110         <report>
111         %s
112         %s
113         %s
114         </report>
115         ''' % (header_xml,date_xml, ustr(emp_xml))
116         return xml
117
118 report_custom('report.hr.analytical.timesheet_users', 'hr.employee', '', 'addons/hr_timesheet/report/users_timesheet.xsl')
119
120 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
121