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 ##############################################################################
24 from openerp.modules.module import get_module_resource
25 from openerp.osv import fields, osv
26 from openerp.tools.translate import _
27 from openerp import tools
28 from openerp.tools.translate import _
30 _logger = logging.getLogger(__name__)
33 class hr_employee_category(osv.osv):
35 def name_get(self, cr, uid, ids, context=None):
38 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
42 if record['parent_id']:
43 name = record['parent_id'][1]+' / '+name
44 res.append((record['id'], name))
47 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
48 res = self.name_get(cr, uid, ids, context=context)
51 _name = "hr.employee.category"
52 _description = "Employee Category"
54 'name': fields.char("Employee Tag", size=64, required=True),
55 'complete_name': fields.function(_name_get_fnc, type="char", string='Name'),
56 'parent_id': fields.many2one('hr.employee.category', 'Parent Employee Tag', select=True),
57 'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories'),
58 'employee_ids': fields.many2many('hr.employee', 'employee_category_rel', 'category_id', 'emp_id', 'Employees'),
61 def _check_recursion(self, cr, uid, ids, context=None):
64 cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
65 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
72 (_check_recursion, 'Error! You cannot create recursive Categories.', ['parent_id'])
76 class hr_job(osv.osv):
78 def _no_of_employee(self, cr, uid, ids, name, args, context=None):
80 for job in self.browse(cr, uid, ids, context=context):
81 nb_employees = len(job.employee_ids or [])
83 'no_of_employee': nb_employees,
84 'expected_employees': nb_employees + job.no_of_recruitment,
88 def _get_job_position(self, cr, uid, ids, context=None):
90 for employee in self.pool.get('hr.employee').browse(cr, uid, ids, context=context):
92 res.append(employee.job_id.id)
96 _description = "Job Description"
97 _inherit = ['mail.thread']
99 'name': fields.char('Job Name', size=128, required=True, select=True),
100 'expected_employees': fields.function(_no_of_employee, string='Total Forecasted Employees',
101 help='Expected number of employees for this job position after new recruitment.',
103 'hr.job': (lambda self,cr,uid,ids,c=None: ids, ['no_of_recruitment'], 10),
104 'hr.employee': (_get_job_position, ['job_id'], 10),
106 multi='no_of_employee'),
107 'no_of_employee': fields.function(_no_of_employee, string="Current Number of Employees",
108 help='Number of employees currently occupying this job position.',
110 'hr.employee': (_get_job_position, ['job_id'], 10),
112 multi='no_of_employee'),
113 'no_of_recruitment': fields.integer('Expected in Recruitment', help='Number of new employees you expect to recruit.'),
114 'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees', groups='base.group_user'),
115 'description': fields.text('Job Description'),
116 'requirements': fields.text('Requirements'),
117 'department_id': fields.many2one('hr.department', 'Department'),
118 'company_id': fields.many2one('res.company', 'Company'),
119 'state': fields.selection([('open', 'No Recruitment'), ('recruit', 'Recruitement in Progress')], 'Status', readonly=True, required=True,
120 help="By default 'In position', set it to 'In Recruitment' if recruitment process is going on for this job position."),
121 'write_date': fields.datetime('Update Date', readonly=True),
124 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
125 'no_of_recruitment': 0,
130 ('name_company_uniq', 'unique(name, company_id, department_id)', 'The name of the job position must be unique per department in company!'),
134 def on_change_expected_employee(self, cr, uid, ids, no_of_recruitment, no_of_employee, context=None):
137 return {'value': {'expected_employees': no_of_recruitment + no_of_employee}}
139 def job_recruitement(self, cr, uid, ids, *args):
140 for job in self.browse(cr, uid, ids):
141 no_of_recruitment = job.no_of_recruitment == 0 and 1 or job.no_of_recruitment
142 self.write(cr, uid, [job.id], {'state': 'recruit', 'no_of_recruitment': no_of_recruitment})
145 def job_open(self, cr, uid, ids, *args):
146 self.write(cr, uid, ids, {'state': 'open', 'no_of_recruitment': 0})
150 class hr_employee(osv.osv):
151 _name = "hr.employee"
152 _description = "Employee"
153 _order = 'name_related'
154 _inherits = {'resource.resource': "resource_id"}
155 _inherit = ['mail.thread']
157 _mail_post_access = 'read'
159 def _get_image(self, cr, uid, ids, name, args, context=None):
160 result = dict.fromkeys(ids, False)
161 for obj in self.browse(cr, uid, ids, context=context):
162 result[obj.id] = tools.image_get_resized_images(obj.image)
165 def _set_image(self, cr, uid, id, name, value, args, context=None):
166 return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
169 #we need a related field in order to be able to sort the employee by name
170 'name_related': fields.related('resource_id', 'name', type='char', string='Name', readonly=True, store=True),
171 'country_id': fields.many2one('res.country', 'Nationality'),
172 'birthday': fields.date("Date of Birth"),
173 'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
174 'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
175 'identification_id': fields.char('Identification No', size=32),
176 'otherid': fields.char('Other Id', size=64),
177 'gender': fields.selection([('male', 'Male'), ('female', 'Female')], 'Gender'),
178 'marital': fields.selection([('single', 'Single'), ('married', 'Married'), ('widower', 'Widower'), ('divorced', 'Divorced')], 'Marital Status'),
179 'department_id': fields.many2one('hr.department', 'Department'),
180 'address_id': fields.many2one('res.partner', 'Working Address'),
181 'address_home_id': fields.many2one('res.partner', 'Home Address'),
182 'bank_account_id': fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',address_home_id)]", help="Employee bank salary account"),
183 'work_phone': fields.char('Work Phone', size=32, readonly=False),
184 'mobile_phone': fields.char('Work Mobile', size=32, readonly=False),
185 'work_email': fields.char('Work Email', size=240),
186 'work_location': fields.char('Office Location', size=32),
187 'notes': fields.text('Notes'),
188 'parent_id': fields.many2one('hr.employee', 'Manager'),
189 'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel', 'emp_id', 'category_id', 'Tags'),
190 'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
191 'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
192 'coach_id': fields.many2one('hr.employee', 'Coach'),
193 'job_id': fields.many2one('hr.job', 'Job Title'),
194 # image: all image fields are base64 encoded and PIL-supported
195 'image': fields.binary("Photo",
196 help="This field holds the image used as photo for the employee, limited to 1024x1024px."),
197 'image_medium': fields.function(_get_image, fnct_inv=_set_image,
198 string="Medium-sized photo", type="binary", multi="_get_image",
200 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
202 help="Medium-sized photo of the employee. It is automatically "\
203 "resized as a 128x128px image, with aspect ratio preserved. "\
204 "Use this field in form views or some kanban views."),
205 'image_small': fields.function(_get_image, fnct_inv=_set_image,
206 string="Smal-sized photo", type="binary", multi="_get_image",
208 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
210 help="Small-sized photo of the employee. It is automatically "\
211 "resized as a 64x64px image, with aspect ratio preserved. "\
212 "Use this field anywhere a small image is required."),
213 'passport_id': fields.char('Passport No', size=64),
214 'color': fields.integer('Color Index'),
215 'city': fields.related('address_id', 'city', type='char', string='City'),
216 'login': fields.related('user_id', 'login', type='char', string='Login', readonly=1),
217 'last_login': fields.related('user_id', 'date', type='datetime', string='Latest Connection', readonly=1),
220 def _get_default_image(self, cr, uid, context=None):
221 image_path = get_module_resource('hr', 'static/src/img', 'default_image.png')
222 return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
226 'image': _get_default_image,
230 def create(self, cr, uid, data, context=None):
233 create_ctx = dict(context, mail_create_nolog=True)
234 employee_id = super(hr_employee, self).create(cr, uid, data, context=create_ctx)
235 employee = self.browse(cr, uid, employee_id, context=context)
237 res_users = self.pool['res.users']
238 # send a copy to every user of the company
239 # TODO: post to the `Whole Company` mail.group when we'll be able to link to the employee record
240 _model, group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user')
241 user_ids = res_users.search(cr, uid, [('company_id', '=', employee.user_id.company_id.id),
242 ('groups_id', 'in', group_id)])
243 partner_ids = list(set(u.partner_id.id for u in res_users.browse(cr, uid, user_ids, context=context)))
246 self.message_post(cr, uid, [employee_id],
247 body=_('Welcome to %s! Please help him/her take the first steps with OpenERP!') % (employee.name),
248 partner_ids=partner_ids,
249 subtype='mail.mt_comment', context=context
253 def unlink(self, cr, uid, ids, context=None):
255 for employee in self.browse(cr, uid, ids, context=context):
256 resource_ids.append(employee.resource_id.id)
257 return self.pool.get('resource.resource').unlink(cr, uid, resource_ids, context=context)
259 def onchange_address_id(self, cr, uid, ids, address, context=None):
261 address = self.pool.get('res.partner').browse(cr, uid, address, context=context)
262 return {'value': {'work_phone': address.phone, 'mobile_phone': address.mobile}}
265 def onchange_company(self, cr, uid, ids, company, context=None):
268 company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
269 address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
270 address_id = address and address['default'] or False
271 return {'value': {'address_id': address_id}}
273 def onchange_department_id(self, cr, uid, ids, department_id, context=None):
274 value = {'parent_id': False}
276 department = self.pool.get('hr.department').browse(cr, uid, department_id)
277 value['parent_id'] = department.manager_id.id
278 return {'value': value}
280 def onchange_user(self, cr, uid, ids, user_id, context=None):
283 work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).email
284 return {'value': {'work_email': work_email}}
286 def action_follow(self, cr, uid, ids, context=None):
287 """ Wrapper because message_subscribe_users take a user_ids=None
288 that receive the context without the wrapper. """
289 return self.message_subscribe_users(cr, uid, ids, context=context)
291 def action_unfollow(self, cr, uid, ids, context=None):
292 """ Wrapper because message_unsubscribe_users take a user_ids=None
293 that receive the context without the wrapper. """
294 return self.message_unsubscribe_users(cr, uid, ids, context=context)
296 def get_suggested_thread(self, cr, uid, removed_suggested_threads=None, context=None):
297 """Show the suggestion of employees if display_employees_suggestions if the
298 user perference allows it. """
299 user = self.pool.get('res.users').browse(cr, uid, uid, context)
300 if not user.display_employees_suggestions:
303 return super(hr_employee, self).get_suggested_thread(cr, uid, removed_suggested_threads, context)
305 def _message_get_auto_subscribe_fields(self, cr, uid, updated_fields, auto_follow_fields=['user_id'], context=None):
306 """ Overwrite of the original method to always follow user_id field,
307 even when not track_visibility so that a user will follow it's employee
310 for name, column_info in self._all_columns.items():
311 if name in auto_follow_fields and name in updated_fields and column_info.column._obj == 'res.users':
312 user_field_lst.append(name)
313 return user_field_lst
315 def _check_recursion(self, cr, uid, ids, context=None):
318 cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
319 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
326 (_check_recursion, 'Error! You cannot create recursive hierarchy of Employee(s).', ['parent_id']),
330 class hr_department(osv.osv):
331 _description = "Department"
332 _inherit = 'hr.department'
334 'manager_id': fields.many2one('hr.employee', 'Manager'),
335 'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
338 def copy(self, cr, uid, ids, default=None, context=None):
341 default = default.copy()
342 default['member_ids'] = []
343 return super(hr_department, self).copy(cr, uid, ids, default, context=context)
345 class res_users(osv.osv):
347 _inherit = 'res.users'
350 'employee_ids': fields.one2many('hr.employee', 'user_id', 'Related employees'),
355 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: