253 lines
9.4 KiB
Python
253 lines
9.4 KiB
Python
from odoo.tests.common import TransactionCase
|
|
from datetime import date
|
|
|
|
from odoo import fields
|
|
|
|
|
|
class TestAttendanceTransactionTiming(TransactionCase):
|
|
|
|
def setUp(self):
|
|
super(TestAttendanceTransactionTiming, self).setUp()
|
|
|
|
self.employee = self.env['hr.employee'].create({'name': 'Test Employee'})
|
|
self.calendar_full = self.env['resource.calendar'].create({
|
|
'name': 'Full Day Calendar',
|
|
'is_full_day': True,
|
|
'full_min_sign_in': 8.0,
|
|
'full_max_sign_in': 9.0,
|
|
'full_min_sign_out': 16.0,
|
|
'full_max_sign_out': 17.0,
|
|
'working_hours': 8.0,
|
|
'state': 'confirm',
|
|
})
|
|
self.calendar_shift1 = self.env['resource.calendar'].create({
|
|
'name': 'Shift 1 Calendar',
|
|
'is_full_day': False,
|
|
'shift_one_min_sign_in': 7.0,
|
|
'shift_one_max_sign_in': 8.0,
|
|
'shift_one_min_sign_out': 15.0,
|
|
'shift_one_max_sign_out': 16.0,
|
|
'shift_one_working_hours': 8.0,
|
|
'state': 'confirm',
|
|
})
|
|
self.employee.resource_calendar_id = self.calendar_full
|
|
|
|
def test_scenario1_lateness_full_day(self):
|
|
transaction = self.env['hr.attendance.transaction'].create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_full.id,
|
|
'date': date(2025, 1, 1),
|
|
'sign_in': 9.5,
|
|
'sign_out': 17.0,
|
|
'sequence': 1,
|
|
})
|
|
|
|
transaction.set_lateness_and_exit(transaction)
|
|
|
|
self.assertEqual(transaction.lateness, 0.5)
|
|
self.assertTrue(transaction.approve_lateness)
|
|
self.assertFalse(transaction.approve_exit_out)
|
|
|
|
def test_scenario2_early_exit_shift1(self):
|
|
transaction = self.env['hr.attendance.transaction'].create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_shift1.id,
|
|
'date': date(2025, 1, 2),
|
|
'sign_in': 7.5,
|
|
'sign_out': 14.0,
|
|
'sequence': 1,
|
|
})
|
|
|
|
transaction.set_lateness_and_exit(transaction)
|
|
|
|
self.assertEqual(transaction.early_exit, 1.5)
|
|
self.assertTrue(transaction.approve_exit_out)
|
|
self.assertFalse(transaction.approve_lateness)
|
|
|
|
|
|
|
|
class TestAttendanceAdditionalHours(TransactionCase):
|
|
|
|
def setUp(self):
|
|
super(TestAttendanceAdditionalHours, self).setUp()
|
|
self.employee = self.env['hr.employee'].sudo().create({'name': 'Test Employee'})
|
|
self.calendar = self.env['resource.calendar'].sudo().create({
|
|
'name': 'Test Calendar',
|
|
'working_hours': 0.0,
|
|
'working_days': 5,
|
|
'state': 'confirm',
|
|
})
|
|
|
|
def test_overtime_calculation(self):
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar.id,
|
|
'plan_hours': 8.0,
|
|
'office_hours': 10.0,
|
|
'sign_in': 8.0,
|
|
})
|
|
|
|
transaction.get_additional_hours()
|
|
|
|
transaction = self.env['hr.attendance.transaction'].sudo().browse(transaction.id)
|
|
|
|
expected = 2.0
|
|
self.assertEqual(transaction.additional_hours, expected)
|
|
|
|
def test_no_overtime(self):
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar.id,
|
|
'plan_hours': 8.0,
|
|
'office_hours': 7.0,
|
|
})
|
|
|
|
transaction.get_additional_hours()
|
|
transaction = self.env['hr.attendance.transaction'].sudo().browse(transaction.id)
|
|
|
|
self.assertEqual(transaction.additional_hours, 0.0)
|
|
|
|
def test_mission_hours(self):
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar.id,
|
|
'sign_in': False,
|
|
'plan_hours': 8.0,
|
|
'total_mission_hours': 9.0,
|
|
})
|
|
|
|
transaction.get_additional_hours()
|
|
transaction = self.env['hr.attendance.transaction'].sudo().browse(transaction.id)
|
|
|
|
self.assertEqual(transaction.office_hours, 9.0)
|
|
self.assertEqual(transaction.official_hours, 9.0)
|
|
self.assertEqual(transaction.additional_hours, 1.0)
|
|
|
|
def test_sign_in_zero_mission(self):
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar.id,
|
|
'sign_in': 0.0,
|
|
'plan_hours': 8.0,
|
|
'office_hours': 6.0,
|
|
})
|
|
|
|
transaction.get_additional_hours()
|
|
transaction = self.env['hr.attendance.transaction'].sudo().browse(transaction.id)
|
|
|
|
self.assertEqual(transaction.additional_hours, 0.0)
|
|
|
|
|
|
class TestDayTimingCalculation(TransactionCase):
|
|
|
|
def setUp(self):
|
|
super(TestDayTimingCalculation, self).setUp()
|
|
self.employee = self.env['hr.employee'].sudo().create({'name': 'Test Employee'})
|
|
|
|
self.calendar_full = self.env['resource.calendar'].sudo().create({
|
|
'name': 'Full Day Calendar',
|
|
'working_hours': 8.0,
|
|
'break_duration': 1.0,
|
|
'full_min_sign_in': 8.0,
|
|
'full_max_sign_in': 9.0,
|
|
'full_min_sign_out': 16.0,
|
|
'full_max_sign_out': 17.0,
|
|
'is_full_day': True,
|
|
})
|
|
|
|
self.calendar_shift = self.env['resource.calendar'].sudo().create({
|
|
'name': 'Shift Calendar',
|
|
'shift_one_working_hours': 4.0,
|
|
'shift_one_break_duration': 0.5,
|
|
'shift_one_min_sign_in': 6.0,
|
|
'shift_one_max_sign_in': 7.0,
|
|
'shift_one_min_sign_out': 10.0,
|
|
'shift_one_max_sign_out': 11.0,
|
|
'shift_two_working_hours': 4.0,
|
|
'shift_two_break_duration': 0.5,
|
|
'shift_two_min_sign_in': 14.0,
|
|
'shift_two_max_sign_in': 15.0,
|
|
'shift_two_min_sign_out': 18.0,
|
|
'shift_two_max_sign_out': 19.0,
|
|
'is_full_day': False,
|
|
})
|
|
|
|
def test_full_day_normal_timing(self):
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_full.id,
|
|
})
|
|
result = transaction.get_day_timing(self.calendar_full, 'monday', '2025-12-22')
|
|
time_list, planed_hours = result
|
|
self.assertEqual(planed_hours, {'one': 7.0, 'two': 0})
|
|
self.assertEqual(time_list, [8.0, 9.0, 16.0, 17.0])
|
|
|
|
def test_full_day_special_timing(self):
|
|
special_day = self.env['attendance.special.days'].sudo().create({
|
|
'special_days_attendance': self.calendar_full.id,
|
|
'name': 'monday',
|
|
'working_hours': 6.0,
|
|
'start_sign_in': 7.0,
|
|
'end_sign_in': 8.0,
|
|
'start_sign_out': 13.0,
|
|
'end_sign_out': 14.0,
|
|
'date_from': fields.Date.to_date('2025-12-22'),
|
|
})
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_full.id,
|
|
})
|
|
result = transaction.get_day_timing(self.calendar_full, 'monday', '2025-12-22')
|
|
_, planed_hours = result
|
|
self.assertEqual(planed_hours, {'one': 5.0, 'two': 0})
|
|
|
|
def test_shift_special_one_timing(self):
|
|
special_day_shift1 = self.env['attendance.special.days'].sudo().create({
|
|
'special_days_attendance': self.calendar_shift.id,
|
|
'name': 'monday',
|
|
'shift': 'one',
|
|
'working_hours': 5.0,
|
|
'start_sign_in': 5.0,
|
|
'end_sign_in': 6.0,
|
|
'start_sign_out': 10.0,
|
|
'end_sign_out': 11.0,
|
|
'date_from': fields.Date.to_date('2025-12-22'),
|
|
})
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_shift.id,
|
|
})
|
|
result = transaction.get_day_timing(self.calendar_shift, 'monday', '2025-12-22')
|
|
_, planed_hours = result
|
|
self.assertEqual(planed_hours, {'one': 4.5, 'two': 3.5})
|
|
|
|
def test_special_day_date_range(self):
|
|
special_day = self.env['attendance.special.days'].sudo().create({
|
|
'special_days_attendance': self.calendar_full.id,
|
|
'name': 'tuesday',
|
|
'working_hours': 10.0,
|
|
'date_from': fields.Date.to_date('2025-12-23'),
|
|
'date_to': fields.Date.to_date('2025-12-25'),
|
|
})
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_full.id,
|
|
})
|
|
result_inside = transaction.get_day_timing(self.calendar_full, 'tuesday', '2025-12-23')
|
|
_, planned_inside = result_inside
|
|
self.assertEqual(planned_inside['one'], 9.0)
|
|
|
|
def test_special_day_no_dates(self):
|
|
special_day = self.env['attendance.special.days'].sudo().create({
|
|
'special_days_attendance': self.calendar_full.id,
|
|
'name': 'wednesday',
|
|
'working_hours': 12.0,
|
|
})
|
|
transaction = self.env['hr.attendance.transaction'].sudo().create({
|
|
'employee_id': self.employee.id,
|
|
'calendar_id': self.calendar_full.id,
|
|
})
|
|
result = transaction.get_day_timing(self.calendar_full, 'wednesday', '2025-12-24')
|
|
_, planned_hours = result
|
|
self.assertEqual(planned_hours['one'], 11.0)
|