Debian Codegraph @

addons/hr_holidays/tests/test_hr_departure_wizard.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from datetime import date, timedelta
3
4 from odoo import Command
5 from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon
6
7
8 class TestHolidaysFlow(TestHrHolidaysCommon):
9 @classmethod
10 def setUpClass(cls):
11 super().setUpClass()
12 cls.employee = cls.env['hr.employee'].create({
13 'name': 'Sky'
14 })
15 cls.departure_date = date.today()
16 departure_reason = cls.env['hr.departure.reason'].create({'name': "Fired"})
17 cls.departure_wizard = cls.env['hr.departure.wizard'].create({
18 'departure_reason_id': departure_reason.id,
19 'departure_date': cls.departure_date,
20 'employee_ids': [Command.link(cls.employee.id)],
21 })
22 cls.leave_type = cls.env['hr.leave.type'].create({
23 'name': 'Paid Time Off',
24 'time_type': 'leave',
25 'requires_allocation': False,
26 })
27
28 def test_departure_without_leave_and_allocation_employee(self):
29 self._check_action_departure()
30
31 def test_departure_leave_before_departure_date(self):
32 leave = self.env['hr.leave'].with_context(leave_fast_create=True).create({
33 'employee_id': self.employee.id,
34 'holiday_status_id': self.leave_type.id,
35 'request_date_from': self.departure_date + timedelta(days=-6),
36 'request_date_to': self.departure_date,
37 })
38 leave.state = 'validate'
39
40 self._check_action_departure()
41
42 def test_departure_leave_after_departure_date(self):
43 leave = self.env['hr.leave'].with_context(leave_fast_create=True).create({
44 'employee_id': self.employee.id,
45 'holiday_status_id': self.leave_type.id,
46 'request_date_from': self.departure_date + timedelta(days=6),
47 'request_date_to': self.departure_date + timedelta(days=8),
48 })
49 leave.state = 'validate'
50
51 self._check_action_departure()
52
53 def test_departure_leave_with_departure_date(self):
54 leave = self.env['hr.leave'].with_context(leave_fast_create=True).create({
55 'employee_id': self.employee.id,
56 'holiday_status_id': self.leave_type.id,
57 'request_date_from': self.departure_date + timedelta(days=-6),
58 'request_date_to': self.departure_date + timedelta(days=8),
59 })
60 leave.state = 'validate'
61
62 self._check_action_departure()
63 message = "<p>End date has been updated because the employee will leave the company on %(departure_date)s.</p>" % {
64 'departure_date': self.departure_date}
65 self.assertTrue(message in leave.message_ids.mapped('body'))
66
67 cancel_message = "<p>The time off request has been cancelled for the following reason:</p><p>The employee will leave the company on %(departure_date)s.</p>" % {
68 'departure_date': self.departure_date
69 }
70 self.assertTrue(cancel_message in self.env['hr.leave'].search([
71 ('request_date_from', '=', self.departure_date + timedelta(days=1)),
72 ('request_date_to', '=', self.departure_date + timedelta(days=8)),
73 ("employee_id", "=", self.employee.id)
74 ]).message_ids.mapped('body'))
75
76 def test_departure_allocation_before_departure_date(self):
77 self.env['hr.leave.allocation'].create([{
78 'name': 'allocation',
79 'holiday_status_id': self.leave_type.id,
80 'number_of_days': 15,
81 'employee_id': self.employee.id,
82 'state': 'confirm',
83 'date_from': self.departure_date + timedelta(days=-10),
84 'date_to': self.departure_date,
85 }]).action_approve()
86 self._check_action_departure()
87
88 def test_departure_allocation_after_departure_date(self):
89 self.env['hr.leave.allocation'].create([{
90 'name': 'allocation',
91 'holiday_status_id': self.leave_type.id,
92 'number_of_days': 15,
93 'employee_id': self.employee.id,
94 'state': 'confirm',
95 'date_from': self.departure_date + timedelta(days=1),
96 'date_to': self.departure_date + timedelta(days=10),
97 }]).action_approve()
98 self._check_action_departure()
99
100 def test_departure_allocation_with_departure_date(self):
101 allocation = self.env['hr.leave.allocation'].create([{
102 'name': 'allocation',
103 'holiday_status_id': self.leave_type.id,
104 'number_of_days': 15,
105 'employee_id': self.employee.id,
106 'state': 'confirm',
107 'date_from': self.departure_date + timedelta(days=-10),
108 'date_to': self.departure_date + timedelta(days=10),
109 }])
110 allocation.action_approve()
111 self._check_action_departure()
112
113 allocation_msg = '<p>Validity End date has been updated because the employee will leave the company on %(departure_date)s.</p>' % {
114 'departure_date': self.departure_date
115 }
116 self.assertTrue(allocation_msg in allocation.message_ids.mapped('body'))
117
118 def _check_action_departure(self):
119 self.departure_wizard.action_register_departure()
120 self._check_employee_allocation()
121 self._check_employee_leave()
122
123 def _check_employee_leave(self):
124 leaves_after_departure_date = self.env['hr.leave'].search([
125 ('employee_id', '=', self.employee.id),
126 ('date_from', '>', self.departure_date),
127 ('state', '!=', 'cancel')
128 ])
129
130 self.assertFalse(leaves_after_departure_date)
131 leaves_before_departure_date = self.env['hr.leave'].search([
132 ('employee_id', '=', self.employee.id),
133 ('date_from', '<=', self.departure_date),
134 ])
135 self.assertFalse(any(leave.date_to.date() > self.departure_date for leave in leaves_before_departure_date))
136
137 def _check_employee_allocation(self):
138 allocations = self.env['hr.leave.allocation'].search([
139 ('employee_id', '=', self.employee.id),
140 '|',
141 ('date_from', '>', self.departure_date),
142 ('date_to', '>', self.departure_date),
143 ])
144 self.assertFalse(allocations)