2022-11-07 09:27:21 +01:00
|
|
|
# Copyright 2018-2022 Camptocamp
|
2018-11-26 17:55:52 +01:00
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
2022-11-07 09:27:21 +01:00
|
|
|
from odoo import models
|
2018-11-26 17:55:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailTemplate(models.Model):
|
|
|
|
|
2022-11-07 09:27:21 +01:00
|
|
|
_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.
|
|
|
|
"""
|
2022-11-07 09:27:21 +01:00
|
|
|
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
|
|
|
)
|
2022-11-07 09:27:21 +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 = []
|
2022-11-07 09:27:21 +01:00
|
|
|
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:
|
2022-11-07 09:27:21 +01:00
|
|
|
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
|