2026-09-28 11:03:02 +05:30

50 lines
1.9 KiB
Python

from odoo import api, models
class DanceAttendance(models.Model):
_inherit = "dance.attendance"
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
records.filtered(lambda line: line.status == "absent")._send_absence_whatsapp()
return records
def write(self, vals):
previous_status = {line.id: line.status for line in self}
result = super().write(vals)
if vals.get("status") == "absent":
absent_lines = self.filtered(lambda line: previous_status.get(line.id) != "absent")
absent_lines._send_absence_whatsapp()
return result
def _send_absence_whatsapp(self):
template = self.env["dance.whatsapp.template"].search(
[("trigger", "=", "absence"), ("active", "=", True)],
limit=1,
)
if not template:
return
for attendance in self:
student = attendance.student_id
guardian = student.guardian_id or student.guardian_ids[:1]
phone = guardian.mobile or guardian.phone if guardian else False
values = {
"student": student.name or "",
"date": attendance.date or "",
"session": attendance.session_id.name or "",
}
self.env["dance.whatsapp.log"].create(
{
"template_id": template.id,
"trigger": "absence",
"student_id": student.id,
"guardian_id": guardian.id if guardian else False,
"attendance_id": attendance.id,
"phone": phone,
"message": template.render_message(values),
"status": "sent" if phone else "failed",
"error_message": False if phone else "Guardian phone number is missing.",
}
)