This question has been flagged
14 Replies
23215 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.

Avatar
Discard

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 ?

Best Answer

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

Avatar
Discard
Author

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

Look on my updated answer.

Author

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?!

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.

Author

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.

Author

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

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

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

"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

Best Answer

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..

Avatar
Discard
Author

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

Best Answer

Is it possible to do this with roles? If so, how? 

Avatar
Discard