Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][FIX] hr_holidays_public: wrong computation in case resources in different countries #47

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions hr_holidays_public/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,45 @@ 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),
)
gurneyalex marked this conversation as resolved.
Show resolved Hide resolved
.filtered(lambda rec: not rec.year_id.country_id)
.mapped("date")
)
.mapped("date")
)
}
if resources:
employees = self.env["hr.employee.public"].search(
gurneyalex marked this conversation as resolved.
Show resolved Hide resolved
[("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(
gurneyalex marked this conversation as resolved.
Show resolved Hide resolved
self.env["hr.holidays.public"]
.get_holidays_list(
start_dt=start_dt.date(),
end_dt=end_dt.date(),
employee_id=employee.id,
)
.mapped("date")
)
# even if employees and resource_country are empty
# we still process holidays, so provide defaults
for resource in resources:
interval_resource = intervals[resource.id]
interval_resource = intervals.get(resource.id, Intervals())
attendances = []
country = resource_country.get(resource.id, self.env["res.country"])
holidays = holidays_by_country.get(country, set())
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
41 changes: 40 additions & 1 deletion hr_holidays_public/tests/test_holidays_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Copyright 2018 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import date
from datetime import date, datetime

import pytz

from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import TransactionCase
Expand Down Expand Up @@ -314,3 +316,40 @@ def test_get_unusual_days_not_return_public_holidays_fallback_to_company_state(
country_id=demo_user_empl_addr.country_id.id,
state_ids=[(6, 0, [self.env.ref("base.state_us_3").id])],
)

def test_calendar_attendance_interval_exclude_public_holidays(self):
# SK employee has holiday 3, so off on 1994-11-14
employee_sk = self.employee_model.create(
{
"name": "Employee Sk",
"address_id": self.env["res.partner"]
.create(
{"name": "Employee Sk", "country_id": self.env.ref("base.sk").id}
)
.id,
}
)
resource_sk = employee_sk.resource_id
# SL employee has holiday2 so off on 1994-10-14
employee_sl = self.employee
resource_sl = employee_sl.resource_id
calendar = employee_sl.resource_id.calendar_id.with_context(
exclude_public_holidays=True
)
start_dt = datetime(1994, 10, 1, tzinfo=pytz.utc)
end_dt = datetime(1994, 11, 30, tzinfo=pytz.utc)
intervals_sk_sl = calendar._attendance_intervals_batch(
start_dt=start_dt, end_dt=end_dt, resources=resource_sk + resource_sl
)
intervals_sk = calendar._attendance_intervals_batch(
start_dt=start_dt, end_dt=end_dt, resources=resource_sk
)
intervals_sl = calendar._attendance_intervals_batch(
start_dt=start_dt, end_dt=end_dt, resources=resource_sl
)
self.assertEqual(
intervals_sk[resource_sk.id]._items, intervals_sk_sl[resource_sk.id]._items
)
self.assertEqual(
intervals_sl[resource_sl.id]._items, intervals_sk_sl[resource_sl.id]._items
)