diff --git a/hr_employee_stats_sheet/models/hr_timesheet_sheet.py b/hr_employee_stats_sheet/models/hr_timesheet_sheet.py index 6159a81..5e59ac1 100644 --- a/hr_employee_stats_sheet/models/hr_timesheet_sheet.py +++ b/hr_employee_stats_sheet/models/hr_timesheet_sheet.py @@ -102,18 +102,21 @@ class HrTimesheetSheet(models.Model): def _get_working_hours_per_day(self): """ - Get the hours per day for the employee according to: - - the employee's work contract, - - or their resource calendar, + Get the hours per day used to convert recovery hours into allocation days. + It must be the same ratio as the one used by Odoo to convert the allocation + days back into hours (see hr.leave.allocation._compute_number_of_hours_display), + otherwise the allocation won't display the recovery hours of the timesheet sheet: + - the employee's resource calendar, - or the company's resource calendar, - or the default value of 8 hours per day. :return: hours per day """ - # get ressource calendar id used during the timesheet sheet time period - ressource_calendar_id = self.employee_id._get_calendar_in_progress_during_a_time_period(self.date_start,self.date_end) - if ressource_calendar_id: - return ressource_calendar_id.hours_per_day - return HOURS_PER_DAY + self.ensure_one() + return ( + self.employee_id.sudo().resource_calendar_id.hours_per_day + or self.company_id.resource_calendar_id.hours_per_day + or HOURS_PER_DAY + ) def action_generate_recovery_allocation(self): # check if the user has the right to review the timesheet sheet @@ -147,6 +150,9 @@ class HrTimesheetSheet(models.Model): # get recovery hours from total gap hours of the timesheet sheet recovery_hours = self._get_timesheet_sheet_recovery_hours() + # raise an error if the employee's calendar changes during the timesheet sheet time period + employee_id._get_calendar_in_progress_during_a_time_period(self.date_start, self.date_end) + # convert recovery hours into days recovery_days = recovery_hours / self._get_working_hours_per_day() diff --git a/hr_employee_stats_sheet/tests/test_employee_stats.py b/hr_employee_stats_sheet/tests/test_employee_stats.py index 2b1903b..f65066e 100644 --- a/hr_employee_stats_sheet/tests/test_employee_stats.py +++ b/hr_employee_stats_sheet/tests/test_employee_stats.py @@ -128,8 +128,27 @@ class TestHrEmployeeStatsRecovery(TransactionCase): timesheet_sheet.action_generate_recovery_allocation() recovery_allocation = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet.id)]) self.assertEqual(len(recovery_allocation), 1, "There should be one recovery") - self.assertEqual(recovery_allocation.number_of_days,0.8928571428571429, "The recovery allocation should be for 6.25h/7h = 0.8928571428571429 day") - + self.assertAlmostEqual(recovery_allocation.number_of_hours_display, 6.25, msg="The recovery allocation should display 6.25 hours") + + def test_recovery_allocation_hours_with_employee_calendar_different_from_planning(self): + # the employee's resource calendar hours per day differs from the planning calendar one: + # the allocation must still display the recovery hours of the timesheet sheet + self.env['hr.employee.calendar'].create({ + 'employee_id': self.employee.id, + 'date_start': Date.to_date("2025-01-01"), + 'date_end': None, + 'calendar_id': self.base_calendar.id, + }) + self.employee.resource_calendar_id.hours_per_day = 7.3 + start_date = date.today() - timedelta(days=date.today().weekday() + 7) # lundi de la semaine dernière + timesheet_sheet = self._create_timesheet_sheet(start_date) + list(self._create_stats(start_date, 5, 8)) + self.assertEqual(timesheet_sheet.timesheet_sheet_recovery_hours, 6.25, "timesheet_sheet_recovery_hours should be 6,25",) + timesheet_sheet.action_generate_recovery_allocation() + recovery_allocation = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet.id)]) + self.assertEqual(len(recovery_allocation), 1, "There should be one recovery") + self.assertAlmostEqual(recovery_allocation.number_of_hours_display, 6.25, msg="The recovery allocation should display 6.25 hours") + def test_negative_recovery_hours(self): self.env['hr.employee.calendar'].create({ 'employee_id': self.employee.id, @@ -153,7 +172,7 @@ class TestHrEmployeeStatsRecovery(TransactionCase): timesheet_sheet.action_generate_recovery_allocation() recovery_allocation = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet.id)]) self.assertEqual(len(recovery_allocation), 1, "There should be one recovery") - self.assertEqual(recovery_allocation.number_of_days, -0.7142857142857143, "The recovery allocation should be for -5/7 hours = −0,714285714") + self.assertAlmostEqual(recovery_allocation.number_of_hours_display, -5, msg="The recovery allocation should display -5 hours") def test_sheet_totals_update_after_timesheet_change(self): """Regression test: the sheet totals must reflect the addition and @@ -371,7 +390,9 @@ class TestHrEmployeeStatsRecovery(TransactionCase): self.assertEqual(timesheet_sheet_1.timesheet_sheet_recovery_hours, 6.25, "timesheet_sheet_recovery_hours should be 6,25",) allocation_1 = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet_1.id)]) self.assertEqual(len(allocation_1), 1, "There should be one recovery") - self.assertEqual(allocation_1.number_of_days,0.8928571428571429, "The recovery allocation should be for 0.8928571428571429 day") + # days are computed with the employee's current calendar (9h/day), as Odoo does to display hours + self.assertAlmostEqual(allocation_1.number_of_days, 6.25 / 9, msg="The recovery allocation should be for 6.25/9 day") + self.assertAlmostEqual(allocation_1.number_of_hours_display, 6.25, msg="The recovery allocation should display 6.25 hours") #create stats during period of part time calendar for the employee # generation 4 stats of 10h each, the employee is supposed to work 9h per day during 4 days @@ -390,8 +411,9 @@ class TestHrEmployeeStatsRecovery(TransactionCase): allocation_2 = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet_2.id)]) self.assertEqual(len(allocation_2), 1, "There should be one recovery") self.assertEqual(allocation_2.number_of_days,0.5555555555555556, "The recovery allocation should be for 0,555555556 (5/9) day") + self.assertAlmostEqual(allocation_2.number_of_hours_display, 5, msg="The recovery allocation should display 5 hours") #check that allocation_1 hasn't changed - self.assertEqual(allocation_1.number_of_days,0.8928571428571429, "The recovery allocation should be for 0,892857143 day") + self.assertAlmostEqual(allocation_1.number_of_hours_display, 6.25, msg="The recovery allocation should still display 6.25 hours") def test_public_holiday(self): self.env['hr.employee.calendar'].create({ @@ -431,7 +453,7 @@ class TestHrEmployeeStatsRecovery(TransactionCase): timesheet_sheet.action_generate_recovery_allocation() recovery_allocation = self.env["hr.leave.allocation"].search([("timesheet_sheet_id","=",timesheet_sheet.id)]) self.assertEqual(len(recovery_allocation), 1, "Il doit y avoir une allocation de récupération générée") - self.assertEqual(recovery_allocation.number_of_days,1.25, "The recovery allocation should be 1,25 days") + self.assertAlmostEqual(recovery_allocation.number_of_hours_display, 8.75, msg="The recovery allocation should display 8.75 hours") @tagged("post_install", "-at_install")