[FIX] base,hr: Ensure employee/user image at creation

Ensure an avatar is generated based on the employee/user name
if no image is provided at the record creation (for internal users only).

closes odoo/odoo#147446

Taskid: 3637523
Related: odoo/enterprise#58646
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Co-authored-by: Yannick Tivisse <yti@odoo.com>
This commit is contained in:
Mélanie
2024-03-15 13:04:43 +00:00
co-authored by Yannick Tivisse
parent 3e8f9ccbc1
commit 941dc4cee0
7 changed files with 57 additions and 16 deletions
+10 -4
View File
@@ -168,16 +168,16 @@ class HrEmployeePrivate(models.Model):
super()._compute_avatar_128()
def _compute_avatar(self, avatar_field, image_field):
employee_wo_user_or_image_ids = []
employee_wo_user_and_image = self.env['hr.employee']
for employee in self:
if not (employee.user_id or employee._origin[image_field]):
employee_wo_user_or_image_ids.append(employee.id)
if not employee.user_id and not employee._origin[image_field]:
employee_wo_user_and_image += employee
continue
avatar = employee._origin[image_field]
if not avatar and employee.user_id:
avatar = employee.user_id.sudo()[avatar_field]
employee[avatar_field] = avatar
super(HrEmployeePrivate, self.browse(employee_wo_user_or_image_ids))._compute_avatar(avatar_field, image_field)
super(HrEmployeePrivate, employee_wo_user_and_image)._compute_avatar(avatar_field, image_field)
@api.depends('name', 'permit_no')
def _compute_work_permit_name(self):
@@ -383,6 +383,12 @@ class HrEmployeePrivate(models.Model):
vals.update(self._sync_user(user, bool(vals.get('image_1920'))))
vals['name'] = vals.get('name', user.name)
employees = super().create(vals_list)
# Sudo in case HR officer doesn't have the Contact Creation group
employees.filtered(lambda e: not e.work_contact_id).sudo()._create_work_contacts()
for employee_sudo in employees.sudo():
if not employee_sudo.image_1920:
employee_sudo.image_1920 = employee_sudo._avatar_generate_svg()
employee_sudo.work_contact_id.image_1920 = employee_sudo.image_1920
if self.env.context.get('salary_simulation'):
return employees
employee_departments = employees.department_id
+17 -7
View File
@@ -198,21 +198,31 @@ class HrEmployeeBase(models.AbstractModel):
employee.mobile_phone = employee.work_contact_id.mobile
employee.work_email = employee.work_contact_id.email
def _create_work_contacts(self):
if any(employee.work_contact_id for employee in self):
raise UserError(_('Some employee already have a work contact'))
work_contacts = self.env['res.partner'].create([{
'email': employee.work_email,
'mobile': employee.mobile_phone,
'name': employee.name,
'image_1920': employee.image_1920,
'company_id': employee.company_id.id
} for employee in self])
for employee, work_contact in zip(self, work_contacts):
employee.work_contact_id = work_contact
def _inverse_work_contact_details(self):
employees_without_work_contact = self.env['hr.employee']
for employee in self:
if not employee.work_contact_id:
employee.work_contact_id = self.env['res.partner'].sudo().create({
'email': employee.work_email,
'mobile': employee.mobile_phone,
'name': employee.name,
'image_1920': employee.image_1920,
'company_id': employee.company_id.id
})
employees_without_work_contact += employee
else:
employee.work_contact_id.sudo().write({
'email': employee.work_email,
'mobile': employee.mobile_phone,
})
if employees_without_work_contact:
employees_without_work_contact.sudo()._create_work_contacts()
@api.depends('company_id')
def _compute_address_id(self):