This question has been flagged

We know that in class sale_order has sale_order_line. I am wondering now how can i filter/set default value for some fields. Here is my illustration.

In class res_partner i add some field of type selection:

'part_type' : fields.selection([('child','Child'),('adult','Adult'),('senior','Senior')],'Partner Type')

In sale_order_line i also add this:

'rule_applied' : fields.selection([('low','Low'),('lower','Lower'),('lowest','Lowest')],'Rule') 

Now, if a user select a partner in sale_order and if he try to add an item on its sale_order_line i want to set the default value of rule_applied according to this criteria: If the partner_type of partner selected is child then the default value of rule_applied is Lowest, if senior then Lower and if adult then Low. I tried this method but it does not work:


    def default_get(self, cr, uid, fields, context=None):
res = super(sale_order_line, self).default_get(cr, uid, fields, context=context)
_logger.info("\n\n\t\t\tSetting up rule >>> fields %s"%(str(context)))
_logger.info("\n\n\t\t\tSetting up rule >>> res %s"%(str(res)))
if 'partner_id' in fields or 'order_id' in fields:
partner_id = res['partner_id']
order_id = res['order_id']
if partner_id:
part_list = self.pool.get('res.partner').search(cr,uid,[('customer','=',True)])
partner = self.pool.get('res.partner').browse(cr,uid,part_list,context=context)
if partner.part_type == 'child':
res['rule_applied'] = 'lowest'
elif partner.part_type == 'adult':
res['rule_applied'] = 'low'
elif partner.part_type == 'senior':
res['rule_applied'] = 'lowe'
else:
res['rule_applied'] = False
if order_id:
order = self.pool.get('sale.order').browse(cr,uid,order_id,context=context)
partner = self.pool.get('res.partner').browse(cr,uid,order.partner_id.id,context=context)
if partner.part_type == 'child':
res['rule_applied'] = 'lowest'
elif partner.part_type == 'adult':
res['rule_applied'] = 'low'
elif partner.part_type == 'senior':
res['rule_applied'] = 'lowe'
else:
res['rule_applied'] = False

return res

But it does not work. upon running, my does not work.

Help is much needed.

Avatar
Discard
Best Answer

If I need to code this I will do the following steps:

Add a field to sale.order to use it only for determine the default value to be passed to the order_lines fields

'rule_tpl' : fields.dummy(type="selection",selection=[('low','Low'),('lower','Lower'),('lowest','Lowest')],string='Rule') 

Create an onchange method on the partner field to return the value of the new field rule_tpl using your conditions, that method already exist for sale.order you will be overriding in order to add more values, for that just call super() of your class (for example for the class sale_order will be super(sale_order, self) ). Something like this:

class sale_order(osv.osv):
_name = 'sale.order'
_inherit = 'sale.order'
...
...
def onchange_partner_id(self, cr, uid, ids, partner_id, context=None):
        res = super(sale_order, self).onchange_partner_id(cr, uid, ids, partner_id, context=context)
        partner = self.pool.get('res.partner').browse(cr,uid,partner_id,context=context)
if partner.part_type == 'child':
  res['value']['rule_tpl'] = 'lowest'
elif partner.part_type == 'adult':
res['value']['rule_tpl'] = 'low'
elif partner.part_type == 'senior':
res['value']['rule_tpl'] = 'lower'
else:
res['value']['rule_applied'] = False
return res
...
... 

sale_order()

then in sale.order view form you need to declare the new field rule_tpl with invisible="1" and use it in the context passed to the field order_lines, like this example:


        <record model="ir.ui.view" id=field_tpl_sale_order">
<field name="name">field.tpl.sale.order.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="arch" type="xml">
<field name="origin" position="after">
<field name="rule_tpl" invisible="1"/>
</field>
<field name="order_lines" position="attributes">
<attribute name="context">{'default_rule_applied': rule_tpl}</attribute>
</field>
</field>
</record>

With this setup I think that you will be ok, the default_ values passed in the context of relation fields cause that records created in that field assume those values as defaults

Avatar
Discard
Author

Okay thanks a lot. I'll try it tomorrow at computer lab. By the way in that case do i still need to define default_get in sale_order_line or no need?

Author

Why does overriding onchange_partner_id produce a client error, it says: "TypeError: val is undefined" i also tried to copy the original method and add what you suggested but same error output.

Author

I guess it's because my field is of type dummy. So, with all due respect, i change it to a regular field selection.

The error you mentioned could be more verbosed?? With what you said is impossible to know what is causing it. I don't think that dummy is the source of the error. Please post the error log

Author

Thanks! It helped me a lot.

Author

with fields.dummy this is what i get: OpenERP Client Error TypeError: val is undefined http://localhost:8069/web/static/src/js/pyeval.js:685

Best Answer

Anirudh,
    

So, If it is not there in res, then ... pass the partner_id in context of sale_order_line(one2many) field and then you can easily get it in default get of sale order line context, which you already implemented ..

There in default_get use that partner_id for your further operation...    

hope it helps!    

Avatar
Discard
Author

Yeah, thanks for pointing it out it's order_partner_id, but it's not present in res.

Author

From sale_order_line how can i get the value of partner_id in sale_order? I think i cannot rely on order_partner_id because it is of type related and by default it has no value. Just correct me if i am wrong.

check my updated answer....