Esempio n. 1
0
    def _embed_edis_to_pdf(self, pdf_content, invoice):
        """ Create the EDI document of the invoice and embed it in the pdf_content.

        :param pdf_content: the bytes representing the pdf to add the EDIs to.
        :param invoice: the invoice to generate the EDI from.
        :returns: the same pdf_content with the EDI of the invoice embed in it.
        """
        attachments = []
        for edi_format in self:
            attachment = invoice.edi_document_ids.filtered(
                lambda d: d.edi_format_id == edi_format).attachment_id
            if attachment and edi_format._is_embedding_to_invoice_pdf_needed():
                datas = base64.b64decode(
                    attachment.with_context(bin_size=False).datas)
                attachments.append({'name': attachment.name, 'datas': datas})

        if attachments:
            # Add the attachments to the pdf file
            reader_buffer = io.BytesIO(pdf_content)
            reader = OdooPdfFileReader(reader_buffer)
            writer = OdooPdfFileWriter()
            writer.cloneReaderDocumentRoot(reader)
            for vals in attachments:
                writer.addAttachment(vals['name'], vals['datas'])
            buffer = io.BytesIO()
            writer.write(buffer)
            pdf_content = buffer.getvalue()
            reader_buffer.close()
            buffer.close()
        return pdf_content
Esempio n. 2
0
    def _embed_edis_to_pdf(self, pdf_content, invoice):
        """ Create the EDI document of the invoice and embed it in the pdf_content.

        :param pdf_content: the bytes representing the pdf to add the EDIs to.
        :param invoice: the invoice to generate the EDI from.
        :returns: the same pdf_content with the EDI of the invoice embed in it.
        """
        attachments = []
        for edi_format in self.filtered(lambda edi_format: edi_format.
                                        _is_embedding_to_invoice_pdf_needed()):
            attach = edi_format._get_embedding_to_invoice_pdf_values(invoice)
            if attach:
                attachments.append(attach)

        if attachments:
            # Add the attachments to the pdf file
            reader_buffer = io.BytesIO(pdf_content)
            reader = OdooPdfFileReader(reader_buffer, strict=False)
            writer = OdooPdfFileWriter()
            writer.cloneReaderDocumentRoot(reader)
            for vals in attachments:
                writer.addAttachment(vals['name'], vals['datas'])
            buffer = io.BytesIO()
            writer.write(buffer)
            pdf_content = buffer.getvalue()
            reader_buffer.close()
            buffer.close()
        return pdf_content
Esempio n. 3
0
    def _embed_edis_to_pdf(self, pdf_content, invoice):
        """ Create the EDI document of the invoice and embed it in the pdf_content.

        :param pdf_content: the bytes representing the pdf to add the EDIs to.
        :param invoice: the invoice to generate the EDI from.
        :returns: the same pdf_content with the EDI of the invoice embed in it.
        """
        attachments = []
        for edi_format in self:
            try:
                vals = edi_format._export_invoice_to_embed_to_pdf(
                    pdf_content, invoice)
            except:
                continue
            if vals:
                attachments.append(vals)

        if attachments:
            # Add the attachments to the pdf file
            reader_buffer = io.BytesIO(pdf_content)
            reader = OdooPdfFileReader(reader_buffer)
            writer = OdooPdfFileWriter()
            writer.cloneReaderDocumentRoot(reader)
            for vals in attachments:
                writer.addAttachment(vals['name'], vals['datas'])
            buffer = io.BytesIO()
            writer.write(buffer)
            pdf_content = buffer.getvalue()
            reader_buffer.close()
            buffer.close()
        return pdf_content
    def _post_pdf(self, save_in_attachment, pdf_content=None, res_ids=None):
        # OVERRIDE
        if self.model == 'account.move' and res_ids and len(res_ids) == 1:
            invoice = self.env['account.move'].browse(res_ids)
            if invoice.is_sale_document() and invoice.state != 'draft':
                xml_content = invoice._export_as_facturx_xml()

                reader_buffer = BytesIO(pdf_content)
                reader = PdfFileReader(reader_buffer)
                writer = OdooPdfFileWriter()
                writer.cloneReaderDocumentRoot(reader)

                if tools.str2bool(
                        self.env['ir.config_parameter'].sudo().get_param(
                            'edi.use_pdfa', 'False')):
                    try:
                        writer.convert_to_pdfa()
                    except Exception as e:
                        _logger.exception(
                            "Error while converting to PDF/A: %s", e)

                    metadata_template = self.env.ref(
                        'account_facturx.account_invoice_pdfa_3_facturx_metadata',
                        False)
                    if metadata_template:
                        metadata_content = metadata_template.render({
                            'title':
                            invoice.name,
                            'date':
                            fields.Date.context_today(self),
                        })
                        writer.add_file_metadata(metadata_content)

                writer.addAttachment('factur-x.xml', xml_content,
                                     '/application#2Fxml')

                buffer = BytesIO()
                writer.write(buffer)
                pdf_content = buffer.getvalue()
                buffer.close()
                reader_buffer.close()

        return super(IrActionsReport, self)._post_pdf(save_in_attachment,
                                                      pdf_content=pdf_content,
                                                      res_ids=res_ids)