diff --git a/hr_holidays_credit/README.rst b/hr_holidays_credit/README.rst index 4399374c..7184113a 100644 --- a/hr_holidays_credit/README.rst +++ b/hr_holidays_credit/README.rst @@ -63,6 +63,8 @@ Contributors * Watcharaporn Charamrum * Tharathip Chaweewongphan +* Dhara Solanki + Maintainers ~~~~~~~~~~~ diff --git a/hr_holidays_credit/__manifest__.py b/hr_holidays_credit/__manifest__.py index b50bdc0b..6a2ceeba 100644 --- a/hr_holidays_credit/__manifest__.py +++ b/hr_holidays_credit/__manifest__.py @@ -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)", diff --git a/hr_holidays_credit/models/__init__.py b/hr_holidays_credit/models/__init__.py index 5501fafd..855d8174 100644 --- a/hr_holidays_credit/models/__init__.py +++ b/hr_holidays_credit/models/__init__.py @@ -2,3 +2,4 @@ from . import hr_leave_type from . import hr_leave +from . import hr_leave_allocation diff --git a/hr_holidays_credit/models/hr_leave.py b/hr_holidays_credit/models/hr_leave.py index f727a919..c9b5287f 100644 --- a/hr_holidays_credit/models/hr_leave.py +++ b/hr_holidays_credit/models/hr_leave.py @@ -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 ( diff --git a/hr_holidays_credit/models/hr_leave_allocation.py b/hr_holidays_credit/models/hr_leave_allocation.py new file mode 100644 index 00000000..a71b0ef9 --- /dev/null +++ b/hr_holidays_credit/models/hr_leave_allocation.py @@ -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 + ) diff --git a/hr_holidays_credit/models/hr_leave_type.py b/hr_holidays_credit/models/hr_leave_type.py index 8063ce23..3c2d8d57 100644 --- a/hr_holidays_credit/models/hr_leave_type.py +++ b/hr_holidays_credit/models/hr_leave_type.py @@ -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." @@ -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") @@ -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 diff --git a/hr_holidays_credit/readme/CONTRIBUTORS.rst b/hr_holidays_credit/readme/CONTRIBUTORS.rst index fc1ec734..3d09304f 100644 --- a/hr_holidays_credit/readme/CONTRIBUTORS.rst +++ b/hr_holidays_credit/readme/CONTRIBUTORS.rst @@ -6,3 +6,5 @@ * Watcharaporn Charamrum * Tharathip Chaweewongphan + +* Dhara Solanki diff --git a/hr_holidays_credit/tests/test_hr_holidays_credit.py b/hr_holidays_credit/tests/test_hr_holidays_credit.py index b2b4b134..29a737c9 100644 --- a/hr_holidays_credit/tests/test_hr_holidays_credit.py +++ b/hr_holidays_credit/tests/test_hr_holidays_credit.py @@ -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, @@ -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} @@ -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])], } @@ -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])], } @@ -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, } ) @@ -138,10 +126,15 @@ 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()[ @@ -149,10 +142,15 @@ def test_6(self): ][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( { diff --git a/hr_holidays_credit/views/hr_leave_type.xml b/hr_holidays_credit/views/hr_leave_type.xml index 466d26c5..e7928024 100644 --- a/hr_holidays_credit/views/hr_leave_type.xml +++ b/hr_holidays_credit/views/hr_leave_type.xml @@ -9,25 +9,25 @@ hr.leave.type - +