This question has been flagged
1 Reply
15445 Views

i want to view button (from inherited form) based on bool field (based on function) to enable manager of employee to approve or refuse leave request of his employees
​views.xml


<record id="manager_approve_refuse_his_employees_leaves" model="ir.ui.view">
  <field name="name">Manager Approve Refuse His Employees Leaves</field>
  <field name="model">hr.holidays</field>
  <field name="inherit_id" ref="hr_holidays.edit_holiday_new"/>
  <field name="arch" type="xml">
    <xpath expr="//field[@name='state']" position="before">
      <field name="view_leaves" invisible="0"/>
    </xpath>
    <xpath expr="//button[@name='action_approve']" position="attributes">
      <attribute name="attrs">{'invisible': [('view_leaves' ,'=', False)]}</attribute>
    </xpath>
  </field>
</record>

models.py

class InheritHrHolidays(models.Model):
    _inherit = 'hr.holidays'

    view_leaves = fields.Boolean(compute="view_leaves_for_manager", store=False)
    @api.multi
    def view_leaves_for_manager(self):
        if self.env.uid == self.employee_id.parent_id.user_id.id:
            self.view_leaves = True

this is the original button

<button string="Approve" name="action_approve" states="confirm" type="object" groups="hr_holidays.group_hr_holidays_user" class="oe_highlight"/>

but button doesn't appear yet .. what is the problem


Avatar
Discard
Best Answer

You need to add @api.depends('employee_id'), once this employee_id the compute method will be called and to use api.multi you need to use loop with self

The @api.depends is necessary if the computation uses other fields: it tells the server when to recompute stored or cached values.

@api.multi 
@api.depends('employee_id')
def view_leaves_for_manager(self):
    for leave in self:                       
    if leave.env.uid == leave.employee_id.parent_id.user_id.id:
   leave.view_leaves = True
    else:
      leave.view_leaves = False

if you want to use api.multi

Avatar
Discard
Author

still doesn't appear because of groups attribute in original form

so i added:

<field name="groups" />

<field name="states" />

but it produced this error:

Only an HR Officer or Manager can approve leave requests.

can i fix it to employee without adding to HR Officer or Manager .

thanks for your response

Try to add employee group as below:

<xpath expr="//button[@name='action_approve']" position="attributes">

<attribute name="attrs">{'invisible': [('view_leaves' ,'=', False)]}</attribute>

<attribute name="groups">base.group_user</attribute>

</xpath>

or

<xpath expr="//button[@name='action_approve']" position="replace">

<button string="Approve" name="action_approve" states="confirm" type="object" groups="base.group_user" class="oe_highlight"

attrs="{'invisible': [('view_leaves' ,'=', False)]}"/>

</xpath>

Author

i tried two cases but still produces this error:

Only an HR Officer or Manager can approve leave requests.

can i redefine action_approve function in my custom module ?

or there is a solution ?