diff --git a/hr_holidays_public/models/hr_leave.py b/hr_holidays_public/models/hr_leave.py index 32a50f35..667859d9 100644 --- a/hr_holidays_public/models/hr_leave.py +++ b/hr_holidays_public/models/hr_leave.py @@ -52,6 +52,13 @@ def _compute_number_of_hours_display(self): def _get_domain_from_get_unusual_days(self, date_from, date_to=None): domain = [("date", ">=", date_from)] + # Use the employee of the user or the one who has the context + employee_id = self.env.context.get("employee_id", False) + employee = ( + self.env["hr.employee"].browse(employee_id) + if employee_id + else self.env.user.employee_id + ) if date_to: domain.append( ( @@ -60,7 +67,7 @@ def _get_domain_from_get_unusual_days(self, date_from, date_to=None): date_to, ) ) - country_id = self.env.user.employee_id.address_id.country_id.id + country_id = employee.address_id.country_id.id if not country_id: country_id = self.env.company.country_id.id or False if country_id: @@ -71,7 +78,7 @@ def _get_domain_from_get_unusual_days(self, date_from, date_to=None): ("year_id.country_id", "=", country_id), ] ) - state_id = self.env.user.employee_id.address_id.state_id.id + state_id = employee.address_id.state_id.id if not state_id: state_id = self.env.company.state_id.id or False if state_id: diff --git a/hr_holidays_public/tests/test_holidays_public.py b/hr_holidays_public/tests/test_holidays_public.py index 8170461c..3487b6fa 100644 --- a/hr_holidays_public/tests/test_holidays_public.py +++ b/hr_holidays_public/tests/test_holidays_public.py @@ -19,6 +19,7 @@ def setUpClass(cls): cls.holiday_model_line = cls.env["hr.holidays.public.line"] cls.employee_model = cls.env["hr.employee"] cls.wizard_next_year = cls.env["public.holidays.next.year.wizard"] + cls.leave_model = cls.env["hr.leave"] # Remove possibly existing public holidays that would interfer. cls.holiday_model_line.search([]).unlink() @@ -211,8 +212,7 @@ def assertPublicHolidayIsUnusualDay( self, expected, country_id=None, state_ids=False ): self.assertFalse( - self.env["hr.leave"] - .with_user(self.env.ref("base.user_demo").id) + self.leave_model.with_user(self.env.ref("base.user_demo").id) .get_unusual_days("2019-07-01", date_to="2019-07-31") .get("2019-07-30", False) ) @@ -226,12 +226,22 @@ def assertPublicHolidayIsUnusualDay( } ) self.assertEqual( - self.env["hr.leave"] - .with_user(self.env.ref("base.user_demo").id) - .get_unusual_days("2019-07-01", date_to="2019-07-31")["2019-07-30"], + self.leave_model.with_user( + self.env.ref("base.user_demo").id + ).get_unusual_days("2019-07-01", date_to="2019-07-31")["2019-07-30"], expected, ) + def test_public_holidays_context(self): + self.env.ref("base.user_demo").employee_id.address_id.country_id = False + self.leave_model = self.leave_model.with_context(employee_id=self.employee.id) + self.assertPublicHolidayIsUnusualDay( + True, + country_id=self.env.ref( + "base.user_demo" + ).employee_id.address_id.country_id.id, + ) + def test_get_unusual_days_return_public_holidays_same_country(self): self.env.ref("base.user_demo").employee_id.address_id.state_id = False self.env.company.state_id = False diff --git a/hr_holidays_public_city/models/hr_leave.py b/hr_holidays_public_city/models/hr_leave.py index 361470ad..a5996ae4 100644 --- a/hr_holidays_public_city/models/hr_leave.py +++ b/hr_holidays_public_city/models/hr_leave.py @@ -10,8 +10,15 @@ def _get_domain_from_get_unusual_days(self, date_from, date_to=None): domain = super()._get_domain_from_get_unusual_days( date_from=date_from, date_to=date_to ) + # Use the employee of the user or the one who has the context + employee_id = self.env.context.get("employee_id", False) + employee = ( + self.env["hr.employee"].browse(employee_id) + if employee_id + else self.env.user.employee_id + ) # Add city domain - city_id = self.env.user.employee_id.address_id.city_id.id + city_id = employee.address_id.city_id.id if not city_id: city_id = self.env.company.partner_id.city_id.id or False if city_id: diff --git a/hr_holidays_public_city/tests/test_holidays_public.py b/hr_holidays_public_city/tests/test_holidays_public.py index 0aa8bcfb..34b24678 100644 --- a/hr_holidays_public_city/tests/test_holidays_public.py +++ b/hr_holidays_public_city/tests/test_holidays_public.py @@ -29,8 +29,7 @@ def assertPublicHolidayIsUnusualDay( self, expected, country_id=None, state_ids=False, city_ids=False ): self.assertFalse( - self.env["hr.leave"] - .with_user(self.env.ref("base.user_demo").id) + self.leave_model.with_user(self.env.ref("base.user_demo").id) .get_unusual_days("2019-07-01", date_to="2019-07-31") .get("2019-07-30", False) ) @@ -45,12 +44,29 @@ def assertPublicHolidayIsUnusualDay( } ) self.assertEqual( - self.env["hr.leave"] - .with_user(self.env.ref("base.user_demo").id) - .get_unusual_days("2019-07-01", date_to="2019-07-31")["2019-07-30"], + self.leave_model.with_user( + self.env.ref("base.user_demo").id + ).get_unusual_days("2019-07-01", date_to="2019-07-31")["2019-07-30"], expected, ) + def test_public_holidays_context(self): + self.env.ref("base.user_demo").employee_id.address_id.country_id = False + self.env.ref("base.user_demo").employee_id.address_id.state_id = False + self.env.ref("base.user_demo").employee_id.address_id.city_id = False + self.employee.address_id.country_id = self.env.ref("base.us") + self.employee.address_id.state_id = self.env.ref("base.state_us_4") + self.employee.address_id.city_id = self.us_city_a + self.leave_model = self.leave_model.with_context(employee_id=self.employee.id) + self.assertPublicHolidayIsUnusualDay( + True, + country_id=self.env.ref( + "base.user_demo" + ).employee_id.address_id.country_id.id, + state_ids=[(6, 0, [self.employee.address_id.state_id.id])], + city_ids=[(6, 0, [self.employee.address_id.city_id.id])], + ) + def test_get_unusual_days_return_public_holidays_same_state_same_city(self): demo_user_empl_addr = self.env.ref("base.user_demo").employee_id.address_id demo_user_empl_addr.country_id = self.env.ref("base.us")