from odoo import models, fields from odoo.tools import html2plaintext from odoo.tools.mail import email_split class MailTemplate(models.Model): _inherit = 'mail.template' send_whatsapp = fields.Boolean(string="Enviar tambiƩn por WhatsApp", default=False) def send_mail_batch(self, res_ids, force_send=False, raise_exception=False, email_values=None, email_layout_xmlid=False): mails = super().send_mail_batch(res_ids, force_send=force_send, raise_exception=raise_exception, email_values=email_values, email_layout_xmlid=email_layout_xmlid) if not self.send_whatsapp or not mails: return mails RecordModel = self.env[self.model] partners_by_mail = {} for mail in mails: for partner in mail.recipient_ids: if partner.email: partners_by_mail.setdefault(partner.email.strip().lower(), []).append(partner) for mail in mails: # Filtrar por modelo habilitado en ir.model model_enabled = self.env['ir.model'].search([('model', '=', mail.model), ('whatsapp_notifications_enabled', '=', True)], limit=1) if not model_enabled: continue to_emails = [] if mail.email_to: to_emails = [e.strip().lower() for e in email_split(mail.email_to)] recipients = set(to_emails) for partner in mail.recipient_ids: if partner.email: recipients.add(partner.email.strip().lower()) subject = mail.subject or '' body_plain = html2plaintext(mail.body_html or '') if mail.body_html else '' message_text = (subject + "\n\n" + body_plain).strip() if (subject or body_plain) else '' sent_partner_ids = set() for email in recipients: partners = partners_by_mail.get(email, []) if not partners: partners = self.env['res.partner'].search([('email', 'ilike', email), ('mobile', '!=', False)]) for partner in partners or []: if partner.id in sent_partner_ids: continue # Filtrar por preferencia del partner pref = self.env['partner.whatsapp.notification'].search([ ('partner_id', '=', partner.id), ('model_id', '=', model_enabled.id), ('active', '=', True), ], limit=1) if not pref: continue if partner.mobile: msg = self.env['mail.message'].sudo().create({ 'body': message_text or subject, 'message_type': 'whatsapp_message', 'model': mail.model, 'res_id': mail.res_id, 'partner_ids': [partner.id], 'subtype_id': self.env.ref("mail.mt_note").id, }) wa_account = self.env['whatsapp.account'].search([('whatsapp_web_url', '!=', False)], limit=1) if not wa_account: continue self.env['whatsapp.message'].sudo().create({ 'mail_message_id': msg.id, 'mobile_number': partner.mobile, 'mobile_number_formatted': partner.mobile, 'wa_template_id': False, 'wa_account_id': wa_account.id, 'state': 'outgoing', })._send_message() for attachment in mail.attachment_ids: msg_att = self.env['mail.message'].sudo().create({ 'body': message_text or subject, 'message_type': 'whatsapp_message', 'model': mail.model, 'res_id': mail.res_id, 'partner_ids': [partner.id], 'attachment_ids': [(4, attachment.id)], 'subtype_id': self.env.ref("mail.mt_note").id, }) self.env['whatsapp.message'].sudo().create({ 'mail_message_id': msg_att.id, 'mobile_number': partner.mobile, 'mobile_number_formatted': partner.mobile, 'wa_template_id': False, 'wa_account_id': wa_account.id, 'state': 'outgoing', })._send_message() sent_partner_ids.add(partner.id) return mails