Files
odoo_source/addons/account/models/mail_thread.py
T
Adrien Widart bdcc012004 [FIX] account, mail: link attachments to invoice
When using the "Send & Print" functionality, if the user adds an
attachment, the latter won't be linked to the record.

To reproduce the error:
1. Open a posted invoice
2. Send & Print
3. Attach a file > Select a file on your device
4. Send the email

Error: The invoice's PDF is added to record's attachments, but not the
other file (from step 3).

When the module selects the attachments that need to be linked to the
invoice, it only keeps the ones currently linked to `mail.compose.message`:
https://github.com/odoo/odoo/blob/6441ab71cb68648aa735057952bf757677a20ee3/addons/mail/models/mail_thread.py#L1719-L1725
This is correct when the user creates a message directly in the chatter.
However, when the mail is created thanks to `Send & Print` functionality,
the attachment is linked to `account.invoice.send`.

Another flow have a similar issue: from invoices list, select few ones,
Action > Send & Print: the generated PDF won't be attached to their
invoice. In such a situation, here is the source of the problem:
https://github.com/odoo/odoo/blob/05ad6c477f7da2a495e5baf5b4629323b9d11fa0/addons/mail/wizard/mail_compose_message.py#L363-L367
There isn't any information about the invoice, therefore it is not
possible to linked the attachment to it.

OPW-2438457

closes odoo/odoo#71495

X-original-commit: 98d3748aca724ef06db89294531d82679df45b74
Signed-off-by: Adrien Widart <adwid@users.noreply.github.com>
2021-05-31 12:39:11 +00:00

28 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
def _message_post_process_attachments(self, attachments, attachment_ids, message_values):
""" This method extension ensures that, when using the "Send & Print" feature, if the user
adds an attachment, the latter will be linked to the record. """
record = self.env.context.get('attached_to')
# link mail.compose.message attachments to attached_to
if record and record._name == 'account.move':
message_values['model'] = record._name
message_values['res_id'] = record.id
res = super()._message_post_process_attachments(attachments, attachment_ids, message_values)
# link account.invoice.send attachments to attached_to
model = message_values['model']
res_id = message_values['res_id']
att_ids = [att[1] for att in res.get('attachment_ids') or []]
if att_ids and model == 'account.move':
filtered_attachment_ids = self.env['ir.attachment'].sudo().browse(att_ids).filtered(
lambda a: a.res_model in ('account.invoice.send',) and a.create_uid.id == self._uid)
if filtered_attachment_ids:
filtered_attachment_ids.write({'res_model': model, 'res_id': res_id})
return res