correct problem for sending invoice emails

This commit is contained in:
Kristof Bernaert
2026-02-01 18:37:06 +01:00
parent fd6d4916fc
commit dae95eaec0
@@ -11,19 +11,17 @@ class MailThread(models.AbstractModel):
_logger.info("=== MAILTHREAD CLASS LOADED (allow_mail_archived_partner) ===") _logger.info("=== MAILTHREAD CLASS LOADED (allow_mail_archived_partner) ===")
def _notify_thread(self, message, msg_vals=False, **kwargs): def _notify_thread(self, message, msg_vals=False, **kwargs):
"""
Keep default behavior.
"""
return super()._notify_thread(message, msg_vals=msg_vals, **kwargs) return super()._notify_thread(message, msg_vals=msg_vals, **kwargs)
def _notify_get_recipients(self, message, msg_vals, **kwargs): def _notify_get_recipients(self, message, msg_vals, **kwargs):
""" """
Force correct email recipients for manual sends (invoices), Ensure archived partners can still be used as recipients for manual sends.
including archived partners, and avoid incomplete recipient dicts. Important: message.partner_ids may appear empty for archived partners if
active_test=True, even if the M2M relation contains IDs.
""" """
_logger.error("🔥🔥🔥 MailThread._notify_get_recipients CALLED 🔥🔥🔥") _logger.error("🔥🔥🔥 MailThread._notify_get_recipients CALLED 🔥🔥🔥")
# --- Ignore system / automatic notifications --- # --- Ignore system/automatic notifications ---
is_system_message = ( is_system_message = (
getattr(message, "message_type", None) == "notification" getattr(message, "message_type", None) == "notification"
or ( or (
@@ -33,7 +31,6 @@ class MailThread(models.AbstractModel):
) )
or (msg_vals and msg_vals.get("message_type") == "notification") or (msg_vals and msg_vals.get("message_type") == "notification")
) )
if is_system_message: if is_system_message:
_logger.debug("SYSTEM MESSAGE using parent behavior") _logger.debug("SYSTEM MESSAGE using parent behavior")
return super()._notify_get_recipients(message, msg_vals, **kwargs) return super()._notify_get_recipients(message, msg_vals, **kwargs)
@@ -47,18 +44,25 @@ class MailThread(models.AbstractModel):
) )
_logger.error(f"🔥 Is manual send? {is_manual_send}") _logger.error(f"🔥 Is manual send? {is_manual_send}")
_logger.error(
f"🔥 Message partner_ids: {getattr(message, 'partner_ids', None)}"
)
if not ( if not is_manual_send:
is_manual_send and hasattr(message, "partner_ids") and message.partner_ids _logger.debug("Not a manual send using parent behavior")
):
_logger.debug("Not a manual send or no partners using parent behavior")
return super()._notify_get_recipients(message, msg_vals, **kwargs) return super()._notify_get_recipients(message, msg_vals, **kwargs)
# --- Manual send with partners --- # 🔥 CRITICAL: read partner_ids with active_test=False, otherwise archived partners disappear
partner_ids = message.partner_ids.ids msg_archived_ok = message.with_context(active_test=False)
msg_partner_rs = getattr(
msg_archived_ok, "partner_ids", self.env["res.partner"]
)
_logger.error(f"🔥 Message partner_ids (active_test=False): {msg_partner_rs}")
if not msg_partner_rs:
_logger.error(
"🔥 Manual send but no partners on message (even with active_test=False)"
)
return super()._notify_get_recipients(message, msg_vals, **kwargs)
partner_ids = msg_partner_rs.ids
_logger.error(f"🔥 Manual send partners: {partner_ids}") _logger.error(f"🔥 Manual send partners: {partner_ids}")
# Get parent recipients (may be incomplete) # Get parent recipients (may be incomplete)
@@ -77,24 +81,20 @@ class MailThread(models.AbstractModel):
_logger.error(f"🔥 Parent returned {len(recipients)} recipients") _logger.error(f"🔥 Parent returned {len(recipients)} recipients")
def _is_bad_recipient(r): def _is_bad_recipient(r):
return not r or not r.get("email") or not r.get("partner_id") return (not r) or (not r.get("email")) or (not r.get("partner_id"))
bad_recipients = [r for r in recipients if _is_bad_recipient(r)] bad_recipients = [r for r in recipients if _is_bad_recipient(r)]
if bad_recipients: if bad_recipients:
_logger.error( _logger.error(
f"🔥 Found {len(bad_recipients)} incomplete recipient(s), forcing from partner_ids" f"🔥 Found {len(bad_recipients)} incomplete recipient(s), forcing from partner_ids"
) )
# Build forced recipients from partner_ids # Build forced recipients from partner_ids (always correct)
forced_recipients = [] forced_recipients = []
for partner_id in partner_ids: for pid in partner_ids:
partner = ( partner = (
self.env["res.partner"] self.env["res.partner"].with_context(active_test=False).browse(pid)
.with_context(active_test=False)
.browse(partner_id)
) )
if partner.exists() and partner.email: if partner.exists() and partner.email:
forced_recipients.append( forced_recipients.append(
{ {
@@ -112,16 +112,14 @@ class MailThread(models.AbstractModel):
) )
_logger.error(f"🔥 Forced recipient: {partner.id} {partner.email}") _logger.error(f"🔥 Forced recipient: {partner.id} {partner.email}")
# Prefer forced recipients if parent gave nothing useful # Prefer forced recipients if parent gave nothing useful / incomplete
if forced_recipients and (not recipients or bad_recipients): if forced_recipients and (not recipients or bad_recipients):
recipients = forced_recipients recipients = forced_recipients
for i, recipient in enumerate(recipients): for i, recipient in enumerate(recipients):
_logger.error( _logger.error(
f"🔥 Recipient {i}: " f"🔥 Recipient {i}: partner_id={recipient.get('partner_id')}, "
f"partner_id={recipient.get('partner_id')}, " f"notif={recipient.get('notif')}, email={recipient.get('email')}"
f"notif={recipient.get('notif')}, "
f"email={recipient.get('email')}"
) )
return recipients return recipients