| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models, tools |
| 5 |
|
| 6 |
|
| 7 |
class HrTimesheetAttendanceReport(models.Model): |
| 8 |
_name = 'hr.timesheet.attendance.report' |
| 9 |
_auto = False |
| 10 |
_description = 'Timesheet Attendance Report' |
| 11 |
|
| 12 |
employee_id = fields.Many2one('hr.employee', readonly=True) |
| 13 |
date = fields.Date(readonly=True) |
| 14 |
total_timesheet = fields.Float("Timesheets Time", readonly=True) |
| 15 |
total_attendance = fields.Float("Attendance Time", readonly=True) |
| 16 |
total_difference = fields.Float("Time Difference", readonly=True) |
| 17 |
timesheets_cost = fields.Float("Timesheet Cost", readonly=True) |
| 18 |
attendance_cost = fields.Float("Attendance Cost", readonly=True) |
| 19 |
cost_difference = fields.Float("Cost Difference", readonly=True) |
| 20 |
company_id = fields.Many2one('res.company', string='Company', readonly=True) |
| 21 |
|
| 22 |
def init(self): |
| 23 |
tools.drop_view_if_exists(self.env.cr, self._table) |
| 24 |
self.env.cr.execute("""CREATE OR REPLACE VIEW %s AS ( |
| 25 |
SELECT |
| 26 |
max(id) AS id, |
| 27 |
t.employee_id, |
| 28 |
t.date, |
| 29 |
t.company_id, |
| 30 |
coalesce(sum(t.attendance), 0) AS total_attendance, |
| 31 |
coalesce(sum(t.timesheet), 0) AS total_timesheet, |
| 32 |
coalesce(sum(t.attendance), 0) - coalesce(sum(t.timesheet), 0) as total_difference, |
| 33 |
NULLIF(sum(t.timesheet) * t.emp_cost, 0) as timesheets_cost, |
| 34 |
NULLIF(sum(t.attendance) * t.emp_cost, 0) as attendance_cost, |
| 35 |
NULLIF((coalesce(sum(t.attendance), 0) - coalesce(sum(t.timesheet), 0)) * t.emp_cost, 0) as cost_difference |
| 36 |
FROM ( |
| 37 |
SELECT |
| 38 |
-hr_attendance.id AS id, |
| 39 |
hr_employee.hourly_cost AS emp_cost, |
| 40 |
hr_attendance.employee_id AS employee_id, |
| 41 |
hr_attendance.worked_hours AS attendance, |
| 42 |
NULL AS timesheet, |
| 43 |
CAST(hr_attendance.check_in |
| 44 |
at time zone 'utc' |
| 45 |
at time zone |
| 46 |
(SELECT calendar.tz FROM resource_calendar as calendar |
| 47 |
INNER JOIN hr_employee as employee ON employee.id = hr_attendance.employee_id |
| 48 |
LEFT JOIN hr_version v ON v.id = employee.current_version_id |
| 49 |
WHERE calendar.id = v.resource_calendar_id) |
| 50 |
as DATE) as date, |
| 51 |
hr_employee.company_id as company_id |
| 52 |
FROM hr_attendance |
| 53 |
LEFT JOIN hr_employee ON hr_employee.id = hr_attendance.employee_id |
| 54 |
WHERE check_in::date <= CURRENT_DATE |
| 55 |
UNION ALL |
| 56 |
SELECT |
| 57 |
ts.id AS id, |
| 58 |
hr_employee.hourly_cost AS emp_cost, |
| 59 |
ts.employee_id AS employee_id, |
| 60 |
NULL AS attendance, |
| 61 |
ts.unit_amount AS timesheet, |
| 62 |
ts.date AS date, |
| 63 |
ts.company_id AS company_id |
| 64 |
FROM account_analytic_line AS ts |
| 65 |
LEFT JOIN hr_employee ON hr_employee.id = ts.employee_id |
| 66 |
WHERE ts.project_id IS NOT NULL |
| 67 |
AND date <= CURRENT_DATE |
| 68 |
) AS t |
| 69 |
GROUP BY t.employee_id, t.date, t.company_id, t.emp_cost |
| 70 |
ORDER BY t.date |
| 71 |
) |
| 72 |
""" % self._table) |
| 73 |
|
| 74 |
@api.model |
| 75 |
def formatted_read_group(self, domain, groupby=(), aggregates=(), having=(), offset=0, limit=None, order=None) -> list[dict]: |
| 76 |
if not order and groupby: |
| 77 |
order = ', '.join(f"{spec} DESC" if spec.startswith('date:') else spec for spec in groupby) |
| 78 |
return super().formatted_read_group(domain, groupby, aggregates, having=having, offset=offset, limit=limit, order=order) |