Skip to content

Commit

Permalink
[IMP] hr_timesheet_sheet_prefill[_multi]: Only use active projects
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
  • Loading branch information
carmenbianca committed Sep 16, 2024
1 parent 8d123ed commit 07548fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
6 changes: 4 additions & 2 deletions hr_timesheet_sheet_prefill/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 15 additions & 7 deletions hr_timesheet_sheet_prefill_multi/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 26 additions & 3 deletions hr_timesheet_sheet_prefill_multi/tests/test_prefill_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 07548fc

Please sign in to comment.