From c7dd4d8242aebfd20bf848f6820fe421f62dab66 Mon Sep 17 00:00:00 2001 From: "Thomas Lefebvre (thle)" Date: Thu, 2 Nov 2023 06:54:02 +0000 Subject: [PATCH] [FIX] hr_attendance: take account lunchtimes Steps to reproduce: ------------------- - add lunchtime in an employee's calendar; - in Attendance app, add attendance for this employee for the current month; (The hours calculated in the attendances take into account the lunchtime.) - go to the form view of this employee; Issue: ------ The statistics button, which displays the hours worked this month, does not take into account lunchtimes. Cause: ------ To find the hours worked, we calculate the difference between the `check_in` and the `check_out`. Solution: --------- Use the `worked_hours` field of attendance, which takes into account the employee's calendar and therefore lunchtimes. opw-3547521 closes odoo/odoo#141282 X-original-commit: 94bd34af19d14da5deafd8f34f93ff71e35c8782 Signed-off-by: Sofie Gvaladze (sgv) Signed-off-by: Thomas Lefebvre (thle) --- addons/hr_attendance/models/hr_employee.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addons/hr_attendance/models/hr_employee.py b/addons/hr_attendance/models/hr_employee.py index 38918bad8a2..064278e0b2c 100644 --- a/addons/hr_attendance/models/hr_employee.py +++ b/addons/hr_attendance/models/hr_employee.py @@ -97,14 +97,15 @@ class HrEmployee(models.Model): start_naive = start_tz.astimezone(pytz.utc).replace(tzinfo=None) end_tz = now_tz end_naive = end_tz.astimezone(pytz.utc).replace(tzinfo=None) - attendances = self.env['hr.attendance'].search([ - ('employee_id', '=', employee.id), - '&', - ('check_in', '>=', start_naive), - ('check_out', '<=', end_naive), - ]) - employee.hours_last_month = round(sum([att.worked_hours for att in attendances]), 2) + hours = sum( + att.worked_hours or 0 + for att in employee.attendance_ids.filtered( + lambda att: att.check_in >= start_naive and att.check_out <= end_naive + ) + ) + + employee.hours_last_month = round(hours, 2) employee.hours_last_month_display = "%g" % employee.hours_last_month def _compute_hours_today(self):