Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1711 Visualizzazioni

Hi, we already have a small modul and script to create/copy PDF-File after an Invoice is confirmed.

But we are unable to find the right place to get this triggered. 


We made a Folder in addons, created the files and also the script. Modul is shown in odoo apps and enabled. But it looks like it never triggers.


Maybe someone can give a hint?


Thank you!


import os
from io import BytesIO
import smbprotocol.connection
import logging
from odoo import models, api

_logger = logging.getLogger(__name__)
SMB_HOST = 'IP-of-server'  # IP-Address
SMB_PORT = 445  # Port
SMB_USERNAME = 'user'  # SMB-Benutzername
SMB_PASSWORD = 'somePass'  # SMB-Passwort
SMB_SHARE = 'aShare'  # share

def save_pdf_invoice(pdf_content, file_name):
    _logger.info(f"Saving PDF invoice {file_name} to SMB server")
    try:
        with smbprotocol.connection.Connection(username=SMB_USERNAME, password=SMB_PASSWORD,
                                               hostname=SMB_HOST, port=SMB_PORT) as connection:
            with connection.connect_share(SMB_SHARE) as share:
                with share.create_file(file_name) as file:
                    file.write(pdf_content)
    except Exception as e:
        _logger.error(f"Error saving PDF invoice {file_name}: {e}")

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

    @api.model
    def _post_validate(self):
        res = super(AccountMove, self)._post_validate()
        for move in self:
            if move.type == 'out_invoice' and move.state == 'posted':
                pdf_content = move._render_pdf()  # Annahme: Hier wird die PDF-Rechnung generiert
                file_name = f'invoice_{move.id​}.pdf'
                save_pdf_invoice(pdf_content, file_name)
                import pdb; pdb.set_trace()  # Debugger
        return res



Avatar
Abbandona
Risposta migliore

Hey, sounds like you're deep in the weeds with that invoice triger! I've been there. First off, you're on the right track with getting your modle recognized in odoo, so nice one. But if it's not triggering, the method you've linked to, `_post_validate`, might be where the snag is. It’s supposed to trigger when an invoice is validated, but odoo’s workflow or tweaks can really mess things up sometimes.


Check if the invoice actually hits the "posted" state. If there's an error earlier in the chain, `_post_validate` might not fire up like you expected. Also, make sure your module loads correctly; odoo’s logging could give you a hint if it’s reaching your method. And pdb's your friend - seriously, pop a breakpoint to see if it hits.


Still stuck? Try running the function outside odoo's lifecycle. A simple button action to call your PDF save function might help pinpoint the issue, confirming if the SMB part is cool. Make sure your share isn’t blocked by network or permission hiccups. Hope this helps to untangle stuff!

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
apr 24
2260
2
apr 24
2353
1
set 25
440
2
giu 25
1777
3
feb 25
3489