odoo18/addons_extensions/grace_period/models/resource_calendar_period.py

112 lines
3.0 KiB
Python

from odoo import api, fields, models, tools
class ResourceCalendar(models.Model):
_inherit = 'resource.calendar'
weekday_ids = fields.Many2many(
'resource.calendar.weekday',
'resource_calendar_weekday_rel',
'calendar_id',
'weekday_id',
string="Weekdays"
)
weekend_ids = fields.Many2many(
'resource.calendar.weekday',
'resource_calendar_weekend_rel',
'calendar_id',
'weekday_id',
string="Weekends"
)
shift_start_time = fields.Float(string="Start Time")
shift_end_time = fields.Float(string="End Time")
late_grace_period = fields.Integer(default=15)
early_out_grace_period = fields.Integer(default=0)
department_grace_ids = fields.One2many(
'resource.calendar.department.grace',
'calendar_id',
string="Department Grace Periods"
)
def action_generate_schedule(self):
for rec in self:
attendance_commands = [(5, 0, 0)]
added_days = []
for day in rec.weekday_ids:
# Avoid duplicate weekdays
if day.code in added_days:
continue
added_days.append(day.code)
attendance_commands.append((0, 0, {
'name': day.name,
'dayofweek': day.code,
'hour_from': rec.shift_start_time,
'hour_to': rec.shift_end_time,
}))
# Single write operation
rec.write({
'attendance_ids': attendance_commands
})
# ------------------------------------
# Department Grace Period Generation
# ------------------------------------
department_commands = [(5, 0, 0)]
departments = self.env['hr.department'].search([])
for dept in departments:
department_commands.append((0, 0, {
'department_id': dept.id,
'grace_period': 0,
}))
rec.write({
'department_grace_ids': department_commands
})
class ResourceCalendarWeekday(models.Model):
_name = 'resource.calendar.weekday'
_description = 'Weekday Master'
name = fields.Char(required=True)
code = fields.Selection([
('0', 'Monday'),
('1', 'Tuesday'),
('2', 'Wednesday'),
('3', 'Thursday'),
('4', 'Friday'),
('5', 'Saturday'),
('6', 'Sunday'),
], required=True)
class ResourceCalendarDepartmentGrace(models.Model):
_name = 'resource.calendar.department.grace'
_description = 'Department Wise Grace Period'
calendar_id = fields.Many2one(
'resource.calendar',
string="Working Schedule",
ondelete='cascade'
)
department_id = fields.Many2one(
'hr.department',
string="Department",
required=True
)
grace_period = fields.Integer(
string="Grace Period (Minutes)",
required=True
)