Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
1 Ответить
2690 Представления

I want add a rule create time off for employees.
But when I check base 'hr_holidays', must to have allow this employee is Time off/ Officer: Manage all requests or this employee is leave manager.


def write(self, values):
​if 'active' in values and not self.
env.context.get('from_cancel_wizard'):
​raise UserError(_("You can't manually archive/unarchive a time off."))

​is_officer = self.env.user.has_group('hr_holidays.group_hr_holidays_user') or self.env.is_superuser()
​if not
is_officer and values.keys() - {'attachment_ids', 'supported_attachment_ids', 'message_main_attachment_id'}:
​if any(
hol.date_from.date() < fields.Date.today() and hol.employee_id.leave_manager_id != self.env.user for hol in self):
​raise UserError(_('You must have manager rights to modify/validate a time off that already begun'))

​# Unlink existing resource.calendar.leaves for validated time off
​if 'state' in values and values['state'] != 'validate':
​validated_leaves = self.filtered(lambda l: l.state == 'validate')
​validated_leaves._remove_resource_leave()

​employee_id = values.get('employee_id', False)
​if not self.
env.context.get('leave_fast_create'):
​​if values.get('state'):
​​self._check_approval_update(values['state'])
​if any(
holiday.validation_type == 'both' for holiday in self):
​if values.get('employee_id'):
​employees = self.env['hr.employee'].browse(values.get('employee_id'))
​​else:
​​employees = self.mapped('employee_id')
​self._check_double_validation_rules(
employees, values['state'])
​if 'date_from' in values:
​​values['request_date_from'] = values['date_from']
​​if 'date_to' in values:
​​values['request_date_to'] = values['date_to']
​result = super(HolidaysRequest, self).write(values)
​if not self.
env.context.get('leave_fast_create'):
​​for
holiday in self:
​​if
employee_id:
​​holiday.add_follower(employee_id)

​return
result

I want to allow create time off in the past for all employees (User internal).
Please help me!​

Аватар
Отменить
Лучший ответ

Hi,

You can try override it and remove this group check block like this:

class HolidaysRequest(models.Model):
​_inherit = "hr.leave"

​def write(self, values):
​if 'active' in values and not self.env.context.get('from_cancel_wizard'):
​ ​raise UserError(_("You can't manually archive/unarchive a time off."))

​# Unlink existing resource.calendar.leaves for validated time off
​if 'state' in values and values['state'] != 'validate':
​ ​validated_leaves = self.filtered(lambda l: l.state == 'validate')
​ ​validated_leaves._remove_resource_leave()

​employee_id = values.get('employee_id', False)
​if not self.env.context.get('leave_fast_create'):
​​if values.get('state'):
​ ​​self._check_approval_update(values['state'])
​ ​ ​if any(holiday.validation_type == 'both' for holiday in self):
​ ​ ​ ​if values.get('employee_id'):
​ ​ ​ ​ ​employees = self.env['hr.employee'].browse(values.get('employee_id'))
​ ​ ​​else:
​ ​ ​ ​​employees = self.mapped('employee_id')
​ ​ ​ ​self._check_double_validation_rules(employees, values['state'])
​ ​if 'date_from' in values:
​ ​​values['request_date_from'] = values['date_from']
​​if 'date_to' in values:
​ ​​values['request_date_to'] = values['date_to']
result = super(type(self.env['mail.thread']), self).write(values)
​if not self.env.context.get('leave_fast_create'):
​​for holiday in self:
​ ​​if employee_id:
​ ​ ​​holiday.add_follower(employee_id)

​return result

Hope it helps!

Аватар
Отменить
Автор

It don't work.
Because "​result = super(HolidaysRequest, self).write(values)" will call all write() method of model 'hr.leave'.
So, It still run write() in addons and raise UserError

do not call super()

Автор

In your code, there has ​"result = super(HolidaysRequest, self).write(values)"

Hi @Nam Nguyen Phuong, you're right, I thought you called super() from your custom function and forgot check whole function. I just edited my answer.
Thanks!

Автор

Hi @Jason Vu, Thank your solution!
We are checking if your solution is suitable.

We have question for your code (result = super(type(self.env['mail.thread']), self).write(values))
What is it for ? Does it have any effect on other logic ?

Hi @Nam Nguyen Phuong, the origin class 'HolidaysRequest' inherit 'mail.thread' which means super(HolidaysRequest,self).write() call 'mail.thread'.write() function.
This function is used to log the change into the chatter.

Автор

Hi @Jason Vu, If so, will the other write function of model 'hr.leave' be affected?

Hi, this class has only 1 write() function, if you worry about other functions it's fine, it only affects the overridden function

Автор

Thanks your support!
We are checking.

Related Posts Ответы Просмотры Активность
1
июн. 23
2113
2
мая 25
1626
0
авг. 23
2035
2
авг. 24
9048
5
июн. 25
8175