Sunday, 23 November 2014

Odoo/OpenERP : Populate one2many list using on_change method

Following is the method to update/populate simple field value using on_change method:

return {'value' : {'field_name' : <field_value>}}

Most of the OpenERP developers will be aware of it.

What we will do if we have to add one2many list using on_change method? For example : I want to display list of leaves allotted, leaves taken and leaves pending of an employee. So on_change employee_id I want to show related updated leave list. leave_ids is one2many field in my main table.

what we have to do is as follow:

leave_ids = []       
for record in records:
    if record:
        leave_ids.append([0,0, {'holiday_status_id':record,'max_leaves':records[record]['max_leaves'],'leaves_taken':records[record]['leaves_taken'],'remaining_leaves':records[record]['remaining_leaves']}])

return {'value' : {'leave_ids' : leave_ids}}

Hopefully it will help someone.

If you need more detail on this. Please post your valuable comments.


Thanks!!!!!!!!!!!! Enjoy Programming :)

Reference Link :

http://stackoverflow.com/questions/20954412/create-and-edit-items-of-a-one2many-field-through-on-change-method

Saturday, 15 November 2014

Odoo/OpenERP: Create multiple sequence codes from single file

You can create multiple sequence codes from single sequence file. For example, if you want to create different employee codes for 'Regular Employee' and 'Trainee Employee' you can use single sequence file and achieve your results. As follow:

If you don't know at all about sequences, how to create sequence in Odoo/OpenERP, Please click here.

emp_code_sequence_view.xml


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

    <!-- Sequences for Regular Employee code -->
    <record id="emp_code_sequence" model="ir.sequence.type">  
        <field name="name">Employee Code</field>
        <field name="code">hr.employee</field>
    </record>

    <record id="seq_hr_employee" model="ir.sequence">
        <field name="name">Employee Code</field>
        <field name="code">hr.employee</field>
        <field name="padding">5</field>
        <field name="company_id" eval="False"/>
    </record>
    <!-- End of code for employee code sequence-->

    <!-- Sequences for Trainee code -->
    <record id="trainee_code_sequence" model="ir.sequence.type">
        <field name="name">Trainee Code</field>
        <field name="code">hr.trainee</field>
    </record>
    <record id="seq_hr_trainee" model="ir.sequence">
        <field name="name">Trainee Code</field>
        <field name="code">hr.trainee</field>
        <field name="padding">5</field>
        <field name="prefix">TR</field>
        <field name="company_id" eval="False"/>
    </record>
    <!-- End of code for Trainee code sequence-->

</data>

</openerp>


hr_employee_view.xml


<button name="generate_emp_code" string="Generate Employee Code" type="object" class="oe_highlight" attrs="{'invisible':[('emp_code','!=',False),]}" groups="base.group_hr_manager" context="{'employment_type_id':employment_type_id}" />


hr_employee.py

def generate_emp_code(self, cr,uid, ids, context=None):
    
    if context == None:
        return False
    
    if not context.get('employment_type_id', False):
        return False

    employment_type_id = context.get('employment_type_id')
    emp_code_details = self.pool.get('employment.type').read(cr, uid, employment_type_id, ['name'])
    
    if emp_code_details['name'] == 'Trainee':
        emp_code_val=self.pool.get('ir.sequence').get(cr, uid,'hr.trainee')
    else:
        emp_code_val=self.pool.get('ir.sequence').get(cr, uid,'hr.employee')

    res=super(hr_employee, self).write(cr, uid, ids, {'emp_code':emp_code_val})

        return res


That's it.


Thanks!!!!!!!!!! Enjoy Programming :)

Saturday, 8 November 2014

Odoo/OpenERP : Module files sequence in __openerp__.py

This post is regarding, how to add module files in sequence in __openerp__.py file. If you are not adding files in proper sequence you will face an error. For example:

Wrong sequence:

{
    'name': 'Employee Data Module',
    'version': '1.0',
    'category': 'Human Resources',
    'description':"""Add details of employee""",
    'author':'Shiv Shankar',
    'website': 'http://dirtyhandsphp.blogspot.in',
    'images': [],
    'depends':['base','mail', 'hr',],
    'data':[
        'view/employee_data_view.xml',
        'workflow/employee_data_workflow_view.xml',
        'security/user_groups.xml',
        'security/ir_rule.xml',
        'security/ir.model.access.csv'
    ],
    'installable':True,
    'auto_install':False
}

In above data files sequence - view & workflow files has been loaded before group and rule files. So if we are using any group defined in user_groups.xml file, in view files it will throw an error because group is still not loaded. It will always be good if we can use the following sequence :

Correct Sequence:

'data':[
        'security/user_groups.xml',
        'security/ir_rule.xml',
        'workflow/employee_data_workflow_view.xml',
        'view/employee_data_view.xml',        
        'security/ir.model.access.csv'
],
 
Hopefully it will help someone.

Thanks!!!!!!!!!!!! Enjoy Programming :)

Saturday, 1 November 2014

Oddo/OpenERP: Overwrite ACL permissions

To overwrite ACL permission in Odoo, you have to overwrite id of that ACL record. For example by default in Employee Appraisals module Human Resource/ Officer user group is given full permissions as follow.

id - access_survey_page_hr_user
name - survey.page.hr.user
model_id:id - survey.model_survey_page
group_id:id - base.group_hr_user
perm_read - 1
perm_write - 1
perm_create - 1
perm_unlink - 1

and later in your customized module you decide to revoke unlink permission for that user group you have to just keep exact id and change permissions section. ACL record will be as follow:

id - access_survey_page_hr_user
name - survey.page.hr.user
model_id:id - survey.model_survey_page
group_id:id - base.group_hr_user
perm_read - 1
perm_write - 1
perm_create - 1
perm_unlink - 0

Hopefully it will help someone.

Thanks!!!!!!!!!!!!! Enjoy Programming :)

Reference Link:

https://bugs.launchpad.net/openobject-server/+bug/944561

Saturday, 25 October 2014

Odoo/OpenERP : Call function on button

There are three ways to add functionality on button in OpenERP/Odoo:

1. Call function directly on button: You can directly call the function defined in model/class using type="object" attribute.

i.e

<button string="Check availability" name="check_username_availability" type="object" class="oe_highlight" />

In this case you have to define 'check_username_availablilty' function in model.

2. Using Workflow: Workflow is the most used functionality in OpenERP/Odoo to process flow of the activity. Default type of button is workflow i.e type="workflow".  Workflow itself is an vital topic so can't talk much about this here.

<button string="Accept" name="signal_accept" type="workflow" class="oe_highlight" />

3. Using action: You have to user type="action" if you want to call any action defined in the .xml file. For example if you want to open any wizard you can use following code snippet:

<button name="%(action_id)d" string="Button Label" type="action">

or you can replace %(action_id)d with action id from database.

Reference Links:

http://stackoverflow.com/questions/17462992/how-to-add-simple-func-to-a-button-in-openerp-7

http://stackoverflow.com/questions/14682253/name-of-type-action-in-openerp-button

Thanks!!!!!!!!!! Enjoy Programming :)