From df0211e9c460ab268e903794e82a624992f59268 Mon Sep 17 00:00:00 2001 From: Antoni Marroig Campomar Date: Tue, 23 Jul 2024 08:41:35 +0200 Subject: [PATCH] [MIG] hr_holidays_natural_period: Migration to 17.0 --- hr_holidays_natural_period/README.rst | 4 ++ hr_holidays_natural_period/__manifest__.py | 2 +- .../demo/hr_leave_type_data.xml | 2 +- hr_holidays_natural_period/models/__init__.py | 2 + .../models/hr_employee.py | 22 ++++++++++ hr_holidays_natural_period/models/hr_leave.py | 8 ++-- .../models/hr_leave_allocation.py | 12 ++++++ .../models/hr_leave_type.py | 19 --------- .../models/resource_calendar.py | 11 +++-- .../readme/CONTRIBUTORS.md | 4 ++ .../static/description/index.html | 7 ++++ .../tests/test_hr_leave.py | 41 +++++++++++-------- 12 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 hr_holidays_natural_period/models/hr_employee.py create mode 100644 hr_holidays_natural_period/models/hr_leave_allocation.py diff --git a/hr_holidays_natural_period/README.rst b/hr_holidays_natural_period/README.rst index 34474664..65ab5ac9 100644 --- a/hr_holidays_natural_period/README.rst +++ b/hr_holidays_natural_period/README.rst @@ -76,6 +76,10 @@ Contributors - Víctor Martínez - Pedro Baeza +- APSL-Nagarro + + - Antoni Marroig + Maintainers ----------- diff --git a/hr_holidays_natural_period/__manifest__.py b/hr_holidays_natural_period/__manifest__.py index 08ff5304..face965a 100644 --- a/hr_holidays_natural_period/__manifest__.py +++ b/hr_holidays_natural_period/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Holidays natural period", "summary": "Apply natural days in holidays", - "version": "16.0.1.0.1", + "version": "17.0.1.0.0", "category": "Human Resources", "website": "https://github.com/OCA/hr-holidays", "author": "Tecnativa, Odoo Community Association (OCA)", diff --git a/hr_holidays_natural_period/demo/hr_leave_type_data.xml b/hr_holidays_natural_period/demo/hr_leave_type_data.xml index a10abd02..b0c82cb3 100644 --- a/hr_holidays_natural_period/demo/hr_leave_type_data.xml +++ b/hr_holidays_natural_period/demo/hr_leave_type_data.xml @@ -3,7 +3,7 @@ Test Time Off (natural day) natural_day - + yes diff --git a/hr_holidays_natural_period/models/__init__.py b/hr_holidays_natural_period/models/__init__.py index ae7ae6ad..3309c690 100644 --- a/hr_holidays_natural_period/models/__init__.py +++ b/hr_holidays_natural_period/models/__init__.py @@ -1,3 +1,5 @@ from . import hr_leave_type from . import hr_leave from . import resource_calendar +from . import hr_leave_allocation +from . import hr_employee diff --git a/hr_holidays_natural_period/models/hr_employee.py b/hr_holidays_natural_period/models/hr_employee.py new file mode 100644 index 00000000..12551b89 --- /dev/null +++ b/hr_holidays_natural_period/models/hr_employee.py @@ -0,0 +1,22 @@ +# Copyright 2024 APSL-Nagarro - Antoni Marroig Campomar +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + def _get_consumed_leaves(self, leave_types, target_date=False, ignore_future=False): + """We need to set request_unit as 'day' to avoid the calculations being done + as hours. + """ + old_request_unit_data = {} + for item in leave_types.filtered(lambda x: x.request_unit == "natural_day"): + old_request_unit_data[item.id] = item.request_unit + item.sudo().request_unit = "day" + res = super()._get_consumed_leaves(leave_types, target_date, ignore_future) + for item in leave_types: + if item.id in old_request_unit_data: + item.sudo().request_unit = old_request_unit_data[item.id] + return res diff --git a/hr_holidays_natural_period/models/hr_leave.py b/hr_holidays_natural_period/models/hr_leave.py index 2d7fe792..3599854e 100644 --- a/hr_holidays_natural_period/models/hr_leave.py +++ b/hr_holidays_natural_period/models/hr_leave.py @@ -7,12 +7,12 @@ class HrLeave(models.Model): _inherit = "hr.leave" - def _get_number_of_days(self, date_from, date_to, employee_id): + def _get_duration(self, check_leave_type=True, resource_calendar=None): instance = self.with_context( natural_period=bool(self.holiday_status_id.request_unit == "natural_day") ) - return super(HrLeave, instance)._get_number_of_days( - date_from, date_to, employee_id + return super(HrLeave, instance)._get_duration( + check_leave_type, resource_calendar ) @api.model_create_multi @@ -22,5 +22,5 @@ def create(self, vals_list): res = super().create(vals_list) res.filtered( lambda x: x.holiday_status_id.request_unit == "natural_day" - )._compute_number_of_days() + )._compute_duration() return res diff --git a/hr_holidays_natural_period/models/hr_leave_allocation.py b/hr_holidays_natural_period/models/hr_leave_allocation.py new file mode 100644 index 00000000..13e80e0d --- /dev/null +++ b/hr_holidays_natural_period/models/hr_leave_allocation.py @@ -0,0 +1,12 @@ +# Copyright 2024 APSL-Nagarro - Antoni Marroig Campomar +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class HrLeaveAllocation(models.Model): + _inherit = "hr.leave.allocation" + + type_request_unit = fields.Selection( + selection_add=[("natural_day", "Natural day")], + ) diff --git a/hr_holidays_natural_period/models/hr_leave_type.py b/hr_holidays_natural_period/models/hr_leave_type.py index 7c8178b0..e6e77ed0 100644 --- a/hr_holidays_natural_period/models/hr_leave_type.py +++ b/hr_holidays_natural_period/models/hr_leave_type.py @@ -11,22 +11,3 @@ class HrLeaveType(models.Model): selection_add=[("natural_day", "Natural day")], ondelete={"natural_day": "set default"}, ) - - def _get_employees_days_per_allocation(self, employee_ids, date=None): - """We need to set request_unit as 'day' to avoid the calculations being done - as hours. - Related code: - hr_holidays/models/hr_leave_type.py#L326 - hr_holidays/models/hr_leave_type.py#L389 - """ - old_request_unit_data = {} - for item in self.filtered(lambda x: x.request_unit == "natural_day"): - old_request_unit_data[item.id] = item.request_unit - item.sudo().request_unit = "day" - res = super()._get_employees_days_per_allocation( - employee_ids=employee_ids, date=date - ) - for item in self: - if item.id in old_request_unit_data: - item.sudo().request_unit = old_request_unit_data[item.id] - return res diff --git a/hr_holidays_natural_period/models/resource_calendar.py b/hr_holidays_natural_period/models/resource_calendar.py index 9ae4784f..871b7650 100644 --- a/hr_holidays_natural_period/models/resource_calendar.py +++ b/hr_holidays_natural_period/models/resource_calendar.py @@ -8,7 +8,7 @@ from odoo import models -from odoo.addons.resource.models.resource import Intervals +from odoo.addons.resource.models.utils import Intervals class ResourceCalendar(models.Model): @@ -41,10 +41,15 @@ def _natural_period_intervals_batch(self, start_dt, end_dt, intervals, resources return intervals def _attendance_intervals_batch( - self, start_dt, end_dt, resources=None, domain=None, tz=None + self, start_dt, end_dt, resources=None, domain=None, tz=None, lunch=False ): res = super()._attendance_intervals_batch( - start_dt=start_dt, end_dt=end_dt, resources=resources, domain=domain, tz=tz + start_dt=start_dt, + end_dt=end_dt, + resources=resources, + domain=domain, + tz=tz, + lunch=lunch, ) if self.env.context.get("natural_period"): return self._natural_period_intervals_batch( diff --git a/hr_holidays_natural_period/readme/CONTRIBUTORS.md b/hr_holidays_natural_period/readme/CONTRIBUTORS.md index ab9d6965..f8463fb0 100644 --- a/hr_holidays_natural_period/readme/CONTRIBUTORS.md +++ b/hr_holidays_natural_period/readme/CONTRIBUTORS.md @@ -2,3 +2,7 @@ > - Víctor Martínez > - Pedro Baeza + +- APSL-Nagarro \<\> + + > - Antoni Marroig \<\> diff --git a/hr_holidays_natural_period/static/description/index.html b/hr_holidays_natural_period/static/description/index.html index 62c841a9..167d3e61 100644 --- a/hr_holidays_natural_period/static/description/index.html +++ b/hr_holidays_natural_period/static/description/index.html @@ -426,6 +426,13 @@

Contributors

+
  • APSL-Nagarro <https://www.apsl.tech>

    +
    + +
    +
  • diff --git a/hr_holidays_natural_period/tests/test_hr_leave.py b/hr_holidays_natural_period/tests/test_hr_leave.py index cd6be330..f5b830ee 100644 --- a/hr_holidays_natural_period/tests/test_hr_leave.py +++ b/hr_holidays_natural_period/tests/test_hr_leave.py @@ -43,7 +43,7 @@ def setUpClass(cls): partner = cls.env["res.partner"].create( { "name": "Test employee", - "type": "private", + "type": "other", "country_id": cls.env.ref("base.es").id, } ) @@ -51,7 +51,7 @@ def setUpClass(cls): cls.employee = cls.env["hr.employee"].create( { "name": "Test employee", - "address_home_id": partner.id, + "address_id": partner.id, "resource_calendar_id": calendar.id, "user_id": cls.user.id, } @@ -64,9 +64,10 @@ def _create_leave_allocation(self, leave_type, days): default_date_to="%s-12-31" % (fields.Date.today().year), ) ) - leave_allocation_form.name = "TEST" + leave_allocation_form.holiday_status_id = leave_type leave_allocation_form.number_of_days_display = days + leave_allocation_form.name = "TEST" return leave_allocation_form.save() def _create_hr_leave(self, leave_type, date_from, date_to): @@ -79,14 +80,17 @@ def _create_hr_leave(self, leave_type, date_from, date_to): @users("test-user") def test_hr_leave_natural_day(self): leave_allocation = self._create_leave_allocation(self.leave_type, 5) - leave_allocation.action_confirm() leave_allocation.sudo().action_validate() - res_leave_type = self.env["hr.leave.type"].get_days_all_request()[0][1] - self.assertEqual(res_leave_type["remaining_leaves"], "5") - self.assertEqual(res_leave_type["virtual_remaining_leaves"], "5") - self.assertEqual(res_leave_type["max_leaves"], "5") - self.assertEqual(res_leave_type["leaves_taken"], "0") - self.assertEqual(res_leave_type["virtual_leaves_taken"], "0") + res_leave_type = ( + self.env["hr.leave.type"] + .with_context(allowed_company_ids=[self.env.company.id]) + .get_allocation_data_request()[0][1] + ) + self.assertEqual(res_leave_type["remaining_leaves"], 5) + self.assertEqual(res_leave_type["virtual_remaining_leaves"], 5) + self.assertEqual(res_leave_type["max_leaves"], 5) + self.assertEqual(res_leave_type["leaves_taken"], 0) + self.assertEqual(res_leave_type["virtual_leaves_taken"], 0) self.assertEqual(res_leave_type["request_unit"], "natural_day") leave = self._create_hr_leave(self.leave_type, "2023-01-02", "2023-01-05") self.assertEqual(leave.number_of_days, 4.0) @@ -95,14 +99,17 @@ def test_hr_leave_natural_day(self): @users("test-user") def test_hr_leave_day(self): leave_allocation = self._create_leave_allocation(self.leave_type_day, 5) - leave_allocation.action_confirm() leave_allocation.sudo().action_validate() - res_leave_type = self.env["hr.leave.type"].get_days_all_request()[0][1] - self.assertEqual(res_leave_type["remaining_leaves"], "5") - self.assertEqual(res_leave_type["virtual_remaining_leaves"], "5") - self.assertEqual(res_leave_type["max_leaves"], "5") - self.assertEqual(res_leave_type["leaves_taken"], "0") - self.assertEqual(res_leave_type["virtual_leaves_taken"], "0") + res_leave_type = ( + self.env["hr.leave.type"] + .with_context(allowed_company_ids=[self.env.company.id]) + .get_allocation_data_request()[0][1] + ) + self.assertEqual(res_leave_type["remaining_leaves"], 5) + self.assertEqual(res_leave_type["virtual_remaining_leaves"], 5) + self.assertEqual(res_leave_type["max_leaves"], 5) + self.assertEqual(res_leave_type["leaves_taken"], 0) + self.assertEqual(res_leave_type["virtual_leaves_taken"], 0) self.assertEqual(res_leave_type["request_unit"], "day") leave = self._create_hr_leave(self.leave_type_day, "2023-01-08", "2023-01-15") self.assertEqual(leave.number_of_days, 5)