feat(odoo): extend res.partner with WhatsApp fields
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
116
odoo_whatsapp_hub/models/res_partner.py
Normal file
116
odoo_whatsapp_hub/models/res_partner.py
Normal file
@@ -0,0 +1,116 @@
|
||||
from odoo import models, fields, api
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
whatsapp_conversation_ids = fields.One2many(
|
||||
'whatsapp.conversation',
|
||||
'partner_id',
|
||||
string='Conversaciones WhatsApp',
|
||||
)
|
||||
whatsapp_conversation_count = fields.Integer(
|
||||
string='Conversaciones',
|
||||
compute='_compute_whatsapp_conversation_count',
|
||||
)
|
||||
whatsapp_last_conversation_id = fields.Many2one(
|
||||
'whatsapp.conversation',
|
||||
string='Última Conversación',
|
||||
compute='_compute_whatsapp_last_conversation',
|
||||
)
|
||||
whatsapp_unread_count = fields.Integer(
|
||||
string='Mensajes No Leídos',
|
||||
compute='_compute_whatsapp_unread_count',
|
||||
)
|
||||
|
||||
@api.depends('whatsapp_conversation_ids')
|
||||
def _compute_whatsapp_conversation_count(self):
|
||||
for partner in self:
|
||||
partner.whatsapp_conversation_count = len(partner.whatsapp_conversation_ids)
|
||||
|
||||
@api.depends('whatsapp_conversation_ids.last_message_at')
|
||||
def _compute_whatsapp_last_conversation(self):
|
||||
for partner in self:
|
||||
conversations = partner.whatsapp_conversation_ids.sorted(
|
||||
'last_message_at', reverse=True
|
||||
)
|
||||
partner.whatsapp_last_conversation_id = conversations[:1].id if conversations else False
|
||||
|
||||
@api.depends('whatsapp_conversation_ids.unread_count')
|
||||
def _compute_whatsapp_unread_count(self):
|
||||
for partner in self:
|
||||
partner.whatsapp_unread_count = sum(
|
||||
partner.whatsapp_conversation_ids.mapped('unread_count')
|
||||
)
|
||||
|
||||
def action_open_whatsapp_conversations(self):
|
||||
"""Open WhatsApp conversations for this partner"""
|
||||
self.ensure_one()
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': f'Conversaciones - {self.name}',
|
||||
'res_model': 'whatsapp.conversation',
|
||||
'view_mode': 'tree,form',
|
||||
'domain': [('partner_id', '=', self.id)],
|
||||
'context': {'default_partner_id': self.id},
|
||||
}
|
||||
|
||||
def action_send_whatsapp(self):
|
||||
"""Open wizard to send WhatsApp message"""
|
||||
self.ensure_one()
|
||||
phone = self.mobile or self.phone
|
||||
if not phone:
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'message': 'El contacto no tiene número de teléfono',
|
||||
'type': 'warning',
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': 'Enviar WhatsApp',
|
||||
'res_model': 'whatsapp.send.wizard',
|
||||
'view_mode': 'form',
|
||||
'target': 'new',
|
||||
'context': {
|
||||
'default_partner_id': self.id,
|
||||
'default_phone': phone,
|
||||
},
|
||||
}
|
||||
|
||||
def action_open_whatsapp_chat(self):
|
||||
"""Open or create WhatsApp conversation"""
|
||||
self.ensure_one()
|
||||
phone = self.mobile or self.phone
|
||||
if not phone:
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'message': 'El contacto no tiene número de teléfono',
|
||||
'type': 'warning',
|
||||
}
|
||||
}
|
||||
|
||||
account = self.env['whatsapp.account'].get_default_account()
|
||||
if not account:
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'message': 'No hay cuenta WhatsApp configurada',
|
||||
'type': 'warning',
|
||||
}
|
||||
}
|
||||
|
||||
conversation = self.env['whatsapp.conversation'].find_or_create_by_phone(
|
||||
phone=phone,
|
||||
account_id=account.id,
|
||||
contact_name=self.name,
|
||||
)
|
||||
conversation.partner_id = self.id
|
||||
|
||||
return conversation.action_open_chat()
|
||||
Reference in New Issue
Block a user