64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class EducationAttendanceLine(models.Model):
|
|
_name = 'education.attendance.line'
|
|
_description = 'Attendance Line Detail'
|
|
_order = 'id asc'
|
|
|
|
session_id = fields.Many2one('education.attendance.session', string='Session', required=True, ondelete='cascade', index=True)
|
|
student_id = fields.Many2one('education.student', string='Student', required=True, index=True)
|
|
status = fields.Selection([
|
|
('present', 'Present'),
|
|
('absent', 'Absent'),
|
|
('late', 'Late'),
|
|
('medical_leave', 'Medical Leave'),
|
|
('on_duty', 'On Duty')
|
|
], string='Status', default='present', required=True, index=True)
|
|
remarks = fields.Char(string='Remarks')
|
|
time_in = fields.Datetime(string='Time In')
|
|
time_out = fields.Datetime(string='Time Out')
|
|
active = fields.Boolean(default=True)
|
|
company_id = fields.Many2one(
|
|
'res.company',
|
|
related='session_id.company_id',
|
|
store=True,
|
|
index=True,
|
|
)
|
|
|
|
_uniq_session_student = models.Constraint(
|
|
'unique(session_id, student_id)',
|
|
'A student cannot be added twice to the same attendance session!'
|
|
)
|
|
|
|
def write(self, vals):
|
|
for rec in self:
|
|
if rec.session_id.state == 'completed':
|
|
if not self.env.user.has_group('education_attendance.group_attendance_manager'):
|
|
raise ValidationError("Only an Attendance Manager can modify attendance lines for a completed session.")
|
|
# Sync correction to permanent history
|
|
if 'status' in vals or 'remarks' in vals:
|
|
history = self.env['education.attendance'].search([
|
|
('session_id', '=', rec.session_id.id),
|
|
('student_id', '=', rec.student_id.id),
|
|
], limit=1)
|
|
if history:
|
|
hist_vals = {}
|
|
if 'status' in vals:
|
|
hist_vals['status'] = vals['status']
|
|
if 'remarks' in vals:
|
|
hist_vals['remarks'] = vals['remarks']
|
|
history.write(hist_vals)
|
|
res = super().write(vals)
|
|
if any(rec.session_id.state == 'completed' for rec in self):
|
|
self.mapped('student_id').invalidate_recordset()
|
|
return res
|
|
|
|
def unlink(self):
|
|
for rec in self:
|
|
if rec.session_id.state == 'completed' and not self.env.user.has_group('education_attendance.group_attendance_manager'):
|
|
raise ValidationError("Cannot delete attendance lines of a completed session. Revert the session to draft first.")
|
|
return super().unlink()
|