Friday, 16 January 2015

Odoo/OpenERP: Create multiple workflows in a single module

Today's post is about : How to create multiple workflows for multiple models in a single module. Few days back, I created a module custom_account with two models custom_expense and custom_payment. I created two different but almost similar workflows for both of them. When I installed the module, only latter one was working. What could be the reason?

Problem was with Activity and Transition ids. I was using same ids in both the workflows. Check following:

Old Code:


custom_expense_workflow_view.xml

        <!-- Activity -->
       
        <record id="act_draft" model="workflow.activity">
            <field name="wkf_id" ref="custom_expense_workflow"/>
            <field name="flow_start">True</field>
            <field name="kind">dummy</field>
            <field name="name">draft</field>
        </record>          
       
        <record id="act_submit" model="workflow.activity">
            <field name="wkf_id" ref="custom_expense_workflow"/>
            <field name="kind">function</field>
            <field name="action">expense_submit()</field>
            <field name="name">submit</field>
        </record>    
       
        <!-- Transistion -->

        <record id="trans_draft_submit" model="workflow.transition">
            <field name="act_from" ref="act_draft"/>
            <field name="act_to" ref="act_submit"/>
            <field name="signal">signal_submit</field>                      
        </record>  
 
custom_payment_workflow_view.xml

        <!-- Activity -->
       
        <record id="act_draft" model="workflow.activity">
            <field name="wkf_id" ref="custom_payment_workflow"/>
            <field name="flow_start">True</field>
            <field name="kind">dummy</field>
            <field name="name">draft</field>
        </record>          
       
        <record id="act_submit" model="workflow.activity">
            <field name="wkf_id" ref="custom_payment_workflow"/>
            <field name="kind">function</field>
            <field name="action">expense_submit()</field>
            <field name="name">submit</field>
        </record>    
       
        <!-- Transistion -->

        <record id="trans_draft_submit" model="workflow.transition">
            <field name="act_from" ref="act_draft"/>
            <field name="act_to" ref="act_submit"/>
            <field name="signal">signal_submit</field>                      
        </record>      


So workflow.activity and workflow.transition were same. That was the catch.

New Code:


custom_expense_workflow_view.xml

         <!-- Activity -->
       
        <record id="expense_act_draft" model="workflow.activity">
            <field name="wkf_id" ref="custom_expense_workflow"/>
            <field name="flow_start">True</field>
            <field name="kind">dummy</field>
            <field name="name">draft</field>
        </record>          
       
        <record id="expense_act_submit" model="workflow.activity">
            <field name="wkf_id" ref="custom_expense_workflow"/>
            <field name="kind">function</field>
            <field name="action">expense_submit()</field>
            <field name="name">submit</field>
        </record>    
       
        <!-- Transistion -->

        <record id="expense_trans_draft_submit" model="workflow.transition">
            <field name="act_from" ref="expense_act_draft"/>
            <field name="act_to" ref="expense_act_submit"/>
            <field name="signal">signal_submit</field>                      
        </record>  
 
custom_payment_workflow_view.xml

        <!-- Activity -->
       
        <record id="payment_act_draft" model="workflow.activity">
            <field name="wkf_id" ref="custom_payment_workflow"/>
            <field name="flow_start">True</field>
            <field name="kind">dummy</field>
            <field name="name">draft</field>
        </record>          
       
        <record id="payment_act_submit" model="workflow.activity">
            <field name="wkf_id" ref="custom_payment_workflow"/>
            <field name="kind">function</field>
            <field name="action">expense_submit()</field>
            <field name="name">submit</field>
        </record>    
       
        <!-- Transistion -->

        <record id="payment_trans_draft_submit" model="workflow.transition">
            <field name="act_from" ref="payment_act_draft"/>
            <field name="act_to" ref="payment_act_submit"/>
            <field name="signal">signal_submit</field>                      
        </record>      

That's it.

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

Monday, 1 December 2014

Ubuntu 14.04 : Add 1280 x 1024 screen resolution

Spent around 2 hours to set new resolution for Ubuntu 14.04 and when come to know the right way it took only 5 minutes. So, writing this post so that it can save someone else's time.

Following are the steps to add 1280 x 1024 screen resolution in Ubuntu 14.04

1) Create /etc/X11/xorg.conf file with following content:

Section "Monitor"
    Identifier    "Monitor0"
    Modeline "1280x1024_60.00"  109.00  1280 1368 1496 1712  1024 1027 1034 1063 -hsync +vsync
EndSection

Section "Screen"
    Identifier     "Screen0"
    Device         "Card0"
    Monitor        "Monitor0"
    SubSection "Display"
        Modes       "1280x1024_60.00"
    EndSubSection
EndSection

Section "Device"
    Identifier    "Card0"
    Driver        "intel"
EndSection 

2) Restart Ubuntu

That's it.


Notes :

a) To get Modeline use following command:

cvt <h-resolution> <v-resolution>

e.g $ cvt 1280 1024

it will return : Modeline "1280x1024_60.00"  109.00  1280 1368 1496 1712  1024 1027 1034 1063 -hsync +vsync

b) To get Device Driver use following command :

lshw -class display | grep "driver"

For intel graphic chip driver is intel only.



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 :)