コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
1696 ビュー

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



アバター
破棄
最善の回答

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!

アバター
破棄
関連投稿 返信 ビュー 活動
2
4月 24
2253
2
4月 24
2346
1
9月 25
428
2
6月 25
1766
3
2月 25
3481