diff --git a/hr_timesheet_sheet_prefill/models/hr_employee.py b/hr_timesheet_sheet_prefill/models/hr_employee.py index 3cd86ae..393051b 100644 --- a/hr_timesheet_sheet_prefill/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill/models/hr_employee.py @@ -12,9 +12,11 @@ class Employee(models.Model): comodel_name="project.project", relation="hr_employee_project_project_rel", string="Projects", + domain=[("active", "=", True)], ) - # This exists solely for extension in hr_timesheet_sheet_prefill_multi. + # This exists solely extension in hr_timesheet_sheet_prefill_multi, and also + # to filter out inactive projects. def all_prefill_projects(self): self.ensure_one() - return self.project_ids + return self.project_ids.filtered(lambda project: project.active) diff --git a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py index 40a08fb..e4eb137 100644 --- a/hr_timesheet_sheet_prefill_multi/models/hr_employee.py +++ b/hr_timesheet_sheet_prefill_multi/models/hr_employee.py @@ -16,10 +16,18 @@ class HrEmployee(models.Model): ) def all_prefill_projects(self): - self.ensure_one() - # The only purpose of the below code is to sort the projects according - # to the sequence of the prefill records. - projects = self.env["project.project"].browse() - for prefill in self.timesheet_prefill_ids.sorted(): - projects += prefill.project_project_id - return projects + projects = super().all_prefill_projects() + # By searching, we get prefills sorted by sequence. + prefills = self.env["hr_timesheet.sheet.prefill"].search( + [ + ("project_project_id", "in", projects.ids), + ("hr_employee_id", "=", self.id), + ] + ) + # Instead of doing `prefills.mapped("project_project_id")`, we manually + # create the recordset. This is because the recordset MAY contain + # duplicates, and `mapped()` removes duplicates. + result = self.env["project.project"] + for prefill in prefills: + result += prefill.project_project_id + return result diff --git a/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py index f8bf60e..8670e20 100644 --- a/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py +++ b/hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py @@ -27,6 +27,29 @@ def setUp(self): } ) + def test_all_prefill_projects_filtered_sorted(self): + """Prefill projects are sorted and filtered.""" + project_03 = self.env["project.project"].create({"name": "Project 03"}) + self.project_02.active = False + + self.employee.timesheet_prefill_ids = [ + (0, False, {"project_project_id": self.project_01.id, "sequence": 1}), + (0, False, {"project_project_id": self.project_02.id, "sequence": 2}), + (0, False, {"project_project_id": project_03.id, "sequence": 3}), + ] + sheet = self.env["hr_timesheet.sheet"].create( + { + "employee_id": self.employee.id, + "date_start": "2024-01-01", + "date_end": "2024-01-01", + } + ) + self.assertEqual(len(sheet.timesheet_ids), 2) + used_projects = sheet.timesheet_ids.mapped("project_id") + self.assertIn(self.project_01, used_projects) + self.assertNotIn(self.project_02, used_projects) + self.assertIn(project_03, used_projects) + def test_project_ids_still_works(self): """You can still use project_ids on hr.employee as normally. It will create prefill records. @@ -62,9 +85,9 @@ def test_project_ids_still_works(self): def test_sequenced_repeated_prefills(self): """You can repeat and sequence prefills.""" self.employee.timesheet_prefill_ids = [ - (0, False, {"project_project_id": self.project_01, "sequence": 1}), - (0, False, {"project_project_id": self.project_02, "sequence": 2}), - (0, False, {"project_project_id": self.project_01, "sequence": 3}), + (0, False, {"project_project_id": self.project_01.id, "sequence": 1}), + (0, False, {"project_project_id": self.project_02.id, "sequence": 2}), + (0, False, {"project_project_id": self.project_01.id, "sequence": 3}), ] # Sanity check.