Skip to Content
Menu
This question has been flagged
2 Replies
895 Views

I want to calculate Late time in attendance

Late: If an employee comes after scheduled time then that count in late

how can I do it by studio /Odoo 17?

Avatar
Discard
Author Best Answer

I made a module with this code but the

scheduled_check_in  get null
from odoo import models, fields, api
from datetime import datetime

class HrAttendance(models.Model):
_inherit = 'hr.attendance'

# Make scheduled_check_in editable
scheduled_check_in = fields.Datetime(string="Scheduled Check-In", default=lambda self: self._default_scheduled_check_in())
late_minutes = fields.Integer(string="Late Minutes", compute="_compute_late_minutes", store=True)

def _default_scheduled_check_in(self):
"""
Automatically populate scheduled_check_in based on the employee's work schedule.
"""
if self.employee_id and self.employee_id.resource_calendar_id and self.check_in:
calendar = self.employee_id.resource_calendar_id
check_in_date = fields.Datetime.to_datetime(self.check_in).date()

# Get the work intervals for the check-in date
work_intervals = calendar._work_intervals_batch(
datetime.combine(check_in_date, datetime.min.time()),
datetime.combine(check_in_date, datetime.max.time()),
resources=self.employee_id.resource_id,
)

if work_intervals and self.employee_id.resource_id in work_intervals:
# Get the start time of the first work interval
return work_intervals[self.employee_id.resource_id][0][0]
return False

@api.depends('check_in', 'scheduled_check_in')
def _compute_late_minutes(self):
"""
Compute late_minutes as the difference between check_in and scheduled_check_in in whole minutes.
"""
for record in self:
if record.check_in and record.scheduled_check_in:
check_in_time = fields.Datetime.to_datetime(record.check_in)
scheduled_time = fields.Datetime.to_datetime(record.scheduled_check_in)

# Ensure both times are in the same timezone (if necessary)
check_in_time = check_in_time.replace(tzinfo=None)
scheduled_time = scheduled_time.replace(tzinfo=None)

if check_in_time > scheduled_time:
delta = check_in_time - scheduled_time
# Convert total seconds to minutes and round to the nearest integer
record.late_minutes = round(delta.total_seconds() / 60)
else:
record.late_minutes = 0
else:
record.late_minutes = 0

@api.model
def create(self, vals):
"""
Automatically populate scheduled_check_in when creating a new attendance record.
"""
if 'scheduled_check_in' not in vals and vals.get('employee_id') and vals.get('check_in'):
employee = self.env['hr.employee'].browse(vals['employee_id'])
if employee.resource_calendar_id:
calendar = employee.resource_calendar_id
check_in_date = fields.Datetime.to_datetime(vals['check_in']).date()

# Get the work intervals for the check-in date
work_intervals = calendar._work_intervals_batch(
datetime.combine(check_in_date, datetime.min.time()),
datetime.combine(check_in_date, datetime.max.time()),
resources=employee.resource_id,
)

if work_intervals and employee.resource_id in work_intervals:
# Get the start time of the first work interval
vals['scheduled_check_in'] = work_intervals[employee.resource_id][0][0]
return super(HrAttendance, self).create(vals)
Avatar
Discard
Best Answer

The attendance data is typically tracked in the hr.attendance model, and the scheduled working time is often linked to the resource.calendar or resource.calendar.attendance model.

  • hr.attendance: Tracks employee check-in and check-out times.
  • resource.calendar: Defines the working schedule for employees.

Steps to Configure in Studio

Step 1: Add a Field for "Late Time"
  1. Go to Studio.
  2. Open the Attendance (hr.attendance) model.
  3. Add a new computed field (e.g., Late Time) with the following configuration:
    • Field Name: x_late_time
    • Type: Float (or Integer for minutes)
    • Label: "Late Time (Minutes)"
    • Computation Logic: Use Python code.
Step 2: Write the Computation Logic

You can use the Odoo Studio computation editor or define the logic via Python. For Studio, you need to approximate it as follows:

if record.check_in: # Get the scheduled start time from the resource calendar scheduled_start = record.employee_id.resource_calendar_id.attendance_ids.filtered( lambda a: a.dayofweek == str(record.check_in.weekday()) ).mapped('hour_from') if scheduled_start: scheduled_start_time = record.check_in.replace( hour=int(scheduled_start[0]), minute=int((scheduled_start[0] % 1) * 60) ) if record.check_in > scheduled_start_time: late_minutes = (record.check_in - scheduled_start_time).total_seconds() / 60 result = late_minutes else: result = 0 else: result = 0

Step 3: Save the Changes

Once the computation is added, save and test the field.

Add a Filter for "Late Employees"
  1. In Studio, create a new filter for records where x_late_time > 0.
  2. This will allow you to easily see employees who were late.
Avatar
Discard
Related Posts Replies Views Activity
2
May 25
770
1
May 25
648
0
Mar 25
36
0
Mar 25
537
1
Feb 25
706