Fix allow_mail_archived_partner causing system notifications to send emails
This commit is contained in:
@@ -7,63 +7,92 @@ from odoo import models, api, fields
|
||||
class AccountInvoiceSend(models.TransientModel):
|
||||
_inherit = "account.invoice.send"
|
||||
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND CLASS LOADED ===")
|
||||
|
||||
# OVERRIDE THE FIELD DEFINITION to remove domain filter
|
||||
partner_ids = fields.Many2many(
|
||||
'res.partner',
|
||||
string='Recipients',
|
||||
help='Contacts of the invoice that will receive the email.',
|
||||
context={'active_test': False},
|
||||
# REMOVE any domain that filters by active=True
|
||||
context={'active_test': False}, # Allow archived in searches
|
||||
check_company=True,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND CREATE ===")
|
||||
_logger.info(f"Create vals: {vals}")
|
||||
return super().create(vals)
|
||||
|
||||
def write(self, vals):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND WRITE ===")
|
||||
_logger.info(f"Write vals: {vals}")
|
||||
return super().write(vals)
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND DEFAULT_GET ===")
|
||||
_logger.info(f"Fields: {fields}")
|
||||
"""
|
||||
Pre-fill invoice email wizard with archived partners.
|
||||
"""
|
||||
_logger.debug("=== ACCOUNT.INVOICE.SEND DEFAULT_GET ===")
|
||||
|
||||
res = super().default_get(fields)
|
||||
_logger.info(f"Result: {res}")
|
||||
_logger.debug(f"Super result partner_ids: {res.get('partner_ids')}")
|
||||
|
||||
active_ids = self.env.context.get('active_ids', [])
|
||||
_logger.debug(f"Active IDs: {active_ids}")
|
||||
|
||||
if active_ids:
|
||||
# Find invoices with archived partners allowed
|
||||
moves = self.env['account.move'].with_context(
|
||||
active_test=False
|
||||
).browse(active_ids)
|
||||
|
||||
partner_ids = []
|
||||
for move in moves:
|
||||
if move.partner_id:
|
||||
partner_ids.append(move.partner_id.id)
|
||||
_logger.debug(f"Found partner {move.partner_id.id} for invoice {move.id}")
|
||||
|
||||
if partner_ids:
|
||||
res['partner_ids'] = [(6, 0, partner_ids)]
|
||||
_logger.debug(f"SET partner_ids: {res['partner_ids']}")
|
||||
|
||||
return res
|
||||
|
||||
def action_send_and_print(self):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND ACTION_SEND_AND_PRINT ===")
|
||||
_logger.info(f"Self ID: {self.id}")
|
||||
_logger.info(f"Partner IDs: {self.partner_ids.ids}")
|
||||
def _get_composer_values(self, res_ids, template):
|
||||
"""
|
||||
Pass context to mail composer to allow archived partners.
|
||||
"""
|
||||
_logger.debug("=== _get_composer_values ===")
|
||||
_logger.debug(f"Passing context to allow archived partners")
|
||||
# FIX: Changed include_archived_partner to include_archived_partners (plural)
|
||||
return super(
|
||||
AccountInvoiceSend,
|
||||
self.with_context(
|
||||
active_test=False,
|
||||
include_archived_partners=True, # FIXED: plural 's'
|
||||
mail_notify_force=True,
|
||||
)
|
||||
)._get_composer_values(res_ids, template)
|
||||
|
||||
# Call parent with ALL context flags
|
||||
def action_send_and_print(self):
|
||||
"""
|
||||
Override send action to add logging and ensure context is passed.
|
||||
"""
|
||||
_logger.debug("=== ACCOUNT.INVOICE.SEND action_send_and_print ===")
|
||||
_logger.debug(f"Context before send: {dict(self.env.context)}")
|
||||
_logger.debug(f"Partner IDs: {self.partner_ids.ids}")
|
||||
|
||||
# Ensure context is passed when calling action
|
||||
result = super(
|
||||
AccountInvoiceSend,
|
||||
self.with_context(
|
||||
active_test=False,
|
||||
include_archived_partners=True,
|
||||
mail_notify_force=True,
|
||||
force_email=True,
|
||||
mark_invoice_as_sent=True,
|
||||
force_email=True, # Ensure mail_thread.py recognizes this
|
||||
mark_invoice_as_sent=True, # Ensure mail_thread.py recognizes this
|
||||
)
|
||||
).action_send_and_print()
|
||||
|
||||
_logger.info(f"Action result: {result}")
|
||||
_logger.debug("Email send action completed")
|
||||
return result
|
||||
|
||||
# Also try overriding _process_send_and_print which might be the actual method
|
||||
def _process_send_and_print(self, invoice, template):
|
||||
_logger.info("=== ACCOUNT.INVOICE.SEND _PROCESS_SEND_AND_PRINT ===")
|
||||
_logger.info(f"Invoice: {invoice.id}, Template: {template.id}")
|
||||
|
||||
# Also override the regular send action if it exists
|
||||
def action_send(self):
|
||||
"""
|
||||
Override send action (without print) to ensure context.
|
||||
"""
|
||||
_logger.debug("=== ACCOUNT.INVOICE.SEND action_send ===")
|
||||
return super(
|
||||
AccountInvoiceSend,
|
||||
self.with_context(
|
||||
@@ -73,4 +102,4 @@ class AccountInvoiceSend(models.TransientModel):
|
||||
force_email=True,
|
||||
mark_invoice_as_sent=True,
|
||||
)
|
||||
)._process_send_and_print(invoice, template)
|
||||
).action_send()
|
||||
Reference in New Issue
Block a user