diff --git a/addons/hr/hr.py b/addons/hr/hr.py index 101629482eb..a13a1866493 100644 --- a/addons/hr/hr.py +++ b/addons/hr/hr.py @@ -21,6 +21,7 @@ import logging +from openerp import SUPERUSER_ID from openerp import tools from openerp.modules.module import get_module_resource from openerp.osv import fields, osv @@ -248,27 +249,46 @@ class hr_employee(osv.osv): 'color': 0, } - def create(self, cr, uid, data, context=None): - if context is None: - context = {} - create_ctx = dict(context, mail_create_nolog=True) - employee_id = super(hr_employee, self).create(cr, uid, data, context=create_ctx) + def _broadcast_welcome(self, cr, uid, employee_id, context=None): + """ Broadcast the welcome message to all users in the employee company. """ employee = self.browse(cr, uid, employee_id, context=context) + partner_ids = [] + _model, group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user') if employee.user_id: - res_users = self.pool['res.users'] - # send a copy to every user of the company - # TODO: post to the `Whole Company` mail.group when we'll be able to link to the employee record - _model, group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base', 'group_user') - user_ids = res_users.search(cr, uid, [('company_id', '=', employee.user_id.company_id.id), - ('groups_id', 'in', group_id)]) - partner_ids = list(set(u.partner_id.id for u in res_users.browse(cr, uid, user_ids, context=context))) + company_id = employee.user_id.company_id.id + elif employee.company_id: + company_id = employee.company_id.id + elif employee.job_id: + company_id = employee.job_id.company_id.id + elif employee.department_id: + company_id = employee.department_id.company_id.id else: - partner_ids = [] - self.message_post(cr, uid, [employee_id], + company_id = self.pool['res.company']._company_default_get(cr, uid, 'hr.employee', context=context) + res_users = self.pool['res.users'] + user_ids = res_users.search( + cr, SUPERUSER_ID, [ + ('company_id', '=', company_id), + ('groups_id', 'in', group_id) + ], context=context) + partner_ids = list(set(u.partner_id.id for u in res_users.browse(cr, SUPERUSER_ID, user_ids, context=context))) + self.message_post( + cr, uid, [employee_id], body=_('Welcome to %s! Please help him/her take the first steps with OpenERP!') % (employee.name), partner_ids=partner_ids, subtype='mail.mt_comment', context=context ) + return True + + def create(self, cr, uid, data, context=None): + if context is None: + context = {} + if context.get("mail_broadcast"): + context['mail_create_nolog'] = True + + employee_id = super(hr_employee, self).create(cr, uid, data, context=context) + + if context.get("mail_broadcast"): + self._broadcast_welcome(cr, uid, employee_id, context=context) return employee_id def unlink(self, cr, uid, ids, context=None): diff --git a/addons/hr_recruitment/hr_recruitment.py b/addons/hr_recruitment/hr_recruitment.py index 05c39147cf9..20e9e0930cc 100644 --- a/addons/hr_recruitment/hr_recruitment.py +++ b/addons/hr_recruitment/hr_recruitment.py @@ -453,6 +453,7 @@ class hr_applicant(osv.Model): contact_name = self.pool.get('res.partner').name_get(cr, uid, [applicant.partner_id.id])[0][1] if applicant.job_id and (applicant.partner_name or contact_name): applicant.job_id.write({'no_of_hired_employee': applicant.job_id.no_of_hired_employee + 1}, context=context) + create_ctx = dict(context, mail_broadcast=True) emp_id = hr_employee.create(cr, uid, {'name': applicant.partner_name or contact_name, 'job_id': applicant.job_id.id, 'address_home_id': address_id, @@ -460,7 +461,7 @@ class hr_applicant(osv.Model): 'address_id': applicant.company_id and applicant.company_id.partner_id and applicant.company_id.partner_id.id or False, 'work_email': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.email or False, 'work_phone': applicant.department_id and applicant.department_id.company_id and applicant.department_id.company_id.phone or False, - }) + }, context=create_ctx) self.write(cr, uid, [applicant.id], {'emp_id': emp_id}, context=context) self.pool['hr.job'].message_post( cr, uid, [applicant.job_id.id],