From 4e61db1cefe866dd80c7c87475aad58b63469b0f Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 2 Oct 2023 17:41:55 +0200 Subject: [PATCH] [ADD] #34 expected hours to attendance report --- verdigado_attendance/models/__init__.py | 1 + .../models/hr_attendance_overtime.py | 30 +++++++++++++++++++ .../models/hr_attendance_report.py | 11 +++++-- .../views/hr_attendance_report.xml | 9 ++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 verdigado_attendance/models/hr_attendance_overtime.py diff --git a/verdigado_attendance/models/__init__.py b/verdigado_attendance/models/__init__.py index 46d8b1a..05b9076 100644 --- a/verdigado_attendance/models/__init__.py +++ b/verdigado_attendance/models/__init__.py @@ -2,5 +2,6 @@ from . import hr_attendance from . import hr_attendance_break +from . import hr_attendance_overtime from . import hr_attendance_report from . import hr_leave_type diff --git a/verdigado_attendance/models/hr_attendance_overtime.py b/verdigado_attendance/models/hr_attendance_overtime.py new file mode 100644 index 0000000..60d5a84 --- /dev/null +++ b/verdigado_attendance/models/hr_attendance_overtime.py @@ -0,0 +1,30 @@ +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from datetime import datetime, time + +import pytz + +from odoo import fields, models + + +class HrAttendanceOvertime(models.Model): + _inherit = "hr.attendance.overtime" + + expected_hours = fields.Float(compute="_compute_expected_hours", store=True) + + def _compute_expected_hours(self): + for this in self: + employee = this.employee_id + tz = pytz.timezone(employee.tz) + this.expected_hours = ( + this.adjustment + and False + or employee._get_work_days_data_batch( + tz.localize(datetime.combine(this.date, time.min)).astimezone( + pytz.utc + ), + tz.localize(datetime.combine(this.date, time.max)).astimezone( + pytz.utc + ), + )[employee.id]["hours"] + ) diff --git a/verdigado_attendance/models/hr_attendance_report.py b/verdigado_attendance/models/hr_attendance_report.py index 3363ede..aaabf32 100644 --- a/verdigado_attendance/models/hr_attendance_report.py +++ b/verdigado_attendance/models/hr_attendance_report.py @@ -2,19 +2,26 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) -from odoo import models +from odoo import fields, models class HrAttendanceReport(models.Model): _inherit = "hr.attendance.report" + expected_hours = fields.Float() + + def _select(self): + """Add expected hours""" + return super()._select() + ", coalesce(ot.expected_hours, 0) expected_hours" + def _join(self): """Add overtime adjustments""" return super()._join() + " UNION %s %s %s" % ( self._select() .replace("hra.worked_hours", "0") .replace("break_hours", "0") - .replace("ot.duration", "0"), + .replace("ot.duration", "0") + .replace("coalesce(ot.expected_hours, 0)", "0"), self._from(), super()._join().replace("ot.adjustment = FALSE", "ot.adjustment = TRUE"), ) diff --git a/verdigado_attendance/views/hr_attendance_report.xml b/verdigado_attendance/views/hr_attendance_report.xml index 47682c7..84644f7 100644 --- a/verdigado_attendance/views/hr_attendance_report.xml +++ b/verdigado_attendance/views/hr_attendance_report.xml @@ -2,6 +2,15 @@ + + hr.attendance.report + + + + + + +