Repository: OCA/hr-attendance (language: python)

Issue:
error related to auto_close in V14
hi,

receiving this error for all employee ( non-admin) when applying time-off
Admin user however can apply. Not sure whats broken here..

Please let me know wats the work around to have this working ?
used hr-autoclose and hr-reason modules.


```
Error:
Odoo Server Error

Traceback (most recent call last):
  File "/odoo/odoo-server/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
    result = request.dispatch()
  File "/odoo/odoo-server/odoo/http.py", line 683, in dispatch
    result = self._call_function(**self.params)
  File "/odoo/odoo-server/odoo/http.py", line 359, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/odoo/odoo-server/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/odoo/odoo-server/odoo/http.py", line 347, in checked_call
    result = self.endpoint(*a, **kw)
  File "/odoo/odoo-server/odoo/http.py", line 912, in __call__
    return self.method(*args, **kw)
  File "/odoo/odoo-server/odoo/http.py", line 531, in response_wrap
    response = f(*args, **kw)
  File "/odoo/odoo-server/addons/web/controllers/main.py", line 1389, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/odoo/odoo-server/addons/web/controllers/main.py", line 1381, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/odoo/odoo-server/odoo/api.py", line 396, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/odoo/odoo-server/odoo/api.py", line 383, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/odoo/odoo-server/odoo/models.py", line 6236, in onchange
    for name in nametree
  File "/odoo/odoo-server/odoo/models.py", line 6237, in <listcomp>
    if name not in done and snapshot0.has_changed(name)
  File "/odoo/odoo-server/odoo/models.py", line 6068, in has_changed
    return self[name] != record[name]
  File "/odoo/odoo-server/odoo/models.py", line 5666, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/odoo/odoo-server/odoo/fields.py", line 1019, in __get__
    self.compute_value(recs)
  File "/odoo/odoo-server/odoo/fields.py", line 1175, in compute_value
    records._compute_field_value(self)
  File "/odoo/odoo-server/addons/mail/models/mail_thread.py", line 410, in _compute_field_value
    return super()._compute_field_value(field)
  File "/odoo/odoo-server/odoo/models.py", line 4061, in _compute_field_value
    getattr(self, field.compute)()
  File "/odoo/odoo-server/addons/hr_holidays/models/hr_leave.py", line 531, in _compute_number_of_hours_text
    float_round(leave.number_of_hours_display, precision_digits=2),
  File "/odoo/odoo-server/odoo/fields.py", line 1019, in __get__
    self.compute_value(recs)
  File "/odoo/odoo-server/odoo/fields.py", line 1175, in compute_value
    records._compute_field_value(self)
  File "/odoo/odoo-server/addons/mail/models/mail_thread.py", line 410, in _compute_field_value
    return super()._compute_field_value(field)
  File "/odoo/odoo-server/odoo/models.py", line 4061, in _compute_field_value
    getattr(self, field.compute)()
  File "/odoo/odoo-server/addons/hr_holidays/models/hr_leave.py", line 492, in _compute_number_of_hours_display
    calendar = holiday._get_calendar()
  File "/odoo/odoo-server/addons/hr_holidays/models/hr_leave.py", line 487, in _get_calendar
    return self.employee_id.resource_calendar_id or self.env.company.resource_calendar_id
  File "/odoo/odoo-server/odoo/fields.py", line 2483, in __get__
    return super().__get__(records, owner)
  File "/odoo/odoo-server/odoo/fields.py", line 996, in __get__
    recs._fetch_field(self)
  File "/odoo/odoo-server/odoo/models.py", line 3069, in _fetch_field
    self._read(fnames)
  File "/odoo/odoo-server/addons/hr/models/hr_employee.py", line 135, in _read
    res = self.env['hr.employee.public'].browse(self.ids).read(fields)
  File "/odoo/odoo-server/odoo/models.py", line 3009, in read
    raise ValueError("Invalid field %r on model %r" % (name, self._name))
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/odoo/odoo-server/odoo/http.py", line 639, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/odoo/odoo-server/odoo/http.py", line 315, in _handle_exception
    raise exception.with_traceback(None) from new_cause
ValueError: Invalid field 'no_autoclose' on model 'hr.employee.public'
```


Discussion / hints:
This works only when each employee has minimum officer permission.
This is not logical. Employee need not always given officer role as he is responsble for only attendance.

Giving officer role to each employee is not a option. Should work with employee typical role
granting read-only-rights on hr.employee for internal user fixed the problem for me

Fix patch:
diff --git a/hr_attendance_autoclose/models/hr_employee.py b/hr_attendance_autoclose/models/hr_employee.py
index dbebb38f..60b808ba 100644
--- a/hr_attendance_autoclose/models/hr_employee.py
+++ b/hr_attendance_autoclose/models/hr_employee.py
@@ -8,4 +8,6 @@ class HrEmployee(models.Model):
 
     _inherit = "hr.employee"
 
-    no_autoclose = fields.Boolean(string="Don't Autoclose Attendances")
+    no_autoclose = fields.Boolean(
+        string="Don't Autoclose Attendances", groups="hr.group_hr_user"
+    )


Test patch:
diff --git a/hr_attendance_autoclose/tests/test_hr_attendance_auto_close.py b/hr_attendance_autoclose/tests/test_hr_attendance_auto_close.py
index 8a02a1b1..c53ef781 100644
--- a/hr_attendance_autoclose/tests/test_hr_attendance_auto_close.py
+++ b/hr_attendance_autoclose/tests/test_hr_attendance_auto_close.py
@@ -50,3 +50,27 @@ def test_employee_edit(self):
         )
         self.hr_attendance.check_for_incomplete_attendances()
         self.assertFalse(att2.attendance_reason_ids)
+
+    def test_hr_employee_can_still_read_employee_and_hr_public_employee(self):
+        """This test ensure the following comment from hr.employee model has been take
+        in consideration::
+
+            NB: Any field only available on the model hr.employee (i.e. not on the
+            hr.employee.public model) should have `groups="hr.group_hr_user"` on its
+            definition to avoid being prefetched when the user hasn't access to the
+            hr.employee model. Indeed, the prefetch loads the data for all the fields
+            that are available according to the group defined on them.
+        """
+        test_user = self.env["res.users"].create(
+            {
+                "name": "test",
+                "login": "test",
+                "groups_id": [
+                    (6, 0, [self.env.ref("base.group_user").id]),
+                ],
+            }
+        )
+
+        employees = self.env["hr.employee"].with_user(test_user).search([])
+        for empl in employees:
+            self.assertTrue(empl.name)