This question has been flagged
5 Replies
13400 Views

I want to edit the xml file to make the field Unit Price Readonly in invoice and sale order..Still i am not getting the field readonly: I have tried <field name= price_unit readonly="1"> but to no avail,, stil the field is not readonly,, and i dont want to make it readonly=True in the .py file as the data will not be saved

Avatar
Discard

Use readonly=True in .py & readonly=1 in .xml file to make any field as readonly.

Best Answer

Hello, in fact readonly for this spécific field is manage by to params:

  1. readonly=True
  2. states="{'draft': [('readonly', False)]}"

You can see it in this line in sale.py

   'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),

So as Sven Boudesseul said: You can inherit the base view and edit the field like this and force states param too :

    <record id="view_order_line_form2_inherit" model="ir.ui.view">
        <field name="name">sale.order.line.form2.inherit</field>
        <field name="model">sale.order.line</field>
        <field name="inherit_id" ref="sale.view_order_line_form2"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='price_unit']" position="replace">
                <field name="price_unit" readonly="1" states="{'draft': [('readonly', True)]}"/>
            </xpath>
        </field>
    </record>

Or just edit the base code : module Sale > sale_view.xml

<field name="price_unit" readonly="1" states="{'draft': [('readonly', True)]}" />

Think it could be a good module, and we can do something better wil user right. If user as right to change the price.

Note:

Be carrful, you have 2 <field name="price_unit"/> on the sale.order. One for the tree definition and an other for the form definition. I try to reproduce and can have readonly field, but I think there is a bug after, if you setup `price_unit as readonly in the view, this field will not be saved when you click on the save button. Can you confirm ?

Screenshot:

image description

Note 2:

readonly field are not saved: handle reading and writing readonly field

Avatar
Discard

Yes, you must have readonly="1" and states="{'draft': [('readonly', True)]}" in your xml

Author

Thats what i have done after editing the base code,sale>sale_view.xml,,but no change in the field..can you elaborate more on,,"inheriting the base view?" and where is this base view?

I update with notes: there is 2 price_unit field on sale.order form view

I think you can not do what you want :) see my note 2 on the answer

Author

Error: QWeb2 - template['ListView.rows']: Runtime Error: Error: QWeb2 - template['ListView.row']: Runtime Error: Error: Unknown field state in domain [["state","not in",["{'draft': [('readonly', True)]}"]]]

Author

Thats the error i have now..FYI am using v 6.1

You need to have <field name="state"/> on your form view ..you can add invisible="1" if you don't want to see it .. but this field must be on form view.

Author

<field name="price_unit" readonly="1"> will make the field readonly,,but will not post the value if you save..what might be the problem? Is it a bug? and where has it been resolved?

Best Answer

You can inherit the base view and edit the field like this :

    <record id="view_order_line_form2_inherit" model="ir.ui.view">
        <field name="name">sale.order.line.form2.inherit</field>
        <field name="model">sale.order.line</field>
        <field name="inherit_id" ref="sale.view_order_line_form2"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='price_unit']" position="replace">
                <field name="price_unit" readonly="1"/>
            </xpath>
        </field>
    </record>

Or just edit the base code : module Sale > sale_view.xml

<field name="price_unit" readonly="1"/>
Avatar
Discard
Author

That's what i had done to invoice line in invoice.view.xml and it worked,,but for sales order it did not..it still not readonly after changing it to <field name="price_unit" readonly="1"/>

If you want to try to edit in the Python file, he is in module : Sale > sale.py (line 698 for me) The strange thing is that he is already readonly = True for me.

You create a new module and don't forget to add "sale" in the dependencies (file __openerp__.py).

Create your file .xml and add the code i've written in my answer.

Author

This new module i will create ,,what will i name it and how will it be related to the price_unit coz i dont want to change everything,,only 1 field,

Best Answer

It depends, if your invoice is generated by some order or by other document you can do what they said above (readonly...). But if you are trying to generate invoices and add lines by yourself, then you have to do develop more code.

A readonly field it's not going to store the price given by the onchange_product. So it's not going to work when you save it. The easiest thing is adding a new field "price_unit2" and make it readonly, then you can add the invisible attribute to price_unit but without making it readonly. Then you have to override the onchange_product to return price_unit and price_unit2. So the user it's going to see the price_unit2 but behind there is price_unit that it's going to be stored.

Avatar
Discard
Author

Hi Grover,,Your solution seems to be the workable,,but implementing it is a problem

Best Answer

Hello,

You have to change line in sale.py to:

'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True),

Bcz, Currently line is :

'price_unit': fields.float('Unit Price', required=True, digits_compute= dp.get_precision('Product Price'), readonly=True, states={'draft': [('readonly', False)]}),

first it applys the readonly=True and then the domain, So it makes the field to readonly when state!='Draft'.

Thanks.

Avatar
Discard
Best Answer

After many days investigating and googleling,  I solved using this function, very usefull, is not read only  , is a :  don't allow users to quote below the sales price of products, and you can make your particular changes.

 

http://stackoverflow.com/questions/23359684/dont-allow-to-quote-below-the-sales-price-of-a-product-in-openerp

Avatar
Discard