1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import datetime
from dateutil.relativedelta import relativedelta
from freezegun import freeze_time
from odoo import fields, SUPERUSER_ID
from odoo.exceptions import UserError
from odoo.tests import common, new_test_user
from odoo.addons.hr_timesheet.tests.test_timesheet import TestCommonTimesheet
import time
class TestTimesheetHolidaysCreate(common.TransactionCase):
def test_status_create(self):
"""Ensure that when a status is created, it fullfills the project and task constrains"""
status = self.env['hr.leave.type'].create({
'name': 'A nice Time Off Type',
'requires_allocation': 'no'
})
self.assertEqual(status.timesheet_project_id, status.company_id.internal_project_id, 'The default project linked to the status should be the same as the company')
self.assertEqual(status.timesheet_task_id, status.company_id.leave_timesheet_task_id, 'The default task linked to the status should be the same as the company')
def test_company_create(self):
main_company = self.env.ref('base.main_company')
user = new_test_user(self.env, login='fru',
groups='base.group_user,base.group_erp_manager,base.group_partner_manager',
company_id=main_company.id,
company_ids=[(6, 0, main_company.ids)])
Company = self.env['res.company']
Company = Company.with_user(user)
Company = Company.with_company(main_company)
company = Company.create({'name': "Wall Company"})
self.assertEqual(company.internal_project_id.sudo().company_id, company, "It should have created a project for the company")
class TestTimesheetHolidays(TestCommonTimesheet):
def setUp(self):
super(TestTimesheetHolidays, self).setUp()
self.employee_working_calendar = self.empl_employee.resource_calendar_id
# leave dates : from next monday to next wednesday (to avoid crashing tests on weekend, when
# there is no work days in working calendar)
# NOTE: second and millisecond can add a working days
self.leave_start_datetime = datetime(2018, 2, 5) # this is monday
self.leave_end_datetime = self.leave_start_datetime + relativedelta(days=2)
# all company have those internal project/task (created by default)
self.internal_project = self.env.company.internal_project_id
self.internal_task_leaves = self.env.company.leave_timesheet_task_id
self.hr_leave_type_with_ts = self.env['hr.leave.type'].create({
'name': 'Time Off Type with timesheet generation',
'requires_allocation': 'no',
'timesheet_generate': True,
'timesheet_project_id': self.internal_project.id,
'timesheet_task_id': self.internal_task_leaves.id,
})
self.hr_leave_type_no_ts = self.env['hr.leave.type'].create({
'name': 'Time Off Type without timesheet generation',
'requires_allocation': 'no',
'timesheet_generate': False,
'timesheet_project_id': False,
'timesheet_task_id': False,
})
# HR Officer allocates some leaves to the employee 1
self.Requests = self.env['hr.leave'].with_context(mail_create_nolog=True, mail_notrack=True)
self.Allocations = self.env['hr.leave.allocation'].with_context(mail_create_nolog=True, mail_notrack=True)
self.hr_leave_allocation_with_ts = self.Allocations.sudo().create({
'name': 'Days for limited category with timesheet',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_with_ts.id,
'number_of_days': 10,
'state': 'confirm',
'date_from': time.strftime('%Y-01-01'),
'date_to': time.strftime('%Y-12-31'),
})
self.hr_leave_allocation_no_ts = self.Allocations.sudo().create({
'name': 'Days for limited category without timesheet',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_no_ts.id,
'number_of_days': 10,
'state': 'confirm',
'date_from': time.strftime('%Y-01-01'),
'date_to': time.strftime('%Y-12-31'),
})
def test_validate_with_timesheet(self):
# employee creates a leave request
holiday = self.Requests.with_user(self.user_employee).create({
'name': 'Time Off 1',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_with_ts.id,
'request_date_from': self.leave_start_datetime,
'request_date_to': self.leave_end_datetime,
})
holiday.with_user(SUPERUSER_ID).action_validate()
# The leave type and timesheet are linked to the same project and task of hr_leave_type_with_ts as the company is set
self.assertEqual(holiday.timesheet_ids.project_id.id, self.hr_leave_type_with_ts.timesheet_project_id.id)
self.assertEqual(holiday.timesheet_ids.task_id.id, self.hr_leave_type_with_ts.timesheet_task_id.id)
self.assertEqual(len(holiday.timesheet_ids), holiday.number_of_days, 'Number of generated timesheets should be the same as the leave duration (1 per day between %s and %s)' % (fields.Datetime.to_string(self.leave_start_datetime), fields.Datetime.to_string(self.leave_end_datetime)))
# manager refuse the leave
holiday.with_user(SUPERUSER_ID).action_refuse()
self.assertEqual(len(holiday.timesheet_ids), 0, 'Number of linked timesheets should be zero, since the leave is refused.')
company = self.env['res.company'].create({"name": "new company"})
self.empl_employee.write({
"company_id": company.id,
})
hr_leave_type_with_ts_without_company = self.hr_leave_type_with_ts.copy()
hr_leave_type_with_ts_without_company.write({
'company_id': False,
})
self.assertTrue(hr_leave_type_with_ts_without_company.timesheet_generate)
holiday = self.Requests.create({
'name': 'Time Off 2',
'employee_id': self.empl_employee.id,
'holiday_status_id': hr_leave_type_with_ts_without_company.id,
'request_date_from': self.leave_start_datetime,
'request_date_to': self.leave_end_datetime,
})
holiday.with_user(SUPERUSER_ID).action_validate()
# The leave type and timesheet are linked to the same project and task of the employee company as the company is not set
self.assertEqual(holiday.timesheet_ids.project_id.id, company.internal_project_id.id)
self.assertEqual(holiday.timesheet_ids.task_id.id, company.leave_timesheet_task_id.id)
def test_validate_without_timesheet(self):
# employee creates a leave request
holiday = self.Requests.with_user(self.user_employee).create({
'name': 'Time Off 1',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_no_ts.id,
'request_date_from': self.leave_start_datetime,
'request_date_to': self.leave_end_datetime,
})
holiday.with_user(SUPERUSER_ID).action_validate()
self.assertEqual(len(holiday.timesheet_ids), 0, 'Number of generated timesheets should be zero since the leave type does not generate timesheet')
@freeze_time('2018-02-05') # useful to be able to cancel the validated time off
def test_cancel_validate_holidays(self):
holiday = self.Requests.with_user(self.user_employee).create({
'name': 'Time Off 1',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_with_ts.id,
'request_date_from': self.leave_start_datetime,
'request_date_to': self.leave_end_datetime,
})
holiday.with_user(self.env.user).action_validate()
self.assertEqual(len(holiday.timesheet_ids), holiday.number_of_days, 'Number of generated timesheets should be the same as the leave duration (1 per day between %s and %s)' % (fields.Datetime.to_string(self.leave_start_datetime), fields.Datetime.to_string(self.leave_end_datetime)))
self.env['hr.holidays.cancel.leave'].with_user(self.user_employee).with_context(default_leave_id=holiday.id) \
.new({'reason': 'Test remove holiday'}) \
.action_cancel_leave()
self.assertEqual(holiday.state, 'cancel', 'The time off should be archived')
self.assertEqual(len(holiday.timesheet_ids), 0, 'The timesheets generated should be unlink.')
def test_timesheet_time_off_including_public_holiday(self):
""" Generate one timesheet for the public holiday and 4 timesheets for the time off.
Test Case:
=========
1) Create a public time off on Wednesday
2) In the same week, create a time off during one week for an employee
3) Check if there are five timesheets generated for time off and public
holiday.4 timesheets should be linked to the time off and 1 for
the public one.
"""
leave_start_datetime = datetime(2022, 1, 24, 7, 0, 0, 0) # Monday
leave_end_datetime = datetime(2022, 1, 28, 18, 0, 0, 0)
# Create a public holiday
self.env['resource.calendar.leaves'].create({
'name': 'Test',
'calendar_id': self.employee_working_calendar.id,
'date_from': datetime(2022, 1, 26, 7, 0, 0, 0), # This is Wednesday and India Independence
'date_to': datetime(2022, 1, 26, 18, 0, 0, 0),
})
holiday = self.Requests.with_user(self.user_employee).create({
'name': 'Time Off 1',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_with_ts.id,
'request_date_from': leave_start_datetime,
'request_date_to': leave_end_datetime,
})
holiday.with_user(SUPERUSER_ID).action_validate()
self.assertEqual(len(holiday.timesheet_ids), 4, '4 timesheets should be generated for this time off.')
timesheets = self.env['account.analytic.line'].search([
('date', '>=', leave_start_datetime),
('date', '<=', leave_end_datetime),
('employee_id', '=', self.empl_employee.id),
])
# should not able to update timeoff timesheets
with self.assertRaises(UserError):
timesheets.with_user(self.empl_employee).write({'task_id': 4})
# should not able to create timesheet in timeoff task
with self.assertRaises(UserError):
self.env['account.analytic.line'].with_user(self.empl_employee).create({
'name': "my timesheet",
'project_id': self.internal_project.id,
'task_id': self.internal_task_leaves.id,
'date': '2021-10-04',
'unit_amount': 8.0,
})
self.assertEqual(len(timesheets.filtered('holiday_id')), 4, "4 timesheet should be linked to employee's timeoff")
self.assertEqual(len(timesheets.filtered('global_leave_id')), 1, '1 timesheet should be linked to global leave')
def test_delete_timesheet_after_new_holiday_covers_whole_timeoff(self):
""" User should be able to delete a timesheet created after a new public holiday is added,
covering the *whole* period of a existing time off.
Test Case:
=========
1) Create a Time off, approve and validate it.
2) Create a new Public Holiday, covering the whole time off created in step 1.
3) Delete the new timesheet associated with the public holiday.
"""
leave_start_datetime = datetime(2022, 1, 31, 7, 0, 0, 0) # Monday
leave_end_datetime = datetime(2022, 1, 31, 18, 0, 0, 0)
# (1) Create a timeoff and validate it
time_off = self.Requests.with_user(self.user_employee).create({
'name': 'Test Time off please',
'employee_id': self.empl_employee.id,
'holiday_status_id': self.hr_leave_type_with_ts.id,
'request_date_from': leave_start_datetime,
'request_date_to': leave_end_datetime,
})
time_off.with_user(SUPERUSER_ID).action_validate()
# (2) Create a public holiday
self.env['resource.calendar.leaves'].create({
'name': 'New Public Holiday',
'calendar_id': self.employee_working_calendar.id,
'date_from': datetime(2022, 1, 31, 5, 0, 0, 0), # Covers the whole time off
'date_to': datetime(2022, 1, 31, 23, 0, 0, 0),
})
# The timeoff should have been force_cancelled and its associated timesheet unlinked.
self.assertFalse(time_off.timesheet_ids, '0 timesheet should remain for this time off.')
# (3) Delete the timesheet
timesheets = self.env['account.analytic.line'].search([
('date', '>=', leave_start_datetime),
('date', '<=', leave_end_datetime),
('employee_id', '=', self.empl_employee.id),
])
# timesheet should be unlinked to the timeoff, and be able to delete it
timesheets.with_user(SUPERUSER_ID).unlink()
self.assertFalse(timesheets.exists(), 'Timesheet should be deleted')
|