Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
659 Vistas

Hi,

I try to automate the creation of reccuring invoices. I manage to create the invoice, send it by mail using my template with the pdf attached. 
Now I try to copy the Pdf file on a nextcloud instance. The connection is Ok, I managed to create the folders but my issue is : when I generate the pdf file, the corresponding invoice is deleted. 
Here is my code : 


from odoo import models, fields, api
import base64
import requests
import locale
import logging
from urllib.parse import quote
_logger = logging.getLogger(__name__)

class AccountMove(models.Model):
    _inherit = 'account.move'

    @api.model
    def envoyer_quittance_et_sauvegarder(self):
      # ID du modèle de facture à copier
        facture_modele_id = 16
        facture_modele = self.env['account.move'].browse(facture_modele_id)

        # Dupliquer et valider
        nouvelle_facture = facture_modele.copy()
        nouvelle_facture.action_post()
        # Envoi par mail
        template = self.env.ref('quittance.modele_email')
template.send_mail(nouvelle_facture.id, force_send=True)

""" Code about construction of the Url and Nextcloud authentication """

# Générer le PDF
        report = self.env['ir.actions.report']._get_report_from_name('account.report_invoice')
        pdf_content, _ = report._render_qweb_pdf(nouvelle_facture.id)
       
        # Envoyer le PDF sur Nextcloud
        response = requests.put(
            url_pdf,
            data=pdf_content,
            auth=(username, password),
            headers={'Content-Type': 'application/pdf'}
        )

        if response.status_code not in (200, 201, 204):
            _logger.warning(f" Erreur envoi PDF : {response.status_code} - {response.text}")
        else:
            _logger.info(f" Quittance enregistrée : {pdf_filename} dans {url_quittances}")


Is there another way for creating a pdf file?

Avatar
Descartar
Mejor respuesta

try:

    report = self.env['ir.actions.report'].search([('model', '=', 'account.move')], limit=1)

    if not report:

        _logger.error("Aucun rapport trouvé pour le modèle account.move")

        return


    pdf_content, _ = self.env['ir.actions.report']._render_qweb_pdf(report.report_name, [nouvelle_facture.id}")


    # Upload to Nextcloud

    response = requests.put(

        url_pdf,

        data=pdf_content,

        auth=(username, password),

        headers={'Content-Type': 'application/pdf'}

    )


    if response.status_code not in (200, 201, 204):

        _logger.warning(f"Erreur envoi PDF : {response.status_code} - {response.text}")

    else:

        _logger.info(f"Quittance enregistrée : {pdf_filename} dans {url_quittances}")


except Exception as e:

    _logger.error(f"Erreur lors de la génération ou l'envoi du PDF : {e}")


Avatar
Descartar
Autor Mejor respuesta

Is there only a way to send a pdf file to any storage?

Edit : Yes! 

Replace the code after # Générer le PDF by : 

# Générer le PDF
        try:
            report = self.env['ir.actions.report'].search([('model', '=', 'account.move')], limit=1)
            if not report:
                _logger.error("Aucun rapport trouvé pour le modèle account.move")
                return

            pdf_content, _ = self.env['ir.actions.report']._render_qweb_pdf(
                report.report_name, nouvelle_facture.ids
            )
            _logger.info(f"PDF généré pour la facture ID {nouvelle_facture.id}")
        except Exception as e:
            _logger.error(f"Erreur lors de la génération du PDF pour facture ID {nouvelle_facture.id} : {e}")
            return
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
3
oct 21
3258
2
nov 16
4235
1
mar 15
4030
0
ago 24
1223
0
jun 23
1718