57 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
import logging
from odoo import models, fields, api, _
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
_inherit = 'sale.order'
whatsapp_notification_ids = fields.One2many(
'social.whatsapp.notification', 'sale_order_id', string='WhatsApp Notifications', readonly=True
)
whatsapp_notification_count = fields.Integer(
string='WhatsApp Notification Count', compute='_compute_whatsapp_notification_count'
)
@api.depends('whatsapp_notification_ids')
def _compute_whatsapp_notification_count(self):
for order in self:
order.whatsapp_notification_count = len(order.whatsapp_notification_ids)
def action_confirm(self):
res = super().action_confirm()
# Non-blocking WhatsApp order confirmation dispatch
for order in self:
try:
self.env['social.whatsapp.notification'].send_order_notification(
order=order, event_type='order_confirmed'
)
except Exception as e:
_logger.warning("Non-blocking error sending WhatsApp confirmation for %s: %s", order.name, e)
return res
def action_send_whatsapp_order_processing(self):
"""Dispatches 'Order in Preparation / Processing' WhatsApp notification."""
for order in self:
self.env['social.whatsapp.notification'].send_order_notification(
order=order, event_type='order_processing'
)
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("WhatsApp Notification"),
'message': _("Processing notification enqueued."),
'type': 'info',
'sticky': False,
}
}
def action_view_whatsapp_notifications(self):
self.ensure_one()
action = self.env["ir.actions.actions"]._for_xml_id("dine360_meta_social.action_social_whatsapp_notification")
action['domain'] = [('sale_order_id', '=', self.id)]
action['context'] = {'default_sale_order_id': self.id}
return action