34 lines
1.8 KiB
Python
34 lines
1.8 KiB
Python
# pyrefly: ignore [missing-import]
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class EducationStudent(models.Model):
|
|
_inherit = 'education.student'
|
|
|
|
attendance_line_ids = fields.One2many('education.attendance', 'student_id', string='Attendance History')
|
|
attendance_pct = fields.Float(string='Attendance %', compute='_compute_attendance_metrics', store=True, index=True)
|
|
present_count = fields.Integer(string='Present Count', compute='_compute_attendance_metrics', store=True)
|
|
absent_count = fields.Integer(string='Absent Count', compute='_compute_attendance_metrics', store=True)
|
|
late_count = fields.Integer(string='Late Count', compute='_compute_attendance_metrics', store=True)
|
|
medical_leave_count = fields.Integer(string='Medical Leave Count', compute='_compute_attendance_metrics', store=True)
|
|
|
|
@api.depends('attendance_line_ids.status', 'attendance_line_ids.active')
|
|
def _compute_attendance_metrics(self):
|
|
for rec in self:
|
|
records = rec.attendance_line_ids.filtered(lambda a: a.active)
|
|
total = len(records)
|
|
|
|
p_count = len(records.filtered(lambda r: r.status in ('present', 'on_duty')))
|
|
l_count = len(records.filtered(lambda r: r.status == 'late'))
|
|
m_count = len(records.filtered(lambda r: r.status == 'medical_leave'))
|
|
a_count = len(records.filtered(lambda r: r.status == 'absent'))
|
|
|
|
rec.present_count = p_count
|
|
rec.absent_count = a_count
|
|
rec.late_count = l_count
|
|
rec.medical_leave_count = m_count
|
|
|
|
# Treat present, late, medical_leave, and on_duty as attended (positive)
|
|
attended = p_count + l_count + m_count
|
|
rec.attendance_pct = (attended / total * 100.0) if total > 0 else 100.0
|