Skip to content

Commit 127fcf0

Browse files
author
sonhd91
committed
[MIG] hr_holidays_remaining_leaves: Migration to 17.0
1 parent ac28cc7 commit 127fcf0

File tree

9 files changed

+108
-738
lines changed

9 files changed

+108
-738
lines changed

Diff for: hr_holidays_remaining_leaves/LICENSE

-674
This file was deleted.

Diff for: hr_holidays_remaining_leaves/README.rst

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://img.shields.io/badge/licence-GPL--3-blue.svg
2-
:target: http://www.gnu.org/licenses/gpl-3.0-standalone.html
3-
:alt: License: GPL-3
4-
51
============================
62
HR Holidays Remaining Leaves
73
============================

Diff for: hr_holidays_remaining_leaves/__manifest__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
# Copyright 2024 Janik von Rotz <[email protected]>
2+
# Copyright 2024 Camptocamp
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
14
{
25
"name": "HR Holidays Remaining Leaves",
36
"summary": """
47
Show remaining leaves per employee in allocation overview.
58
""",
6-
"author": "Mint System GmbH, Odoo Community Association (OCA)",
9+
"author": "Mint System GmbH, Camptocamp, Odoo Community Association (OCA)",
710
"website": "https://github.com/OCA/hr-holidays",
811
"category": "Human Resources",
9-
"version": "16.0.1.0.0",
12+
"version": "17.0.1.0.0",
1013
"license": "AGPL-3",
1114
"depends": ["hr_holidays"],
1215
"data": ["views/hr_leave_allocation.xml"],

Diff for: hr_holidays_remaining_leaves/models/hr_leave.py

+38-58
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
# Copyright 2024 Janik von Rotz <[email protected]>
2+
# Copyright 2024 Camptocamp
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
14
import logging
25

36
from odoo import _, fields, models
7+
from odoo.tools.float_utils import float_round
48

5-
_logger = logging.getLogger(__name__)
6-
from datetime import datetime
9+
from odoo.addons.resource.models.utils import HOURS_PER_DAY
710

8-
from odoo.tools.float_utils import float_round
11+
_logger = logging.getLogger(__name__)
912

1013

1114
class HolidaysAllocation(models.Model):
@@ -36,72 +39,49 @@ def _get_number_of_days_and_hours(self, date_from, date_to, employee_id):
3639
)[employee.id]
3740

3841
def _compute_remaining_leaves(self):
39-
now = fields.Datetime.now()
40-
now = datetime.combine(now, datetime.min.time())
41-
42+
all_consumed_leaves = self.employee_id._get_consumed_leaves(
43+
self.holiday_status_id
44+
)[0]
45+
all_consumed_leaves_current = self.employee_id._get_consumed_leaves(
46+
self.holiday_status_id, ignore_future=True
47+
)[0]
4248
for allocation in self:
43-
# Get all validated leaves filtered by employee and leave type
44-
leaves = self.env["hr.leave"].search(
45-
[
46-
("employee_id", "=", allocation.employee_id.id),
47-
("state", "=", "validate"),
48-
("holiday_status_id", "=", allocation.holiday_status_id.id),
49-
"|",
50-
("holiday_allocation_id", "=", allocation.id),
51-
("holiday_allocation_id", "=", False),
52-
]
49+
consumes_allo = all_consumed_leaves[allocation.employee_id][
50+
allocation.holiday_status_id
51+
][allocation]
52+
consumes_allo_current = all_consumed_leaves_current[allocation.employee_id][
53+
allocation.holiday_status_id
54+
][allocation]
55+
allocation_calendar = (
56+
allocation.holiday_status_id.company_id.resource_calendar_id
5357
)
54-
55-
# Set the remaining leaves
56-
allocation.remaining_leaves_hours = (
57-
allocation.number_of_hours_display
58-
- sum(leaves.mapped("number_of_hours_display"))
59-
)
60-
allocation.remaining_leaves_days = allocation.number_of_days - sum(
61-
leaves.mapped("number_of_days")
62-
)
63-
64-
# Get past leaves
65-
past_leaves = leaves.filtered(lambda l: l.date_to < now)
66-
past_leave_hours = sum(past_leaves.mapped("number_of_hours_display"))
67-
past_leave_days = sum(past_leaves.mapped("number_of_days"))
68-
69-
# Check for leaves that are active and calculate the exact number of hours and days
70-
active_leave_hours = 0
71-
active_leave_days = 0
72-
for leave in leaves.filtered(lambda l: l.date_from < now < l.date_to):
73-
result = self._get_number_of_days_and_hours(
74-
leave.date_from, now, leave.employee_id.id
75-
)
76-
active_leave_days = result["days"]
77-
active_leave_hours = result["hours"]
78-
79-
allocation.remaining_leaves_current_hours = (
80-
allocation.number_of_hours_display
81-
- past_leave_hours
82-
- active_leave_hours
83-
)
84-
allocation.remaining_leaves_current_days = (
85-
allocation.number_of_days - past_leave_days - active_leave_days
58+
if allocation.holiday_type == "employee" and allocation.employee_id:
59+
allocation_calendar = allocation.employee_id.sudo().resource_calendar_id
60+
allocation.remaining_leaves_days = consumes_allo["remaining_leaves"]
61+
allocation.remaining_leaves_hours = consumes_allo["remaining_leaves"] * (
62+
allocation_calendar.hours_per_day or HOURS_PER_DAY
8663
)
64+
allocation.remaining_leaves_current_days = consumes_allo_current[
65+
"remaining_leaves"
66+
]
67+
allocation.remaining_leaves_current_hours = consumes_allo_current[
68+
"remaining_leaves"
69+
] * (allocation_calendar.hours_per_day or HOURS_PER_DAY)
8770

8871
def _compute_remaining_leaves_display(self):
8972
for allocation in self:
90-
allocation.remaining_leaves_display = "%g %s" % (
91-
(
92-
float_round(allocation.remaining_leaves_hours, precision_digits=2)
93-
if allocation.type_request_unit == "hour"
94-
else float_round(
95-
allocation.remaining_leaves_days, precision_digits=2
96-
)
97-
),
73+
allocation.remaining_leaves_display = "{} {}".format(
74+
float_round(allocation.remaining_leaves_hours, precision_digits=2)
75+
if allocation.type_request_unit == "hour"
76+
else float_round(allocation.remaining_leaves_days, precision_digits=2),
9877
_("hours") if allocation.type_request_unit == "hour" else _("days"),
9978
)
10079

101-
allocation.remaining_leaves_current_display = "%g %s" % (
80+
allocation.remaining_leaves_current_display = "{} {}".format(
10281
(
10382
float_round(
104-
allocation.remaining_leaves_current_hours, precision_digits=2
83+
allocation.remaining_leaves_current_hours,
84+
precision_digits=2,
10585
)
10686
if allocation.type_request_unit == "hour"
10787
else float_round(

Diff for: hr_holidays_remaining_leaves/readme/CONTRIBUTORS.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* Janik von Rotz <[email protected]>
2+

Diff for: hr_holidays_remaining_leaves/readme/CREDITS.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
The original development has been done development of this module has been done by Mint System.
3+
It can be found in: https://github.com/Mint-System/Odoo-Apps-HR/tree/16.0/hr_holidays_remaining_leaves
4+
5+
This module has been ported to the OCA with their agreement

Diff for: hr_holidays_remaining_leaves/readme/USAGE.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show remaining leaves per employee in allocation overview.

Diff for: hr_holidays_remaining_leaves/tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_hr_holidays_remaining_leaves
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2024 Camptocamp
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
from freezegun import freeze_time
4+
5+
from odoo.addons.hr_holidays.tests.test_allocations import TestAllocations
6+
7+
8+
class TestHrHolidaysRemainLeaves(TestAllocations):
9+
@classmethod
10+
def setUpClass(cls):
11+
super().setUpClass()
12+
13+
def test_hr_holidays_remaining_leaves(self):
14+
emp_allocation = self.env["hr.leave.allocation"].create(
15+
{
16+
"name": "Bank Holiday",
17+
"holiday_type": "employee",
18+
"employee_ids": [(4, self.employee.id)],
19+
"employee_id": self.employee.id,
20+
"date_from": "2024-06-17",
21+
"holiday_status_id": self.leave_type.id,
22+
"number_of_days": 3,
23+
"allocation_type": "regular",
24+
}
25+
)
26+
emp_allocation.action_validate()
27+
with freeze_time("2024-06-19"):
28+
leave_current = self.env["hr.leave"].create(
29+
{
30+
"holiday_status_id": self.leave_type.id,
31+
"employee_id": self.employee.id,
32+
"date_from": "2024-06-19",
33+
"date_to": "2024-06-19",
34+
}
35+
)
36+
leave_current.action_validate()
37+
with freeze_time("2024-06-25"):
38+
leave_fututre = self.env["hr.leave"].create(
39+
{
40+
"holiday_status_id": self.leave_type.id,
41+
"employee_id": self.employee.id,
42+
"date_from": "2024-06-25",
43+
"date_to": "2024-06-25",
44+
}
45+
)
46+
leave_fututre.action_validate()
47+
48+
with freeze_time("2024-06-23"):
49+
self.assertEqual(emp_allocation.remaining_leaves_days, 1.0)
50+
self.assertEqual(emp_allocation.remaining_leaves_hours, 8.0)
51+
self.assertEqual(emp_allocation.remaining_leaves_display, "1.0 days")
52+
self.assertEqual(emp_allocation.remaining_leaves_current_days, 2.0)
53+
self.assertEqual(emp_allocation.remaining_leaves_current_hours, 16.0)
54+
self.assertEqual(
55+
emp_allocation.remaining_leaves_current_display, "2.0 days"
56+
)

0 commit comments

Comments
 (0)