From 77c6b0146f962c449930ebb41174b2933a349e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Thu, 4 Aug 2022 14:34:12 +0000 Subject: [PATCH] [IMP] hr_attendance: Separate the attendance report in functions to allow inheritance. TT36461 closes odoo/odoo#98037 X-original-commit: 9468c62a470455043c1fc45edf18df614c2d2d18 Signed-off-by: Kevin Baptiste --- .../report/hr_attendance_report.py | 75 +++++++++++-------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/addons/hr_attendance/report/hr_attendance_report.py b/addons/hr_attendance/report/hr_attendance_report.py index f8d885e97cc..227d57defe9 100644 --- a/addons/hr_attendance/report/hr_attendance_report.py +++ b/addons/hr_attendance/report/hr_attendance_report.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models, tools +from odoo import api, fields, models, tools class HRAttendanceReport(models.Model): @@ -16,39 +16,52 @@ class HRAttendanceReport(models.Model): worked_hours = fields.Float("Hours Worked", readonly=True) overtime_hours = fields.Float("Extra Hours", readonly=True) + @api.model + def _select(self): + return """ + SELECT + hra.id, + hr_employee.department_id, + hra.employee_id, + hr_employee.company_id, + hra.check_in, + hra.worked_hours, + coalesce(ot.duration, 0) as overtime_hours + """ + + @api.model + def _from(self): + return """ + FROM ( + SELECT + id, + row_number() over (partition by employee_id, CAST(check_in AS DATE)) as ot_check, + employee_id, + CAST(check_in as DATE) as check_in, + worked_hours + FROM + hr_attendance + ) as hra + """ + + def _join(self): + return """ + LEFT JOIN hr_employee ON hr_employee.id = hra.employee_id + LEFT JOIN hr_attendance_overtime ot + ON hra.ot_check = 1 + AND ot.employee_id = hra.employee_id + AND ot.date = hra.check_in + AND ot.adjustment = FALSE + """ + def init(self): tools.drop_view_if_exists(self.env.cr, self._table) self.env.cr.execute(""" CREATE OR REPLACE VIEW %s AS ( - ( - SELECT - hra.id, - hr_employee.department_id, - hra.employee_id, - hr_employee.company_id, - hra.check_in, - hra.worked_hours, - coalesce(ot.duration, 0) as overtime_hours - FROM ( - SELECT - id, - row_number() over (partition by employee_id, CAST(check_in AS DATE)) as ot_check, - employee_id, - CAST(check_in as DATE) as check_in, - worked_hours - FROM - hr_attendance - ) as hra - LEFT JOIN - hr_employee - ON hr_employee.id = hra.employee_id - LEFT JOIN - hr_attendance_overtime ot - ON hra.ot_check = 1 - AND ot.employee_id = hra.employee_id - AND ot.date = hra.check_in - AND ot.adjustment = FALSE - ) + %s + %s + %s ) - """ % (self._table)) + """ % (self._table, self._select(), self._from(), self._join()) + )