Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -3,4 +3,4 @@ from . import mail_thread
|
||||
from . import res_partner
|
||||
from . import mail_template
|
||||
from . import mail_compose_message
|
||||
#from . import account_move_send
|
||||
from . import account_move_send
|
||||
@@ -1,14 +1,22 @@
|
||||
from odoo import models, api
|
||||
|
||||
|
||||
class AccountMoveSend(models.TransientModel):
|
||||
_inherit = "account.move.send"
|
||||
class AccountMoveSendWizard(models.TransientModel):
|
||||
_inherit = "account.move.send.wizard"
|
||||
|
||||
@api.model
|
||||
def _get_default_mail_partner_ids(self, move, mail_template, mail_lang):
|
||||
"""
|
||||
The invoice 'Send by Email' wizard uses this to compute partner_ids (To:).
|
||||
We must disable active_test here to include archived partners.
|
||||
Allow archived partners when sending invoices via
|
||||
the 'Send by Email' wizard.
|
||||
"""
|
||||
wiz = self.with_context(active_test=False, include_archived_partners=True)
|
||||
return super(AccountMoveSend, wiz)._get_default_mail_partner_ids(move, mail_template, mail_lang)
|
||||
# Force archived partners to be visible ONLY in this context
|
||||
wiz = self.with_context(
|
||||
active_test=False,
|
||||
include_archived_partners=True,
|
||||
)
|
||||
|
||||
return super(
|
||||
AccountMoveSendWizard,
|
||||
wiz
|
||||
)._get_default_mail_partner_ids(move, mail_template, mail_lang)
|
||||
|
||||
@@ -1,37 +1,33 @@
|
||||
from odoo import models
|
||||
from odoo import models, api
|
||||
|
||||
|
||||
class MailComposeMessage(models.TransientModel):
|
||||
_inherit = "mail.compose.message"
|
||||
|
||||
def _get_default_recipients(self):
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
"""
|
||||
This is the ONLY place that fills the 'To' field in Odoo 16.
|
||||
We explicitly allow archived partners here for user-initiated sends.
|
||||
For sales orders and other models using generic composer.
|
||||
"""
|
||||
recipients = super()._get_default_recipients()
|
||||
|
||||
res = super().default_get(fields)
|
||||
|
||||
model = self.env.context.get("active_model")
|
||||
active_ids = self.env.context.get("active_ids") or []
|
||||
|
||||
if model == "account.move" and active_ids:
|
||||
moves = self.env["account.move"].with_context(
|
||||
res_id = self.env.context.get("active_id")
|
||||
|
||||
# For sales orders
|
||||
if model == "sale.order" and res_id:
|
||||
order = self.env["sale.order"].with_context(
|
||||
active_test=False
|
||||
).browse(active_ids)
|
||||
|
||||
partners = moves.mapped("partner_id").filtered(
|
||||
lambda p: p.email
|
||||
)
|
||||
|
||||
if partners:
|
||||
recipients["partner_ids"] = [(6, 0, partners.ids)]
|
||||
recipients["email_to"] = ", ".join(partners.mapped("email"))
|
||||
|
||||
return recipients
|
||||
).browse(res_id)
|
||||
|
||||
if order.exists() and order.partner_id and order.partner_id.email:
|
||||
res["partner_ids"] = [(6, 0, [order.partner_id.id])]
|
||||
|
||||
return res
|
||||
|
||||
def _prepare_mail_values(self, res_ids):
|
||||
"""
|
||||
Explicit user send → allow archived partners internally.
|
||||
For explicit user sends from sales orders.
|
||||
"""
|
||||
model = self.env.context.get("active_model") or self.model
|
||||
|
||||
@@ -46,3 +42,22 @@ class MailComposeMessage(models.TransientModel):
|
||||
)._prepare_mail_values(res_ids)
|
||||
|
||||
return super()._prepare_mail_values(res_ids)
|
||||
|
||||
def _prepare_recipient_values(self, partner):
|
||||
"""
|
||||
For sales orders and generic composers.
|
||||
"""
|
||||
model = self.env.context.get("active_model") or self.model
|
||||
|
||||
if (
|
||||
model in ["sale.order", "account.move"]
|
||||
and self.env.context.get("include_archived_partners")
|
||||
and partner
|
||||
):
|
||||
# Use parent method with proper context
|
||||
return super(
|
||||
MailComposeMessage,
|
||||
self.with_context(active_test=False)
|
||||
)._prepare_recipient_values(partner)
|
||||
|
||||
return super()._prepare_recipient_values(partner)
|
||||
Reference in New Issue
Block a user