diff --git a/hr_holidays_overlap/models/hr_leave_type.py b/hr_holidays_overlap/models/hr_leave_type.py index c598b8ad..7a23ed13 100644 --- a/hr_holidays_overlap/models/hr_leave_type.py +++ b/hr_holidays_overlap/models/hr_leave_type.py @@ -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), ] ): @@ -51,8 +51,9 @@ 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 ) @@ -60,7 +61,18 @@ def _get_employees_days_per_allocation(self, employee_ids, date=None): continue 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 diff --git a/hr_holidays_overlap/tests/test_hr_holidays_overlap.py b/hr_holidays_overlap/tests/test_hr_holidays_overlap.py index cd750f08..c2303741 100644 --- a/hr_holidays_overlap/tests/test_hr_holidays_overlap.py +++ b/hr_holidays_overlap/tests/test_hr_holidays_overlap.py @@ -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), } @@ -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"""