From a2759049b4521c2c8d6378b65869f31292f2db96 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 4 Sep 2023 17:46:34 +0200 Subject: [PATCH] fixup! [ADD] hr_attendance_break --- hr_attendance_break/models/hr_attendance.py | 29 +------------------ hr_attendance_break/models/hr_employee.py | 2 +- .../tests/test_hr_attendance_break.py | 13 +++++++++ 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/hr_attendance_break/models/hr_attendance.py b/hr_attendance_break/models/hr_attendance.py index b0d75712..a86f6029 100644 --- a/hr_attendance_break/models/hr_attendance.py +++ b/hr_attendance_break/models/hr_attendance.py @@ -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): @@ -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) diff --git a/hr_attendance_break/models/hr_employee.py b/hr_attendance_break/models/hr_employee.py index ac67806a..3124ed83 100644 --- a/hr_attendance_break/models/hr_employee.py +++ b/hr_attendance_break/models/hr_employee.py @@ -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), ], diff --git a/hr_attendance_break/tests/test_hr_attendance_break.py b/hr_attendance_break/tests/test_hr_attendance_break.py index 26b301cd..2728393b 100644 --- a/hr_attendance_break/tests/test_hr_attendance_break.py +++ b/hr_attendance_break/tests/test_hr_attendance_break.py @@ -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")