[IMP] resource: define a conversion factor hour to day on working calendar
Using a working calendar, you might want to know how much hour correspond a work day. This depends of you working calendar. This might be usefull to compute leave duration (half a day is not 8 hours, but 4). The hours_per_day field is not computed as the average of working hour of all work day of calendar to allow customization. By default, it is 8, as the default attendances are the one from the 40h/week calendar. A constant is also available to handle case where resource calendar is not available.
This commit is contained in:
@@ -12,11 +12,10 @@ from odoo.exceptions import UserError, AccessError, ValidationError
|
||||
from odoo.tools import float_compare
|
||||
from odoo.tools.translate import _
|
||||
|
||||
from odoo.addons.resource.models.resource import HOURS_PER_DAY
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
HOURS_PER_DAY = 8
|
||||
|
||||
class HolidaysRequest(models.Model):
|
||||
_name = "hr.leave"
|
||||
_description = "Leave"
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
<record id="resource_calendar_std_35h" model="resource.calendar">
|
||||
<field name="name">Standard 35 Hours/Week</field>
|
||||
<field name="company_id" ref="base.main_company"/>
|
||||
<field name="hours_per_day">7.0</field>
|
||||
<field name="attendance_ids"
|
||||
eval="[
|
||||
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12}),
|
||||
@@ -43,6 +44,7 @@
|
||||
<record id="resource_calendar_std_38h" model="resource.calendar">
|
||||
<field name="name">Standard 38 Hours/Week</field>
|
||||
<field name="company_id" ref="base.main_company"/>
|
||||
<field name="hours_per_day">7.6</field>
|
||||
<field name="attendance_ids"
|
||||
eval="[
|
||||
(0, 0, {'name': 'Monday Morning', 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12}),
|
||||
|
||||
@@ -14,7 +14,12 @@ from operator import itemgetter
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.addons.base.models.res_partner import _tz_get
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo.tools.float_utils import float_compare
|
||||
from odoo.tools.float_utils import float_compare, float_round
|
||||
|
||||
# Default hour per day value. The one should
|
||||
# only be used when the one from the calendar
|
||||
# is not available.
|
||||
HOURS_PER_DAY = 8
|
||||
|
||||
|
||||
def float_to_time(float_hour):
|
||||
@@ -87,6 +92,15 @@ class ResourceCalendar(models.Model):
|
||||
'resource.calendar.leaves', 'calendar_id', 'Global Leaves',
|
||||
domain=[('resource_id', '=', False)]
|
||||
)
|
||||
hours_per_day = fields.Float("Average hour per day", default=HOURS_PER_DAY, help="Average hours per day a resource is supposed to work with this calendar.")
|
||||
|
||||
@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)
|
||||
hour_count = 0.0
|
||||
for attendance in attendances:
|
||||
hour_count += attendance.hour_to - attendance.hour_from
|
||||
self.hours_per_day = float_round(hour_count / float(len(set(attendances.mapped('dayofweek')))), precision_digits=2)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Utility methods
|
||||
@@ -645,6 +659,12 @@ class ResourceCalendarAttendance(models.Model):
|
||||
hour_to = fields.Float(string='Work to', required=True)
|
||||
calendar_id = fields.Many2one("resource.calendar", string="Resource's Calendar", required=True, ondelete='cascade')
|
||||
|
||||
@api.constrains('hour_from', 'hour_to')
|
||||
def _check_hours(self):
|
||||
for attendance in self:
|
||||
if attendance.hour_from > attendance.hour_to:
|
||||
raise ValidationError(_("The start-hour must be lower than end-hour."))
|
||||
|
||||
|
||||
class ResourceResource(models.Model):
|
||||
_name = "resource.resource"
|
||||
|
||||
@@ -249,6 +249,7 @@
|
||||
</h1>
|
||||
<group>
|
||||
<field name="company_id" groups="base.group_multi_company"/>
|
||||
<field name="hours_per_day"/>
|
||||
</group>
|
||||
<notebook>
|
||||
<page string="Working Hours">
|
||||
|
||||
Reference in New Issue
Block a user