[FIX] hr_holidays: fix multi company issue in days calculation

Prior to this commit, the multi company rules were bypassed due implicit
sudos (compute_sudo and self.env.user).
The function computing the number of days will now make sure to include
the right domain.

TaskId-2791030

closes odoo/odoo#86361

X-original-commit: dbcb37ec80bb0fef0115c879c15bdc7073894290
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
Signed-off-by: William Braeckman (wbr) <wbr@odoo.com>
This commit is contained in:
William Braeckman
2022-03-14 16:36:12 +00:00
parent 469ab9445e
commit e8129cd0e7
3 changed files with 28 additions and 3 deletions
+4 -2
View File
@@ -770,7 +770,9 @@ class HolidaysRequest(models.Model):
""" Returns a float equals to the timedelta between two dates given as string."""
if employee_id:
employee = self.env['hr.employee'].browse(employee_id)
result = employee._get_work_days_data_batch(date_from, date_to)[employee.id]
# We force the company in the domain as we are more than likely in a compute_sudo
domain = [('company_id', 'in', self.env.company.ids + self.env.context.get('allowed_company_ids', []))]
result = employee._get_work_days_data_batch(date_from, date_to, domain=domain)[employee.id]
if self.request_unit_half and result['hours'] > 0:
result['days'] = 0.5
return result
@@ -1516,4 +1518,4 @@ class HolidaysRequest(models.Model):
@api.model
def get_unusual_days(self, date_from, date_to=None):
return self.env.user.employee_id._get_unusual_days(date_from, date_to)
return self.env.user.employee_id.sudo(False)._get_unusual_days(date_from, date_to)
@@ -562,3 +562,24 @@ class TestLeaveRequests(TestHrHolidaysCommon):
req2_form.request_date_to = fields.Date.to_date('2021-12-08')
self.assertEqual(req2_form.number_of_days, 3)
def test_leave_with_public_holiday_other_company(self):
other_company = self.env['res.company'].create({
'name': 'Test Company',
})
# Create a public holiday for the second company
p_leave = self.env['resource.calendar.leaves'].create({
'date_from': datetime(2022, 3, 11),
'date_to': datetime(2022, 3, 11, 23, 59, 59),
})
p_leave.company_id = other_company
leave = self.env['hr.leave'].with_user(self.user_employee_id).create({
'name': 'Holiday Request',
'holiday_type': 'employee',
'employee_id': self.employee_emp.id,
'holiday_status_id': self.holidays_type_1.id,
'date_from': datetime(2022, 3, 11),
'date_to': datetime(2022, 3, 11, 23, 59, 59),
})
self.assertEqual(leave.number_of_days, 1)
@@ -203,7 +203,9 @@ class HrLeave(models.Model):
contracts = employee.sudo()._get_contracts(date_from, date_to, states=['open'])
contracts |= employee.sudo()._get_incoming_contracts(date_from, date_to)
calendar = contracts[:1].resource_calendar_id if contracts else None # Note: if len(contracts)>1, the leave creation will crash because of unicity constaint
result = employee._get_work_days_data_batch(date_from, date_to, calendar=calendar)[employee.id]
# We force the company in the domain as we are more than likely in a compute_sudo
domain = [('company_id', 'in', self.env.company.ids + self.env.context.get('allowed_company_ids', []))]
result = employee._get_work_days_data_batch(date_from, date_to, calendar=calendar, domain=domain)[employee.id]
if self.request_unit_half and result['hours'] > 0:
result['days'] = 0.5
return result