How do you create journal entries on an invoice from a sales order via Python?
We have an eCommerce website and we're creating the Sales Order via the XML RPC API, without any issues.
However, in order to create the invoice, I would like to use the following approach;
- Create an automation in Odoo, which utilises a webhook as a trigger
- In the webhook, we pass the order_id from the website (to a custom field in Odoo called x_ecom_order_id)
- The webhook then executes python code, within which we find the Sales Order associated to the Order ID in the x_ecom_order_id custom field (this is already stored on the Sales Order) and then we call _create_invoices on the sale.order model
All of the above works as expected and a draft invoice is correctly generated, with invoice details, including the product line items and a journal entry to recognise the revenue in Sales Account.
However, it does not automatically generate the VAT Output Credit and Accounts Receivable Debit, as per the behaviour if you manually create a draft invoice via the Odoo UI.
Need some help and guidance on how we can automatically create the missing journal entires, prior to posting the invoice.
***********
For your reference, this is the pyhton that is executed, when we call the webhook;
# Retrieve the x_ecom_order_id from the payload
x_ecom_order_id = payload.get('x_ecom_order_id')
if x_ecom_order_id:
sales_orders = env['sale.order'].search([('x_ecom_order_id', '=', x_ecom_order_id)], limit=1)
if len(sales_orders) == 1:
sales_order = sales_orders[0]
if sales_order.invoice_status == 'to invoice':
try:
# Create invoices from the Sales Order
invoice_ids = sales_order._create_invoices()
except Exception as e:
# Log or handle any exception during invoice creation
pass
else:
# Log or handle the case where the Sales Order is not ready for invoicing
pass
else:
# Log or handle the case where the Sales Order is not found or multiple are found
pass
else:
# Log or handle the case where x_ecom_order_id is not provided or invalid
pass
Hello,
Thank you for reaching out.
I'm currently on my yearly vacation, and won't be available to check or respond to emails. I'll attend to your message promptly upon my return on January 02nd, 2024.
Regards,
Debashish Roy