From 08da07b1cab808ab749770e8392fefb79f5b567a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre?= Date: Thu, 18 Apr 2019 13:22:58 +0000 Subject: [PATCH] [FIX] hr_holidays: Fix deleted default employee The goal of commit 2dbce5e was to handle the company holiday type in onchange which was probably forgotten when the company type was introduced. However because the last `elif` was changed to `else`, the `else` clause is executed in some situations where it shouldn't. e.g. If holiday type is `employee` and an employee is set, the `else` clause is (obviously wrongly) executed. This is particularly a problem if the employee is not HR user nor manager. In that case, the field employee_id is invisible and its value has been deleted by the onchange. The allocation cannot be saved because it violates the sql constraint (employee is null). Also, the `mode_company_id` and `category_id` should be set to `False` when the holiday type is not company/category. --- .../hr_holidays/models/hr_leave_allocation.py | 26 +++++++++++++------ .../hr_holidays/tests/test_leave_requests.py | 8 ++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave_allocation.py b/addons/hr_holidays/models/hr_leave_allocation.py index 1ef1a13b3a4..2ec46d6568b 100644 --- a/addons/hr_holidays/models/hr_leave_allocation.py +++ b/addons/hr_holidays/models/hr_leave_allocation.py @@ -244,15 +244,25 @@ class HolidaysAllocation(models.Model): @api.onchange('holiday_type') def _onchange_type(self): if self.holiday_type == 'employee': - if not self.employee_id and self.env.user.employee_ids: - self.employee_id = self.env.user.employee_ids[0] + if not self.employee_id: + self.employee_id = self.env.user.employee_ids[:1].id + self.mode_company_id = False + self.category_id = False + elif self.holiday_type == 'company': + self.employee_id = False + if not self.mode_company_id: + self.mode_company_id = self.env.user.company_id.id + self.category_id = False elif self.holiday_type == 'department': - if self.env.user.employee_ids: - self.department_id = self.department_id or self.env.user.employee_ids[0].department_id - self.employee_id = None - else: - self.employee_id = None - self.department_id = None + self.employee_id = False + self.mode_company_id = False + self.category_id = False + if not self.department_id: + self.department_id = self.env.user.employee_ids[:1].department_id.id + elif self.holiday_type == 'category': + self.employee_id = False + self.mode_company_id = False + self.department_id = False @api.onchange('employee_id') def _onchange_employee(self): diff --git a/addons/hr_holidays/tests/test_leave_requests.py b/addons/hr_holidays/tests/test_leave_requests.py index 18eb117c7bc..8f437333033 100644 --- a/addons/hr_holidays/tests/test_leave_requests.py +++ b/addons/hr_holidays/tests/test_leave_requests.py @@ -165,3 +165,11 @@ class TestLeaveRequests(TestHrHolidaysBase): leave.action_approve() member_ids = self.hr_dept.member_ids.ids self.assertEqual(self.env['hr.leave'].search_count([('employee_id', 'in', member_ids)]), len(member_ids), "Leave should be created for members of department") + + @mute_logger('odoo.models.unlink', 'odoo.addons.mail.models.mail_mail') + def test_allocation_request(self): + """ Create an allocation request """ + # employee should be set to current user + allocation_form = Form(self.env['hr.leave.allocation'].sudo(self.user_employee)) + allocation_form.holiday_status_id = self.holidays_type_1 + allocation = allocation_form.save()