Skip to content

Commit

Permalink
[MIG] hr_holidays_natural_period: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
peluko00 committed Jul 23, 2024
1 parent 568b0a2 commit df0211e
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 45 deletions.
4 changes: 4 additions & 0 deletions hr_holidays_natural_period/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ Contributors
- Víctor Martínez
- Pedro Baeza

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

- Antoni Marroig <[email protected]>

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion hr_holidays_natural_period/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
2 changes: 1 addition & 1 deletion hr_holidays_natural_period/demo/hr_leave_type_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<record id="hr_leave_type_natural_day_test" model="hr.leave.type">
<field name="name">Test Time Off (natural day)</field>
<field name="request_unit">natural_day</field>
<field name="responsible_id" ref="base.user_admin" />
<field name="responsible_ids" eval="[(4, ref('base.user_admin'))]" />
<field name="employee_requests">yes</field>
</record>
</odoo>
2 changes: 2 additions & 0 deletions hr_holidays_natural_period/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions hr_holidays_natural_period/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions hr_holidays_natural_period/models/hr_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
12 changes: 12 additions & 0 deletions hr_holidays_natural_period/models/hr_leave_allocation.py
Original file line number Diff line number Diff line change
@@ -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")],
)
19 changes: 0 additions & 19 deletions hr_holidays_natural_period/models/hr_leave_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions hr_holidays_natural_period/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions hr_holidays_natural_period/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

> - Víctor Martínez
> - Pedro Baeza
- APSL-Nagarro \<<https://www.apsl.tech>\>

> - Antoni Marroig \<<[email protected]>\>
7 changes: 7 additions & 0 deletions hr_holidays_natural_period/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
</ul>
</blockquote>
</li>
<li><p class="first">APSL-Nagarro &lt;<a class="reference external" href="https://www.apsl.tech">https://www.apsl.tech</a>&gt;</p>
<blockquote>
<ul class="simple">
<li>Antoni Marroig &lt;<a class="reference external" href="mailto:amarroig&#64;apsl.net">amarroig&#64;apsl.net</a>&gt;</li>
</ul>
</blockquote>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
41 changes: 24 additions & 17 deletions hr_holidays_natural_period/tests/test_hr_leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def setUpClass(cls):
partner = cls.env["res.partner"].create(
{
"name": "Test employee",
"type": "private",
"type": "other",
"country_id": cls.env.ref("base.es").id,
}
)
cls.user = new_test_user(cls.env, login="test-user")
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,
}
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit df0211e

Please sign in to comment.