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.

3

How to allow manager to access his employees or his department only?

By
Elmasry
on 3/28/13, 11:28 AM 8,018 views

I'm new in openERP and i use openERP V7

i have hr manager M1 and under him 3 employees emp1 , emp2 and emp3

and another hr manager M2 under him >> emp4 , emp5 , emp6

i want M1 just see Timesheets of himself and emp1 , emp2 and emp3 only

and M2 just see Timesheets of himself and emp4 , emp5 , emp6

i tried to use record rules but i couldn't create the expression right

also if i want to let him access his department only what can i do?

any one can help me please

thanks to all in advance

anyone can help me?

the answer work with "Timesheets to Validate" menu only

and now I want to do same record rule on "Timesheet Activities" menu but no result I try to add this line to hr.analytic.timesheet model 'employee_id': fields.many2one('hr.employee', 'Employee', required=True), add the result was that no records in tree.


please help me to understand how to do it and explain me in more detaisls how can i create domains like that easily.

Hello. Did u managed to resolve this problem ?

"I try to add this line to hr.analytic.timesheet model 'employee_id': fields.many2one('hr.employee', 'Employee', required=True)"

Where did you modify this. What file path... or menu ?

Mihalache Ciprian
on 12/19/13, 6:40 AM
3

saad

--saad--
1008
| 5 4 8
Casablanca, Morocco
--saad--
saad
On 3/28/13, 3:11 PM

For Timesheets access, add a record rule with ['|',('employee_id.user_id','=',user.id),('employee_id.parent_id.user_id','=',user.id)] in domain filter field.

Adding the employee_id field is not sufficient, you should fill it by overriding the on_change_user_id method, I suppose that your employees are linked to users and you have added employee_id field in hr.analytic.timesheet form view and tree view.

In .py file:

def on_change_user_id(self, cr, uid, ids, user_id):
    if not user_id:
        return {}
    context = {'user_id': user_id}
    emp = self.pool.get("hr.employee").search(cr,uid,[('user_id','=',user_id)])
    return {'value': {
        'product_id': self. _getEmployeeProduct(cr, uid, context),
        'product_uom_id': self._getEmployeeUnit(cr, uid, context),
        'general_account_id': self._getGeneralAccount(cr, uid, context),
        'journal_id': self._getAnalyticJournal(cr, uid, context),
        'employee_id': emp and emp[0],
    }}

You should also fill the employee_id field when you are creating Timesheet Activities form timesheet form by overrinding the create method:

def create(self, cr, uid, vals, context=None):
    if context is None:
        context = {}
    emp_obj = self.pool.get('hr.employee')
    emp_id = emp_obj.search(cr, uid, [('user_id', '=', context.get('user_id') or uid)], context=context)
    vals['employee_id'] = emp_id and emp_id[0]
    ename = ''
    if emp_id:
        ename = emp_obj.browse(cr, uid, emp_id[0], context=context).name
    if not vals.get('journal_id',False):
       raise osv.except_osv(_('Warning!'), _('No \'Analytic Journal\' is defined for employee %s \nDefine an employee for the selected user and assign an \'Analytic Journal\'!')%(ename,))
    if not vals.get('account_id',False):
       raise osv.except_osv(_('Warning!'), _('No analytic account is defined on the project.\nPlease set one or we cannot automatically fill the timesheet.'))
    return super(hr_analytic_timesheet, self).create(cr, uid, vals, context=context)

At this moment, the same rule can be applied on Timesheet Activities and Timesheets

thank you very match saad

please could u explain me ho to create expression like that , i can do it but dt understand how and can't know the attributes that i can work on it, how can i know them.

i will be so thankfull if u help me more to learn it

Elmasry
on 3/29/13, 2:39 PM

Look on my updated answer.

saad
on 4/4/13, 7:49 AM

thanks very much Saad i'm so happy to see your update but after i do that i faced problem that the timesheets and which created from My Timesheet form is not filtered right under timesheet activities. have u any idea why?! can i connect u through linked in , Gmail or facebook?!

Elmasry
on 4/7/13, 7:43 AM

yes, the 'onchange_user_id' method is not triggered when you create timesheet activities from timesheet form, so the 'employee_id' field remains empty. You should manually add a value for employee_id in the create method. I will update my answer to be more complete. You can find me in the french community or moroccan community.

saad
on 4/8/13, 8:44 AM

thanks my Saad i tested your code before update but "change_user_id" is already exist but i understand your code and what you aim to so i do it by my way and it is run correctly. you update is very good but still have logical error cuz the user_id in creat method 'll be always the user id of the user who log in system .. so if manger create timesheets for his employee so the employee id 'll be id of manger not employee.

Elmasry
on 4/8/13, 9:34 AM

so we have 3 steps to avoid it

1- add global variable --> _employee=0 ..... 2- assign employee id to it in onchange_user_id method --> self._employee = self.pool.get("hr.employee").search(cr,uid,[('user_id','=',user_id)]) .... 3- add it to vals in create method --> vals['employee_id']=self._employee ... and if you have Gmail or linked in or FB it 'll be great thanks brother

Elmasry
on 4/8/13, 9:41 AM

did anyone managed to do this ? Please add a video support on youtube. Thanks.

Mihalache Ciprian
on 9/18/13, 4:49 AM

I modified the file hr_timesheet.py and restarted openerp server. I added this rule on timesheet activities ['|', '|', ('employee_id.user_id','=',user.id),('message_follower_ids','in',[user.partner_id.id]),('employee_id.parent_id.user_id','=',user.id)] but I get this error invalid field 'employee_id.user_id' in leaf

Mihalache Ciprian
on 12/19/13, 4:16 AM

"and you have added employee_id field in hr.analytic.timesheet form view and tree view"

How to add this because on this model you cannot add employee_id ?

http://i.imgur.com/Ho5TKhT.jpg

Mihalache Ciprian
on 12/19/13, 5:01 AM
6
Nimesh Contractor
On 4/1/13, 3:02 PM

To show his own department you can set the domain on the department field

and access rule may be like as follows..

<record id="emp_rule_personal_record" model="ir.rule">
    <field name="name">Personal Leads</field>
    <field ref="model_yourobject" name="model_id"/>
    <field name="domain_force">['|',('employee_id.user_id','=',user.id),('employee_id.parent_id.user_id','=',user.id),('user_id','=',False)]</field>
    <field name="groups" eval="[(4, ref('yourGroupRef'))]"/>
</record>

Hope this will help you..

my friend this domain dt work with "Timesheet Activities" menu cuz no employee_id field in the model and when i created it manualy i find it empty field and could u explain me how i can create the domain

Elmasry
on 4/1/13, 9:03 PM

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

2 follower(s)

Stats

Asked: 3/28/13, 11:28 AM
Seen: 8018 times
Last updated: 3/16/15, 8:10 AM