Skip to content

Commit 0dc7784

Browse files
[FIX] account_invoice_ubl,base_ubl: backport fix from v13
This way, we avoid the issue of factur-x.
1 parent a8cc237 commit 0dc7784

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

account_invoice_ubl/models/account_invoice.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,19 @@ def get_ubl_version(self):
289289
def get_ubl_lang(self):
290290
return self.partner_id.lang or 'en_US'
291291

292+
def add_xml_in_pdf_buffer(self, buffer):
293+
self.ensure_one()
294+
if self.is_ubl_sale_invoice_posted():
295+
version = self.get_ubl_version()
296+
xml_filename = self.get_ubl_filename(version=version)
297+
xml_string = self.generate_ubl_xml_string(version=version)
298+
buffer = self._ubl_add_xml_in_pdf_buffer(xml_string, xml_filename, buffer)
299+
return buffer
300+
292301
@api.multi
293302
def embed_ubl_xml_in_pdf(self, pdf_content=None, pdf_file=None):
294303
self.ensure_one()
295-
if (
296-
self.type in ('out_invoice', 'out_refund') and
297-
self.state in ('open', 'paid')):
304+
if self.is_ubl_sale_invoice_posted():
298305
version = self.get_ubl_version()
299306
ubl_filename = self.get_ubl_filename(version=version)
300307
xml_string = self.generate_ubl_xml_string(version=version)
@@ -330,3 +337,11 @@ def attach_ubl_xml_file_button(self):
330337
'view_mode': 'form,tree'
331338
})
332339
return action
340+
341+
def is_ubl_sale_invoice_posted(self):
342+
self.ensure_one()
343+
is_ubl = self.company_id.xml_format_in_pdf_invoice == "ubl"
344+
if (is_ubl and self.type in ('out_invoice', 'out_refund')
345+
and self.state in ('open', 'paid')):
346+
return True
347+
return False

account_invoice_ubl/models/ir_actions_report.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,29 @@
88
class IrActionsReport(models.Model):
99
_inherit = "ir.actions.report"
1010

11+
def postprocess_pdf_report(self, record, buffer):
12+
if self.is_ubl_xml_to_embed_in_invoice():
13+
buffer = record.add_xml_in_pdf_buffer(buffer)
14+
return super().postprocess_pdf_report(record, buffer)
15+
1116
@api.multi
1217
def _post_pdf(self, save_in_attachment, pdf_content=None, res_ids=None):
1318
"""We go through that method when the PDF is generated for the 1st
1419
time and also when it is read from the attachment.
15-
This method is specific to QWeb"""
16-
invoice_reports = self._get_invoice_reports_ubl()
17-
if (
18-
len(self) == 1 and
19-
self.report_name in invoice_reports and
20-
res_ids and
21-
len(res_ids) == 1 and
22-
not self._context.get('no_embedded_ubl_xml')):
23-
invoice = self.env['account.invoice'].browse(res_ids[0])
24-
if (
25-
invoice.type in ('out_invoice', 'out_refund') and
26-
invoice.company_id.xml_format_in_pdf_invoice == 'ubl'):
27-
pdf_content = invoice.with_context(
28-
no_embedded_pdf=True).embed_ubl_xml_in_pdf(
29-
pdf_content=pdf_content)
30-
return super()._post_pdf(
20+
"""
21+
pdf_content = super()._post_pdf(
3122
save_in_attachment, pdf_content=pdf_content, res_ids=res_ids)
23+
if res_ids and len(res_ids) == 1:
24+
if self.is_ubl_xml_to_embed_in_invoice():
25+
invoice = self.env['account.invoice'].browse(res_ids)
26+
if invoice.is_ubl_sale_invoice_posted():
27+
pdf_content = invoice.embed_ubl_xml_in_pdf(pdf_content)
28+
return pdf_content
29+
30+
def is_ubl_xml_to_embed_in_invoice(self):
31+
return (self.model == 'account.invoice'
32+
and not self.env.context.get('no_embedded_ubl_xml')
33+
and self.report_name in self._get_invoice_reports_ubl())
3234

3335
@classmethod
3436
def _get_invoice_reports_ubl(cls):

base_ubl/models/ubl.py

+16
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,22 @@ def _ubl_check_xml_schema(self, xml_string, document, version='2.1'):
510510
% str(e))
511511
return True
512512

513+
@api.model
514+
def _ubl_add_xml_in_pdf_buffer(self, xml_string, xml_filename, buffer):
515+
# Add attachment to PDF content.
516+
reader = PdfFileReader(buffer)
517+
writer = PdfFileWriter()
518+
writer.appendPagesFromReader(reader)
519+
writer.addAttachment(xml_filename, xml_string)
520+
# show attachments when opening PDF
521+
writer._root_object.update(
522+
{NameObject("/PageMode"): NameObject("/UseAttachments")}
523+
)
524+
new_buffer = BytesIO()
525+
writer.write(new_buffer)
526+
buffer.close()
527+
return new_buffer
528+
513529
@api.model
514530
def embed_xml_in_pdf(
515531
self, xml_string, xml_filename, pdf_content=None, pdf_file=None):

0 commit comments

Comments
 (0)