Skip to content

Commit

Permalink
fixup! [ADD] hr_holidays_overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Nov 22, 2023
1 parent 00c7d28 commit b20af31
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
28 changes: 20 additions & 8 deletions hr_holidays_overlap/models/hr_leave_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def _get_employees_days_per_allocation(self, employee_ids, date=None):
for possible_overlap in HrLeave.search(
[
("employee_id", "=", employee_id),
("state", "not in", ("draft", "refuse")),
("state", "=", "validate"),
("holiday_status_id.can_overlap", "=", True),
]
):
for overlap in HrLeave.search(
[
("employee_id", "=", employee_id),
("state", "not in", ("draft", "refuse")),
("state", "in", ("confirm", "validate1", "validate")),
("id", "not in", possible_overlap.ids),
("date_from", "<", possible_overlap.date_to),
("date_to", ">", possible_overlap.date_from),
("date_from", "<=", possible_overlap.date_to),
("date_to", ">=", possible_overlap.date_from),
("holiday_status_id", "in", self.ids),
]
):
Expand All @@ -51,16 +51,28 @@ def _get_employees_days_per_allocation(self, employee_ids, date=None):
)[employee_id]["days"]
for allocation, allocation_days in allocation_dict.items():
if (
allocation
and allocation.date_to
not allocation
or isinstance(allocation, str)
or allocation.date_to
and (
allocation.date_to < date or allocation.date_from > date
)
):
continue

Check warning on line 61 in hr_holidays_overlap/models/hr_leave_type.py

View check run for this annotation

Codecov / codecov/patch

hr_holidays_overlap/models/hr_leave_type.py#L61

Added line #L61 was not covered by tests
allocation_days["virtual_remaining_leaves"] += number_of_days
allocation_days["virtual_leaves_taken"] -= number_of_days
allocation_days["remaining_leaves"] += number_of_days
allocation_days["leaves_taken"] -= number_of_days
if possible_overlap.state == "validate":
allocation_days["remaining_leaves"] += number_of_days
allocation_days["leaves_taken"] -= number_of_days
break
if "error" in allocation_dict:
allocation_dict["error"][
"virtual_remaining_leaves"
] += number_of_days
allocation_dict[False][
"virtual_remaining_leaves"
] += number_of_days
if not allocation_dict["error"]["virtual_remaining_leaves"]:
del allocation_dict["error"]
del allocation_dict[False]
return result
31 changes: 30 additions & 1 deletion hr_holidays_overlap/tests/test_hr_holidays_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
"name": "Paid leave",
"state": "confirm",
"holiday_status_id": self.env.ref("hr_holidays.holiday_status_cl").id,
# those two are needed to circumvent hr.leave#copy_data's check
# those two are needed to bypass hr.leave#copy_data's check
"date_from": demo_sick_leave.date_to,
"date_to": demo_sick_leave.date_to + timedelta(days=2),
}
Expand Down Expand Up @@ -47,6 +47,35 @@ def test_overlap(self):
paid_leave.holiday_status_id.remaining_leaves,
19,
)
# be sure we can use all 19 days for leave
new_leave = self.paid_leave.copy(
default={
"name": "Paid leave",
"state": "confirm",
"holiday_status_id": self.env.ref("hr_holidays.holiday_status_cl").id,
"date_from": self.paid_leave.date_to,
"date_to": self.paid_leave.date_to + timedelta(days=27),
}
)
new_leave.action_validate()
self.assertEqual(new_leave.number_of_days, 19)
self.assertEqual(new_leave.holiday_status_id.virtual_remaining_leaves, 0)

with self.assertRaisesRegex(
ValidationError,
"The number of remaining time off is not sufficient for this time off type.",
):
self.paid_leave.copy(
default={
"name": "Paid leave",
"state": "confirm",
"holiday_status_id": self.env.ref(
"hr_holidays.holiday_status_cl"
).id,
"date_from": new_leave.date_from,
"date_to": new_leave.date_to + timedelta(days=1),
}
)

def test_no_overlap(self):
"""Test that we still don't allow overlapping for types not configured accordingly"""
Expand Down

0 comments on commit b20af31

Please sign in to comment.