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 openerp import addons
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26 from openerp import tools
28 _logger = logging.getLogger(__name__)
30 class hr_employee_category(osv.osv):
32 def name_get(self, cr, uid, ids, context=None):
35 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
39 if record['parent_id']:
40 name = record['parent_id'][1]+' / '+name
41 res.append((record['id'], name))
44 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
45 res = self.name_get(cr, uid, ids, context=context)
48 _name = "hr.employee.category"
49 _description = "Employee Category"
51 'name': fields.char("Category", size=64, required=True),
52 'complete_name': fields.function(_name_get_fnc, type="char", string='Name'),
53 'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
54 'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories'),
55 'employee_ids': fields.many2many('hr.employee', 'employee_category_rel', 'category_id', 'emp_id', 'Employees'),
58 def _check_recursion(self, cr, uid, ids, context=None):
61 cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
62 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
69 (_check_recursion, 'Error! You cannot create recursive Categories.', ['parent_id'])
72 hr_employee_category()
74 class hr_job(osv.osv):
76 def _no_of_employee(self, cr, uid, ids, name, args, context=None):
78 for job in self.browse(cr, uid, ids, context=context):
79 nb_employees = len(job.employee_ids or [])
81 'no_of_employee': nb_employees,
82 'expected_employees': nb_employees + job.no_of_recruitment,
86 def _get_job_position(self, cr, uid, ids, context=None):
88 for employee in self.pool.get('hr.employee').browse(cr, uid, ids, context=context):
90 res.append(employee.job_id.id)
94 _description = "Job Description"
95 _inherit = ['mail.thread']
97 'name': fields.char('Job Name', size=128, required=True, select=True),
98 'expected_employees': fields.function(_no_of_employee, string='Total Forecasted Employees',
99 help='Expected number of employees for this job position after new recruitment.',
101 'hr.job': (lambda self,cr,uid,ids,c=None: ids, ['no_of_recruitment'], 10),
102 'hr.employee': (_get_job_position, ['job_id'], 10),
104 multi='no_of_employee'),
105 'no_of_employee': fields.function(_no_of_employee, string="Current Number of Employees",
106 help='Number of employees currently occupying this job position.',
108 'hr.employee': (_get_job_position, ['job_id'], 10),
110 multi='no_of_employee'),
111 'no_of_recruitment': fields.float('Expected in Recruitment', help='Number of new employees you expect to recruit.'),
112 'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees', groups='base.group_user'),
113 'description': fields.text('Job Description'),
114 'requirements': fields.text('Requirements'),
115 'department_id': fields.many2one('hr.department', 'Department'),
116 'company_id': fields.many2one('res.company', 'Company'),
117 'state': fields.selection([('open', 'No Recruitment'), ('recruit', 'Recruitement in Progress')], 'Status', readonly=True, required=True,
118 help="By default 'In position', set it to 'In Recruitment' if recruitment process is going on for this job position."),
121 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
126 ('name_company_uniq', 'unique(name, company_id)', 'The name of the job position must be unique per company!'),
130 def on_change_expected_employee(self, cr, uid, ids, no_of_recruitment, no_of_employee, context=None):
133 return {'value': {'expected_employees': no_of_recruitment + no_of_employee}}
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 _get_image(self, cr, uid, ids, name, args, context=None):
153 result = dict.fromkeys(ids, False)
154 for obj in self.browse(cr, uid, ids, context=context):
155 result[obj.id] = tools.image_get_resized_images(obj.image)
158 def _set_image(self, cr, uid, id, name, value, args, context=None):
159 return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
162 #we need a related field in order to be able to sort the employee by name
163 'name_related': fields.related('resource_id', 'name', type='char', string='Name', readonly=True, store=True),
164 'country_id': fields.many2one('res.country', 'Nationality'),
165 'birthday': fields.date("Date of Birth"),
166 'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
167 'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
168 'identification_id': fields.char('Identification No', size=32),
169 'otherid': fields.char('Other Id', size=64),
170 'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
171 'marital': fields.selection([('single', 'Single'), ('married', 'Married'), ('widower', 'Widower'), ('divorced', 'Divorced')], 'Marital Status'),
172 'department_id':fields.many2one('hr.department', 'Department'),
173 'address_id': fields.many2one('res.partner', 'Working Address'),
174 'address_home_id': fields.many2one('res.partner', 'Home Address'),
175 'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',address_home_id)]", help="Employee bank salary account"),
176 'work_phone': fields.char('Work Phone', size=32, readonly=False),
177 'mobile_phone': fields.char('Work Mobile', size=32, readonly=False),
178 'work_email': fields.char('Work Email', size=240),
179 'work_location': fields.char('Office Location', size=32),
180 'notes': fields.text('Notes'),
181 'parent_id': fields.many2one('hr.employee', 'Manager'),
182 'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel', 'emp_id', 'category_id', 'Tags'),
183 'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
184 'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
185 'coach_id': fields.many2one('hr.employee', 'Coach'),
186 'job_id': fields.many2one('hr.job', 'Job'),
187 # image: all image fields are base64 encoded and PIL-supported
188 'image': fields.binary("Photo",
189 help="This field holds the image used as photo for the employee, limited to 1024x1024px."),
190 'image_medium': fields.function(_get_image, fnct_inv=_set_image,
191 string="Medium-sized photo", type="binary", multi="_get_image",
193 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
195 help="Medium-sized photo of the employee. It is automatically "\
196 "resized as a 128x128px image, with aspect ratio preserved. "\
197 "Use this field in form views or some kanban views."),
198 'image_small': fields.function(_get_image, fnct_inv=_set_image,
199 string="Smal-sized photo", type="binary", multi="_get_image",
201 'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
203 help="Small-sized photo of the employee. It is automatically "\
204 "resized as a 64x64px image, with aspect ratio preserved. "\
205 "Use this field anywhere a small image is required."),
206 'passport_id':fields.char('Passport No', size=64),
207 'color': fields.integer('Color Index'),
208 'city': fields.related('address_id', 'city', type='char', string='City'),
209 'login': fields.related('user_id', 'login', type='char', string='Login', readonly=1),
210 'last_login': fields.related('user_id', 'date', type='datetime', string='Latest Connection', readonly=1),
213 _order='name_related'
215 def create(self, cr, uid, data, context=None):
216 employee_id = super(hr_employee, self).create(cr, uid, data, context=context)
218 (model, mail_group_id) = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'group_all_employees')
219 employee = self.browse(cr, uid, employee_id, context=context)
220 self.pool.get('mail.group').message_post(cr, uid, [mail_group_id],
221 body=_('Welcome to %s! Please help him/her take the first steps with OpenERP!') % (employee.name),
222 subtype='mail.mt_comment', context=context)
224 pass # group deleted: do not push a message
227 def unlink(self, cr, uid, ids, context=None):
229 for employee in self.browse(cr, uid, ids, context=context):
230 resource_ids.append(employee.resource_id.id)
231 return self.pool.get('resource.resource').unlink(cr, uid, resource_ids, context=context)
233 def onchange_address_id(self, cr, uid, ids, address, context=None):
235 address = self.pool.get('res.partner').browse(cr, uid, address, context=context)
236 return {'value': {'work_phone': address.phone, 'mobile_phone': address.mobile}}
239 def onchange_company(self, cr, uid, ids, company, context=None):
242 company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
243 address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
244 address_id = address and address['default'] or False
245 return {'value': {'address_id' : address_id}}
247 def onchange_department_id(self, cr, uid, ids, department_id, context=None):
248 value = {'parent_id': False}
250 department = self.pool.get('hr.department').browse(cr, uid, department_id)
251 value['parent_id'] = department.manager_id.id
252 return {'value': value}
254 def onchange_user(self, cr, uid, ids, user_id, context=None):
257 work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).email
258 return {'value': {'work_email' : work_email}}
260 def _get_default_image(self, cr, uid, context=None):
261 image_path = addons.get_module_resource('hr', 'static/src/img', 'default_image.png')
262 return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
266 'image': _get_default_image,
270 def _check_recursion(self, cr, uid, ids, context=None):
273 cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
274 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
281 (_check_recursion, 'Error! You cannot create recursive hierarchy of Employee(s).', ['parent_id']),
286 class hr_department(osv.osv):
287 _description = "Department"
288 _inherit = 'hr.department'
290 'manager_id': fields.many2one('hr.employee', 'Manager'),
291 'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
294 def copy(self, cr, uid, ids, default=None, context=None):
297 default = default.copy()
298 default['member_ids'] = []
299 return super(hr_department, self).copy(cr, uid, ids, default, context=context)
301 class res_users(osv.osv):
303 _inherit = 'res.users'
305 def create(self, cr, uid, data, context=None):
306 user_id = super(res_users, self).create(cr, uid, data, context=context)
308 # add shortcut unless 'noshortcut' is True in context
309 if not(context and context.get('noshortcut', False)):
310 data_obj = self.pool.get('ir.model.data')
312 data_id = data_obj._get_id(cr, uid, 'hr', 'ir_ui_view_sc_employee')
313 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
314 self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
315 'user_id': user_id}, context=context)
317 # Tolerate a missing shortcut. See product/product.py for similar code.
318 _logger.debug('Skipped meetings shortcut for user "%s".', data.get('name','<new'))
323 'employee_ids': fields.one2many('hr.employee', 'user_id', 'Related employees'),
329 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: