Skip to content

Commit

Permalink
[FIX] hr_holidays_public: wrong computation in case resources in diff…
Browse files Browse the repository at this point in the history
…erent countries

Fix an issue in hr_holidays_public where
resource.calendar::_attendance_intervals_batch_exclude_public_holidays
would return a wrong result when called with multiple resources working
in different countries.

The original implementation uses a context key employee_id to find the
employee, which only allows this method to be called with a single
employee. The fix searches for the employees related to the resources
passed in arguments, and default to the context key if there are no
provided resources.

Also rework the implementation to use a python set rather than a list
for faster in test.
  • Loading branch information
gurneyalex committed Jun 16, 2022
1 parent 7fb059d commit 0912bef
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions hr_holidays_public/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,42 @@ class ResourceCalendar(models.Model):
def _attendance_intervals_batch_exclude_public_holidays(
self, start_dt, end_dt, intervals, resources, tz
):
list_by_dates = (
self.env["hr.holidays.public"]
.get_holidays_list(
start_dt=start_dt.date(),
end_dt=end_dt.date(),
employee_id=self.env.context.get("employee_id", False),
holidays_by_country = {
False: set(
self.env["hr.holidays.public"]
.get_holidays_list(
start_dt=start_dt.date(),
end_dt=end_dt.date(),
employee_id=self.env.context.get("employee_id", False),
)
.mapped("date")
)
.mapped("date")
)
}
if resources:
employees = self.env["hr.employee.public"].search(
[("resource_id", "in", resources.ids)]
)
resource_country = {}
for employee in employees:
country = employee.address_id.country_id
resource_country[employee.resource_id.id] = country.id
if country.id not in holidays_by_country:
holidays_by_country[country.id] = set(
self.env["hr.holidays.public"]
.get_holidays_list(
start_dt=start_dt.date(),
end_dt=end_dt.date(),
employee_id=employee.id,
)
.mapped("date")
)
for resource in resources:
interval_resource = intervals[resource.id]
attendances = []
country = resource_country[resource.id]
holidays = holidays_by_country[country]
for attendance in interval_resource._items:
if attendance[0].date() not in list_by_dates:
if attendance[0].date() not in holidays:
attendances.append(attendance)
intervals[resource.id] = Intervals(attendances)
return intervals
Expand Down

0 comments on commit 0912bef

Please sign in to comment.