diff --git a/hr_employee_calendar_planning/__manifest__.py b/hr_employee_calendar_planning/__manifest__.py index 606c79f6519..02025bedad4 100644 --- a/hr_employee_calendar_planning/__manifest__.py +++ b/hr_employee_calendar_planning/__manifest__.py @@ -9,7 +9,11 @@ "license": "AGPL-3", "installable": True, "depends": ["hr"], - "data": ["security/ir.model.access.csv", "views/hr_employee_views.xml"], + "data": [ + "security/ir.model.access.csv", + "views/hr_employee_views.xml", + "views/resource_calendar_views.xml", + ], "post_init_hook": "post_init_hook", "maintainers": ["victoralmau", "pedrobaeza"], } diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index bd0a4c10880..e005af1adb0 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -122,6 +122,23 @@ def copy(self, default=None): new.filtered("calendar_ids").regenerate_calendar() return new + def _sync_user(self, user): + res = super()._sync_user(user=user) + # set calendar_ids from Create employee button from user + if not self.calendar_ids: + res.update( + { + "calendar_ids": [ + ( + 0, + 0, + {"calendar_id": user.company_id.resource_calendar_id.id}, + ), + ] + } + ) + return res + @api.model_create_multi def create(self, vals_list): res = super().create(vals_list) diff --git a/hr_employee_calendar_planning/models/resource_calendar.py b/hr_employee_calendar_planning/models/resource_calendar.py index 718e6e9b1e6..3e21a656784 100644 --- a/hr_employee_calendar_planning/models/resource_calendar.py +++ b/hr_employee_calendar_planning/models/resource_calendar.py @@ -11,12 +11,18 @@ class ResourceCalendar(models.Model): active = fields.Boolean(default=True) auto_generate = fields.Boolean() + employee_calendar_ids = fields.One2many("hr.employee.calendar", "calendar_id") @api.constrains("active") def _check_active(self): for item in self: total_items = self.env["hr.employee.calendar"].search_count( - [("calendar_id", "=", item.id)] + [ + ("calendar_id", "=", item.id), + "|", + ("date_end", "=", False), + ("date_end", "<=", fields.Date.today()), + ] ) if total_items: raise ValidationError( diff --git a/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py b/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py index 20ff5e437a9..aec73b7bf31 100644 --- a/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py +++ b/hr_employee_calendar_planning/tests/test_hr_employee_calendar_planning.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import exceptions, fields -from odoo.tests import common +from odoo.tests import common, new_test_user from ..hooks import post_init_hook @@ -12,6 +12,15 @@ class TestHrEmployeeCalendarPlanning(common.SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() + cls.env = cls.env( + context=dict( + cls.env.context, + mail_create_nolog=True, + mail_create_nosubscribe=True, + mail_notrack=True, + no_reset_password=True, + ) + ) resource_calendar = cls.env["resource.calendar"] cls.calendar1 = resource_calendar.create( {"name": "Test calendar 1", "attendance_ids": []} @@ -373,3 +382,11 @@ def test_employee_copy(self): self.assertNotEqual( self.employee.resource_calendar_id, employee2.resource_calendar_id ) + + def test_user_action_create_employee(self): + user = new_test_user(self.env, login="test-user") + user.action_create_employee() + self.assertIn( + user.company_id.resource_calendar_id, + user.employee_id.mapped("calendar_ids.calendar_id"), + ) diff --git a/hr_employee_calendar_planning/views/resource_calendar_views.xml b/hr_employee_calendar_planning/views/resource_calendar_views.xml new file mode 100644 index 00000000000..83f86bf9fee --- /dev/null +++ b/hr_employee_calendar_planning/views/resource_calendar_views.xml @@ -0,0 +1,23 @@ + + + + + resource.calendar + + + + + + + + + + + + + + + + +