This question has been flagged
4 Replies
5855 Views

I want to add a function in sale.py so that if a user edits the unit price,, it will still return the initial pricelist when he tries to save the form, in sale.order.line. The values passed to the form should be the one from the database.

i.e if price_unit != pricelist _warn mssg: ("you are not allowed to change unit price")

what the function should do is check, if the value that is in field is same as the one in the pricelist, then allow to save and close or new, if less or more.. return error,

How can i add it?

Avatar
Discard
Best Answer

If want to make the price not editable, and all items will be priced making it so no user will ever need to type in a price, just modify the XML to make the field read only.

You could make it so a group does have edit rights and just make managers of the company members of the group.

1or2 lines of XML versus several 10s of lines of python code

Avatar
Discard
Author

I did that..but its not making the field readonly.. <field name= 'price_unit' readonly="1"/> It does not make the field readonly..what makes it readonly is in python code. 'price_unit' =fields.float("Unit Price"......,states=('draft:[reaonly, True]) but it will not save the price in the server.so u will return 0.. post your xml code here that functions..

You must reinstall the module after you make a modification to the XML, not just start stop the server. If you made the field readonly correctly, it is readonly.

Yes, patently's answer is much easier than mine. You should do this. But you do not need to reinstall the entire module, just update normally.

Author

That seems much better,,coz i was worried how comes i make fields readonly in the xml and yet they dont show any difference,, I will definately try it and if it works i will let u both know.. Thanks Pals for your continous assistance.

Best Answer

You need to add the write function in sale.py like that :

class sale_order_line(osv.osv):
    # Somme code #
    def write(self, cr, uid, ids, vals, context=None):
        # Do some code like this :
        # Browse the actual price_unit, compare this price with the new price value in vals
        # If the price is different : ignore him, else do nothing
        order_line_price_unit = self.read(cr,uid,ids,['price_unit'])['price_unit'][0]
        if 'price_unit' in vals and vals['price_unit'] != order_line_price_unit:
            vals['price_unit'] = this.price_unit
            return super(sale_order_line, self).write(cr, uid, ids, vals, context)
        else:
            return super(sale_order_line, self).write(cr, uid, ids, vals, context)

# Some code #
sale_order_line()
Avatar
Discard

Add "sale" in the dependencies of your module. (File __openerp__.py)

"depends": [ ...., "sale", .... ],

You edit the sale.py of the base's code directly ? If you do so, you don't need _inherit and add 'sale' on the dependencies anymore. Just add the def write(...

I've edited my answer. So you have something like that ? Just adding the write in the sale.py ?

We don't have edited the xml, the error is strange. Have you done some others changes ?

In this case the unit price from the sale order line is updated? or make the change in the database?

Ok, starting from the beginning... You go in sale module, on the base code of openerp. Open the sale.py file. Add my write() on the sale.order class. That's all. No others edits.

Can you edit your question and add in her your previous comment ? Like that we can have the entire code.

Author

Your code has errors

Author

Sven..The code still is returning the error.No handler found

Author Best Answer

Here is where i have added your code

    def unlink(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        """Allows to delete sales order lines in draft,cancel states"""
        for rec in self.browse(cr, uid, ids, context=context):
            if rec.state not in ['draft', 'cancel']:
                raise osv.except_osv(_('Invalid action !'), _('Cannot delete a sales order line which is in state \'%s\'!') %(rec.state,))
        return super(sale_order_line, self).unlink(cr, uid, ids, context=context)

    def write(self, cr, uid, ids, vals, context=None):
        for this in self.browse(cr, uid, ids, context):
            if vals['price_unit'] != this.price_unit:
                vals['price_unit'] = this.price_unit
                result = super(sale_order_line, self).write(cr, uid, ids, vals, context)
        return result

sale_order_line()

class sale_config_picking_policy(osv.osv_memory):
Avatar
Discard

result = super(sale_order_line, self).write(cr, uid, ids, vals, context) is too much indented here.

Can you print vals please and said to me the value please ? (I edit my answer with an exemple)

Author

The error dissapears when oi remove result = super(sale_order_line, self).write(cr, uid, ids, vals, context) return result

Author

and replace with return super(sale_order_line, self).write(cr, uid, ids, vals, context) but still no changes

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