Alpesh Valaki
Alpesh Valaki

Reputation: 1631

How to access sale.order fields from sale.order.line onchange?

i have override product_id_change() of sale.order.line its working fine. Now my requirement is to get sale.order fields in my onchange so how can i get field value of sale.order from sale.order.line product_id_change()

here is my code odoov8

def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,  uom=False, qty_uos=0, uos=False, name='', partner_id=False, lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
    #how to access self.order_id
    print "order id===", order_id
    
    res = super(sale_order_line, self).product_id_change( cr, uid, ids, pricelist, product, qty=qty,uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,lang=lang, update_tax=update_tax, date_order= date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
    return res

Upvotes: 1

Views: 1774

Answers (2)

You can do it using following steps.

1.In On change method you can pass one argument parent.

<record model="ir.ui.view" id="sale_margin_sale_order_line">
    <field name="name">sale.order.line.margin_and_quantities.view.form</field>
    <field name="type">form</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='order_line']/form[@string='Sales Order Lines']/group/group[1]/field[@name='product_id']"
            position="attributes"> position="attributes">
            <attribute name="on_change">    
                    product_id_change(parent.pricelist_id,product_id,product_uom_qty,product_uom,product_uos_qty,
                    product_uos,name,parent.partner_id, False, False, parent.date_order, False, parent.fiscal_position, True, context,parent)               
            </attribute>
        </xpath>
    </field>
</record>

In above view we have used position attributes option to replace on_change method.

In on_change method just add one parameter at last parent, parent means sale.order object.

product_id field is available in sale.order.line, you can access order_id using parent keyword in the view.

2.Inherit product_id_change method in py file.

def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
            lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None,order_id=False):


    res=super(sale_order,self).product_id_change(cr,uid,ids,pricelist,product,qty,uom,qty_uos,uos,name,partner_id,lang,update_tax,date_order,packaging,fiscal_position,flag,context)


    return res

In above method order_id is available in method argument, so you can directly access it.

If you have installed sale_stock module then you should inherit product_id_change_with_wh method in py file & change position attributes in product_id_change_with_wh on_change in view, also must give dependency of sale_stock in openerp.py file.

After that you will get order_id field in product_id_change_with_wh on_change method & pass this parameter in on_change product_id method.

Ex :

def product_id_change_with_wh(self, cr, uid, ids, pricelist, product, qty=0,
        uom=False, qty_uos=0, uos=False, name='', partner_id=False,
        lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, warehouse_id=False, context=None,order_id=False):
    context = context or {}
    product_uom_obj = self.pool.get('product.uom')
    product_obj = self.pool.get('product.product')
    warning = {}
    #UoM False due to hack which makes sure uom changes price, ... in product_id_change
    res = self.product_id_change(cr, uid, ids, pricelist, product, qty=qty,
        uom=False, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
        lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context,order_id=order_id)

Upvotes: 1

CZoellner
CZoellner

Reputation: 14768

This method is written in the old API so you have to browse the sale.order.line record, which is changed.

def product_id_change(
        self, cr, uid, ids, pricelist, product, qty=0,  uom=False,
        qty_uos=0, uos=False, name='', partner_id=False, lang=False,
        update_tax=True, date_order=False, packaging=False,
        fiscal_position=False, flag=False, context=None):
    line = self.browse(cr, uid, ids[0], context)
    # print line.order_id.name

    res = super(sale_order_line, self).product_id_change(
        cr, uid, ids, pricelist, product, qty=qty,uom=uom,
        qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id
        lang=lang, update_tax=update_tax, date_order= date_order,
        packaging=packaging, fiscal_position=fiscal_position,
        flag=flag, context=context)
    return res

Upvotes: 0

Related Questions