40c428a966311881486aa830f40fcd68072db9cb
[odoo/odoo.git] / addons / hr / hr.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 from mx import DateTime
23 import time
24 import math
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     def interval_min_get(self, cr, uid, id, dt_from, hours):
38         if not id:
39             return [(dt_from-DateTime.RelativeDateTime(hours=int(hours)*3), dt_from)]
40         todo = hours
41         cycle = 0
42         result = []
43         maxrecur = 100
44         current_hour = dt_from.hour
45         while (todo>0) and maxrecur:
46             cr.execute("select hour_from,hour_to from hr_timesheet where dayofweek='%s' and tgroup_id=%s order by hour_from desc", (dt_from.day_of_week,id))
47             for (hour_from,hour_to) in cr.fetchall():
48                 if (hour_from<current_hour) and (todo>0):
49                     m = min(hour_to, current_hour)
50                     if (m-hour_from)>todo:
51                         hour_from = m-todo
52                     d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_from)),int((hour_from%1) * 60))
53                     d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
54                     result.append((d1, d2))
55                     current_hour = hour_from
56                     todo -= (m-hour_from)
57             dt_from -= DateTime.RelativeDateTime(days=1)
58             current_hour = 24
59             maxrecur -= 1
60         result.reverse()
61         return result
62
63     def interval_get(self, cr, uid, id, dt_from, hours, byday=True):
64         if not id:
65             return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=int(hours)*3))]
66         todo = hours
67         cycle = 0
68         result = []
69         maxrecur = 100
70         current_hour = dt_from.hour
71         while (todo>0) and maxrecur:
72             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))
73             for (hour_from,hour_to) in cr.fetchall():
74                 if (hour_to>current_hour) and (todo>0):
75                     m = max(hour_from, current_hour)
76                     if (hour_to-m)>todo:
77                         hour_to = m+todo
78                     d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
79                     d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
80                     result.append((d1, d2))
81                     current_hour = hour_to
82                     todo -= (hour_to - m)
83             dt_from += DateTime.RelativeDateTime(days=1)
84             current_hour = 0
85             maxrecur -= 1
86         return result
87
88 hr_timesheet_group()
89
90
91 class hr_employee_category(osv.osv):
92     _name = "hr.employee.category"
93     _description = "Employee Category"
94     
95     _columns = {
96         'name' : fields.char("Category", size=64, required=True),
97         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
98         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
99     }
100     
101     def _check_recursion(self, cr, uid, ids):
102         level = 100
103         while len(ids):
104             cr.execute('select distinct parent_id from hr_employee_category where id in ('+','.join(map(str, ids))+')')
105             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
106             if not level:
107                 return False
108             level -= 1
109         return True
110     
111     _constraints = [
112         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
113     ]
114     
115 hr_employee_category()
116
117 class hr_employee(osv.osv):
118     _name = "hr.employee"
119     _description = "Employee"
120
121     _columns = {
122         'name' : fields.char("Employee's Name", size=128, required=True),
123         'active' : fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the employee record without removing it."),
124         'company_id': fields.many2one('res.company', 'Company'),
125         'user_id' : fields.many2one('res.users', 'Related User', help='Related user name for an employee to manage his access rights.'),
126
127         'country_id' : fields.many2one('res.country', 'Nationality'),
128         'birthday' : fields.date("Birthday"),
129         'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
130         'sinid': fields.char('SIN No', size=32),
131         'otherid': fields.char('Other ID', size=32),
132         'gender': fields.selection([('',''),('male','Male'),('female','Female')], 'Gender'),
133         'marital': fields.selection([('married','Married'),('unmarried','Unmarried'),('divorced','Divorced'),('other','Other')],'Marital Status', size=32),
134
135         'address_id': fields.many2one('res.partner.address', 'Working Address'),
136         'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
137         'work_phone': fields.char('Work Phone', size=32),
138         'work_email': fields.char('Work Email', size=128),
139         'work_location': fields.char('Office Location', size=32),
140
141         'notes': fields.text('Notes'),
142         'parent_id': fields.many2one('hr.employee', 'Manager', select=True),
143         'category_id' : fields.many2one('hr.employee.category', 'Category'),
144         'child_ids': fields.one2many('hr.employee', 'parent_id','Subordinates'),
145     }
146     _defaults = {
147         'active' : lambda *a: True,
148     }
149     
150     def _check_recursion(self, cr, uid, ids):
151         level = 100
152         while len(ids):
153             cr.execute('select distinct parent_id from hr_employee where id in ('+','.join(map(str, ids))+')')
154             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
155             if not level:
156                 return False
157             level -= 1
158         return True
159     
160     _constraints = [
161         (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id'])
162     ]
163     
164 hr_employee()
165
166 class hr_timesheet(osv.osv):
167     _name = "hr.timesheet"
168     _description = "Timesheet Line"
169     _columns = {
170         'name' : fields.char("Name", size=64, required=True),
171         'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
172         'date_from' : fields.date('Starting date'),
173         'hour_from' : fields.float('Work from', size=8, required=True),
174         'hour_to' : fields.float("Work to", size=8, required=True),
175         'tgroup_id' : fields.many2one("hr.timesheet.group", "Employee's timesheet group", select=True),
176     }
177     _order = 'dayofweek, hour_from'
178 hr_timesheet()
179
180
181 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: