Skip to Content
Menu
This question has been flagged
4 Replies
6042 Views

i am doing migration to odoo 15 and i am uploading the Sales order and invoices separately 
so in there is any way to link sales order to the invoice after i uploading them ? 

 

Avatar
Discard

Hello Ahmed,

Any answer ?

Thanks

Author

Hello Julien
unfortunately i got no answer
and for now i still don't have answer of this question

hello together, did you found a solution for linking a invoice to an existing SO?

thx, michael

Best Answer

Hi,

If you are familiar with code you can try the following.

In the sale order, we have a many2many field named invoice_ids. The invoice corresponding to the sale order is linked there.

So you can create a one-time running code and by searching the corresponding invoice id   and you can Update that to the sale order record

def update_invocie_ids(self):
sale_ids = self.env['sale.order'].search([])
for sale_id in sale_ids:
inovice_id = self.env['account.move'].search(["YOUR CONDITION TO MATCH THE RECORD"])
sale_id.write{(
'invoice_ids': [(4, "YOUR INVOICE IDS")],
)}

Regards

Avatar
Discard
Best Answer

Correct way to do this is if the sale order and the invoice has just one line that match is :

# Assume you already have the sale order and invoice records
so = self.env['sale.order'].browse(SALE_ORDER_ID)
inv = self.env['account.move'].browse(INVOICE_ID)

# 1. Link invoice to sale order
inv.invoice_origin = so.name

# 2. Get the only line from each
so_line = so.order_line[0]
inv_line = inv.invoice_line_ids[0]

# 3. Link invoice line to sale order line
inv_line.sale_line_ids = [(6, 0, [so_line.id])]

# 4. Optional: Set analytic account if needed
if so.analytic_account_id:
    inv_line.analytic_account_id = so.analytic_account_id.id

# 5. Optional: Link invoice back to sale order (inverse)
so.invoice_ids = [(4, inv.id)]

inv.message_post(body=f"Linked manually to SO <b>{so.name}</b>")
so.message_post(body=f"Linked manually to Invoice <b>{inv.name}</b>")

Avatar
Discard
Best Answer

 You cannot edit the invoice_ids field because it is a computed field.

It updates the order lines with the corresponding invoice lines.

it ensures that the invoices are correctly related to the sales

Avatar
Discard
Best Answer

This is a very good question! actualy I have same problem... @julien did you find a way?


What I found in other  posts is the following:



Hi,

If you create sale order and  the invoice, Then to link the invoice with the sales, you can add the id's of the invoices you have created to the field name invoice_ids in sale.order model.  This is a many2many field.


Avatar
Discard