| 1 |
|
| 2 |
|
| 3 |
from datetime import timedelta |
| 4 |
|
| 5 |
from odoo import _, models |
| 6 |
|
| 7 |
|
| 8 |
class HrDepartureWizard(models.TransientModel): |
| 9 |
_inherit = 'hr.departure.wizard' |
| 10 |
|
| 11 |
def action_register_departure(self): |
| 12 |
action = super().action_register_departure() |
| 13 |
employee_leaves = self.env['hr.leave'].search([ |
| 14 |
('employee_id', 'in', self.employee_ids.ids), |
| 15 |
('date_to', '>', self.departure_date), |
| 16 |
]) |
| 17 |
|
| 18 |
if employee_leaves: |
| 19 |
leaves_with_departure = employee_leaves.filtered( |
| 20 |
lambda leave: leave.date_from.date() <= self.departure_date) |
| 21 |
leaves_after_departure = employee_leaves - leaves_with_departure |
| 22 |
|
| 23 |
new_leaves = leaves_with_departure._split_leaves( |
| 24 |
split_date_from=(self.departure_date + timedelta(days=1))) |
| 25 |
|
| 26 |
changes_leaves = leaves_with_departure.filtered(lambda leave: leave.date_to.date() <= self.departure_date) |
| 27 |
changes_msg = _('End date has been updated because ' |
| 28 |
'the employee will leave the company on %(departure_date)s.', |
| 29 |
departure_date=self.departure_date |
| 30 |
) |
| 31 |
for leave in changes_leaves: |
| 32 |
leave.message_post(body=changes_msg, message_type="comment", subtype_xmlid="mail.mt_comment") |
| 33 |
|
| 34 |
|
| 35 |
leaves_after_departure |= leaves_with_departure - changes_leaves |
| 36 |
leaves_after_departure |= new_leaves |
| 37 |
leaves_to_cancel = leaves_after_departure.filtered(lambda leave: leave.state in ['validate', 'validate1']) |
| 38 |
cancel_msg = _('The employee will leave the company on %(departure_date)s.', |
| 39 |
departure_date=self.departure_date) |
| 40 |
leaves_to_cancel._force_cancel(cancel_msg, notify_responsibles=False) |
| 41 |
|
| 42 |
leaves_to_delete = leaves_after_departure - leaves_to_cancel |
| 43 |
leaves_to_delete.with_context(leave_skip_state_check=True).unlink() |
| 44 |
|
| 45 |
employee_allocations = self.env['hr.leave.allocation'].search([ |
| 46 |
('employee_id', 'in', self.employee_ids.ids), |
| 47 |
'|', |
| 48 |
('date_to', '=', False), |
| 49 |
('date_to', '>', self.departure_date), |
| 50 |
]) |
| 51 |
if not employee_allocations: |
| 52 |
return action |
| 53 |
to_delete = self.env['hr.leave.allocation'] |
| 54 |
to_modify = self.env['hr.leave.allocation'] |
| 55 |
allocation_msg = _('Validity End date has been updated because ' |
| 56 |
'the employee will leave the company on %(departure_date)s.', |
| 57 |
departure_date=self.departure_date |
| 58 |
) |
| 59 |
for allocation in employee_allocations: |
| 60 |
if allocation.date_from > self.departure_date: |
| 61 |
to_delete |= allocation |
| 62 |
else: |
| 63 |
to_modify |= allocation |
| 64 |
allocation.message_post(body=allocation_msg, subtype_xmlid='mail.mt_comment') |
| 65 |
to_delete.with_context(allocation_skip_state_check=True).unlink() |
| 66 |
to_modify.date_to = self.departure_date |
| 67 |
|
| 68 |
return action |