From faf0f0130bf39dddee5e5940136715dcd4e11086 Mon Sep 17 00:00:00 2001 From: sofiagvaladze Date: Fri, 7 Apr 2023 08:07:58 +0000 Subject: [PATCH] [FIX] hr_recruitment: correctly count the number of applications Previously, the "Other applications" smartbutton was incorrectly matching with the applicants with '' on the fields. We don't want applicants to match if both of them has '' on the matching fields. Moreover, record rules were not correctly applied for application_count calculation, as current record will not match with similar record which has no company defined on it. This commit fixes the above mentioned issues. task - 3266694 closes odoo/odoo#118805 X-original-commit: e8263e56423432e9465f70d26fce60c021eabaa2 Signed-off-by: Kevin Baptiste --- addons/hr_recruitment/models/hr_applicant.py | 18 +++++--- .../hr_recruitment/tests/test_recruitment.py | 45 +++++++++++++++++-- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/addons/hr_recruitment/models/hr_applicant.py b/addons/hr_recruitment/models/hr_applicant.py index e078d0ce8fb..fd2f1478593 100644 --- a/addons/hr_recruitment/models/hr_applicant.py +++ b/addons/hr_recruitment/models/hr_applicant.py @@ -140,7 +140,7 @@ class Applicant(models.Model): # Count into the companies that are selected from the multi-company widget company_ids = self.env.context.get('allowed_company_ids') if company_ids: - query.add_where('other.company_id in %s', [tuple(company_ids)]) + query.add_where('other.company_id is null or other.company_id in %s', [tuple(company_ids)]) self._apply_ir_rules(query) from_clause, where_clause, where_clause_params = query.get_sql() # In case the applicant phone or mobile is configured in wrong field @@ -148,9 +148,11 @@ class Applicant(models.Model): SELECT hr_applicant.id as appl_id, COUNT(other.id) as count FROM hr_applicant - JOIN hr_applicant other ON LOWER(other.email_from) = LOWER(hr_applicant.email_from) - OR other.partner_phone = hr_applicant.partner_phone OR other.partner_phone = hr_applicant.partner_mobile - OR other.partner_mobile = hr_applicant.partner_mobile OR other.partner_mobile = hr_applicant.partner_phone + JOIN hr_applicant other ON NULLIF(LOWER(other.email_from), '') = NULLIF(LOWER(hr_applicant.email_from), '') + OR NULLIF(other.partner_phone, '') = NULLIF(hr_applicant.partner_phone, '') + OR NULLIF(other.partner_phone, '') = NULLIF(hr_applicant.partner_mobile, '') + OR NULLIF(other.partner_mobile, '') = NULLIF(hr_applicant.partner_mobile, '') + OR NULLIF(other.partner_mobile, '') = NULLIF(hr_applicant.partner_phone, '') %(where)s GROUP BY hr_applicant.id """ % { @@ -420,9 +422,11 @@ class Applicant(models.Model): self.env.cr.execute(""" SELECT other.id FROM hr_applicant - JOIN hr_applicant other ON LOWER(other.email_from) = LOWER(hr_applicant.email_from) - OR other.partner_phone = hr_applicant.partner_phone OR other.partner_phone = hr_applicant.partner_mobile - OR other.partner_mobile = hr_applicant.partner_mobile OR other.partner_mobile = hr_applicant.partner_phone + JOIN hr_applicant other ON NULLIF(LOWER(other.email_from), '') = NULLIF(LOWER(hr_applicant.email_from), '') + OR NULLIF(other.partner_phone, '') = NULLIF(hr_applicant.partner_phone, '') + OR NULLIF(other.partner_phone, '') = NULLIF(hr_applicant.partner_mobile, '') + OR NULLIF(other.partner_mobile, '') = NULLIF(hr_applicant.partner_mobile, '') + OR NULLIF(other.partner_mobile, '') = NULLIF(hr_applicant.partner_phone, '') WHERE hr_applicant.id in %s """, (tuple(self.ids),) ) diff --git a/addons/hr_recruitment/tests/test_recruitment.py b/addons/hr_recruitment/tests/test_recruitment.py index 81230248e16..1183f9fa23c 100644 --- a/addons/hr_recruitment/tests/test_recruitment.py +++ b/addons/hr_recruitment/tests/test_recruitment.py @@ -31,7 +31,7 @@ class TestRecruitment(TransactionCase): """ Test that we find same applicants based on simmilar mail, phone or mobile phone. """ - A, B, C, D, E, F = self.env['hr.applicant'].create([ + A, B, C, D, E, F, _ = self.env['hr.applicant'].create([ { 'name': 'Application A', 'email_from': 'abc@odoo.com', @@ -57,15 +57,54 @@ class TestRecruitment(TransactionCase): }, { 'name': 'Application E', + 'partner_phone': '', }, { 'name': 'Application F', 'partner_phone': '11-12-13', # In case phone is configured in a wrong field - } + }, + { + 'name': 'Application G', + 'partner_phone': '', + }, ]) + self.assertEqual(A.application_count, 2) # C, D self.assertEqual(B.application_count, 2) # D, F self.assertEqual(C.application_count, 2) # A, D self.assertEqual(D.application_count, 3) # A, B, C - self.assertEqual(E.application_count, 0) + self.assertEqual(E.application_count, 0) # Should not match with G self.assertEqual(F.application_count, 1) # B + + def test_application_count_multi_company(self): + company_A = self.env['res.company'].create({'name': 'A company'}) + company_B = self.env['res.company'].create({'name': 'B company'}) + appX, _, _ = self.env['hr.applicant'].create([ + { + 'name': 'Application X', + 'email_from': 'abc@odoo.com', + 'partner_phone': '123', + 'partner_mobile': '14-15-16', + 'company_id': company_A.id + }, + { + 'name': 'Application Y', + 'email_from': 'abc@odoo.com', + 'partner_phone': '123', + 'partner_mobile': '14-15-16', + 'company_id': company_B.id + }, + { + 'name': 'Application Z', + 'email_from': 'abc@odoo.com', + 'partner_phone': '123', + 'partner_mobile': '14-15-16', + 'company_id': False + }, + ]) + + appX.with_context(allowed_company_ids=[company_A.id])._compute_application_count() + self.assertEqual(appX.application_count, 1, "Should not have counted the applicant from another company.") # Z + + appX.with_context(allowed_company_ids=[company_A.id, company_B.id])._compute_application_count() + self.assertEqual(appX.application_count, 2, "Should have counted the applicant from another company as well.") # Y, Z