[FIX] Revert
[odoo/odoo.git] / addons / hr / hr.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 mx import DateTime
24 import time
25
26 from osv import fields, osv
27 from tools.translate import _
28
29 class hr_timesheet_group(osv.osv):
30     _name = "hr.timesheet.group"
31     _description = "Working Time"
32     _columns = {
33         'name' : fields.char("Group name", size=64, required=True),
34         'timesheet_id' : fields.one2many('hr.timesheet', 'tgroup_id', 'Working Time'),
35         'manager' : fields.many2one('res.users', 'Workgroup manager'),
36     }
37     #
38     # TODO: improve; very slow !
39     #       bug if transition to another period
40     #
41     def interval_get(self, cr, uid, id, dt_from, hours, byday=True):
42         if not id:
43             return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=int(hours)*3))]
44         todo = hours
45         cycle = 0
46         result = []
47         while todo>0:
48             cr.execute("select hour_from,hour_to from hr_timesheet where dayofweek='%s' and tgroup_id=%s order by hour_from", (dt_from.day_of_week,id))
49             for (hour_from,hour_to) in cr.fetchall():
50                 
51                 import math
52
53                 hour_from = '%02d:%02d' % (math.floor(abs(hour_from)),round(abs(hour_from)%1+0.01,2) * 60)
54                 hour_to = '%02d:%02d' % (math.floor(abs(hour_to)),round(abs(hour_to)%1+0.01,2) * 60)
55                 
56                 h1,m1 = map(int,hour_from.split(':'))
57                 h2,m2 = map(int,hour_to.split(':'))
58                 d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,h1,m1)
59                 d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,h2,m2)
60                 if dt_from<d2:
61                     date1 = max(dt_from,d1)
62                     if date1+DateTime.RelativeDateTime(hours=todo)<=d2:
63                         result.append((date1, date1+DateTime.RelativeDateTime(hours=todo)))
64                         todo = 0
65                     else:
66                         todo -= (d2-date1).hours
67                         result.append((date1, d2))
68             dt_from = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day)+DateTime.RelativeDateTime(days=1)
69             cycle+=1
70             if cycle>7 and todo==hours:
71                 return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=hours*3))]
72         if byday:
73             i = 1
74             while i<len(result):
75                 if (result[i][0]-result[i-1][1]).days<1:
76                     result[i-1]=(result[i-1][0],result[i][1])
77                     del result[i]
78                 else:
79                     i+=1
80         return result
81 hr_timesheet_group()
82
83
84 class hr_employee_category(osv.osv):
85     _name = "hr.employee.category"
86     _description = "Employee Category"
87     
88     _columns = {
89         'name' : fields.char("Category", size=64, required=True),
90         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
91         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
92     }
93     
94     def _check_recursion(self, cr, uid, ids):
95         level = 100
96         while len(ids):
97             cr.execute('select distinct parent_id from hr_employee_category where id in ('+','.join(map(str,ids))+')')
98             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
99             if not level:
100                 return False
101             level -= 1
102         return True
103     
104     _constraints = [
105         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
106     ]
107     
108 hr_employee_category()
109
110 class hr_employee(osv.osv):
111     _name = "hr.employee"
112     _description = "Employee"
113
114     _columns = {
115         'name' : fields.char("Employee", size=128, required=True),
116         'active' : fields.boolean('Active'),
117         'company_id': fields.many2one('res.company', 'Company'),
118         'user_id' : fields.many2one('res.users', 'Related User'),
119
120         'country_id' : fields.many2one('res.country', 'Nationality'),
121         'birthday' : fields.date("Birthday"),
122         'ssnid': fields.char('SSN No', size=32),
123         'sinid': fields.char('SIN No', size=32),
124         'otherid': fields.char('Other ID', size=32),
125         'gender': fields.selection([('',''),('male','Male'),('female','Female')], 'Gender'),
126         'marital': fields.selection([('maried','Maried'),('unmaried','Unmaried'),('divorced','Divorced'),('other','Other')],'Marital Status', size=32),
127
128         'address_id': fields.many2one('res.partner.address', 'Working Address'),
129         'work_phone': fields.char('Work Phone', size=32),
130         'work_email': fields.char('Work Email', size=128),
131         'work_location': fields.char('Office Location', size=32),
132
133         'notes': fields.text('Notes'),
134         'parent_id': fields.many2one('hr.employee', 'Manager', select=True),
135         'category_id' : fields.many2one('hr.employee.category', 'Category'),
136         'child_ids': fields.one2many('hr.employee', 'parent_id','Subordinates'),
137     }
138     _defaults = {
139         'active' : lambda *a: True,
140     }
141     
142     def _check_recursion(self, cr, uid, ids):
143         level = 100
144         while len(ids):
145             cr.execute('select distinct parent_id from hr_employee where id in ('+','.join(map(str,ids))+')')
146             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
147             if not level:
148                 return False
149             level -= 1
150         return True
151     
152     _constraints = [
153         (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id'])
154     ]
155     
156 hr_employee()
157
158 class hr_timesheet(osv.osv):
159     _name = "hr.timesheet"
160     _description = "Timesheet Line"
161     _columns = {
162         'name' : fields.char("Name", size=64, required=True),
163         'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
164         'date_from' : fields.date('Starting date'),
165         'hour_from' : fields.float('Work from', size=8, required=True),
166         'hour_to' : fields.float("Work to", size=8, required=True),
167         'tgroup_id' : fields.many2one("hr.timesheet.group", "Employee's timesheet group", select=True),
168     }
169     _order = 'dayofweek, hour_from'
170 hr_timesheet()
171
172