From 1b7592b58dcca0bbfbf84c8457b97065b29d5e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Fri, 28 Apr 2023 09:55:05 +0200 Subject: [PATCH 1/4] [IMP] hr_employee_calendar_planning: set calendar_ids from Create employee button from user --- .../models/hr_employee.py | 19 +++++++++++++++++++ .../test_hr_employee_calendar_planning.py | 19 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index bd0a4c10880..ab8b9f604ef 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -122,6 +122,25 @@ 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 + res.update( + { + "calendar_ids": [ + ( + 0, + 0, + { + "date_start": fields.Date.today(), + "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/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"), + ) From d28a5ba9d38f6ad0debffbd7ca8ba6916b236c80 Mon Sep 17 00:00:00 2001 From: jb Date: Tue, 16 May 2023 12:37:34 +0200 Subject: [PATCH 2/4] [IMP] hr_employee_calendar_planning: remove date_start from _sync_user --- hr_employee_calendar_planning/models/hr_employee.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index ab8b9f604ef..2251205c44f 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -132,7 +132,6 @@ def _sync_user(self, user): 0, 0, { - "date_start": fields.Date.today(), "calendar_id": user.company_id.resource_calendar_id.id, }, ), From 58599e94512337f5d1dea75980d59e8d9ed6aa33 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Fri, 2 Jun 2023 10:06:09 +0200 Subject: [PATCH 3/4] [FIX] hr_employee_calendar_planning: fill calendar if empty We should only fill the calendar with sync_user if and only if you don't have one Sync user is used also when we modify the user of an employee --- .../models/hr_employee.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/hr_employee_calendar_planning/models/hr_employee.py b/hr_employee_calendar_planning/models/hr_employee.py index 2251205c44f..e005af1adb0 100644 --- a/hr_employee_calendar_planning/models/hr_employee.py +++ b/hr_employee_calendar_planning/models/hr_employee.py @@ -125,19 +125,18 @@ def copy(self, default=None): def _sync_user(self, user): res = super()._sync_user(user=user) # set calendar_ids from Create employee button from user - res.update( - { - "calendar_ids": [ - ( - 0, - 0, - { - "calendar_id": user.company_id.resource_calendar_id.id, - }, - ), - ] - } - ) + 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 From ba30d534f685bea23b3014ab85af2ffa0d644756 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Fri, 2 Jun 2023 15:06:22 +0200 Subject: [PATCH 4/4] [IMP] hr_employee_calendar_planning: Allow to archive when calendars have finished. Show them on calendar view --- hr_employee_calendar_planning/__manifest__.py | 6 ++++- .../models/resource_calendar.py | 8 ++++++- .../views/resource_calendar_views.xml | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 hr_employee_calendar_planning/views/resource_calendar_views.xml 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/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/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 + + + + + + + + + + + + + + + + +