Skip to content

Commit

Permalink
[ADD] #75 wizard for allocation vacations
Browse files Browse the repository at this point in the history
fixes #75
  • Loading branch information
hbrunn committed Feb 26, 2024
1 parent 868dfc8 commit 2914603
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions verdigado_attendance/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import wizards
1 change: 1 addition & 0 deletions verdigado_attendance/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"views/hr_menu_human_resources_configuration.xml",
"views/menu.xml",
"views/res_config_settings.xml",
"wizards/verdigado_holidays_wizard.xml",
],
"demo": [
"demo/res_users.xml",
Expand Down
1 change: 1 addition & 0 deletions verdigado_attendance/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ access_hr_attendance_report_verdigado,hr.attendance.report.verdigado,hr_attendan
access_hr_attendance_user_verdigado,hr.attendance.user.verdigado,hr_attendance.model_hr_attendance,hr_attendance.group_hr_attendance,1,1,1,1
access_resource_calendar_officer_verdigado,resource.calendar.system,resource.model_resource_calendar,hr.group_hr_manager,1,1,1,1
access_resource_calendar_attendance_officer_verdigado,resource.calendar.attendance.system,resource.model_resource_calendar_attendance,hr.group_hr_manager,1,1,1,1
access_verdigado_holidays_wizard,access_verdigado_holidays_wizard,verdigado_attendance.model_verdigado_holidays_wizard,hr.group_hr_manager,1,1,1,1
hr_attendance_break.access_hr_attendance_break,access_hr_attendance_break,model_hr_attendance_break,base.group_user,1,1,1,1
69 changes: 69 additions & 0 deletions verdigado_attendance/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,72 @@ def test_update_overtime_date(self):
}
)
self.assertTrue(overtime.exists())

def _test_holidays_wizard(self):
employee = self.env.ref("hr.employee_qdp")
wizard = (
self.env["verdigado.holidays.wizard"]
.with_context(
active_model="hr.employee",
active_ids=employee.ids,
active_id=employee.id,
)
.create({})
)
action = wizard.button_assign_vacation()
return self.env[action["res_model"]].search(action["domain"])

def test_holidays_wizard_no_validation(self):
"""Test that the holidays wizard creates allocations with slightly changed defaults"""
self.env.ref("hr_holidays.holiday_status_cl").allocation_validation_type = "no"
self.env.ref("hr.employee_qdp").calendar_ids.unlink()
allocation = self._test_holidays_wizard()
self.assertFalse(allocation)

def test_holidays_wizard_25h_week(self):
"""Test an employee with a 2 day week"""
calendar_25h = self.env["resource.calendar"].create(
{
"name": "25h week",
"attendance_ids": [
(
0,
0,
{
"name": "Monday",
"dayofweek": "0",
"hour_from": 8,
"hour_to": 16,
},
),
(
0,
0,
{
"name": "Tuesday",
"dayofweek": "1",
"hour_from": 8,
"hour_to": 16,
},
),
(
0,
0,
{
"name": "Wednesday",
"dayofweek": "2",
"hour_from": 8,
"hour_to": 17,
},
),
],
}
)
self.env.ref("hr.employee_qdp").write(
{
"calendar_ids": [(6, 0, []), (0, 0, {"calendar_id": calendar_25h.id})],
}
)
allocation = self._test_holidays_wizard()
self.assertEqual(allocation.employee_id, self.env.ref("hr.employee_qdp"))
self.assertEqual(allocation.number_of_days, 18)
2 changes: 2 additions & 0 deletions verdigado_attendance/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from . import verdigado_holidays_wizard
82 changes: 82 additions & 0 deletions verdigado_attendance/wizards/verdigado_holidays_wizard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from dateutil.relativedelta import relativedelta

from odoo import _, fields, models


class VerdigadoHolidaysWizard(models.TransientModel):
_name = "verdigado.holidays.wizard"
_description = "Create holidays allocations"

full_vacation = fields.Float(
default=30,
string="Vacation days (100%)",
required=True,
help="Vacation of a FTE in days",
)
date_start = fields.Date(
required=True,
default=lambda self: fields.Date.today()
+ relativedelta(month=1, day=1, years=1),
)
date_end = fields.Date(
required=True,
default=lambda self: fields.Date.today()
+ relativedelta(month=12, day=31, years=1),
)
leave_type_id = fields.Many2one(
"hr.leave.type",
required=True,
default=lambda self: self.env.ref("hr_holidays.holiday_status_cl", False),
)
employee_ids = fields.Many2many("hr.employee", string="Employees")

def button_assign_vacation(self):
days = (self.date_end - self.date_start).days
allocations = self.env["hr.leave.allocation"].browse([])
for employee in self.employee_ids or self.env["hr.employee"].browse(
self.env.context.get("active_ids", [])
):
percentage = 0.0
for calendar in employee.calendar_ids:
if (
calendar.date_start
and calendar.date_start >= self.date_end
or calendar.date_end
and calendar.date_end <= self.date_start
):
continue

Check warning on line 49 in verdigado_attendance/wizards/verdigado_holidays_wizard.py

View check run for this annotation

Codecov / codecov/patch

verdigado_attendance/wizards/verdigado_holidays_wizard.py#L49

Added line #L49 was not covered by tests
week_days = len(
set(calendar.calendar_id.mapped("attendance_ids.dayofweek"))
)
percentage += round(
(
min(calendar.date_end or self.date_end, self.date_end)
- max(calendar.date_start or self.date_start, self.date_start)
).days
/ days,
2,
) * round(float(week_days) / 5, 2)
if percentage:
allocations += self.env["hr.leave.allocation"].create(
{
"name": str(self.date_start.year),
"employee_id": employee.id,
"holiday_status_id": self.leave_type_id.id,
"date_from": self.date_start,
"date_to": self.date_end,
"number_of_days": round(percentage * self.full_vacation, 2),
}
)
allocations.filtered(lambda x: x.state == "draft").action_confirm()
allocations.filtered(
lambda x: x.state in ("confirm", "validate1")
).action_validate()
return {
"type": "ir.actions.act_window",
"res_model": "hr.leave.allocation",
"views": [(False, "list"), (False, "form")],
"domain": [("id", "in", allocations.ids)],
"name": _("Created allocations"),
}
49 changes: 49 additions & 0 deletions verdigado_attendance/wizards/verdigado_holidays_wizard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="verdigado_holiday_wizard_form" model="ir.ui.view">
<field name="model">verdigado.holidays.wizard</field>
<field name="arch" type="xml">
<form>
<div>
This wizard creates allocations proportional to a FTE with following
values
</div>
<group>
<field name="full_vacation" />
<field name="date_start" />
<field name="date_end" />
<field name="leave_type_id" />
<field
name="employee_ids"
widget="many2many_tags"
invisible="context.get('active_model') == 'hr.employee'"
required="context.get('active_model') != 'hr.employee'"
/>
</group>
<footer>
<button
string="Create allocations"
name="button_assign_vacation"
type="object"
class="btn-primary"
/>
<button string="Discard" special="cancel" />
</footer>
</form>
</field>
</record>

<record id="action_verdigado_holiday_wizard" model="ir.actions.act_window">
<field name="name">Allocate vacation</field>
<field name="res_model">verdigado.holidays.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="binding_model_id" ref="hr.model_hr_employee" />
</record>

<menuitem
id="menu_verdigado_holiday_wizard"
parent="hr_holidays.menu_hr_holidays_approvals"
action="action_verdigado_holiday_wizard"
/>
</odoo>

0 comments on commit 2914603

Please sign in to comment.