| 1 |
|
| 2 |
|
| 3 |
from dateutil.relativedelta import relativedelta |
| 4 |
|
| 5 |
from odoo import api, fields, models |
| 6 |
from odoo.tools.sql import SQL, drop_view_if_exists |
| 7 |
|
| 8 |
|
| 9 |
class HrLeaveAttendanceReport(models.Model): |
| 10 |
_name = "hr.leave.attendance.report" |
| 11 |
_description = "Attendance and Leave Analysis Report" |
| 12 |
_auto = False |
| 13 |
|
| 14 |
def _compute_display_name(self): |
| 15 |
for rec in self: |
| 16 |
rec.display_name = f"{rec.employee_id.display_name}, {rec.date}" |
| 17 |
|
| 18 |
date = fields.Date("Date") |
| 19 |
employee_id = fields.Many2one("hr.employee", string="Employee") |
| 20 |
active = fields.Boolean(related="employee_id.active") |
| 21 |
department_id = fields.Many2one(related="employee_id.department_id", string="Department") |
| 22 |
job_id = fields.Many2one(related="employee_id.job_id", string="Job Position") |
| 23 |
schedule_id = fields.Many2one("resource.calendar", string="Working Schedule") |
| 24 |
expected_hours = fields.Float("Expected Hours") |
| 25 |
worked_hours = fields.Float("Worked Hours") |
| 26 |
leave_hours = fields.Float("Approved Time Off") |
| 27 |
difference_hours = fields.Float("Difference", help="Worked Hours - Expected Hours + Approved Time Off") |
| 28 |
|
| 29 |
leave_type_names = fields.Char("Time Off Types", compute="_compute_leave_attendance_fields") |
| 30 |
leave_ids = fields.Many2many("hr.leave", string="Time Offs", compute="_compute_leave_attendance_fields") |
| 31 |
attendance_ids = fields.Many2many("hr.attendance", string="Attendances", compute="_compute_leave_attendance_fields") |
| 32 |
|
| 33 |
@api.depends('employee_id', 'date') |
| 34 |
def _compute_leave_attendance_fields(self): |
| 35 |
today = fields.Date.today() |
| 36 |
min_date = today - relativedelta(years=1) |
| 37 |
max_date = today - relativedelta(days=1) |
| 38 |
|
| 39 |
leaves_by_employees = dict(self.env['hr.leave']._read_group( |
| 40 |
domain=[ |
| 41 |
('employee_id', 'in', self.employee_id.ids), |
| 42 |
('state', '=', 'validate'), |
| 43 |
('date_from', '<=', max_date), |
| 44 |
('date_to', '>=', min_date), |
| 45 |
], |
| 46 |
groupby=['employee_id'], |
| 47 |
aggregates=['id:recordset'], |
| 48 |
)) |
| 49 |
attendances_by_employees = dict(self.env['hr.attendance']._read_group( |
| 50 |
domain=[ |
| 51 |
('employee_id', 'in', self.employee_id.ids), |
| 52 |
('check_in', '>=', min_date), |
| 53 |
('check_in', '<=', max_date), |
| 54 |
], |
| 55 |
groupby=['employee_id'], |
| 56 |
aggregates=['id:recordset'], |
| 57 |
)) |
| 58 |
|
| 59 |
for rec in self: |
| 60 |
leaves = leaves_by_employees.get(rec.employee_id, self.env['hr.leave']) |
| 61 |
rec_date_leaves = leaves.filtered( |
| 62 |
lambda lv: self._timestamped(lv.date_from) <= rec.date <= self._timestamped(lv.date_to), |
| 63 |
) |
| 64 |
rec.leave_ids = rec_date_leaves.ids |
| 65 |
leave_type_ids = rec_date_leaves.mapped('holiday_status_id') |
| 66 |
rec.leave_type_names = ', '.join(leave_type_ids.mapped('name')) |
| 67 |
|
| 68 |
attendances = attendances_by_employees.get(rec.employee_id, self.env['hr.attendance']) |
| 69 |
rec.attendance_ids = attendances.filtered( |
| 70 |
lambda att: self._timestamped(att.check_in) == rec.date, |
| 71 |
).ids |
| 72 |
|
| 73 |
def _timestamped(self, date): |
| 74 |
return fields.Datetime.context_timestamp(self, date).date() |
| 75 |
|
| 76 |
def _select(self): |
| 77 |
return """ |
| 78 |
SELECT row_number() OVER (ORDER BY gs.day DESC, emp.id) AS id, |
| 79 |
gs.day::date AS date, |
| 80 |
emp.id AS employee_id, |
| 81 |
rc.id AS schedule_id, |
| 82 |
ROUND(COALESCE(att.worked_hours, 0.0)::numeric, 2) AS worked_hours, |
| 83 |
ROUND(COALESCE(rc.hours_per_day, 0.0)::numeric, 2) AS expected_hours, |
| 84 |
ROUND(COALESCE(dlh.leave_hours, 0.0)::numeric, 2) AS leave_hours, |
| 85 |
( |
| 86 |
ROUND(COALESCE(att.worked_hours, 0.0)::numeric, 2) |
| 87 |
- ROUND(COALESCE(rc.hours_per_day, 0.0)::numeric, 2) |
| 88 |
+ ROUND(COALESCE(dlh.leave_hours, 0.0)::numeric, 2) |
| 89 |
) AS difference_hours |
| 90 |
""" |
| 91 |
|
| 92 |
def _from(self): |
| 93 |
return """ |
| 94 |
FROM hr_employee AS emp |
| 95 |
CROSS JOIN LATERAL generate_series( |
| 96 |
(date_trunc('month', CURRENT_DATE) - INTERVAL '1 year')::date, |
| 97 |
(CURRENT_DATE - 1)::date, |
| 98 |
INTERVAL '1 day' |
| 99 |
) AS gs(day) |
| 100 |
LEFT JOIN LATERAL ( |
| 101 |
SELECT resource_calendar_id |
| 102 |
FROM hr_version AS v |
| 103 |
WHERE v.employee_id = emp.id |
| 104 |
AND v.contract_date_start IS NOT NULL |
| 105 |
AND v.contract_date_start <= gs.day |
| 106 |
AND (v.contract_date_end >= gs.day OR v.contract_date_end IS NULL) |
| 107 |
AND v.date_version <= gs.day |
| 108 |
ORDER BY v.date_version DESC |
| 109 |
LIMIT 1 |
| 110 |
) AS ver |
| 111 |
ON TRUE |
| 112 |
""" |
| 113 |
|
| 114 |
def _join_attendance(self): |
| 115 |
return """ |
| 116 |
LEFT JOIN ( |
| 117 |
SELECT employee_id, |
| 118 |
(check_in AT TIME ZONE 'UTC')::date AS check_date, |
| 119 |
SUM(worked_hours) AS worked_hours |
| 120 |
FROM hr_attendance |
| 121 |
GROUP BY employee_id, check_date |
| 122 |
) AS att |
| 123 |
ON att.employee_id = emp.id |
| 124 |
AND att.check_date = gs.day |
| 125 |
""" |
| 126 |
|
| 127 |
def _join_calendar(self): |
| 128 |
return """ |
| 129 |
JOIN resource_calendar AS rc |
| 130 |
ON ver.resource_calendar_id = rc.id |
| 131 |
""" |
| 132 |
|
| 133 |
def _join_calendar_leaves(self): |
| 134 |
return """ |
| 135 |
LEFT JOIN resource_calendar_leaves AS rcl |
| 136 |
ON ( |
| 137 |
(rc.id = rcl.calendar_id OR rcl.calendar_id IS NULL) |
| 138 |
AND rcl.resource_id IS NULL |
| 139 |
AND rcl.company_id = emp.company_id |
| 140 |
AND gs.day |
| 141 |
BETWEEN (rcl.date_from AT TIME ZONE 'UTC')::date |
| 142 |
AND (rcl.date_to AT TIME ZONE 'UTC')::date |
| 143 |
) |
| 144 |
""" |
| 145 |
|
| 146 |
def _join_resource_calendar_attendance(self): |
| 147 |
return """ |
| 148 |
LEFT JOIN ( |
| 149 |
SELECT DISTINCT ON (calendar_id, dayofweek) * |
| 150 |
FROM resource_calendar_attendance |
| 151 |
ORDER BY calendar_id, dayofweek |
| 152 |
) AS rca |
| 153 |
ON rc.id = rca.calendar_id |
| 154 |
AND CAST(rca.dayofweek AS INTEGER) = ( |
| 155 |
CASE |
| 156 |
WHEN EXTRACT(DOW FROM gs.day) = 0 THEN 6 |
| 157 |
ELSE EXTRACT(DOW FROM gs.day) - 1 |
| 158 |
END |
| 159 |
) -- to map days between Odoo (Monday = 0, Tuesday = 1, ...) and Postgres (Sunday = 0, Monday = 1, ...) |
| 160 |
""" |
| 161 |
|
| 162 |
def _join_daily_leave_hours(self): |
| 163 |
""" Generates a SQL join clause to calculate the total `leave_hours` taken for a specific day: |
| 164 |
- Sums up `number_of_hours` for all validated time offs within the day. |
| 165 |
- Excludes non-working days based on the employee's resource calendar and calendar leaves. |
| 166 |
- Handles leaves spanning multiple days, ensuring only working days are counted. |
| 167 |
- Converts all date fields to UTC to maintain consistency with Odoo's date storage. |
| 168 |
""" |
| 169 |
return """ |
| 170 |
LEFT JOIN LATERAL ( |
| 171 |
SELECT SUM(lv.number_of_hours / NULLIF(wd.working_days,0)) AS leave_hours |
| 172 |
FROM hr_leave AS lv |
| 173 |
JOIN LATERAL ( |
| 174 |
SELECT COUNT(*) AS working_days |
| 175 |
FROM generate_series( |
| 176 |
(lv.date_from AT TIME ZONE 'UTC')::date, |
| 177 |
(lv.date_to AT TIME ZONE 'UTC')::date, |
| 178 |
INTERVAL '1 day' |
| 179 |
) |
| 180 |
AS d(day) |
| 181 |
INNER JOIN ( |
| 182 |
SELECT DISTINCT ON (calendar_id, dayofweek) * |
| 183 |
FROM resource_calendar_attendance |
| 184 |
ORDER BY calendar_id, dayofweek |
| 185 |
) AS rca2 |
| 186 |
ON rc.id = rca2.calendar_id |
| 187 |
AND CAST(rca2.dayofweek AS INTEGER) = ( |
| 188 |
CASE |
| 189 |
WHEN EXTRACT(DOW FROM d.day) = 0 THEN 6 |
| 190 |
ELSE EXTRACT(DOW FROM d.day) - 1 |
| 191 |
END |
| 192 |
) |
| 193 |
LEFT JOIN resource_calendar_leaves rcl2 |
| 194 |
ON ( |
| 195 |
(rc.id = rcl2.calendar_id OR rcl2.calendar_id IS NULL) |
| 196 |
AND rcl2.resource_id IS NULL |
| 197 |
AND rcl2.company_id = emp.company_id |
| 198 |
AND d.day |
| 199 |
BETWEEN (rcl2.date_from AT TIME ZONE 'UTC')::date |
| 200 |
AND (rcl2.date_to AT TIME ZONE 'UTC')::date |
| 201 |
) |
| 202 |
WHERE rcl2.id IS NULL |
| 203 |
) AS wd ON TRUE |
| 204 |
WHERE lv.employee_id = emp.id |
| 205 |
AND gs.day |
| 206 |
BETWEEN (lv.date_from AT TIME ZONE 'UTC')::date |
| 207 |
AND (lv.date_to AT TIME ZONE 'UTC')::date |
| 208 |
AND lv.state = 'validate' |
| 209 |
) AS dlh |
| 210 |
ON TRUE |
| 211 |
""" |
| 212 |
|
| 213 |
def _where(self): |
| 214 |
return """ |
| 215 |
WHERE rca.id IS NOT NULL |
| 216 |
AND rcl.id IS NULL |
| 217 |
""" |
| 218 |
|
| 219 |
def init(self): |
| 220 |
drop_view_if_exists(self.env.cr, self._table) |
| 221 |
self.env.cr.execute(SQL(""" |
| 222 |
CREATE OR REPLACE VIEW %s AS ( |
| 223 |
%s -- select |
| 224 |
%s -- from |
| 225 |
%s -- join_attendance |
| 226 |
%s -- join_calendar |
| 227 |
%s -- join_calendar_leaves |
| 228 |
%s -- join_resource_calendar_attendance |
| 229 |
%s -- join_daily_leave_hours |
| 230 |
%s -- where |
| 231 |
)""", |
| 232 |
SQL.identifier(self._table), |
| 233 |
SQL(self._select()), |
| 234 |
SQL(self._from()), |
| 235 |
SQL(self._join_attendance()), |
| 236 |
SQL(self._join_calendar()), |
| 237 |
SQL(self._join_calendar_leaves()), |
| 238 |
SQL(self._join_resource_calendar_attendance()), |
| 239 |
SQL(self._join_daily_leave_hours()), |
| 240 |
SQL(self._where()), |
| 241 |
)) |