Skip to content

Commit

Permalink
[MIG] hr_holidays_credit: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dsolanki-initos committed Jul 4, 2023
1 parent fe1ff30 commit f59764c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 41 deletions.
2 changes: 2 additions & 0 deletions hr_holidays_credit/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Contributors
* Watcharaporn Charamrum <[email protected]>
* Tharathip Chaweewongphan <[email protected]>

* Dhara Solanki <[email protected]>

Maintainers
~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion hr_holidays_credit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
"name": "Leave Credit",
"version": "14.0.1.0.0",
"version": "15.0.1.0.0",
"category": "Human Resources",
"website": "https://github.com/OCA/hr-holidays",
"author": "CorporateHub, Odoo Community Association (OCA)",
Expand Down
1 change: 1 addition & 0 deletions hr_holidays_credit/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from . import hr_leave_type
from . import hr_leave
from . import hr_leave_allocation
19 changes: 16 additions & 3 deletions hr_holidays_credit/models/hr_leave.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
# Copyright (C) 2018 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo import api, fields, models


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

holiday_status_id = fields.Many2one(
domain=[
"|",
("has_valid_allocation", "=", True),
"|",
("requires_allocation", "=", "no"),
("allow_credit", "=", True),
]
)

@api.constrains("state", "number_of_days", "holiday_status_id")
def _check_holidays(self):
uncreditable_requests = self.filtered(
lambda holiday: not holiday._is_holiday_credit_allowed()
)

super(HrLeave, uncreditable_requests)._check_holidays()
return super(HrLeave, uncreditable_requests)._check_holidays()

def _is_holiday_credit_allowed(self):
self.ensure_one()

leave_type = self.holiday_status_id
hr_leave_employees = set(self.employee_ids.ids)
hr_leave_type_employees = set(leave_type.creditable_employee_ids.ids)
check_same_employees = hr_leave_employees.issubset(hr_leave_type_employees)

if not leave_type.allow_credit:
return False

if self.employee_id in leave_type.creditable_employee_ids:
if check_same_employees:
return True

if self.employee_id in (
Expand Down
17 changes: 17 additions & 0 deletions hr_holidays_credit/models/hr_leave_allocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2018 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo.osv import expression


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

def _domain_holiday_status_id(self):
res = super(HolidaysAllocation, self)._domain_holiday_status_id()
return expression.OR([[("allow_credit", "=", True)], res])

holiday_status_id = fields.Many2one(
"hr.leave.type", domain=_domain_holiday_status_id
)
38 changes: 36 additions & 2 deletions hr_holidays_credit/models/hr_leave_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class HrLeaveType(models.Model):
_inherit = "hr.leave.type"

allow_credit = fields.Boolean(
string="Allow Credit",
help=(
"If set to true, employees would be able to make requests for this"
" leave type even if allocated amount is insufficient."
Expand Down Expand Up @@ -42,7 +41,7 @@ def name_get(self):
record_name = record.name

extra = None
if record.allocation_type != "no" and context_employee_id:
if record.requires_allocation != "no" and context_employee_id:
if record.virtual_remaining_leaves >= 0:
if record.allow_credit:
extra = _("%g available + credit")
Expand All @@ -69,3 +68,38 @@ def name_get(self):
res.append((record.id, record_name))

return res

def get_employees_days(self, employee_ids, date=None):
"""Use this method to compute the virtual remaining leaves
when the allow credit is enabled. In Odoo standard v15, it will
calculate only when the allocation is set."""
res = super().get_employees_days(employee_ids=employee_ids)
for record in self:
hr_leave = self.env["hr.leave"].search(
[
("employee_id", "in", employee_ids),
("state", "in", ["confirm", "validate1", "validate"]),
("holiday_status_id.id", "=", record.id),
]
)
allocations = (
self.env["hr.leave.allocation"]
.with_context(active_test=False)
.search(
[
("employee_id", "in", employee_ids),
("state", "in", ["validate"]),
("holiday_status_id", "in", record.ids),
]
)
)

for hr_leave_record in hr_leave:
if record.allow_credit and not allocations:
virtual_remaining_leaves = res[hr_leave_record.employee_id.id][
record.id
]["virtual_remaining_leaves"]
res[hr_leave_record.employee_id.id][record.id][
"virtual_remaining_leaves"
] = (virtual_remaining_leaves - hr_leave_record.number_of_days)
return res
2 changes: 2 additions & 0 deletions hr_holidays_credit/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

* Watcharaporn Charamrum <[email protected]>
* Tharathip Chaweewongphan <[email protected]>

* Dhara Solanki <[email protected]>
58 changes: 28 additions & 30 deletions hr_holidays_credit/tests/test_hr_holidays_credit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,16 @@ def setUp(self):
self.SudoLeave = self.Leave.sudo()

def test_1(self):
employee = self.SudoEmployee.create({"name": "Employee #1"})
employee = self.SudoEmployee.create({"name": "Employee #2"})
leave_type = self.SudoLeaveType.create(
{
"name": "Leave Type #1",
"allocation_type": "fixed",
"allow_credit": False,
"name": "Leave Type #2",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": True,
}
)

with self.assertRaises(ValidationError):
self.SudoLeave.create(
{
"holiday_status_id": leave_type.id,
"holiday_type": "employee",
"employee_id": employee.id,
"number_of_days": 1,
}
)

def test_2(self):
employee = self.SudoEmployee.create({"name": "Employee #2"})
leave_type = self.SudoLeaveType.create(
{"name": "Leave Type #2", "allocation_type": "fixed", "allow_credit": True}
)

self.SudoLeave.create(
{
"holiday_status_id": leave_type.id,
Expand All @@ -57,7 +42,7 @@ def test_2(self):
}
)

def test_3(self):
def test_2(self):
department = self.SudoDepartment.create({"name": "Department #3"})
employee_1 = self.SudoEmployee.create(
{"name": "Employee #3-1", "department_id": department.id}
Expand All @@ -66,7 +51,8 @@ def test_3(self):
leave_type = self.SudoLeaveType.create(
{
"name": "Leave Type #3",
"allocation_type": "fixed",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": True,
"creditable_department_ids": [(6, False, [department.id])],
}
Expand All @@ -91,13 +77,14 @@ def test_3(self):
}
)

def test_4(self):
def test_3(self):
employee_1 = self.SudoEmployee.create({"name": "Employee #4-1"})
employee_2 = self.SudoEmployee.create({"name": "Employee #4-2"})
leave_type = self.SudoLeaveType.create(
{
"name": "Leave Type #4",
"allocation_type": "fixed",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": True,
"creditable_employee_ids": [(6, False, [employee_1.id])],
}
Expand All @@ -122,12 +109,13 @@ def test_4(self):
}
)

def test_5(self):
def test_4(self):
employee = self.SudoEmployee.create({"name": "Employee #5"})
leave_type = self.SudoLeaveType.create(
{
"name": "Leave Type #5",
"allocation_type": "fixed",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": False,
}
)
Expand All @@ -138,21 +126,31 @@ def test_5(self):
self.assertTrue("available" in name)
self.assertTrue("credit" not in name)

def test_6(self):
def test_5(self):
employee = self.SudoEmployee.create({"name": "Employee #6"})
leave_type = self.SudoLeaveType.create(
{"name": "Leave Type #6", "allocation_type": "fixed", "allow_credit": True}
{
"name": "Leave Type #6",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": True,
}
)

name = leave_type.with_context(employee_id=employee.id,).name_get()[
0
][1]
self.assertTrue("available + credit" in name)

def test_7(self):
def test_6(self):
employee = self.SudoEmployee.create({"name": "Employee #7"})
leave_type = self.SudoLeaveType.create(
{"name": "Leave Type #7", "allocation_type": "fixed", "allow_credit": True}
{
"name": "Leave Type #7",
"requires_allocation": "yes",
"allocation_validation_type": "officer",
"allow_credit": True,
}
)
self.SudoLeave.create(
{
Expand Down
10 changes: 5 additions & 5 deletions hr_holidays_credit/views/hr_leave_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
<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="allocation_type" position="after">
<field name="allocation_validation_type" position="after">
<field
name="allow_credit"
attrs="{'invisible': [('allocation_type', '=', 'no')]}"
attrs="{'invisible': ['|',('requires_allocation', '=', 'no'),('allocation_validation_type','=','no')]}"
/>
<field
name="creditable_employee_ids"
widget="many2many_tags"
attrs="{'invisible': [('allow_credit', '!=', True)]}"
attrs="{'invisible': ['|',('allow_credit', '!=', True), '|', ('allocation_validation_type','=','no'),('requires_allocation', '=', 'no')]}"
/>
<field
name="creditable_employee_category_ids"
widget="many2many_tags"
attrs="{'invisible': [('allow_credit', '!=', True)]}"
attrs="{'invisible': ['|',('allow_credit', '!=', True),'|', ('allocation_validation_type','=','no'),('requires_allocation', '=', 'no')]}"
/>
<field
name="creditable_department_ids"
widget="many2many_tags"
attrs="{'invisible': [('allow_credit', '!=', True)]}"
attrs="{'invisible': ['|',('allow_credit', '!=', True),'|', ('allocation_validation_type','=','no'),('requires_allocation', '=', 'no')]}"
/>
</field>
</field>
Expand Down

0 comments on commit f59764c

Please sign in to comment.