[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) <sgv@odoo.com>
Signed-off-by: Thomas Lefebvre (thle) <thle@odoo.com>
This commit is contained in:
Thomas Lefebvre (thle)
2023-11-07 21:02:22 +00:00
parent 546f77d1ca
commit c7dd4d8242
+8 -7
View File
@@ -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):