Skip to content
Permalink
Browse files

[FIX] hr_payroll: Set benefit type on resource leave

Currently, when creating `resource.calendar.leaves` for a
`hr.leave` it creates one in the employee's calendar and one
in the contract's calendar if it's different.
But if both calendar are the same, it does not associate
the benefit type to the resource leave.

This commit fixes the issue and add unit tests for this use
case.
  • Loading branch information...
LucasLefevre committed Feb 20, 2019
1 parent 2140ccb commit db485ab5fb771d3ad3db60c815c1c54e798fd4bd
@@ -568,12 +568,15 @@ def copy_data(self, default=None):

@api.multi
def _create_resource_leave(self):
""" This method will create entry in resource calendar time off object at the time of holidays validated """
""" This method will create entry in resource calendar time off object at the time of holidays validated
:returns: created `resource.calendar.leaves`
"""
vals_list = []
for leave in self:
date_from = fields.Datetime.from_string(leave.date_from)
date_to = fields.Datetime.from_string(leave.date_to)

self.env['resource.calendar.leaves'].create({
vals_list.append({
'name': leave.name,
'date_from': fields.Datetime.to_string(date_from),
'holiday_id': leave.id,
@@ -582,7 +585,7 @@ def _create_resource_leave(self):
'calendar_id': leave.employee_id.resource_calendar_id.id,
'time_type': leave.holiday_status_id.time_type,
})
return True
return self.env['resource.calendar.leaves'].create(vals_list)

@api.multi
def _remove_resource_leave(self):
@@ -43,7 +43,10 @@ def _create_resource_leave(self):
This is needed in order to compute the correct number of hours/days of the leave
according to the contract's calender.
"""
super(HrLeave, self)._create_resource_leave()
resource_leaves = super(HrLeave, self)._create_resource_leave()
for resource_leave in resource_leaves:
resource_leave.benefit_type_id = resource_leave.holiday_id.holiday_status_id.benefit_type_id.id

resource_leave_values = []

for leave in self.filtered(lambda l: l.employee_id):
@@ -61,8 +64,7 @@ def _create_resource_leave(self):
'calendar_id': contract.resource_calendar_id.id,
}]

self.env['resource.calendar.leaves'].create(resource_leave_values)
return True
return resource_leaves | self.env['resource.calendar.leaves'].create(resource_leave_values)

@api.constrains('date_from', 'date_to')
def _check_contracts(self):
@@ -5,3 +5,4 @@
from . import test_benefit
from . import test_multi_contract
from . import test_calendar_sync
from . import test_leave
@@ -0,0 +1,53 @@
# # -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from datetime import datetime
import pytz
from dateutil.relativedelta import relativedelta
from odoo import exceptions
from odoo.tests.common import tagged
from odoo.addons.hr_payroll.tests.common import TestPayslipBase


class TestPayrollLeave(TestPayslipBase):

def create_leave(self):
return self.env['hr.leave'].create({
'name': 'Holiday !!!',
'employee_id': self.richard_emp.id,
'holiday_status_id': self.leave_type.id,
'date_to': datetime.today() + relativedelta(days=1),
'date_from': datetime.today(),
'number_of_days': 1,
})

def test_resource_leave_has_benefit_type(self):
leave = self.create_leave()

resource_leave = leave._create_resource_leave()
self.assertEqual(resource_leave.benefit_type_id, self.leave_type.benefit_type_id, "it should have the corresponding benefit type")

def test_resource_leave_in_contract_calendar(self):
other_calendar = self.env['resource.calendar'].create({'name': 'New calendar'})
contract = self.richard_emp.contract_ids[0]
contract.resource_calendar_id = other_calendar
contract.state = 'open' # this set richard's calendar to New calendar
leave = self.create_leave()

resource_leave = leave._create_resource_leave()
self.assertEqual(len(resource_leave), 1, "it should have created only one resource leave")
self.assertEqual(resource_leave.benefit_type_id, self.leave_type.benefit_type_id, "it should have the corresponding benefit type")

def test_resource_leave_different_calendars(self):
other_calendar = self.env['resource.calendar'].create({'name': 'New calendar'})
contract = self.richard_emp.contract_ids[0]
contract.resource_calendar_id = other_calendar
contract.state = 'open' # this set richard's calendar to New calendar

# set another calendar
self.richard_emp.resource_calendar_id = self.env['resource.calendar'].create({'name': 'Other calendar'})

leave = self.create_leave()
resource_leave = leave._create_resource_leave()
self.assertEqual(len(resource_leave), 2, "it should have created one resource leave per calendar")
self.assertEqual(resource_leave.mapped('benefit_type_id'), self.leave_type.benefit_type_id, "they should have the corresponding benefit type")

0 comments on commit db485ab

Please sign in to comment.
You can’t perform that action at this time.