79170618

Date: 2024-11-08 15:29:13
Score: 0.5
Natty:
Report link

I am using Odoo.sh enterprise version

After searching on internet and with my understanding about odoo platform related invoice, at last found a possible to way to get access token for a customer invoice and implemented in my system as per my requirement.

Way 1:

Steps

  1. Go to settings -> developer tools -> Activate developer mode.

  2. Go to settings -> Technical -> Automation -> Automation Rules.

  3. Create a new Automation rule.

  4. Choose Model as Journal Entry.

  5. Choose Trigger as State is set to -> Posted.

  6. Under Action to do -> Add an action -> Choose execute code.

  7. Enter this python code alone

    if record: record.preview_invoice()

  8. save code option and automation rule.

  9. Go to a draft State invoice and confirm it.

How it works:

After creating an invoice whether manually created on odoo platform or created via any third party API, Go to invoice and confirm so invoice state changes from draft to posted state, so at that time, automation rule will be triggered so that Access token for an customer invoice is again updated in journal entry model of that customer invoice.

In my project, when customer request to view their invoice, I query odoo using journal entry model with customer invoice ID, at that I will get access token and with formed URL I get pdf content and show to user with anchor tag with download option.

{1}/my/invoices/{2}?report_type=pdf&download=true&access_token={3}

  1. Odoo base Url
  2. Odoo customer invoice ID
  3. Access token retrieved

Way 2

After created an customer invoice in odoo platform manually or created in odoo platform, use below python code to get access token from any system.

My Idea is

Create a API project in any python framework and include this code and after hosting, any other API project can make use and get access token.

Python Code

import xmlrpc.client

url = 'http://localhost:8069'
db = 'my_database'
username = 'my_username'
password = 'my_password'
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})

# Create a new XML-RPC client object
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

# Find the invoice you want to download
invoice_id = [9147]


# Download the PDF file for the first invoice in the list
invoice = models.execute_kw(db, uid, password, 'account.move', 'read', [invoice_id[0]], {'fields': ['name', 'invoice_date', 'amount_total']})

pdf_file = models.execute_kw(db, uid, password, 'account.move', 'preview_invoice', [invoice_id[0]])

access_token = pdf_file["url"].split("=")[1]

print(access_token)
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Sureya Pragaash