From 571d5a1b9a2a752fa5e4fd3b3029ac26cbd0222b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 2 Jul 2024 16:52:46 +0200 Subject: [PATCH] [FIX] hr_holidays_public*: Use the context employee or the user's employee Use case: - Go to Employees to an employee with a different address (country) than our own and with specific public holidays for that country. - Go to the Time-off smart-buttons - We will have to see there the public holidays according to the employee's address TT49839 --- hr_holidays_public/models/hr_leave.py | 11 ++++++-- .../tests/test_holidays_public.py | 20 ++++++++++---- hr_holidays_public_city/models/hr_leave.py | 9 ++++++- .../tests/test_holidays_public.py | 26 +++++++++++++++---- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/hr_holidays_public/models/hr_leave.py b/hr_holidays_public/models/hr_leave.py index 5b4ba8e7..24d7934f 100644 --- a/hr_holidays_public/models/hr_leave.py +++ b/hr_holidays_public/models/hr_leave.py @@ -55,6 +55,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( ( @@ -63,7 +70,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: @@ -74,7 +81,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 0bbea38a..e985b103 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() @@ -208,8 +209,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) ) @@ -223,12 +223,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")