From f6b1e3144b514bb3de834694e4a4e544ddc431ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre?= Date: Fri, 4 Jan 2019 15:06:48 +0000 Subject: [PATCH] [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 odoo/odoo#29951 --- addons/hr_payroll/models/resource.py | 11 ++++++++++- addons/hr_payroll/views/resource_views.xml | 5 ++++- addons/resource/models/resource.py | 6 +++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/addons/hr_payroll/models/resource.py b/addons/hr_payroll/models/resource.py index 440820f2017..6a68ed1b395 100644 --- a/addons/hr_payroll/models/resource.py +++ b/addons/hr_payroll/models/resource.py @@ -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' diff --git a/addons/hr_payroll/views/resource_views.xml b/addons/hr_payroll/views/resource_views.xml index 5fe67b67128..831e2206613 100644 --- a/addons/hr_payroll/views/resource_views.xml +++ b/addons/hr_payroll/views/resource_views.xml @@ -7,7 +7,10 @@ resource.calendar - + + 1 + + diff --git a/addons/resource/models/resource.py b/addons/resource/models/resource.py index 481cb9b57df..b16c121799b 100644 --- a/addons/resource/models/resource.py +++ b/addons/resource/models/resource.py @@ -186,9 +186,13 @@ class ResourceCalendar(models.Model): default=lambda self: self._context.get('tz') or self.env.user.tz 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) + @api.onchange('attendance_ids') 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