93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
from odoo import api, fields, models
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class DanceAttendance(models.Model):
|
|
_name = "dance.attendance"
|
|
_description = "Dance Attendance"
|
|
_order = "session_id desc, student_id"
|
|
|
|
student_id = fields.Many2one(
|
|
"res.partner",
|
|
required=True,
|
|
domain="[('is_student', '=', True)]",
|
|
ondelete="restrict",
|
|
)
|
|
session_id = fields.Many2one(
|
|
"dance.session",
|
|
required=True,
|
|
ondelete="cascade",
|
|
)
|
|
batch_id = fields.Many2one(
|
|
related="session_id.batch_id",
|
|
store=True,
|
|
readonly=True,
|
|
)
|
|
date = fields.Date(related="session_id.date", store=True, readonly=True)
|
|
status = fields.Selection(
|
|
[
|
|
("present", "Present"),
|
|
("absent", "Absent"),
|
|
("late", "Late"),
|
|
],
|
|
default="present",
|
|
required=True,
|
|
)
|
|
attendance_percent = fields.Float(
|
|
string="Attendance %",
|
|
compute="_compute_report_measures",
|
|
store=True,
|
|
group_operator="avg",
|
|
)
|
|
present_count = fields.Integer(
|
|
string="Present/Late",
|
|
compute="_compute_report_measures",
|
|
store=True,
|
|
)
|
|
absent_count = fields.Integer(
|
|
string="Absentees",
|
|
compute="_compute_report_measures",
|
|
store=True,
|
|
)
|
|
total_count = fields.Integer(
|
|
string="Sessions",
|
|
compute="_compute_report_measures",
|
|
store=True,
|
|
)
|
|
remarks = fields.Text()
|
|
|
|
_sql_constraints = [
|
|
(
|
|
"unique_attendance",
|
|
"unique(student_id, session_id)",
|
|
"Attendance is already marked for this student in this session.",
|
|
),
|
|
]
|
|
|
|
@api.depends("status")
|
|
def _compute_report_measures(self):
|
|
for attendance in self:
|
|
is_absent = attendance.status == "absent"
|
|
attendance.attendance_percent = 0.0 if is_absent else 100.0
|
|
attendance.present_count = 0 if is_absent else 1
|
|
attendance.absent_count = 1 if is_absent else 0
|
|
attendance.total_count = 1
|
|
|
|
@api.constrains("student_id", "session_id")
|
|
def _check_student_in_batch(self):
|
|
for attendance in self:
|
|
if not attendance.student_id or not attendance.session_id:
|
|
continue
|
|
enrollment = self.env["dance.enrollment"].search(
|
|
[
|
|
("student_id", "=", attendance.student_id.id),
|
|
("batch_id", "=", attendance.session_id.batch_id.id),
|
|
("status", "=", "active"),
|
|
],
|
|
limit=1,
|
|
)
|
|
if not enrollment:
|
|
raise ValidationError(
|
|
"Attendance can only be marked for active students in this batch."
|
|
)
|