Fix allow_mail_archived_partner causing system notifications to send emails

This commit is contained in:
Kristof Bernaert
2026-01-26 03:39:33 +01:00
parent 4e4a41183a
commit 0c4ef37a0a
@@ -18,35 +18,56 @@ class AccountInvoiceSend(models.TransientModel):
@api.model @api.model
def default_get(self, fields): def default_get(self, fields):
_logger.info("=== ACCOUNT.INVOICE.SEND DEFAULT_GET ===")
res = super().default_get(fields) res = super().default_get(fields)
_logger.info(f"Initial result: {res}")
active_ids = self.env.context.get('active_ids', []) active_ids = self.env.context.get('active_ids', [])
_logger.info(f"Active IDs: {active_ids}")
if active_ids: if active_ids:
# Find invoices with archived partners allowed
moves = self.env['account.move'].with_context( moves = self.env['account.move'].with_context(
active_test=False active_test=False
).browse(active_ids) ).browse(active_ids)
partners = moves.mapped('partner_id').filtered(lambda p: p.email) partners = moves.mapped('partner_id').filtered(lambda p: p.email)
_logger.info(f"Found partners: {partners.ids}")
if partners: if partners:
# Set BOTH fields
res['partner_ids'] = [(6, 0, partners.ids)] res['partner_ids'] = [(6, 0, partners.ids)]
res['email_to'] = ",".join(partners.mapped('email'))
_logger.info(f"Set partner_ids: {res['partner_ids']}")
_logger.info(f"Set email_to: {res['email_to']}")
return res return res
def action_send_and_print(self): def action_send_and_print(self):
""" """
Explicitly set email_to on the wizard record. Ensure email_to is set before sending.
This is REQUIRED for account.invoice.send.
""" """
self.ensure_one() _logger.info("=== ACCOUNT.INVOICE.SEND action_send_and_print ===")
_logger.info(f"Wizard ID: {self.id}")
_logger.info(f"Partner IDs: {self.partner_ids.ids}")
_logger.info(f"Current email_to: {self.email_to}")
emails = [p.email for p in self.partner_ids if p.email] # Ensure email_to is set from partner_ids
if not emails: if self.partner_ids and not self.email_to:
_logger.warning("No recipient emails found; skipping send.") emails = [p.email for p in self.partner_ids if p.email]
return {'type': 'ir.actions.act_window_close'} if emails:
self.email_to = ",".join(emails)
_logger.info(f"Updated email_to: {self.email_to}")
# 🔥 THIS is the critical line # Also ensure partner_ids are written to the record
self.email_to = ",".join(emails) if self.partner_ids:
_logger.info(f"Writing partner_ids to record")
self.write({'partner_ids': [(6, 0, self.partner_ids.ids)]})
_logger.info(f"Final context before send: {dict(self.env.context)}")
# Call parent with context
return super( return super(
AccountInvoiceSend, AccountInvoiceSend,
self.with_context( self.with_context(
@@ -57,3 +78,28 @@ class AccountInvoiceSend(models.TransientModel):
mark_invoice_as_sent=True, mark_invoice_as_sent=True,
) )
).action_send_and_print() ).action_send_and_print()
def write(self, vals):
"""
Ensure when partner_ids are written, email_to is also updated.
"""
_logger.info("=== ACCOUNT.INVOICE.SEND WRITE ===")
_logger.info(f"Write vals keys: {vals.keys()}")
if 'partner_ids' in vals and not vals.get('email_to'):
# Get partner emails
partner_ids = vals['partner_ids'][0][2] if vals['partner_ids'] else []
_logger.info(f"Partner IDs in write: {partner_ids}")
if partner_ids:
partners = self.env['res.partner'].with_context(
active_test=False
).browse(partner_ids)
emails = [p.email for p in partners if p.email]
if emails:
vals['email_to'] = ",".join(emails)
_logger.info(f"Auto-updated email_to in write: {vals['email_to']}")
result = super().write(vals)
_logger.info(f"Write completed")
return result