From 8b6f1a195a90f0b75168925009bfeebaf7d70258 Mon Sep 17 00:00:00 2001 From: Philippe Wauthy Date: Wed, 7 Sep 2022 09:20:14 +0000 Subject: [PATCH] [FIX] hr_holidays: delete calendar event when time off is deleted Since https://github.com/odoo/odoo/pull/76722, it is possible to cancel an approved time off. However, the calendar event created when the leave is validated is not archived when the leave is cancelled. This commit archives the calendar event when the leave is cancelled. task-2965754 closes odoo/odoo#100413 X-original-commit: d292891997a378578e072cfb01bba612501aa82a Signed-off-by: Kevin Baptiste Signed-off-by: phwa-odoo --- addons/hr_holidays/models/hr_leave.py | 1 + .../hr_holidays/tests/test_leave_requests.py | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 7a638064f97..b2f994636ac 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -1351,6 +1351,7 @@ class HolidaysRequest(models.Model): leave_sudo = self.sudo() leave_sudo.with_context(from_cancel_wizard=True).active = False + leave_sudo.meeting_id.active = False leave_sudo._remove_resource_leave() def action_documents(self): diff --git a/addons/hr_holidays/tests/test_leave_requests.py b/addons/hr_holidays/tests/test_leave_requests.py index 1448d0acfe2..e07e363344d 100644 --- a/addons/hr_holidays/tests/test_leave_requests.py +++ b/addons/hr_holidays/tests/test_leave_requests.py @@ -52,6 +52,13 @@ class TestLeaveRequests(TestHrHolidaysCommon): 'leave_validation_type': 'manager', }) + cls.holidays_type_4 = LeaveType.create({ + 'name': 'Limited with 2 approvals', + 'requires_allocation': 'yes', + 'employee_requests': 'yes', + 'leave_validation_type': 'both', + }) + cls.set_employee_create_date(cls.employee_emp_id, '2010-02-03 00:00:00') cls.set_employee_create_date(cls.employee_hruser_id, '2010-02-03 00:00:00') @@ -843,3 +850,38 @@ class TestLeaveRequests(TestHrHolidaysCommon): 'virtual_leaves_taken': 6, } ) + + def test_cancel_leave(self): + with freeze_time('2020-09-15'): + allocation = self.env['hr.leave.allocation'].create({ + 'name': 'Annual Time Off', + 'employee_id': self.employee_emp_id, + 'holiday_status_id': self.holidays_type_4.id, + 'number_of_days': 20, + 'state': 'confirm', + 'date_from': '2020-01-01', + 'date_to': '2020-12-31', + }) + allocation.action_validate() + + leave = self.env['hr.leave'].with_user(self.user_employee_id).create({ + 'name': 'Holiday Request', + 'employee_id': self.employee_emp_id, + 'holiday_status_id': self.holidays_type_4.id, + 'date_from': '2020-09-21', + 'date_to': '2020-09-23', + 'number_of_days': 3, + }) + + # A meeting is only created once the leave is validated + self.assertFalse(leave.meeting_id) + leave.with_user(self.user_hrmanager_id).action_approve() + self.assertFalse(leave.meeting_id) + + # A meeting is created in the user's calendar when a leave is validated + leave.with_user(self.user_hrmanager_id).action_validate() + self.assertTrue(leave.meeting_id.active) + + # The meeting is archived when the leave is cancelled + leave.with_user(self.user_employee_id)._action_user_cancel('Cancel leave') + self.assertFalse(leave.meeting_id.active)