1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
29 class hr_employee_category(osv.osv):
31 def name_get(self, cr, uid, ids, context=None):
34 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
38 if record['parent_id']:
39 name = record['parent_id'][1]+' / '+name
40 res.append((record['id'], name))
43 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
44 res = self.name_get(cr, uid, ids, context=context)
47 _name = "hr.employee.category"
48 _description = "Employee Category"
50 'name': fields.char("Category", size=64, required=True),
51 'complete_name': fields.function(_name_get_fnc, type="char", string='Name'),
52 'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
53 'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories'),
54 'employee_ids': fields.many2many('hr.employee', 'employee_category_rel', 'category_id', 'emp_id', 'Employees'),
57 def _check_recursion(self, cr, uid, ids, context=None):
60 cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
61 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
68 (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
71 hr_employee_category()
73 class hr_job(osv.osv):
75 def _no_of_employee(self, cr, uid, ids, name, args, context=None):
77 for job in self.browse(cr, uid, ids, context=context):
78 nb_employees = len(job.employee_ids or [])
80 'no_of_employee': nb_employees,
81 'expected_employees': nb_employees + job.no_of_recruitment,
85 def _get_job_position(self, cr, uid, ids, context=None):
87 for employee in self.pool.get('hr.employee').browse(cr, uid, ids, context=context):
89 res.append(employee.job_id.id)
93 _description = "Job Description"
95 'name': fields.char('Job Name', size=128, required=True, select=True),
96 'expected_employees': fields.function(_no_of_employee, string='Expected Employees', help='Required number of employees in total for that job.',
98 'hr.job': (lambda self,cr,uid,ids,c=None: ids, ['no_of_recruitment'], 10),
99 'hr.employee': (_get_job_position, ['job_id'], 10),
101 multi='no_of_employee'),
102 'no_of_employee': fields.function(_no_of_employee, string="Number of Employees", help='Number of employees with that job.',
104 'hr.employee': (_get_job_position, ['job_id'], 10),
106 multi='no_of_employee'),
107 'no_of_recruitment': fields.float('Expected in Recruitment'),
108 'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
109 'description': fields.text('Job Description'),
110 'requirements': fields.text('Requirements'),
111 'department_id': fields.many2one('hr.department', 'Department'),
112 'company_id': fields.many2one('res.company', 'Company'),
113 'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
116 'expected_employees': 1,
117 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
122 ('name_company_uniq', 'unique(name, company_id)', 'The name of the job position must be unique per company!'),
126 def on_change_expected_employee(self, cr, uid, ids, no_of_recruitment, no_of_employee, context=None):
129 return {'value': {'expected_employees': no_of_recruitment + no_of_employee}}
131 def job_old(self, cr, uid, ids, *args):
132 self.write(cr, uid, ids, {'state': 'old', 'no_of_recruitment': 0})
135 def job_recruitement(self, cr, uid, ids, *args):
136 for job in self.browse(cr, uid, ids):
137 no_of_recruitment = job.no_of_recruitment == 0 and 1 or job.no_of_recruitment
138 self.write(cr, uid, [job.id], {'state': 'recruit', 'no_of_recruitment': no_of_recruitment})
141 def job_open(self, cr, uid, ids, *args):
142 self.write(cr, uid, ids, {'state': 'open', 'no_of_recruitment': 0})
147 class hr_employee(osv.osv):
148 _name = "hr.employee"
149 _description = "Employee"
150 _inherits = {'resource.resource': "resource_id"}
152 def onchange_photo_mini(self, cr, uid, ids, value, context=None):
153 return {'value': {'photo': value, 'photo_mini': self._photo_resize(cr, uid, value) } }
155 def _set_photo_mini(self, cr, uid, id, name, value, args, context=None):
156 return self.write(cr, uid, [id], {'photo': value}, context=context)
158 def _photo_resize(self, cr, uid, photo, context=None):
159 image_stream = io.BytesIO(photo.decode('base64'))
160 img = Image.open(image_stream)
161 img.thumbnail((180, 150), Image.ANTIALIAS)
162 img_stream = StringIO.StringIO()
163 img.save(img_stream, "JPEG")
164 return img_stream.getvalue().encode('base64')
166 def _get_photo_mini(self, cr, uid, ids, name, args, context=None):
168 for hr_empl in self.browse(cr, uid, ids, context=context):
169 if not hr_empl.photo:
170 result[hr_empl.id] = False
172 result[hr_empl.id] = self._photo_resize(cr, uid, hr_empl.photo, context=context)
176 'country_id': fields.many2one('res.country', 'Nationality'),
177 'birthday': fields.date("Date of Birth"),
178 'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
179 'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
180 'identification_id': fields.char('Identification No', size=32),
181 'otherid': fields.char('Other Id', size=64),
182 'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
183 'marital': fields.selection([('single', 'Single'), ('married', 'Married'), ('widower', 'Widower'), ('divorced', 'Divorced')], 'Marital Status'),
184 'department_id':fields.many2one('hr.department', 'Department'),
185 'address_id': fields.many2one('res.partner.address', 'Working Address'),
186 'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
187 'partner_id': fields.related('address_home_id', 'partner_id', type='many2one', relation='res.partner', readonly=True, help="Partner that is related to the current employee. Accounting transaction will be written on this partner belongs to employee."),
188 'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
189 'work_phone': fields.char('Work Phone', size=32, readonly=False),
190 'mobile_phone': fields.char('Work Mobile', size=32, readonly=False),
191 'work_email': fields.char('Work E-mail', size=240),
192 'work_location': fields.char('Office Location', size=32),
193 'notes': fields.text('Notes'),
194 'parent_id': fields.many2one('hr.employee', 'Manager'),
195 'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel', 'emp_id', 'category_id', 'Categories'),
196 'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
197 'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
198 'coach_id': fields.many2one('hr.employee', 'Coach'),
199 'job_id': fields.many2one('hr.job', 'Job'),
200 'photo': fields.binary('Photo'),
201 'photo_mini': fields.function(_get_photo_mini, fnct_inv=_set_photo_mini, string='Photo Mini', type="binary",
203 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['photo'], 10),
205 'passport_id':fields.char('Passport No', size=64),
206 'color': fields.integer('Color Index'),
207 'city': fields.related('address_id', 'city', type='char', string='City'),
208 'login': fields.related('user_id', 'login', type='char', string='Login', readonly=1),
211 def unlink(self, cr, uid, ids, context=None):
212 resource_obj = self.pool.get('resource.resource')
214 for employee in self.browse(cr, uid, ids, context=context):
215 resource = employee.resource_id
217 resource_ids.append(resource.id)
219 resource_obj.unlink(cr, uid, resource_ids, context=context)
220 return super(hr_employee, self).unlink(cr, uid, ids, context=context)
222 def onchange_address_id(self, cr, uid, ids, address, context=None):
224 address = self.pool.get('res.partner.address').browse(cr, uid, address, context=context)
225 return {'value': {'work_email': address.email, 'work_phone': address.phone, 'mobile_phone': address.mobile}}
228 def onchange_company(self, cr, uid, ids, company, context=None):
231 company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
232 address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
233 address_id = address and address['default'] or False
234 return {'value': {'address_id' : address_id}}
236 def onchange_department_id(self, cr, uid, ids, department_id, context=None):
237 value = {'parent_id': False}
239 department = self.pool.get('hr.department').browse(cr, uid, department_id)
240 value['parent_id'] = department.manager_id.id
241 return {'value': value}
243 def onchange_user(self, cr, uid, ids, user_id, context=None):
246 work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).user_email
247 return {'value': {'work_email' : work_email}}
249 def _get_photo(self, cr, uid, context=None):
250 photo_path = addons.get_module_resource('hr','images','photo.png')
251 return self._photo_resize(cr, uid, open(photo_path, 'rb').read().encode('base64'))
255 'photo_mini': _get_photo,
260 def _check_recursion(self, cr, uid, ids, context=None):
263 cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
264 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
271 (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
276 class hr_department(osv.osv):
277 _description = "Department"
278 _inherit = 'hr.department'
280 'manager_id': fields.many2one('hr.employee', 'Manager'),
281 'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
287 class res_users(osv.osv):
289 _inherit = 'res.users'
291 def create(self, cr, uid, data, context=None):
292 user_id = super(res_users, self).create(cr, uid, data, context=context)
294 # add shortcut unless 'noshortcut' is True in context
295 if not(context and context.get('noshortcut', False)):
296 data_obj = self.pool.get('ir.model.data')
298 data_id = data_obj._get_id(cr, uid, 'hr', 'ir_ui_view_sc_employee')
299 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
300 self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
301 'user_id': user_id}, context=context)
303 # Tolerate a missing shortcut. See product/product.py for similar code.
304 logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
311 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: