如何仅限制团队经理的批准动作

时间:2019-06-24 15:49:12

标签: python-3.x odoo odoo-11

我使用的是odoo11,我安装了一个自定义模块来计算加班时间,我有很多团队负责人,每个团队负责人都有很多员工。

我想限制经理的批准动作,使每个经理仅批准自己员工的请求。

我已覆盖批准动作,以核实被批准的人是否是他的经理,但始终允许任何经理批准所有请求。这是代码:

overtime.py

class BtHrOvertime(models.Model):
_name = "bt.hr.overtime"
_description = "Bt Hr Overtime"
_rec_name = 'employee_id'
_order = 'id desc'

employee_id = fields.Many2one('hr.employee', string="Employee")
manager_id = fields.Many2one('hr.employee', string='Manager')
start_date = fields.Datetime('Date check in')
end_date = fields.Datetime('Date check out')
overtime_hours = fields.Float('Overtime Hours')
notes = fields.Text(string='Notes')
state = fields.Selection([('draft', 'Draft'), ('confirm', 'Waiting for Approval'), ('refuse', 'Refused'),
                          ('validate', 'Approved'), ('cancel', 'Cancelled')], default='draft', copy=False)
attendance_id = fields.Many2one('hr.attendance', string='Attendance')
user_id = fields.Many2one('res.users', string='User', related='employee_id.user_id', related_sudo=True,
                          compute_sudo=True, store=True, default=lambda self: self.env.uid, readonly=True)
parent_id = fields.Many2one('bt.hr.overtime', string='Parent', copy=False)

@api.multi
def action_approve(self):
    if (self.employee_id.parent_id.user_id.id != uid):
        raise exceptions.UserError(_('You cannot approve.'))
    elif  self.attendance_id.check_in and self.attendance_id.check_out:
            start_date = datetime.datetime.strptime(self.attendance_id.check_in, DEFAULT_SERVER_DATETIME_FORMAT)
            end_date = datetime.datetime.strptime(self.attendance_id.check_out, DEFAULT_SERVER_DATETIME_FORMAT)
            difference = end_date - start_date
            hour_diff = str(difference).split(':')[0]
            min_diff = str(difference).split(':')[1]
            tot_diff = hour_diff + '.' + min_diff
            actual_working_hours = float(tot_diff)
            for record in self:
                if actual_working_hours > 9:
                    record.overtime_hours = actual_working_hours - 9
                    record.start_date = record.attendance_id.check_in
                    record.end_date = record.attendance_id.check_out
                else:
                    record.overtime_hours = 0
                    record.start_date = record.attendance_id.check_in
                    record.end_date = record.attendance_id.check_out
    return self.write({'state': 'validate'})

1 个答案:

答案 0 :(得分:0)

请使用以下代码或进行检查:

if (self.employee_id.parent_id.user_id.id != self.env.user.id):
    raise exceptions.UserError(_('You cannot approve.'))

我认为employee_id.parent_id是员工的经理。另外请告知您可以创建一个新的用户组

    <record id="group_is_manager" model="res.groups">
        <field name="name">Is a manager?</field>
    </record>

对于Manager用户,您可以检查该组并为该组提供批准按钮

<button name="" string="" type="" groups="module_name.group_is_manager"/>

因此,具有该组的用户将看到该按钮。