How to Add Invoice to Purchase Order in Odoo Automatically

I was facing an issue in two way matching algorithm for Incoming Receipts & Vendor bills in my Odoo installation. As vendor bills are tagged in Purchase Orders only through use of ‘Create Bill’, I had to make an algorithm to calculate the matching. Remember, the match would only work if the supply chain members follows the right way of doing it, otherwise, it would fail. That’s what happened, it fails almost 3 times out of 10. Then I realized, the invoice needs to be pinned automatically in the stock.picking model, instead of purchase.order model at the time a Receipt is validated, to keep the tracking for my matching.

While doing so, first challenge is to pin an invoice to a purchase id. By default, the model for invoicing, ‘account.invoice’ has a foreign read only key for purchase, which is purchase_id. But remember, this is a read only field, and can’t be written/created in a new or existing record using record. I dig down the Odoo codes a little under purchase/models/account_invoice.py and could find the Odoo function that can do the job for us. It’s called ‘purchase_order_change()’.

Here is a snippet of adding an invoice automatically to a purchase ID:

invoice_id = self.env['account.invoice'].create({
'type': 'in_invoice',
'purchase_id': self.purchase_id.id,
'partner_id': self.purchase_id.partner_id.id,
})

invoice_id.purchase_order_change()

This will create an invoice in Draft state. Remember, you need to validate this invoice, if you would like to create the invoice number, not just an invoice entry. You may do it manually or you may do it automatically ‘How to Automatically Validate an Invoice in Odoo

Remember, I am triggering the snippet from the stock.picking, not from purchase.order, which is why, the ‘self’, is related to stock.picking and calling self.purchase_id to link the original purchase_id that triggered the stock.picking record.

Now, a small tip. What if, if you want to align the invoice to only have the products you have received in this particular receipt? Easy as pie:

for invoice_line in invoice_id.mapped('invoice_line_ids'):
check = 1
for move_line in self.mapped('move_line_ids'):
if invoice_line.product_id.id == move_line.porduct_id.id:
check = 0

if check:
invoice_line.unlink()

Simple! Isn’t it? Happy coding!

How to Add Custom Sale Order Line in Odoo 12

There are two ways you can add custom sale order line in Odoo 12.

First Method

You can search the sale order first with the name or browse with the ID

sale_order = self.env['sale.order'].search([('name', '=', 'SO009')])

Now, you can see the sale order line IDs using the following:

sale_order.order_line

As this is a One2many relation, we need to use a tuple in the Odoo ORM write() method. Here is the list of data you can pass in a tuple to change data that has One2many or Many2many relation

Odoo ORM Documentation

You can now pass this as python dictionary for ‘order_line’ to sale.order of desired product as following:

sale_order_new.write({
'order_line': [
(0,0, {
'order_id': sale_order.id,
'product_id': 2003,
'price_unit': 3000.0,
'product_uom_qty': 2.0,
'name': ''
})
]
})

Second Method

In this method, you can simply define the order dictionary, and create a sale.order.line item with the order_id in reference. Here is an example

# define dictionary to add order_line
order_line_dict = { 'order_id' : 9, 'product_id': 2003, 'price_unit': 3000.0, 'product_uom_qty': 2.0, 'name': '' }

# search the order line for the sale order
sale_order_line = self.env['sale.order.line'].browse(9)

# create a new order line
sale_order_line.create(order_line_dict)

# print sale order lines
sale_order_line

Note

If you are doing/testing this on the Odoo Shell, then make sure to run the following after the changes to reflect on database:

self.env.cr.commit()

Odoo doesn’t call database cursor commit if you are coding on Odoo shell, it calls automatically though if you are doing it in a module. Just a friendly reminder!