From a0c370412109a992cf8f35b7b5097398c430a026 Mon Sep 17 00:00:00 2001 From: Kristof Bernaert Date: Mon, 26 Jan 2026 01:33:32 +0100 Subject: [PATCH] Fix allow_mail_archived_partner causing system notifications to send emails --- .../models/account_move_send.py | 45 +++++++++++- .../models/mail_compose_message.py | 70 +++++-------------- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/allow_mail_archived_partner/models/account_move_send.py b/allow_mail_archived_partner/models/account_move_send.py index 5fcf87e..3e19c2d 100755 --- a/allow_mail_archived_partner/models/account_move_send.py +++ b/allow_mail_archived_partner/models/account_move_send.py @@ -4,11 +4,39 @@ from odoo import models, api class AccountInvoiceSend(models.TransientModel): _inherit = "account.invoice.send" + @api.model + def default_get(self, fields): + """ + Override default_get to pre-fill archived partners. + This is often called BEFORE _get_default_mail_partner_ids. + """ + res = super().default_get(fields) + + # Check if we're in the right context + active_model = self.env.context.get('active_model') + active_ids = self.env.context.get('active_ids', []) + + if active_model == 'account.move' and active_ids: + # Get the moves with archived partners allowed + moves = self.env['account.move'].with_context( + active_test=False + ).browse(active_ids) + + # Collect all unique partners from the moves + partner_ids = set() + for move in moves: + if move.partner_id and move.partner_id.email: + partner_ids.add(move.partner_id.id) + + if partner_ids: + res['partner_ids'] = [(6, 0, list(partner_ids))] + + return res + @api.model def _get_default_mail_partner_ids(self, move, mail_template, mail_lang): """ - Allow archived partners when sending invoices via - the 'Send by Email' wizard. + Also fix this method to include archived partners. """ wiz = self.with_context( active_test=False, @@ -18,3 +46,16 @@ class AccountInvoiceSend(models.TransientModel): AccountInvoiceSend, wiz )._get_default_mail_partner_ids(move, mail_template, mail_lang) + + def _get_mail_composer_values(self, move, template, partner_ids): + """ + Ensure context is passed to mail composer. + """ + return super( + AccountInvoiceSend, + self.with_context( + active_test=False, + include_archived_partners=True, + mail_notify_force=True, + ) + )._get_mail_composer_values(move, template, partner_ids) \ No newline at end of file diff --git a/allow_mail_archived_partner/models/mail_compose_message.py b/allow_mail_archived_partner/models/mail_compose_message.py index d319442..12cd4fb 100755 --- a/allow_mail_archived_partner/models/mail_compose_message.py +++ b/allow_mail_archived_partner/models/mail_compose_message.py @@ -4,60 +4,26 @@ from odoo import models, api class MailComposeMessage(models.TransientModel): _inherit = "mail.compose.message" - @api.model - def default_get(self, fields): - """ - For sales orders and other models using generic composer. - """ - res = super().default_get(fields) - - model = self.env.context.get("active_model") - 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(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): - """ - For explicit user sends from sales orders. - """ - model = self.env.context.get("active_model") or self.model - - if model in ["sale.order", "account.move"]: - return super( - MailComposeMessage, - self.with_context( - active_test=False, - include_archived_partners=True, - mail_notify_force=True, - ) - )._prepare_mail_values(res_ids) - - return super()._prepare_mail_values(res_ids) - def _prepare_recipient_values(self, partner): """ - For sales orders and generic composers. + Handle archived partners when context allows. """ - 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) + # Check if we should include archived partners + # Look for context flags from account.invoice.send OR direct context + include_archived = ( + self.env.context.get("include_archived_partners") or + self.env.context.get("mail_notify_force") or + (self.env.context.get("active_model") in ["account.move", "sale.order"] and + not getattr(partner, 'active', True)) + ) + + if include_archived and partner and not partner.active: + # Return complete data for archived partner + return { + "partner_id": partner.id, + "email": partner.email, + "name": partner.name, + "lang": partner.lang or self.env.context.get("lang") or "en_US", + } return super()._prepare_recipient_values(partner) \ No newline at end of file