Browse files

[FIX] resource, hr_payroll: fix hours_per_day onchange

In resource, calendar.attendances with a resource_id specified
were taken into account to compute the average hours per day.
This is incorrect and could lead to hours_per_day being > 24h.

Also when hr_payroll is installed, attendance_ids is replaced
by normal attendance (those without a resource_id set) in the view.
The onchange method was not called to recompute the average hours per day.

This commit fixes both issues.

closes #29951
  • Loading branch information...
Musvol committed Jan 4, 2019
1 parent d682137 commit f6b1e3144b514bb3de834694e4a4e544ddc431ec
@@ -1,6 +1,6 @@
# -*- coding:utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields
from odoo import models, fields, api
from odoo.osv.expression import AND, OR

@@ -16,6 +16,15 @@ class ResourceCalendar(models.Model):
'resource.calendar.attendance', 'calendar_id', 'Employees working Time',
domain=[('resource_id', '!=', False)])

def _get_global_attendances(self):
res = super(ResourceCalendar, self)._get_global_attendances()
res |= self.normal_attendance_ids.filtered(lambda attendance: not attendance.date_from and not attendance.date_to)
return res

# Add a key on the api.onchange decorator
@api.onchange('attendance_ids', 'normal_attendance_ids')
def _onchange_hours_per_day(self):
return super(ResourceCalendar, self)._onchange_hours_per_day()

class ResourceCalendarAttendance(models.Model):
_inherit = 'resource.calendar.attendance'
@@ -7,7 +7,10 @@
<field name="model">resource.calendar</field>
<field name="inherit_id" ref="resource.resource_calendar_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='attendance_ids']" position="replace">
<xpath expr="//field[@name='attendance_ids']" position="attributes">
<attribute name="invisible">1</attribute>
<xpath expr="//field[@name='attendance_ids']" position="after">
<field name="normal_attendance_ids"/>
<xpath expr="//notebook" position="inside">
@@ -186,9 +186,13 @@ def _get_default_attendance_ids(self):
default=lambda self: self._context.get('tz') or or 'UTC',
help="This field is used in order to define in which timezone the resources will work.")

def _get_global_attendances(self):
return self.attendance_ids.filtered(lambda attendance: not attendance.date_from and not attendance.date_to and not attendance.resource_id)

def _onchange_hours_per_day(self):
attendances = self.attendance_ids.filtered(lambda attendance: not attendance.date_from and not attendance.date_to)
attendances = self._get_global_attendances()
hour_count = 0.0
for attendance in attendances:
hour_count += attendance.hour_to - attendance.hour_from

0 comments on commit f6b1e31

Please sign in to comment.