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

Sunday, 5 October 2014

Odoo/OpenERP: one2one relational field example

one2one relational field is deprecated in OpenERP version>5 but you can achieve the same using many2one relational field. You can achieve it in following two ways :

1) using many2one field in both the objects (http://tutorialopenerp.wordpress.com/2014/04/23/one2one/)

2)  using inheritance by deligation

You can easily find the first solution with little search over internet so let's start with 2nd solution.

Scenario : 

I want to create a one2one relation between two objects of openerp

hr.employee

and

hr.employee.medical.details

What I should do 

i. Add _inherits section in hr_employee class
ii. Add field medical_detail_id in hr_employee class

class hr_employee(osv.osv):
    _name = 'hr.employee'
    _inherits = {'hr.employee.medical.details': "medical_detail_id"}
    _inherit = 'hr.employee'    
    _columns = {
             'emp_code':fields.char('Employee Code', size=6, readonly=True),
             'medical_detail_id':fields.many2one('hr.employee.medical.details', 'Medical       
             Details',help="Link this employee to it's medical details", ondelete="cascade", required=True),
             ............
    }

You don't have to do anything with 'hr.employee.medical.details' object

iii. Add fields in hr_employee_view.xml file

<group col="4" string="Medical Details">            
    <field name="medical_detail_id" invisible="1" required="0" colspan="4" />
    <field name="blood_group" />
    <field name="medicine" />
    <field name="medicine_desc" />
    <field name="allergies" />
    <field name="allergies_desc" />
</group>

and you are done. It will create exactly one record each employee entry in
hr.employee.medical.details table.

Please let me know if you are facing any issue to implement it.


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