My Problem
My Problem is after creating invoice in odoo accounting, both invoice created mannully or invoice created from any third party i.e. my dotnet application, for viewing the invoice in PDF format we need access token, so when I preview my invoice at that time only I can get the access token.
In odoo Accounting platform, from our dotnet application I am creating invoices, send to odoo platform, after I logined to my odoo instance and I can view my invoice send from my dotnet application, once I have previewed my invoice PDF at that time I am able to get the access token, what are the ways to the access token to the view invoice in PDF format from my dotnet application, there are two important parameter need to view the invoice in PDF format.
To view the invoice PDF every time I need to login to my odoo instance and for creating invoice via dotnet need to be previewed once after I can view the invoice PDF in my dotnet application.
So can any one help to get the access token after once I have created an invoice via dotnet application.
For creating and sending invoice to odoo from my dotnet application I am using this package from nugget package called portacapena.odoojsonrpcclient.1.0.20
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.
Steps
Go to settings -> developer tools -> Activate developer mode.
Go to settings -> Technical -> Automation -> Automation Rules.
Create a new Automation rule.
Choose Model as Journal Entry.
Choose Trigger as State is set to -> Posted.
Under Action to do -> Add an action -> Choose execute code.
Enter this python code alone
if record: record.preview_invoice()
save code option and automation rule.
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}
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)