跳至內容
選單
此問題已被標幟
7 回覆
8142 瀏覽次數

Hii,

I have added one extra level of approval that is substitute employee when any employee creates leave request in Leaves App in statusbar.

state = fields.Selection([
        ('draft', 'To Submit'),
('cancel', 'Cancelled'),
('confirm', 'To Approve'),
('refuse', 'Refused'),
('validate_substitute', 'Substitute Approval'), # Additional level of approval, customization
('validate1', 'Second Approval'),
('validate', 'Approved')])

After substitute employee approval, when clicking on Approve button getting following error..

Leave request must be confirmed ("To Approve") in order to approve it.

This error message is coming from following method in hr_leave.py class.... 

@api.multi
    def action_approve(self):
# if validation_type == 'both': this method is the first approval approval
# if validation_type != 'both': this method calls action_validate() below
if any(holiday.state != 'confirm' for holiday in self):
raise UserError(_('Leave request must be confirmed ("To Approve") in order to approve it.'))
current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
self.filtered(lambda hol: hol.validation_type == 'both').write({'state': 'validate1', 'first_approver_id': current_employee.id})
self.filtered(lambda hol: not hol.validation_type == 'both').action_validate()
if not self.env.context.get('leave_fast_create'):
self.activity_update()
return True

In this method I want to change 'confirm' to 'validate_substitute' like following

if any(holiday.state != 'validate_substitute' for holiday in self):

How I can achieve it in method overriding? 
Or should I write it complete in my inherited class?

Thanks,

頭像
捨棄
最佳答案

Hi,

There are two things you can do,

1. To rewrite the entire function by inheriting the corresponding model in your custom module. For this in the python file, inherit the corresponding model and copy the function into it and make necessary changes where ever you needed.


2. This method is to super the original function so that the both the function will get executed. For supering/overriding the existing function, see this:  Overriding method in a model


Example:

class HRLeave(models.Model):
_inherit = 'hr.leave'

@api.multi
def action_approve(self):

# add custom codes here
        
       res = super(HRLeave, self).action_approve()
return res

In this the second method is the recommend one.

Thanks

頭像
捨棄
作者

Hii @Niyas Raphy.

Thanks for the reply

Here is my code in inherited calss

class HrLeave(models.Model):

_inherit = 'hr.leave'

@api.multi

def action_approve(self):

record = super(HrLeave, self).action_approve()

record['state'] = 'confirm'

logging.warning("=======Called action_approve Method========")

return record

But it is never called...

logging.warning is not printed on terminal

record['state'] = 'confirm' is not set

What I am missing?

Thanks,

make sure that the python file is called in init file and also make sure that the module contain this code is installed

作者

Yes, the file is being called in the init. There are some other methods on the file working fine. Module containing this code is also installed.

This has to be worked, i think debug the code from your side and see.

作者 最佳答案

Its working

@api.multi
def action_confirm(self): record = super(HrLeave, self).action_confirm() if self.message_main_attachment_id.id is False and self.holiday_status_id.time_type == 'full_day_training': raise ValidationError("Please attach document for leave type: %s" % self.holiday_status_id.name) return record


頭像
捨棄
最佳答案

Hello, 

For any method override, you can use a super method.

    @api.multi
def action_approve(self):
res = super(Class Name, self).action_approve()
# add your condition
return res


頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
4
5月 24
7304
1
7月 23
2396
3
6月 23
3278
2
6月 23
2334
4
6月 22
5019