Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

Programmatically create a sale order line

By
Peter Nietz
on 3/30/16, 7:07 AM 1,310 views

In our custom Odoo module we need to programmatically create new sales order lines. The purpose is to assist our sales teams to create sales orders for follow-up purchases (the customer has already purchased a software license, we want to offer him an extension/prolongation/...). We have created a wizard for this purpose, which fills in the sales order lines (automatically using various information from other models).

Our code looks roughly as follows:

@api.multi
def add_to_offer(self):
for wizard in self:
new_lines = []
for what in wizard.entries:
new_lines.append((0, 0, {
'name' : what.product_id.name,
'product_id' : what.product_id.id,
'product_uom' : what.product_id.uom_id.id,
'product_uom_qty' : 1
}))
wizard.sale_order_id.write({ 'order_line' : new_lines })

In principle this works at large. We don't get any error messages from Odoo (although we had to explicitly include the fields "name" and "product_uom" in order to work around errors on missing fields.

But... apparently there are still some bits missing. Compared to regular order lines our programmatically created ones are missing the unit price, taxes and subtotal price (potentially even more fields). We could try to calculate and set that stuff explicitly (just as we did with "name" and "product_uom") but that would require us to duplicate lots of code including tax and pricelist stuff. Surely not a good idea...

What is the correct way to programmatically create order lines?


Update: Based on Ahmed's tip we are now invoking the methods listed below on all freshly created order line. This seems to work fine. Many thanks for pointing out this direction, Ahmed.

product_id_change()
product_uom_change()
_compute_invoice_status()
_compute_amount()
_get_to_invoice_qty()
_get_price_reduce()
2

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 3/31/16, 8:19 AM

Hello,

You can call the on_change methods in the order.line model to get the line update with calculations ...

let's try:

    @api.multi
def add_to_offer(self):
line_env = self.env['sale.order.line']
for wizard in self:
for what in wizard.entries:
new_line = line_env.create({
                            'product_id': what.product_id.id,
                            'name': what.product_id.name,
                            'order_id': what.sale_order_id.id,
                            'product_uom' : what.product_id.uom_id.id})
new_line.product_id_change() #Calling an onchange method to update the record


Then you'll get the Sale order update by the new values ..

Hope this could helps

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/30/16, 7:07 AM
Seen: 1310 times
Last updated: 6/7/16, 12:03 PM