From ed6e5bd7b2b7fcde091b555ee03b0249d4f8a460 Mon Sep 17 00:00:00 2001 From: AaronHForgeFlow Date: Tue, 19 Nov 2024 10:02:40 +0100 Subject: [PATCH] [FIX] hr_period_create_timesheet: do not create timesheets for employees whose contract has not started --- .../tests/test_hr_period_create_timesheet.py | 55 +++++++++++++++++++ .../wizards/hr_period_create_timesheet.py | 4 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/hr_period_create_timesheet/tests/test_hr_period_create_timesheet.py b/hr_period_create_timesheet/tests/test_hr_period_create_timesheet.py index 001c60b8e..589ecd42f 100644 --- a/hr_period_create_timesheet/tests/test_hr_period_create_timesheet.py +++ b/hr_period_create_timesheet/tests/test_hr_period_create_timesheet.py @@ -59,6 +59,52 @@ def setUp(self): "company_id": self.company_id.id, } ) + # create another User + self.user_test_2 = self.user_model.create( + { + "name": "User 2", + "login": "anoth@example.com", + "password": "base-test-passwd", + } + ) + # create another Employee + self.employee_2 = self.hr_employee.create( + { + "name": "Employee 2", + "user_id": self.user_test_2.id, + "address_id": self.user_test_2.partner_id.id, + "parent_id": self.root.id, + "company_id": self.company_id.id, + } + ) + # create contracts only open for the first employee + self.contract1 = self.env["hr.contract"].create( + { + "name": "Contract 1", + "employee_id": self.employee.id, + "wage": 1000, + "date_start": time.strftime("%Y-01-01"), + "date_end": time.strftime("%Y-12-31"), + "state": "open", + "resource_calendar_id": self.env["resource.calendar"].browse([1]).id, + } + ) + current_year = datetime.now().year + new_year = current_year + 1 + date_start = datetime(new_year, 1, 1).strftime("%Y-%m-%d") + date_end = datetime(new_year, 12, 31).strftime("%Y-%m-%d") + # Creating the contract + self.contract2 = self.env["hr.contract"].create( + { + "name": "Contract 2", + "employee_id": self.employee_2.id, + "wage": 1000, + "date_start": date_start, + "date_end": date_end, + "state": "open", + "resource_calendar_id": self.env["resource.calendar"].browse([1]).id, + } + ) def create_data_range_type(self, name): # create Data Range Type @@ -167,3 +213,12 @@ def test_create_periods_future(self): periods_left = last_period.date_end.month - datetime.now().month + 1 # timesheets for the rest of the year self.assertEqual(len(timesheets), periods_left) + # check no timesheet created for the employee whose contract has not started + timesheets = self.timesheet_sheet.search( + [ + ("employee_id", "=", self.employee_2.id), + ("date_end", ">", datetime.now()), + ("company_id", "=", periods[0].company_id.id), + ] + ) + self.assertEqual(len(timesheets), 0) diff --git a/hr_period_create_timesheet/wizards/hr_period_create_timesheet.py b/hr_period_create_timesheet/wizards/hr_period_create_timesheet.py index 6e9fdd7f4..5e524dc03 100644 --- a/hr_period_create_timesheet/wizards/hr_period_create_timesheet.py +++ b/hr_period_create_timesheet/wizards/hr_period_create_timesheet.py @@ -94,7 +94,9 @@ def create_timesheets_on_future_periods(self): timesheet_obj = self.env["hr_timesheet.sheet"] today = fields.Date.today() periods = self.env["hr.period"].search([("date_end", ">=", today)]) - employees = self.env["hr.employee"].search([("user_id", "!=", False)]) + employees = self.env["hr.employee"].search( + [("user_id", "!=", False), ("contract_id.date_start", "<=", today)] + ) if not periods or not employees: return # Create a dictionary to store existing timesheets per employee