Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to prevent from changing discount field?

By
Paulo Matos‏
on 9/8/14, 12:14 PM 1,380 views

Hi all,

I need to prevent a group from changing unit price on sale order.

I have found this code, but I really do not know where to put it. On the code, "Fellow" is the group name and "permissions" is a function field.

def _check_permissions(self, cr, uid, ids, field_name, arg, context):
        res = {}
        formid = ids[0]
        group_obj = self.pool.get('res.groups')
        group_ids = group_obj.search(cr, uid, [('name','=','Fellow')])
        group_obj = group_obj.browse(cr, uid, group_ids)
        group_users = len(fellow[0].users)
        i = 0
        while i < group_users :
            if group_obj[0].users[i].id == uid :
                res[formid] = True
        i = i + i
        return res    

and

'permissions': fields.function(_check_permissions, type='char', method=True, string="Permissions"), 

'price': fields.float('price'),

and also this code

<field name="permissions" invisible="1"/>
<field name="price" attrs="{'readonly':[('permissions','=',True)]}"/>

Can you please help me on where to put each piece of code?

Paulo

 

1

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 9/9/14, 1:06 AM

Hi Paulo,

Please refer the sructure of the new (custome) module as like.

1) File :  __openerp__.py : This file contains the simple parameters like name of module, depends etc. You can also copy this file from any other module and made change according your requirement.

2) File :  __init__.py : In this file you have to write down.

import sale_order.py

3) File : sale_order.py

class sale_order_line(osv.Model)

    _inherit = "sale.order.line"

    _name = "sale.order.line"

    def _check_permissions(self, cr, uid, ids, field_name, arg, context):
        res = {}
        formid = ids[0]
        group_obj = self.pool.get('res.groups')
        group_ids = group_obj.search(cr, uid, [('name','=','Fellow')])
        group_obj = group_obj.browse(cr, uid, group_ids)
        group_users = len(fellow[0].users)
        i = 0
        while i < group_users :
            if group_obj[0].users[i].id == uid :
                res[formid] = True
        i = i + i
        return res    

    _columns = {

                       'permissions': fields.function(_check_permissions, type='char', method=True, string="Permissions"), 
                       'price': fields.float('price'),

                  }

4) File : sale_order_view.xml

        <record id="inherited_view_order_form_add_permission" model="ir.ui.view">
            <field name="name">sale.order.form.add.permission</field>
            <field name="inherit_id" ref="sale.view_order_form" />
            <field name="model">sale.order</field>
            <field name="arch" type="xml">
                <field name="" position="before">
                    <field name="permissions" invisible="1" />
                    <field name="price" attrs="{'readonly':[('permissions','=',True)]}" />
                </field> 
            </field>

 

Then after just installe your new module. Thats it.

Dear chirag, Thank you very much. I just did not understood the last part "inherit it into .xml" file. I lack of knowledge on programming principles of Odoo. Is it possible for you to send me a sample _init_.py, _openerp_.py, testmodel.py and the .xml you refer? This way will be easier for me to read the code and fully understand the flow. Thank you once again

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 5:09 AM

hi, I have just updated my answer. According your need. Just give me up vote for this. Thanks.

Emipro Technologies Pvt. Ltd.
on 9/9/14, 5:25 AM

Thank you very much chirag. You saved the day!

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 5:30 AM
0
Paulo Matos‏
On 9/9/14, 6:05 AM

chirag,

Sorry but I still need your help.

Followed the instructions and created the module with the files. The odoo server is start with no errors on logfile and seems to everything ok. Just found an error when installing the module. It's a "RuntimeError: maximum recursion depth exceeded".

Part of the logfile:

  File "/home/openerp/openerp-7.0/server/openerp/modules/module.py", line 133, in load_module
    mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
  File "/home/openerp/openerp-7.0/my_addons/sale_order/__init__.py", line 34, in <module>
    import sale_order
  File "/home/openerp/openerp-7.0/server/openerp/modules/module.py", line 133, in load_module
    mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
  File "/home/openerp/openerp-7.0/my_addons/sale_order/__init__.py", line 34, in <module>
    import sale_order

Can you please help me identify this error?

Revied the code and instructions you sent and are all exactly as instructed.

Can you please help once again?

Regards

Paulo

 

 

hi, can you give me your module name ?

Emipro Technologies Pvt. Ltd.
on 9/9/14, 6:20 AM

chirag, I found the problem. Windows added an "py" extension to the file sales_order.py. The file was named "sales_order.py.py" No I am having a different error when installing the module. The error is: "File "/home/openerp/openerp-7.0/my_addons/permissions/__init__.py", line 34, in import sale_order File "/home/openerp/openerp-7.0/my_addons/permissions/sale_order.py", line 25 class sale_order(osv.Model) ^ SyntaxError: invalid syntax " But anyway and answering to your question: - for testing purposes the module name is "permissions" (first time it was "sale_order" but changed it to permissions - folder named permissions). - on _openerp_.py the value name is "Permissions". - According to instructions, added 2 additional files. sales_order.py and sales_order_view.xml with the exact code for each one of them. Regards

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 6:38 AM

ohh, I just forgot to put the ":" at the end. class sale_order(osv.Model):

Emipro Technologies Pvt. Ltd.
on 9/9/14, 7:02 AM

Thank you chirag. I urgently need to learn python :o) Now I have another error: "NameError: name '_check_permissions' is not defined". At the top of sale_order.py I added the code "from openerp.osv import fields, osv" and the rest of the file is exactly as you sent. I am sorry chirag...

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 7:12 AM

I think you are right ;). Just give four space (" ") before def _check_permissions thats it. I have also updated in my answer.

Emipro Technologies Pvt. Ltd.
on 9/9/14, 7:23 AM

heheheh. Indent on pyhon.. I think I read about that somewhere. It worked, but now I have another error: "ValueError The class sale_order has to have a _name attribute "

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 7:29 AM

I have done changes in my answer.

Emipro Technologies Pvt. Ltd.
on 9/9/14, 7:34 AM

Thank you very much chirag. Odoo accepted the module. But I've missing something here. I can see that the field "permissions" was added to the system (Settings - Technical - Database Structure - fields), but when I create a Sale Order with a user from the group "Fellow", the price field is still editable (price_unit instead of price on the code - 'price': fields.float('price')). When I "View fields" on developer mode I can see the field there. Perhaps it must be inherit on sale order lines and not on sales order? Any idea?

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 8:52 AM

hi Paulo, I have updated my answer. You are right. Today I am quite busy with R&D task so, my concentration is not here. Sorry for that.

Emipro Technologies Pvt. Ltd.
on 9/9/14, 9:00 AM

I am sorry being disturbing you since you are busy. The code did not worked. With your last instructions the permissions field was added to Sale Order Lines sucessfully, the Fellow group exist and when I make a sale order the user is still able to change the field price_unit on sales order lines. I can see that the default field price_unit from sales module was updated with this new price_unit field which is ok, but when I access this price unit settings (database structure / fields) I do not see the Fellow group on "Groups" on its settings. Also when I try to uninstall the module I get an error that might help on identify the problem TypeError: The model "sale.order.line" specifies an unexisting parent class "sale.order.line" You may need to add a dependency on the parent class' module.

ThinkOpen Solutions (Angola), Paulo Matos‏
on 9/9/14, 10:17 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/8/14, 12:14 PM
Seen: 1380 times
Last updated: 3/16/15, 8:10 AM