Skip to content

Commit

Permalink
[IMP] hr_attendance_missing_days: don't break on multi work day atten…
Browse files Browse the repository at this point in the history
…dances (#1)

* [IMP] hr_attendance_missing_days: don't break on multi work day attendances

* [ADD] hr_attendance_contract_missing_days: tests

* fixup! [ADD] hr_attendance_contract_missing_days: tests
  • Loading branch information
hbrunn authored and fkantelberg committed Jan 15, 2024
1 parent 583a3b8 commit ddf28b8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions hr_attendance_contract_missing_days/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_hr_attendance_contract_missing_days
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from odoo.addons.hr_attendance_missing_days.tests import test_attendance


class TestAttendance(test_attendance.TestAttendance):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.contract = cls.env["hr.contract"].create(
{
"name": "2023",
"date_start": "2023-01-01",
"date_end": "2023-12-31",
"state": "open",
"wage": 42,
"employee_id": cls.employee.id,
}
)

def _clone_employee(self, employee, defaults):
result = super()._clone_employee(employee, defaults)
for contract in employee.contract_ids:
contract.copy({"employee_id": result.id, "state": "open"})
return result
6 changes: 4 additions & 2 deletions hr_attendance_missing_days/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ def _create_missing_attendances(self, date_from=None, date_to=None):
("check_in", ">=", dt_from.replace(tzinfo=None)),
("check_in", "<=", dt_to.replace(tzinfo=None)),
]
attendance_records = self.attendance_ids.filtered_domain(domain)
attendances = {
ensure_tz(attendance.check_in, tz).date()
for attendance in self.attendance_ids.filtered_domain(domain)
ensure_tz(attendance_date, tz).date()
for attendance_date in attendance_records.mapped("check_in")
+ attendance_records.mapped("check_out")
}

vals = []
Expand Down
25 changes: 24 additions & 1 deletion hr_attendance_missing_days/tests/test_attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def test_attendance_creation(self):

attended = {date(2023, 7, 3 + offset) for offset in range(4)}
for tz in ["Europe/Amsterdam", "Pacific/Auckland", "America/New_York"]:
employee = self.employee.copy({"tz": tz, "name": f"Employee {tz}"})
employee = self._clone_employee(
self.employee, {"tz": tz, "name": f"Employee {tz}"}
)
for offset, times in enumerate(((0, 30), (23, 30), (11, 30), (12, 30))):
# Convert the times from the employee TZ zo UTC. 3rd is monday
start = convert_tz(
Expand Down Expand Up @@ -109,3 +111,24 @@ def test_attendance_creation_during_day(self):

attendances_new = attendances_after - attendances_before
self.assertFalse(attendances_new)

def test_multi_day_attendance(self):
"""Test that having an attendance crossing a day border doesn't break"""
self.employee.tz = "Europe/Amsterdam"
attendance = self.env["hr.attendance"].create(
{
"employee_id": self.employee.id,
"check_in": "2023-12-18 21:00:00",
"check_out": "2023-12-19 13:00:00",
}
)
self.employee._create_missing_attendances(
date(2023, 12, 18), date(2023, 12, 19)
)
self.assertEqual(
self.env["hr.attendance"].search([("employee_id", "=", self.employee.id)]),
attendance,
)

def _clone_employee(self, employee, defaults):
return employee.copy(defaults)

0 comments on commit ddf28b8

Please sign in to comment.