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

65 lines
2.5 KiB
Python

from odoo import fields, models
class AccountMove(models.Model):
_inherit = "account.move"
def cron_send_dance_fee_whatsapp_reminders(self):
today = fields.Date.context_today(self)
invoices = self.search(
[
("move_type", "=", "out_invoice"),
("state", "=", "posted"),
("payment_state", "in", ["not_paid", "partial"]),
("invoice_date_due", "!=", False),
("dance_student_id", "!=", False),
]
)
for invoice in invoices:
days_overdue = (today - invoice.invoice_date_due).days
if days_overdue in (0, 7, 15, 30):
invoice._send_fee_due_whatsapp(days_overdue)
return True
def _send_fee_due_whatsapp(self, days_overdue):
template = self.env["dance.whatsapp.template"].search(
[("trigger", "=", "fee_due"), ("active", "=", True)],
limit=1,
)
if not template:
return
for invoice in self:
student = invoice.dance_student_id or invoice.partner_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 "",
"amount": invoice.amount_residual,
"due_date": invoice.invoice_date_due or "",
"invoice": invoice.name or "",
"days_overdue": days_overdue,
}
existing_log = self.env["dance.whatsapp.log"].search(
[
("trigger", "=", "fee_due"),
("invoice_id", "=", invoice.id),
("create_date", ">=", fields.Datetime.to_string(fields.Datetime.now().replace(hour=0, minute=0, second=0))),
],
limit=1,
)
if existing_log:
continue
self.env["dance.whatsapp.log"].create(
{
"template_id": template.id,
"trigger": "fee_due",
"student_id": student.id,
"guardian_id": guardian.id if guardian else False,
"invoice_id": invoice.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.",
}
)