social/mail_parent_recipient/models/mail_template.py

36 lines
1.5 KiB
Python
Raw Normal View History

# Copyright 2018-2022 Camptocamp
2018-11-26 17:55:52 +01:00
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
2018-11-26 17:55:52 +01:00
class MailTemplate(models.Model):
_inherit = "mail.template"
2018-11-26 17:55:52 +01:00
def generate_recipients(self, results, res_ids):
"""Use partner's parent email as recipient.
Walk up the hierarchy of recipient partners via `parent_id`
and pick the 1st one having an email.
"""
results = super().generate_recipients(results, res_ids)
use_parent_address = (
self.env["ir.config_parameter"].sudo().get_param("mail.use_parent_address")
2018-11-26 17:55:52 +01:00
)
disabled = self.env.context.get("no_parent_mail_recipient")
if use_parent_address and not disabled:
for res_id, values in results.items():
partner_ids = values.get("partner_ids", [])
2018-11-26 17:55:52 +01:00
partners_with_emails = []
partners = self.env["res.partner"].sudo().browse(partner_ids)
2018-11-26 17:55:52 +01:00
for partner in partners:
if partner.email:
partners_with_emails.append(partner.id)
elif partner.commercial_partner_id.email:
if partner.commercial_partner_id.id not in partners.ids:
partners_with_emails.append(
partner.commercial_partner_id.id
)
results[res_id]["partner_ids"] = partners_with_emails
2018-11-26 17:55:52 +01:00
return results