[FIX] hr_work_entry_contract: fix traceback when no contract for employee

This traceback arises when the user changes the date with a new employee
of no contract, while creating a new Work Entry.

To reproduce this issue:

Install "hr_work_entry_contract"
1) Open payroll and create a new "Work Entry"
2) Select Work Entry Type as Unpaid and create a new employee
3) Now try to change the date

Error:- 'KeyError: False'

On the '_get_duration_batch' method, when the user creates a new employee
without a contract, the calendar remains an empty recordset.

which leads to the traceback from the below lines.

https://github.com/odoo/odoo/blob/a277faa2ffab7559fcbad95fcc1e8fd6a26d756b/addons/hr_work_entry_contract/models/hr_work_entry.py#L110-L112

After applying this commit will resolve the issue by making code more robust and
give default value.

sentry-5095168051

closes odoo/odoo#159958

X-original-commit: dff6e723580162044d25e98e0394d28e00bab520
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
alsh-odoo
2024-04-02 12:09:33 +00:00
parent 5b55bb9df2
commit 15de3f7bf3
2 changed files with 19 additions and 2 deletions
@@ -116,7 +116,7 @@ class HrWorkEntry(models.Model):
date_stop = work_entry.date_stop
calendar = work_entry.contract_id.resource_calendar_id
employee = work_entry.contract_id.employee_id
result[work_entry.id] = mapped_contract_data[(date_start, date_stop)][calendar][employee.id]['hours']
result[work_entry.id] = mapped_contract_data[(date_start, date_stop)][calendar][employee.id]['hours'] if calendar else 0.0
return result
@api.model
@@ -6,7 +6,8 @@ from dateutil.relativedelta import relativedelta
from psycopg2 import IntegrityError
import pytz
from odoo.tests.common import tagged
from odoo.exceptions import ValidationError
from odoo.tests.common import tagged, Form
from odoo.tools import mute_logger
from odoo.addons.hr_work_entry_contract.tests.common import TestWorkEntryBase
@@ -242,3 +243,19 @@ class TestWorkEntry(TestWorkEntryBase):
hk_employee.generate_work_entries(datetime(2023, 8, 1), datetime(2023, 8, 1))
work_entries = self.env['hr.work.entry'].search([('employee_id', '=', hk_employee.id)])
self.assertEqual(work_entries[0].date_start, datetime(2023, 7, 31, 23, 0))
def test_work_entry_employee_without_contract(self):
""" Test work entries by creating an employee without contract which leads to trigger a constraint. """
new_employee = self.env['hr.employee'].create({
'name': 'New employee'
})
work_entry = Form(self.env['hr.work.entry'])
work_entry.date_start = self.start
work_entry.employee_id = new_employee
work_entry.work_entry_type_id = self.work_entry_type_leave
work_entry.date_stop = self.end
with self.assertRaises(ValidationError):
work_entry.save()
self.assertEqual(work_entry.employee_id.id, new_employee.id)
self.assertEqual(work_entry.duration, 0.0)