Skip to content

Commit

Permalink
fixup! [ADD] hr_attendance_break
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Sep 4, 2023
1 parent 3572b25 commit a275904
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
29 changes: 1 addition & 28 deletions hr_attendance_break/models/hr_attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)


import datetime

from odoo import _, api, exceptions, fields, models
from odoo import _, api, fields, models


class HrAttendance(models.Model):
Expand Down Expand Up @@ -51,28 +49,3 @@ def button_show_breaks(self):
"default_end": this.check_in,
},
}

def hr_attendance_break(self, break_hours):
"""
Split attendance into two so that there's break_hours time between the first
check out and the second check in.
Return the newly created attendance, which is at the start of the original
attendance.
"""
self.ensure_one()
delta = self.check_out - self.check_in
if delta.total_seconds() / 3600 < break_hours:
raise exceptions.UserError(
_("This attendance is too short to fit in a break of %d.2h!")
% break_hours
)
break_start = (
self.check_in + delta / 2 - datetime.timedelta(hours=break_hours) / 2
).replace(minute=0, second=0, microsecond=0)
new_attendance_data = self.copy_data(
default={
"check_out": break_start,
}
)
self.check_in = break_start + datetime.timedelta(hours=break_hours)
return self.create(new_attendance_data)
2 changes: 1 addition & 1 deletion hr_attendance_break/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_current_break(self):
HrAttendanceBreak = self.env["hr.attendance.break"].sudo()
return HrAttendanceBreak.search(
[
("attendance_id.employee_id", "in", self.ids),
("attendance_id", "in", self.mapped("last_attendance_id").ids),
("begin", "<=", fields.Datetime.now()),
("end", "=", False),
],
Expand Down
13 changes: 13 additions & 0 deletions hr_attendance_break/tests/test_hr_attendance_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ def test_break_flagging(self):
self.employee._check_mandatory_break(datetime.datetime(2023, 8, 21))
flag_activity = self.employee.activity_ids - original_activities
self.assertTrue(flag_activity)

def test_manual_break(self):
"""Test break taking works as expected"""
self.employee.invalidate_cache(["hr_presence_state", "break_state"])
attendance = self.employee._attendance_action_change()
self.assertFalse(attendance.break_ids)
self.assertEqual(self.employee.hr_presence_state, "present")
self.employee.attendance_manual_break(None)
self.assertTrue(attendance.break_ids)
self.assertEqual(self.employee.hr_presence_state, "absent")
self.employee.attendance_manual_break(None)
self.assertTrue(attendance.break_ids.end)
self.assertEqual(self.employee.hr_presence_state, "present")

0 comments on commit a275904

Please sign in to comment.