I would like to implement a Scheduled action to assign unpaid leave in the attendance module where no check in and check out is marked on days employees are meant to work. Does anyone know the python code pretty please? I know how to created the Schedule Action, I just need the python code. I'm using Odoo Online v17
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- Müşteri İlişkileri Yönetimi
- e-Commerce
- Muhasebe
- Envanter
- PoS
- Project
- MRP
Bu soru işaretlendi
Hi,
Try the following Python code in the scheduled action.
# Set the date to check (yesterday)
yesterday = date.today() - timedelta(days=1)
# Get employees with work schedule defined
employees = env['hr.employee'].search([('resource_calendar_id', '!=', False)])
# Get unpaid leave type (you may want to filter by name or type)
unpaid_leave_type = env['hr.leave.type'].search([('name', 'ilike', 'Unpaid'), ('allocation_type', '=', 'no')], limit=1)
for emp in employees:
# Check if employee is supposed to work on that day
is_working_day = emp.resource_calendar_id.attendance_ids.filtered(
lambda att: int(att.dayofweek) == yesterday.weekday()
)
if not is_working_day:
continue # Skip if not a working day
# Check if there's an attendance
attendance = env['hr.attendance'].search([
('employee_id', '=', emp.id),
('check_in', '>=', fields.Datetime.to_datetime(yesterday)),
('check_in', '<', fields.Datetime.to_datetime(yesterday + timedelta(days=1)))
], limit=1)
# Check if already on leave
leave = env['hr.leave'].search([
('employee_id', '=', emp.id),
('state', 'not in', ['cancel', 'refuse']),
('request_date_from', '<=', yesterday),
('request_date_to', '>=', yesterday)
], limit=1)
if not attendance and not leave and unpaid_leave_type:
# Create unpaid leave
env['hr.leave'].create({
'name': 'Auto Unpaid Leave',
'holiday_status_id': unpaid_leave_type.id,
'employee_id': emp.id,
'request_date_from': yesterday,
'request_date_to': yesterday,
'date_from': fields.Datetime.to_datetime(yesterday),
'date_to': fields.Datetime.to_datetime(yesterday + timedelta(hours=8)), # Assuming 1 work day
'state': 'confirm', # You may want to auto-validate ('validate')
})
Hope it helps
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Üye Olİlgili Gönderiler | Cevaplar | Görünümler | Aktivite | |
---|---|---|---|---|
|
2
Ağu 25
|
1491 | ||
|
3
Tem 25
|
2214 | ||
|
1
Tem 25
|
1746 | ||
|
0
Oca 25
|
1362 | ||
|
1
Ara 24
|
1727 |