[FIX] l10n_it_edi: fix uploading Fattura PA XML bill with documents

# Steps to reproduce

* install account_accountant, documents and l10n_it_edi
* Switch to an Italian company
* go to Documents > Configuration > Workspaces and create a new
  workspace `W` and it's child `W1`
* under Documents > Settings, activate Accounting and set `Workspace`to
  `W`
* save and go back to Documents > Settings, and click `Journals` under
  'Accounting'
* add a line with Journals = 'Vendor Bills' and Workspace = `W1`
* go to Accounting > Vendor > Bills and upload a Fattura PA XML bill
* now open the newly generated bill

You should see that the chatter does not load.

# Cause

When uploading a Fattura PA XML bill, a PDF attachment is created and
added to the chatter.  The issue is happening because the
`ir.attachment` record ends up in an invalid state where its 'res_model'
field is false.

The 'res_model' field is actually defined when the attachment is
initially created. However, that value ends up being overwritten when a
related `documents.document` is created here: https://github.com/odoo/enterprise/blob/3f53e50989fbb9551565429aede7c0a1e4d6e5a4/documents_account/models/account_move.py#L88

Because of the following line, that `documents.document` will be
assigned a default `res_id` (but no `res_model`).
https://github.com/odoo/odoo/blob/26b2cbd4e5c73ea66456a91ea9cb36d6b174cae7/addons/l10n_it_edi/models/account_edi_format.py#L767

However, setting `res_id` (or `res_model`) on a `documents.document`
will also overwrite the related attachments' values (in this case, the
attachment's `res_model` is set to false because the document's
`res_model` is false).
This behavior happens because of this inverse compute: https://github.com/odoo/enterprise/blob/b4201e6b422e5b0bde1f04db37bcda4941d0665f/documents/models/document.py#L97-L104

opw-3119669

closes odoo/odoo#113299

X-original-commit: 6839abc79a947e30750ec535058828c190c18955
Signed-off-by: Josse Colpaert <jco@odoo.com>
Signed-off-by: Nshimiyimana Serge Séna (sesn) <sesn@odoo.com>
This commit is contained in:
Nshimiyimana Séna
2023-02-22 08:30:26 +01:00
parent 660fec0277
commit fba274f8d1
@@ -700,9 +700,8 @@ class AccountEdiFormat(models.Model):
'res_id': new_invoice.id,
})
# default_res_id is had to context to avoid facturx to import his content
# no_new_invoice to prevent from looping on the message_post that would create a new invoice without it
new_invoice.with_context(no_new_invoice=True, default_res_id=new_invoice.id).message_post(
new_invoice.with_context(no_new_invoice=True).message_post(
body=(_("Attachment from XML")),
attachment_ids=[attachment_64.id]
)