diff --git a/hr_holidays_remaining_leaves/__manifest__.py b/hr_holidays_remaining_leaves/__manifest__.py index bca32fca..c1c6f02e 100644 --- a/hr_holidays_remaining_leaves/__manifest__.py +++ b/hr_holidays_remaining_leaves/__manifest__.py @@ -6,7 +6,7 @@ "author": "Mint System GmbH, Odoo Community Association (OCA)", "website": "https://github.com/OCA/hr-holidays", "category": "Human Resources", - "version": "16.0.1.0.0", + "version": "17.0.1.0.0", "license": "AGPL-3", "depends": ["hr_holidays"], "data": ["views/hr_leave_allocation.xml"], diff --git a/hr_holidays_remaining_leaves/models/hr_leave.py b/hr_holidays_remaining_leaves/models/hr_leave.py index 261cf5b7..95fde795 100644 --- a/hr_holidays_remaining_leaves/models/hr_leave.py +++ b/hr_holidays_remaining_leaves/models/hr_leave.py @@ -1,11 +1,11 @@ import logging from odoo import _, fields, models +from odoo.tools.float_utils import float_round -_logger = logging.getLogger(__name__) -from datetime import datetime +from odoo.addons.resource.models.utils import HOURS_PER_DAY -from odoo.tools.float_utils import float_round +_logger = logging.getLogger(__name__) class HolidaysAllocation(models.Model): @@ -36,72 +36,49 @@ def _get_number_of_days_and_hours(self, date_from, date_to, employee_id): )[employee.id] def _compute_remaining_leaves(self): - now = fields.Datetime.now() - now = datetime.combine(now, datetime.min.time()) - + all_consumed_leaves = self.employee_id._get_consumed_leaves( + self.holiday_status_id + )[0] + all_consumed_leaves_current = self.employee_id._get_consumed_leaves( + self.holiday_status_id, ignore_future=True + )[0] for allocation in self: - # Get all validated leaves filtered by employee and leave type - leaves = self.env["hr.leave"].search( - [ - ("employee_id", "=", allocation.employee_id.id), - ("state", "=", "validate"), - ("holiday_status_id", "=", allocation.holiday_status_id.id), - "|", - ("holiday_allocation_id", "=", allocation.id), - ("holiday_allocation_id", "=", False), - ] + consumes_allo = all_consumed_leaves[allocation.employee_id][ + allocation.holiday_status_id + ][allocation] + consumes_allo_current = all_consumed_leaves_current[allocation.employee_id][ + allocation.holiday_status_id + ][allocation] + allocation_calendar = ( + allocation.holiday_status_id.company_id.resource_calendar_id ) - - # Set the remaining leaves - allocation.remaining_leaves_hours = ( - allocation.number_of_hours_display - - sum(leaves.mapped("number_of_hours_display")) - ) - allocation.remaining_leaves_days = allocation.number_of_days - sum( - leaves.mapped("number_of_days") - ) - - # Get past leaves - past_leaves = leaves.filtered(lambda l: l.date_to < now) - past_leave_hours = sum(past_leaves.mapped("number_of_hours_display")) - past_leave_days = sum(past_leaves.mapped("number_of_days")) - - # Check for leaves that are active and calculate the exact number of hours and days - active_leave_hours = 0 - active_leave_days = 0 - for leave in leaves.filtered(lambda l: l.date_from < now < l.date_to): - result = self._get_number_of_days_and_hours( - leave.date_from, now, leave.employee_id.id - ) - active_leave_days = result["days"] - active_leave_hours = result["hours"] - - allocation.remaining_leaves_current_hours = ( - allocation.number_of_hours_display - - past_leave_hours - - active_leave_hours - ) - allocation.remaining_leaves_current_days = ( - allocation.number_of_days - past_leave_days - active_leave_days + if allocation.holiday_type == "employee" and allocation.employee_id: + allocation_calendar = allocation.employee_id.sudo().resource_calendar_id + allocation.remaining_leaves_days = consumes_allo["remaining_leaves"] + allocation.remaining_leaves_hours = consumes_allo["remaining_leaves"] * ( + allocation_calendar.hours_per_day or HOURS_PER_DAY ) + allocation.remaining_leaves_current_days = consumes_allo_current[ + "remaining_leaves" + ] + allocation.remaining_leaves_current_hours = consumes_allo_current[ + "remaining_leaves" + ] * (allocation_calendar.hours_per_day or HOURS_PER_DAY) def _compute_remaining_leaves_display(self): for allocation in self: - allocation.remaining_leaves_display = "%g %s" % ( - ( - float_round(allocation.remaining_leaves_hours, precision_digits=2) - if allocation.type_request_unit == "hour" - else float_round( - allocation.remaining_leaves_days, precision_digits=2 - ) - ), + allocation.remaining_leaves_display = "{} {}".format( + float_round(allocation.remaining_leaves_hours, precision_digits=2) + if allocation.type_request_unit == "hour" + else float_round(allocation.remaining_leaves_days, precision_digits=2), _("hours") if allocation.type_request_unit == "hour" else _("days"), ) - allocation.remaining_leaves_current_display = "%g %s" % ( + allocation.remaining_leaves_current_display = "{} {}".format( ( float_round( - allocation.remaining_leaves_current_hours, precision_digits=2 + allocation.remaining_leaves_current_hours, + precision_digits=2, ) if allocation.type_request_unit == "hour" else float_round( diff --git a/hr_holidays_remaining_leaves/readme/CONTRIBUTORS.md b/hr_holidays_remaining_leaves/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..3ca41363 --- /dev/null +++ b/hr_holidays_remaining_leaves/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +* Janik von Rotz +* Son Ho diff --git a/hr_holidays_remaining_leaves/readme/CREDITS.md b/hr_holidays_remaining_leaves/readme/CREDITS.md new file mode 100644 index 00000000..940a58b8 --- /dev/null +++ b/hr_holidays_remaining_leaves/readme/CREDITS.md @@ -0,0 +1,5 @@ + +The original development has been done development of this module has been done by Mint System. +It can be found in: https://github.com/Mint-System/Odoo-Apps-HR/tree/16.0/hr_holidays_remaining_leaves + +This module has been ported to the OCA with their agreement \ No newline at end of file diff --git a/hr_holidays_remaining_leaves/readme/USAGE.md b/hr_holidays_remaining_leaves/readme/USAGE.md new file mode 100644 index 00000000..f348dda7 --- /dev/null +++ b/hr_holidays_remaining_leaves/readme/USAGE.md @@ -0,0 +1 @@ +Show remaining leaves per employee in allocation overview. \ No newline at end of file diff --git a/hr_holidays_remaining_leaves/tests/__init__.py b/hr_holidays_remaining_leaves/tests/__init__.py new file mode 100644 index 00000000..b305fb63 --- /dev/null +++ b/hr_holidays_remaining_leaves/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hr_holidays_remaining_leaves diff --git a/hr_holidays_remaining_leaves/tests/test_hr_holidays_remaining_leaves.py b/hr_holidays_remaining_leaves/tests/test_hr_holidays_remaining_leaves.py new file mode 100644 index 00000000..d149bafa --- /dev/null +++ b/hr_holidays_remaining_leaves/tests/test_hr_holidays_remaining_leaves.py @@ -0,0 +1,54 @@ +from freezegun import freeze_time + +from odoo.addons.hr_holidays.tests.test_allocations import TestAllocations + + +class TestHrHolidaysRemainLeaves(TestAllocations): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_hr_holidays_remaining_leaves(self): + emp_allocation = self.env["hr.leave.allocation"].create( + { + "name": "Bank Holiday", + "holiday_type": "employee", + "employee_ids": [(4, self.employee.id)], + "employee_id": self.employee.id, + "date_from": "2024-06-17", + "holiday_status_id": self.leave_type.id, + "number_of_days": 3, + "allocation_type": "regular", + } + ) + emp_allocation.action_validate() + with freeze_time("2024-06-19"): + leave_current = self.env["hr.leave"].create( + { + "holiday_status_id": self.leave_type.id, + "employee_id": self.employee.id, + "date_from": "2024-06-19", + "date_to": "2024-06-19", + } + ) + leave_current.action_validate() + with freeze_time("2024-06-25"): + leave_fututre = self.env["hr.leave"].create( + { + "holiday_status_id": self.leave_type.id, + "employee_id": self.employee.id, + "date_from": "2024-06-25", + "date_to": "2024-06-25", + } + ) + leave_fututre.action_validate() + + with freeze_time("2024-06-23"): + self.assertEqual(emp_allocation.remaining_leaves_days, 1.0) + self.assertEqual(emp_allocation.remaining_leaves_hours, 8.0) + self.assertEqual(emp_allocation.remaining_leaves_display, "1.0 days") + self.assertEqual(emp_allocation.remaining_leaves_current_days, 2.0) + self.assertEqual(emp_allocation.remaining_leaves_current_hours, 16.0) + self.assertEqual( + emp_allocation.remaining_leaves_current_display, "2.0 days" + )