|
|
@@ -7,33 +7,29 @@ _logger = logging.getLogger(__name__)
|
|
|
class WhatsAppComposer(models.TransientModel):
|
|
|
_inherit = 'whatsapp.composer'
|
|
|
|
|
|
- # Campos para soporte de grupos
|
|
|
+ # Campos para soporte básico de grupos (solo por ID string, sin Many2one)
|
|
|
+ # La funcionalidad completa de grupos con Many2one está en whatsapp_web_groups
|
|
|
recipient_type = fields.Selection([
|
|
|
('phone', 'Phone Number'),
|
|
|
('group', 'WhatsApp Group')
|
|
|
], string='Send To', default='phone', help="Choose recipient type")
|
|
|
|
|
|
- # Campo para ID de grupo como string (más robusto)
|
|
|
+ # Campo para ID de grupo como string
|
|
|
whatsapp_group_id_char = fields.Char(string='Group ID',
|
|
|
help="WhatsApp Group ID (e.g., 120363158956331133@g.us)")
|
|
|
|
|
|
- # Campo Many2one opcional si el modelo existe
|
|
|
- whatsapp_group_id = fields.Many2one('ww.group', string='WhatsApp Group',
|
|
|
- help="Select WhatsApp group to send message to",
|
|
|
- ondelete='set null')
|
|
|
-
|
|
|
# Campo para mensaje libre (sin plantilla)
|
|
|
body = fields.Html(string='Message Body', help="Free text message (for WhatsApp Web accounts without template)")
|
|
|
|
|
|
- @api.constrains('recipient_type', 'phone', 'whatsapp_group_id', 'wa_template_id', 'body')
|
|
|
+ @api.constrains('recipient_type', 'phone', 'whatsapp_group_id_char', 'wa_template_id', 'body')
|
|
|
def _check_recipient_configuration(self):
|
|
|
"""Validar configuración de destinatario en composer"""
|
|
|
for record in self:
|
|
|
# Si está en contexto de skip_template_validation, saltar validaciones de plantilla
|
|
|
if self.env.context.get('skip_template_validation'):
|
|
|
# Solo validar configuración básica de destinatario
|
|
|
- if record.recipient_type == 'group' and not record.whatsapp_group_id and not record.whatsapp_group_id_char:
|
|
|
- raise ValidationError("Please select a WhatsApp group or enter a Group ID when sending to groups")
|
|
|
+ if record.recipient_type == 'group' and not record.whatsapp_group_id_char:
|
|
|
+ raise ValidationError("Please enter a Group ID when sending to groups")
|
|
|
elif record.recipient_type == 'phone' and not record.phone:
|
|
|
raise ValidationError("Please provide a phone number when sending to individuals")
|
|
|
return # Saltar el resto de validaciones
|
|
|
@@ -44,8 +40,8 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
])
|
|
|
has_whatsapp_web = bool(whatsapp_web_accounts)
|
|
|
|
|
|
- if record.recipient_type == 'group' and not record.whatsapp_group_id and not record.whatsapp_group_id_char:
|
|
|
- raise ValidationError("Please select a WhatsApp group or enter a Group ID when sending to groups")
|
|
|
+ if record.recipient_type == 'group' and not record.whatsapp_group_id_char:
|
|
|
+ raise ValidationError("Please enter a Group ID when sending to groups")
|
|
|
elif record.recipient_type == 'phone' and not record.phone:
|
|
|
raise ValidationError("Please provide a phone number when sending to individuals")
|
|
|
|
|
|
@@ -60,7 +56,7 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
if record.body and not record.wa_template_id and not has_whatsapp_web:
|
|
|
raise ValidationError("Free text messages require WhatsApp Web account configuration")
|
|
|
|
|
|
- @api.depends('phone', 'batch_mode', 'recipient_type', 'whatsapp_group_id', 'whatsapp_group_id_char')
|
|
|
+ @api.depends('phone', 'batch_mode', 'recipient_type', 'whatsapp_group_id_char')
|
|
|
def _compute_invalid_phone_number_count(self):
|
|
|
"""Override SOLO para casos específicos de grupos - NO interferir con funcionalidad nativa"""
|
|
|
for composer in self:
|
|
|
@@ -77,14 +73,6 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
# TODOS LOS DEMÁS CASOS: usar lógica original sin modificar
|
|
|
super(WhatsAppComposer, composer)._compute_invalid_phone_number_count()
|
|
|
|
|
|
- @api.onchange('recipient_type')
|
|
|
- def _onchange_recipient_type(self):
|
|
|
- """Limpiar campos al cambiar tipo de destinatario"""
|
|
|
- if self.recipient_type == 'group':
|
|
|
- self.phone = False
|
|
|
- else:
|
|
|
- self.whatsapp_group_id = False
|
|
|
-
|
|
|
def action_send_whatsapp_template(self):
|
|
|
"""Override del método de envío SOLO para casos específicos de WhatsApp Web sin plantilla"""
|
|
|
|
|
|
@@ -119,12 +107,10 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
for record in records:
|
|
|
# Determinar destinatario
|
|
|
if self.recipient_type == 'group':
|
|
|
- if self.whatsapp_group_id:
|
|
|
- mobile_number = self.whatsapp_group_id.whatsapp_web_id
|
|
|
- elif self.whatsapp_group_id_char:
|
|
|
+ if self.whatsapp_group_id_char:
|
|
|
mobile_number = self.whatsapp_group_id_char
|
|
|
else:
|
|
|
- raise ValidationError("Please specify a group")
|
|
|
+ raise ValidationError("Please specify a group ID")
|
|
|
else:
|
|
|
mobile_number = self.phone
|
|
|
if not mobile_number:
|
|
|
@@ -170,26 +156,11 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
|
|
|
# SOLO agregar información de grupo si es caso específico
|
|
|
if (hasattr(self, 'recipient_type') and self.recipient_type == 'group'):
|
|
|
- group_id_to_use = None
|
|
|
- if self.whatsapp_group_id:
|
|
|
- try:
|
|
|
- group_id_to_use = self.whatsapp_group_id.whatsapp_web_id
|
|
|
- values.update({
|
|
|
- 'whatsapp_group_id': self.whatsapp_group_id.id,
|
|
|
- })
|
|
|
- except:
|
|
|
- # Si falla, usar campo manual
|
|
|
- pass
|
|
|
-
|
|
|
- if not group_id_to_use and self.whatsapp_group_id_char:
|
|
|
- group_id_to_use = self.whatsapp_group_id_char
|
|
|
-
|
|
|
- # Solo actualizar si hay un grupo válido
|
|
|
- if group_id_to_use:
|
|
|
+ if self.whatsapp_group_id_char:
|
|
|
values.update({
|
|
|
'recipient_type': 'group',
|
|
|
- 'mobile_number': group_id_to_use,
|
|
|
- 'mobile_number_formatted': group_id_to_use,
|
|
|
+ 'mobile_number': self.whatsapp_group_id_char,
|
|
|
+ 'mobile_number_formatted': self.whatsapp_group_id_char,
|
|
|
})
|
|
|
|
|
|
# Siempre agregar recipient_type para compatibilidad
|
|
|
@@ -229,8 +200,7 @@ class WhatsAppComposer(models.TransientModel):
|
|
|
'state': 'outgoing',
|
|
|
}
|
|
|
|
|
|
- if group_id and hasattr(self, 'whatsapp_group_id') and self.whatsapp_group_id:
|
|
|
- message_vals['whatsapp_group_id'] = self.whatsapp_group_id.id
|
|
|
+ # Nota: El campo whatsapp_group_id Many2one está en whatsapp_web_groups
|
|
|
|
|
|
# Crear mail.message
|
|
|
mail_message = self.env['mail.message'].create({
|