This question has been flagged

I am creating a module which defines a new field named "sale_margin_percent" to sales.order.lines, and then a xml file which adds the field to forms and tries to add an "on_change" attribute to the new generated field.

sales_order_percent.py (note that sale_margin_percent field is defined)

# -*- coding: utf-8 -*-
from openerp import models, fields, api
class sale_order_line(models.Model):

_inherit = "sale.order.line"

sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))

def update_sale_margin(self, cr, uid, ids, price_unit, purchase_price, discount):
print 'DUMMY'

    def update_sale_price(self, cr, uid, ids, sale_margin_percent, purchase_price, discount):
       print 'DUMMY'


XML:

First sale_margin_order is added, and then an "on_change" attribute is tried to be added without success

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record model="ir.ui.view" id="sale_margin_percent_2">
<field name="name">sale.margin.percent.view.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//field[@name='price_unit']" position="after">
<field name="sale_margin_percent" groups="base.group_user"/>
</xpath>
</field>
</record>

<record model="ir.ui.view" id="sale_margin_percent_3">
<field name="name">sale.order.line.tree.margin.view.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']/tree//field[@name='price_unit']" position="after">
<field name="sale_margin_percent" groups="base.group_user"/>
</xpath>
</field>
</record>


<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.view.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']/tree//field[@name='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>

</data>
</openerp>


ERROR:

ParseError: "ValidateError
Field(s) `arch` failed against a constraint: Invalid view definition

Error details:
Element '<xpath expr="//field[@name='order_line']/tree//field[@name='sale_margin_percent']">' cannot be located in parent view

Error context:
View `sale.margin.percent.view.form`
[view_id: 733, xml_id: n/a, model: sale.order, parent_id: 577]" while parsing /usr/lib/python2.7/dist-packages/openerp/addons/custom/sale_margin_percent/sale_margin_percent.xml:38, near
<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.view.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']/tree//field[@name='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>

Avatar
Discard
Best Answer

1. Do you know the module sale_margin?

2. Try this:

<record model="ir.ui.view" id="sale_margin_percent_u3">
<field name="name">sale.margin.percent.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale_margin_percent_3"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/tree//field[@name='sale_margin_percent']" position="attributes">
<attribute name="on_change">update_sale_price(sale_margin_percent, purchase_price, discount)</attribute>
</xpath>
</field>
</record>



Avatar
Discard
Author

Thanks a lot, now I understand this much better and I even got two ways of doing it. Regarding first question: I do know module sale_margin; but sale_margin does not provide sale margin per line, just sale margin for the whole sale order. Additionally, it provides absolute margin, where most of sales chaps use sale margin in %, which is what they are used to all around the planet. Thanks a lot for your answers, they really help.