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?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
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?
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)
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.
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
Once the computation is added, save and test the field.
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
2
May 25
|
770 | ||
|
1
May 25
|
648 | ||
|
0
Mar 25
|
36 | ||
|
0
Mar 25
|
537 | ||
|
1
Feb 25
|
706 |