Skip to content

Commit

Permalink
[FIX] hr_holidays_public*: Use the context employee or the user's emp…
Browse files Browse the repository at this point in the history
…loyee

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
  • Loading branch information
victoralmau committed Jul 23, 2024
1 parent 1254419 commit ce3a7b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
11 changes: 9 additions & 2 deletions hr_holidays_public/models/hr_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand All @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 15 additions & 5 deletions hr_holidays_public/tests/test_holidays_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
)
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion hr_holidays_public_city/models/hr_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 21 additions & 5 deletions hr_holidays_public_city/tests/test_holidays_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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")
Expand Down

0 comments on commit ce3a7b3

Please sign in to comment.