[FIX] account_edi_ubl_cii: no xml declaration with the send & print

Issue: the xml declaration "<?xml version='1.0' encoding='UTF-8'?>" is
lost when opening the send & print wizard with existing xml attachments.

Explanation: When opening the send and print wizard, the PDF are
generated and postprocessed by `_postprocess_pdf_report`. In the
`_postprocess_pdf_report` override in `account_edi_ubl_cii`, the xml
attachments are parsed through `tree = etree.fromstring(xml)`, then the
base64 PDF is inserted, and the etree is converted to a bytes through
`etree.tostring(cleanup_xml_node(tree))` and the resulting bytes is
written back on the attachment, but the xml declaration disappeared.

Solution: include the arguments `xml_declaration=True, encoding='UTF-8'`
in the `tostring` function when converting the etree to a bytes.

opw-3144519

closes odoo/odoo#125523

X-original-commit: ed9a1d83bd7d496a316e8da6a201ca3a14c99d19
Signed-off-by: Julien Van Roy <juvr@odoo.com>
Signed-off-by: Laurent Smet <las@odoo.com>
This commit is contained in:
Julien Van Roy
2023-06-19 03:24:39 +02:00
parent 4e09f0d86e
commit 9a2e53e45c
3 changed files with 5 additions and 6 deletions
@@ -236,8 +236,7 @@ class AccountEdiXmlCII(models.AbstractModel):
vals = self._export_invoice_vals(invoice)
errors = [constraint for constraint in self._export_invoice_constraints(invoice, vals).values() if constraint]
xml_content = self.env['ir.qweb']._render('account_edi_ubl_cii.account_invoice_facturx_export_22', vals)
xml_content = b"<?xml version='1.0' encoding='UTF-8'?>\n" + etree.tostring(cleanup_xml_node(xml_content))
return xml_content, set(errors)
return etree.tostring(cleanup_xml_node(xml_content), xml_declaration=True, encoding='UTF-8'), set(errors)
# -------------------------------------------------------------------------
# IMPORT
@@ -478,8 +478,7 @@ class AccountEdiXmlUBL20(models.AbstractModel):
vals = self._export_invoice_vals(invoice)
errors = [constraint for constraint in self._export_invoice_constraints(invoice, vals).values() if constraint]
xml_content = self.env['ir.qweb']._render(vals['main_template'], vals)
xml_content = b"<?xml version='1.0' encoding='UTF-8'?>\n" + etree.tostring(cleanup_xml_node(xml_content))
return xml_content, set(errors)
return etree.tostring(cleanup_xml_node(xml_content), xml_declaration=True, encoding='UTF-8'), set(errors)
# -------------------------------------------------------------------------
# IMPORT
@@ -216,8 +216,9 @@ class AccountMoveSend(models.Model):
anchor_index = tree.index(anchor_elements[0])
tree.insert(anchor_index, etree.fromstring(to_inject))
invoice_data['ubl_cii_xml_attachment_values']['raw'] = b"<?xml version='1.0' encoding='UTF-8'?>\n" \
+ etree.tostring(cleanup_xml_node(tree))
invoice_data['ubl_cii_xml_attachment_values']['raw'] = etree.tostring(
cleanup_xml_node(tree), xml_declaration=True, encoding='UTF-8'
)
def _link_invoice_documents(self, invoice, invoice_data):
# EXTENDS 'account'