diff --git a/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py b/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py index 632035d374ec..29c17edd3c8a 100644 --- a/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py +++ b/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py @@ -16,10 +16,10 @@ def create_work_contact(env): res.user as a work_contact_id. If the employee is not linked to a res.user. Then we try to match an - existing partner with the same email address. If one is found, then - we assign it as work_contact_id. If several are found, we raise a - warning and we link the first one found. If none are found, then we - create a new partner. + existing partner with the same email address and mobile (when the employee + has one of them). If one is found, then we assign it as work_contact_id. + If several are found, we raise a warning and we link the first one found. + If none are found, then we create a new partner. """ employees = env["hr.employee"].search([]) @@ -44,12 +44,17 @@ def create_work_contact(env): partner.id, ) else: - matching_partner = env["res.partner"].search( - [ - ("email", "=", employee.work_email), - ("mobile", "=", employee.mobile_phone), - ] - ) + # Without email nor mobile, the search would match every partner + # that has neither of them, and an unrelated contact would become + # the work contact (the private address is merged into it in 17.0) + matching_partner = env["res.partner"] + if employee.work_email or employee.mobile_phone: + matching_partner = matching_partner.search( + [ + ("email", "=", employee.work_email), + ("mobile", "=", employee.mobile_phone), + ] + ) nb_matching_partner = len(matching_partner) if nb_matching_partner == 1: employee.work_contact_id = matching_partner