[FIX] hr: Redirect to company employee profile

Only one employee per company and per user is allowed.
Based on the company the user is logged in, "My profile"
should redirect to the employee profile of the current
company.
If the user has no employee in the current company,
"My Profile" redirects to the simple "Preferences" screen.

Currently, it always redirects to the employee in the user's
main company (`user.company_id`)

This issue is a consequence of a5b6f31 which uses the context
to know the current company instead of `user.company_id`.

Specification
=============

Un-store the employee_id field on the res.users model.

To achieve this, we need to implement a _search method for this field.

That way, each time we try to access to the employee_id value, the
returned employee depends on the context.
This commit is contained in:
Lucas Lefèvre
2019-09-10 11:46:01 +00:00
committed by Yannick Tivisse
parent c55c3e7388
commit 55a4910b2e
+14 -3
View File
@@ -10,7 +10,8 @@ class User(models.Model):
# note: a user can only be linked to one employee per company (see sql constraint in ´hr.employee´)
employee_ids = fields.One2many('hr.employee', 'user_id', string='Related employee')
employee_id = fields.Many2one('hr.employee', string="Company employee", compute='_compute_company_employee', store=True)
employee_id = fields.Many2one('hr.employee', string="Company employee",
compute='_compute_company_employee', search='_search_company_employee', store=False)
job_title = fields.Char(related='employee_id.job_title', readonly=False)
work_phone = fields.Char(related='employee_id.work_phone', readonly=False)
@@ -182,10 +183,20 @@ class User(models.Model):
return self.sudo().env.ref('hr.res_users_action_my').read()[0]
return super(User, self).action_get()
@api.depends('company_id', 'employee_ids')
@api.depends('employee_ids')
@api.depends_context('force_company')
def _compute_company_employee(self):
for user in self:
user.employee_id = self.env['hr.employee'].search([('id', 'in', user.employee_ids.ids), ('company_id', '=', user.company_id.id)], limit=1)
user.employee_id = self.env['hr.employee'].search([('id', 'in', user.employee_ids.ids), ('company_id', '=', self.env.company.id)], limit=1)
def _search_company_employee(self, operator, value):
employees = self.env['hr.employee'].search([
('name', operator, value),
'|',
('company_id', '=', self.env.company.id),
('company_id', '=', False)
], order='company_id ASC')
return [('id', 'in', employees.mapped('user_id').ids)]
def action_create_employee(self):
self.ensure_one()