From f7c35701f69a12ddecf674221a55e0e62fdd1267 Mon Sep 17 00:00:00 2001 From: Roy Le Date: Thu, 1 Oct 2026 13:20:37 +0700 Subject: [PATCH] [FIX] hr: do not match a random partner as work contact An employee without user, work email and mobile phone was matched with the partners having no email and no mobile, i.e. most contacts, and the first one became its work contact. In 17.0 its private address is then merged into that unrelated partner, with its journal items. The existing partners are only searched when the employee has an email or a mobile phone; otherwise a work contact is created. --- .../scripts/hr/16.0.1.1/post-migration.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) 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