Initial commit: Hr packages

This commit is contained in:
Ernad Husremovic 2025-08-29 15:20:50 +02:00
commit 62531cd146
2820 changed files with 1432848 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
from . import test_hr_attendance_constraints
from . import test_hr_attendance_overtime
from . import test_hr_attendance_process

View file

@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
import time
from odoo.tests.common import tagged, TransactionCase
@tagged('jesaispas')
class TestHrAttendance(TransactionCase):
"""Tests for attendance date ranges validity"""
@classmethod
def setUpClass(cls):
super(TestHrAttendance, cls).setUpClass()
cls.attendance = cls.env['hr.attendance']
cls.test_employee = cls.env['hr.employee'].create({'name': "Jacky"})
# demo data contains set up for cls.test_employee
cls.open_attendance = cls.attendance.create({
'employee_id': cls.test_employee.id,
'check_in': time.strftime('%Y-%m-10 10:00'),
})
def test_attendance_in_before_out(self):
# Make sure check_out is before check_in
with self.assertRaises(Exception):
self.my_attend = self.attendance.create({
'employee_id': self.test_employee.id,
'check_in': time.strftime('%Y-%m-10 12:00'),
'check_out': time.strftime('%Y-%m-10 11:00'),
})
def test_attendance_no_check_out(self):
# Make sure no second attandance without check_out can be created
with self.assertRaises(Exception):
self.attendance.create({
'employee_id': self.test_employee.id,
'check_in': time.strftime('%Y-%m-10 11:00'),
})
# 5 next tests : Make sure that when attendances overlap an error is raised
def test_attendance_1(self):
self.attendance.create({
'employee_id': self.test_employee.id,
'check_in': time.strftime('%Y-%m-10 07:30'),
'check_out': time.strftime('%Y-%m-10 09:00'),
})
with self.assertRaises(Exception):
self.attendance.create({
'employee_id': self.test_employee.id,
'check_in': time.strftime('%Y-%m-10 08:30'),
'check_out': time.strftime('%Y-%m-10 09:30'),
})
def test_new_attendances(self):
# Make sure attendance modification raises an error when it causes an overlap
self.attendance.create({
'employee_id': self.test_employee.id,
'check_in': time.strftime('%Y-%m-10 11:00'),
'check_out': time.strftime('%Y-%m-10 12:00'),
})
with self.assertRaises(Exception):
self.open_attendance.write({
'check_out': time.strftime('%Y-%m-10 11:30'),
})

View file

@ -0,0 +1,266 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from datetime import date, datetime
from odoo.tests import new_test_user
from odoo.tests.common import tagged, TransactionCase
@tagged('post_install', '-at_install', 'hr_attendance_overtime')
class TestHrAttendanceOvertime(TransactionCase):
""" Tests for overtime """
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.company = cls.env['res.company'].create({
'name': 'SweatChipChop Inc.',
'hr_attendance_overtime': True,
'overtime_start_date': datetime(2021, 1, 1),
'overtime_company_threshold': 10,
'overtime_employee_threshold': 10,
})
cls.user = new_test_user(cls.env, login='fru', groups='base.group_user,hr_attendance.group_hr_attendance', company_id=cls.company.id).with_company(cls.company)
cls.employee = cls.env['hr.employee'].create({
'name': "Marie-Edouard De La Court",
'user_id': cls.user.id,
'company_id': cls.company.id,
'tz': 'UTC',
})
cls.other_employee = cls.env['hr.employee'].create({
'name': 'Yolanda',
'company_id': cls.company.id,
})
cls.jpn_employee = cls.env['hr.employee'].create({
'name': 'Sacha',
'company_id': cls.company.id,
'tz': 'Asia/Tokyo',
})
cls.honolulu_employee = cls.env['hr.employee'].create({
'name': 'Susan',
'company_id': cls.company.id,
'tz': 'Pacific/Honolulu',
})
def test_overtime_company_settings(self):
self.company.hr_attendance_overtime = False
self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 8, 0),
'check_out': datetime(2021, 1, 4, 20, 0)
})
overtime = self.env['hr.attendance.overtime'].search_count([('employee_id', '=', self.employee.id), ('date', '=', date(2021, 1, 4))])
self.assertFalse(overtime, 'No overtime should be created')
self.company.write({
'hr_attendance_overtime': True,
'overtime_start_date': date(2021, 1, 1)
})
overtime = self.env['hr.attendance.overtime'].search_count([('employee_id', '=', self.employee.id), ('date', '=', date(2021, 1, 4))])
self.assertTrue(overtime, 'Overtime should be created')
self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2020, 12, 30, 8, 0),
'check_out': datetime(2020, 12, 30, 20, 0)
})
overtime = self.env['hr.attendance.overtime'].search_count([('employee_id', '=', self.employee.id), ('date', '=', date(2020, 12, 30))])
self.assertFalse(overtime, 'No overtime should be created before the start date')
def test_simple_overtime(self):
checkin_am = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 8, 0)
})
self.env['hr.attendance'].create({
'employee_id': self.other_employee.id,
'check_in': datetime(2021, 1, 4, 8, 0),
'check_out': datetime(2021, 1, 4, 22, 0)
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id), ('date', '=', date(2021, 1, 4))])
self.assertFalse(overtime, 'No overtime record should exist for that employee')
checkin_am.write({'check_out': datetime(2021, 1, 4, 12, 0)})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id), ('date', '=', date(2021, 1, 4))])
self.assertTrue(overtime, 'An overtime record should be created')
self.assertEqual(overtime.duration, -4)
checkin_pm = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 13, 0)
})
self.assertEqual(overtime.duration, 0, 'Overtime duration should be 0 when an attendance has not been checked out.')
checkin_pm.write({'check_out': datetime(2021, 1, 4, 18, 0)})
self.assertTrue(overtime.exists(), 'Overtime should not be deleted')
self.assertAlmostEqual(overtime.duration, 1)
self.assertAlmostEqual(self.employee.total_overtime, 1)
def test_overtime_weekend(self):
self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 2, 8, 0),
'check_out': datetime(2021, 1, 2, 11, 0)
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id), ('date', '=', date(2021, 1, 2))])
self.assertTrue(overtime, 'Overtime should be created')
self.assertEqual(overtime.duration, 3, 'Should have 3 hours of overtime')
self.assertEqual(self.employee.total_overtime, 3, 'Should still have 3 hours of overtime')
def test_overtime_multiple(self):
attendance = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 2, 8, 0),
'check_out': datetime(2021, 1, 2, 19, 0)
})
self.assertEqual(self.employee.total_overtime, 11)
self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 7, 0),
'check_out': datetime(2021, 1, 4, 16, 0)
})
self.assertEqual(self.employee.total_overtime, 12)
attendance.unlink()
self.assertEqual(self.employee.total_overtime, 1)
def test_overtime_change_employee(self):
attendance = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 7, 0),
'check_out': datetime(2021, 1, 4, 18, 0)
})
self.assertEqual(self.employee.total_overtime, 3)
self.assertEqual(self.other_employee.total_overtime, 0)
attendance.employee_id = self.other_employee.id
self.assertEqual(self.other_employee.total_overtime, 3)
self.assertEqual(self.employee.total_overtime, 0)
def test_overtime_far_timezones(self):
self.env['hr.attendance'].create({
'employee_id': self.jpn_employee.id,
# Since dates have to be stored in utc these are the tokyo timezone times for 7-18 (UTC+9)
'check_in': datetime(2021, 1, 3, 22, 0),
'check_out': datetime(2021, 1, 4, 9, 0),
})
self.env['hr.attendance'].create({
'employee_id': self.honolulu_employee.id,
# Same but for alaskan times (UTC-10)
'check_in': datetime(2021, 1, 4, 17, 0),
'check_out': datetime(2021, 1, 5, 4, 0),
})
self.assertEqual(self.jpn_employee.total_overtime, 3)
self.assertEqual(self.honolulu_employee.total_overtime, 3)
def test_overtime_unclosed(self):
attendance = self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 8, 0),
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertFalse(overtime, 'Overtime entry should not exist at this point.')
# Employees goes to eat
attendance.write({
'check_out': datetime(2021, 1, 4, 12, 0),
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertTrue(overtime, 'An overtime entry should have been created.')
self.assertEqual(overtime.duration, -4, 'User still has to work the afternoon.')
self.env['hr.attendance'].create({
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 13, 0),
})
self.assertEqual(overtime.duration, 0, 'Overtime entry has been reset due to an unclosed attendance.')
def test_overtime_company_threshold(self):
self.env['hr.attendance'].create([
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 7, 55),
'check_out': datetime(2021, 1, 4, 12, 0),
},
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 13, 0),
'check_out': datetime(2021, 1, 4, 17, 5),
}
])
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertFalse(overtime, 'No overtime should be counted because of the threshold.')
self.company.write({
'hr_attendance_overtime': True,
'overtime_start_date': date(2021, 1, 1),
'overtime_company_threshold': 4,
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertTrue(overtime, 'Overtime entry should exist since the threshold has been lowered.')
self.assertAlmostEqual(overtime.duration, 10 / 60, msg='Overtime should be equal to 10 minutes.')
def test_overtime_employee_threshold(self):
self.env['hr.attendance'].create([
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 8, 5),
'check_out': datetime(2021, 1, 4, 12, 0),
},
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 13, 0),
'check_out': datetime(2021, 1, 4, 16, 55),
}
])
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertFalse(overtime, 'No overtime should be counted because of the threshold.')
self.company.write({
'hr_attendance_overtime': True,
'overtime_start_date': date(2021, 1, 1),
'overtime_employee_threshold': 4,
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertTrue(overtime, 'Overtime entry should exist since the threshold has been lowered.')
self.assertAlmostEqual(overtime.duration, -(10 / 60), msg='Overtime should be equal to -10 minutes.')
def test_overtime_both_threshold(self):
self.env['hr.attendance'].create([
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 8, 5),
'check_out': datetime(2021, 1, 4, 12, 0),
},
{
'employee_id': self.employee.id,
'check_in': datetime(2021, 1, 4, 13, 0),
'check_out': datetime(2021, 1, 4, 17, 5),
}
])
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertFalse(overtime, 'No overtime should be counted because of the threshold.')
self.company.write({
'hr_attendance_overtime': True,
'overtime_start_date': date(2021, 1, 1),
'overtime_employee_threshold': 4,
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertTrue(overtime, 'Overtime entry should exist since the employee threshold has been lowered.')
self.assertAlmostEqual(overtime.duration, -(5 / 60), msg='Overtime should be equal to -5 minutes.')
self.company.write({
'hr_attendance_overtime': True,
'overtime_start_date': date(2021, 1, 1),
'overtime_company_threshold': 4,
})
overtime = self.env['hr.attendance.overtime'].search([('employee_id', '=', self.employee.id)])
self.assertFalse(overtime, 'Overtime entry should be unlinked since both overtime cancel each other.')

View file

@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
import pytz
from datetime import datetime
from unittest.mock import patch
from odoo import fields
from odoo.tests import new_test_user
from odoo.tests.common import tagged, TransactionCase
@tagged('attendance_process')
class TestHrAttendance(TransactionCase):
"""Test for presence validity"""
@classmethod
def setUpClass(cls):
super(TestHrAttendance, cls).setUpClass()
cls.user = new_test_user(cls.env, login='fru', groups='base.group_user,hr_attendance.group_hr_attendance_use_pin')
cls.user_no_pin = new_test_user(cls.env, login='gru', groups='base.group_user')
cls.test_employee = cls.env['hr.employee'].create({
'name': "François Russie",
'user_id': cls.user.id,
'pin': '1234',
})
cls.employee_kiosk = cls.env['hr.employee'].create({
'name': "Machiavel",
'pin': '5678',
})
def setUp(self):
super().setUp()
# Cache error if not done during setup
(self.test_employee | self.employee_kiosk).last_attendance_id.unlink()
def test_employee_state(self):
# Make sure the attendance of the employee will display correctly
assert self.test_employee.attendance_state == 'checked_out'
self.test_employee._attendance_action_change()
assert self.test_employee.attendance_state == 'checked_in'
self.test_employee._attendance_action_change()
assert self.test_employee.attendance_state == 'checked_out'
def test_checkin_self_without_pin(self):
""" Employee can check in/out without pin with his own account """
employee = self.test_employee.with_user(self.user)
employee.with_user(self.user).attendance_manual({}, entered_pin=None)
self.assertEqual(employee.attendance_state, 'checked_in', "He should be able to check in without pin")
employee.attendance_manual({}, entered_pin=None)
self.assertEqual(employee.attendance_state, 'checked_out', "He should be able to check out without pin")
def test_checkin_self_with_pin(self):
""" Employee can check in/out with pin with his own account """
employee = self.test_employee.with_user(self.user)
employee.attendance_manual({}, entered_pin='1234')
self.assertEqual(employee.attendance_state, 'checked_in', "He should be able to check in with his pin")
employee.attendance_manual({}, entered_pin='1234')
self.assertEqual(employee.attendance_state, 'checked_out', "He should be able to check out with his pin")
def test_checkin_self_wrong_pin(self):
""" Employee cannot check in/out with wrong pin with his own account """
employee = self.test_employee.with_user(self.user)
action = employee.attendance_manual({}, entered_pin='9999')
self.assertNotEqual(employee.attendance_state, 'checked_in', "He should not be able to check in with a wrong pin")
self.assertTrue(action.get('warning'))
def test_checkin_kiosk_with_pin(self):
""" Employee can check in/out with his pin in kiosk """
employee = self.employee_kiosk.with_user(self.user)
employee.attendance_manual({}, entered_pin='5678')
self.assertEqual(employee.attendance_state, 'checked_in', "He should be able to check in with his pin")
employee.attendance_manual({}, entered_pin='5678')
self.assertEqual(employee.attendance_state, 'checked_out', "He should be able to check out with his pin")
def test_checkin_kiosk_with_wrong_pin(self):
""" Employee cannot check in/out with wrong pin in kiosk """
employee = self.employee_kiosk.with_user(self.user)
action = employee.attendance_manual({}, entered_pin='8888')
self.assertNotEqual(employee.attendance_state, 'checked_in', "He should not be able to check in with a wrong pin")
self.assertTrue(action.get('warning'))
def test_checkin_kiosk_without_pin(self):
""" Employee cannot check in/out without his pin in kiosk """
employee = self.employee_kiosk.with_user(self.user)
action = employee.attendance_manual({}, entered_pin=None)
self.assertNotEqual(employee.attendance_state, 'checked_in', "He should not be able to check in with no pin")
self.assertTrue(action.get('warning'))
def test_checkin_kiosk_no_pin_mode(self):
""" Employee cannot check in/out without pin in kiosk when user has not group `use_pin` """
employee = self.employee_kiosk.with_user(self.user_no_pin)
employee.attendance_manual({}, entered_pin=None)
self.assertEqual(employee.attendance_state, 'checked_out', "He shouldn't be able to check in without")
def test_hours_today(self):
""" Test day start is correctly computed according to the employee's timezone """
def tz_datetime(year, month, day, hour, minute):
tz = pytz.timezone('Europe/Brussels')
return tz.localize(datetime(year, month, day, hour, minute)).astimezone(pytz.utc).replace(tzinfo=None)
employee = self.env['hr.employee'].create({'name': 'Cunégonde', 'tz': 'Europe/Brussels'})
self.env['hr.attendance'].create({
'employee_id': employee.id,
'check_in': tz_datetime(2019, 3, 1, 22, 0), # should count from midnight in the employee's timezone (=the previous day in utc!)
'check_out': tz_datetime(2019, 3, 2, 2, 0),
})
self.env['hr.attendance'].create({
'employee_id': employee.id,
'check_in': tz_datetime(2019, 3, 2, 11, 0),
})
# now = 2019/3/2 14:00 in the employee's timezone
with patch.object(fields.Datetime, 'now', lambda: tz_datetime(2019, 3, 2, 14, 0).astimezone(pytz.utc).replace(tzinfo=None)):
self.assertEqual(employee.hours_today, 5, "It should have counted 5 hours")