Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] hr_holidays_auto_extend #67

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hr_holidays_auto_extend/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: https://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=======================
Hr Holidays Auto Extend
=======================

Allow to extend some kind of holidays
1 change: 1 addition & 0 deletions hr_holidays_auto_extend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
22 changes: 22 additions & 0 deletions hr_holidays_auto_extend/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 CreuBlanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Hr Holidays Auto Extend",
"summary": """
Allow to extend some kind of holidays""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "CreuBlanca,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/hr-holidays",
"depends": [
"hr_holidays",
],
"data": [
"views/hr_leave.xml",
"views/hr_leave_type.xml",
"data/cron.xml",
"data/mail.xml",
],
"demo": [],
}
13 changes: 13 additions & 0 deletions hr_holidays_auto_extend/data/cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<record model="ir.cron" id="auto_extend_cron">
<field name="name">Holidays: Auto extend</field>
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="model_id" ref="model_hr_leave" />
<field name="state">code</field>
<field name="code">model._cron_auto_extend()</field>
</record>
</odoo>
13 changes: 13 additions & 0 deletions hr_holidays_auto_extend/data/mail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
<record id="mail_activity_error_auto_extend" model="mail.activity.type">
<field name="name">Leave cannot be extended due to overlapping</field>
<field name="summary">note</field>
<field name="category">default</field>
<field name="res_model_id" ref="hr_holidays.model_hr_leave" />
<field name="icon">fa-tasks</field>
<field name="delay_count">0</field>
</record>
</data>
</odoo>
2 changes: 2 additions & 0 deletions hr_holidays_auto_extend/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import hr_leave_type
from . import hr_leave
78 changes: 78 additions & 0 deletions hr_holidays_auto_extend/models/hr_leave.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2023 CreuBlanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import timedelta

from odoo import api, fields, models


class HrLeave(models.Model):
_inherit = "hr.leave"

auto_extend_type = fields.Boolean(
related="holiday_status_id.auto_extend", string="Auto extend type"
)
auto_extend = fields.Boolean(
compute="_compute_auto_extend", store=True, readonly=False
)
auto_extend_period = fields.Integer(
compute="_compute_auto_extend", store=True, readonly=False
)

def _check_date_state(self):
if not self.env.context.get("__no_check_state_date"):
super()._check_date_state()

Check warning on line 24 in hr_holidays_auto_extend/models/hr_leave.py

View check run for this annotation

Codecov / codecov/patch

hr_holidays_auto_extend/models/hr_leave.py#L24

Added line #L24 was not covered by tests

def _get_number_of_days(self, date_from, date_to, employee_id):
"""Returns a float equals to the timedelta between two dates given as string.
We need to modify in order to add the compute_leaves = False
"""
if not self.env.context.get("__no_check_state_date") or not employee_id:
return super()._get_number_of_days(date_from, date_to, employee_id)
employee = self.env["hr.employee"].browse(employee_id)
return employee._get_work_days_data_batch(
date_from, date_to, compute_leaves=False
)[employee.id]

@api.depends("holiday_status_id")
def _compute_auto_extend(self):
for record in self:
record.auto_extend = record.holiday_status_id.auto_extend
record.auto_extend_period = record.holiday_status_id.auto_extend_period

def _cron_auto_extend_domain(self):
return [
("request_date_to", "<=", fields.Date.today()),
("auto_extend", "=", True),
("auto_extend_period", ">", 0),
("state", "=", "validate"),
]

def _cron_auto_extend(self):
leaves = self.search(self._cron_auto_extend_domain())
for leave in leaves.with_context(__no_check_state_date=True):
request_date_to = leave.request_date_to + timedelta(
days=leave.auto_extend_period
)
domain = [
("date_from", "<", request_date_to),
("date_from", ">", leave.date_from),
("employee_id", "=", leave.employee_id.id),
("id", "!=", leave.id),
("state", "not in", ["cancel", "refuse"]),
]
if self.search(domain, limit=1):
leave.auto_extend = False
leave.activity_schedule(
"hr_holidays_auto_extend.mail_activity_error_auto_extend",
)
continue
vals = {"request_date_to": request_date_to}
vals.update(
leave.onchange(vals, ["request_date_to"], leave._onchange_spec())[
"value"
]
)
leave.write(vals)
leave._remove_resource_leave()
leave._create_resource_leave()
11 changes: 11 additions & 0 deletions hr_holidays_auto_extend/models/hr_leave_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2023 CreuBlanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class HrLeaveType(models.Model):
_inherit = "hr.leave.type"

auto_extend = fields.Boolean()
auto_extend_period = fields.Integer(default=7)
2 changes: 2 additions & 0 deletions hr_holidays_auto_extend/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Enric Tobella
*
3 changes: 3 additions & 0 deletions hr_holidays_auto_extend/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
With this module, we will be able to extend automatically a leave when the period is reached.

This might be necessary for leaves without a clear return date for the employee.
4 changes: 4 additions & 0 deletions hr_holidays_auto_extend/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* Access leave types and mark it as "Auto extendable"
* When the manager approves the leave, the "Auto extendable" check will be marked
* When we decide that the leave is no longer extendable, we can uncheck it.
*
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions hr_holidays_auto_extend/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_extend
89 changes: 89 additions & 0 deletions hr_holidays_auto_extend/tests/test_extend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2023 CreuBlanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import timedelta

from odoo import fields
from odoo.tests.common import Form

from odoo.addons.hr_holidays.tests.common import TestHrHolidaysCommon


class TestExtend(TestHrHolidaysCommon):
def test_time_type(self):
leave_type = self.env["hr.leave.type"].create(
{
"name": "Paid Time Off",
"time_type": "leave",
"auto_extend": True,
"allocation_type": "no",
"validity_start": False,
}
)
date_to = fields.Date.today() - timedelta(days=2)
f = Form(self.env["hr.leave"])
f.name = "Doctor Appointment"
f.employee_id = self.env["hr.employee"].browse(self.employee_hruser_id)
f.holiday_status_id = leave_type
f.request_date_from = fields.Date.today() - timedelta(days=5)
f.request_date_to = date_to
leave_1 = f.save()
self.assertEqual(leave_1.request_date_to, date_to)
self.env["hr.leave"]._cron_auto_extend()
self.assertEqual(leave_1.request_date_to, date_to)
leave_1.action_approve()
self.assertEqual(leave_1.request_date_to, date_to)
self.assertEqual(
self.env["resource.calendar.leaves"]
.search([("holiday_id", "=", leave_1.id)])
.time_type,
"leave",
)
self.env["hr.leave"]._cron_auto_extend()
self.assertEqual(leave_1.request_date_to, date_to + timedelta(days=7))

def test_extend_overlap(self):
leave_type = self.env["hr.leave.type"].create(
{
"name": "Paid Time Off",
"time_type": "leave",
"auto_extend": True,
"allocation_type": "no",
"validity_start": False,
}
)
leave_type_02 = self.env["hr.leave.type"].create(
{
"name": "Another leave type",
"time_type": "leave",
"allocation_type": "no",
"validity_start": False,
}
)
date_to = fields.Date.today() - timedelta(days=2)
f = Form(self.env["hr.leave"])
f.name = "Doctor Appointment"
f.employee_id = self.env["hr.employee"].browse(self.employee_hruser_id)
f.holiday_status_id = leave_type
f.request_date_from = fields.Date.today() - timedelta(days=5)
f.request_date_to = date_to
leave_1 = f.save()

f2 = Form(self.env["hr.leave"])
f2.name = "Doctor Appointment"
f2.employee_id = self.env["hr.employee"].browse(self.employee_hruser_id)
f2.holiday_status_id = leave_type_02
f2.request_date_from = fields.Date.today() - timedelta(days=1)
f2.request_date_to = fields.Date.today()
leave_2 = f2.save()
self.assertTrue(leave_1.auto_extend)
self.assertFalse(leave_2.auto_extend)
leave_1.action_approve()
leave_2.action_approve()
self.assertTrue(leave_1.auto_extend)
self.assertFalse(leave_1.activity_ids)
self.env["hr.leave"]._cron_auto_extend()
leave_1.flush()
leave_1.refresh()
self.assertFalse(leave_1.auto_extend)
self.assertTrue(leave_1.activity_ids)
30 changes: 30 additions & 0 deletions hr_holidays_auto_extend/views/hr_leave.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 CreuBlanca
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="hr_leave_form_view">
<field name="name">hr.leave.form (in hr_holidays_auto_extend)</field>
<field name="model">hr.leave</field>
<field name="inherit_id" ref="hr_holidays.hr_leave_view_form_manager" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="auto_extend_type" invisible="1" />
<field
name="auto_extend"
groups="hr_holidays.group_hr_holidays_user"
widget="boolean_toggle"
attrs="{'invisible':[('auto_extend_type', '=', False), ('auto_extend', '=', False)]}"
/>
<field
name="auto_extend_period"
groups="hr_holidays.group_hr_holidays_user"
attrs="{'invisible':[('auto_extend', '=', False)]}"
/>
</field>
</field>
</record>



</odoo>
23 changes: 23 additions & 0 deletions hr_holidays_auto_extend/views/hr_leave_type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 CreuBlanca
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="hr_leave_type_form_view">
<field name="name">hr.leave.type.form (in hr_holidays_auto_extend)</field>
<field name="model">hr.leave.type</field>
<field name="inherit_id" ref="hr_holidays.edit_holiday_status_form" />
<field name="arch" type="xml">
<field name="request_unit" position="after">
<field name="auto_extend" widget="boolean_toggle" />
<field
name="auto_extend_period"
attrs="{'invisible':[('auto_extend', '=', False)]}"
/>
</field>
</field>
</record>



</odoo>
6 changes: 6 additions & 0 deletions setup/hr_holidays_auto_extend/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading