Skip to content

Commit

Permalink
[MIG] hr_holidays_remaining_leaves: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhd91 committed Jun 17, 2024
1 parent ac28cc7 commit 127fcf0
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 738 deletions.
674 changes: 0 additions & 674 deletions hr_holidays_remaining_leaves/LICENSE

This file was deleted.

4 changes: 0 additions & 4 deletions hr_holidays_remaining_leaves/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://img.shields.io/badge/licence-GPL--3-blue.svg
:target: http://www.gnu.org/licenses/gpl-3.0-standalone.html
:alt: License: GPL-3

============================
HR Holidays Remaining Leaves
============================
Expand Down
7 changes: 5 additions & 2 deletions hr_holidays_remaining_leaves/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright 2024 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "HR Holidays Remaining Leaves",
"summary": """
Show remaining leaves per employee in allocation overview.
""",
"author": "Mint System GmbH, Odoo Community Association (OCA)",
"author": "Mint System GmbH, Camptocamp, 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"],
Expand Down
96 changes: 38 additions & 58 deletions hr_holidays_remaining_leaves/models/hr_leave.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Copyright 2024 Janik von Rotz <[email protected]>
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
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):
Expand Down Expand Up @@ -36,72 +39,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(
Expand Down
2 changes: 2 additions & 0 deletions hr_holidays_remaining_leaves/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Janik von Rotz <[email protected]>
* Son Ho <[email protected]>
5 changes: 5 additions & 0 deletions hr_holidays_remaining_leaves/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions hr_holidays_remaining_leaves/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show remaining leaves per employee in allocation overview.
1 change: 1 addition & 0 deletions hr_holidays_remaining_leaves/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_hr_holidays_remaining_leaves
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
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"
)

0 comments on commit 127fcf0

Please sign in to comment.