summaryrefslogtreecommitdiff
path: root/addons/resource/models/resource.py
diff options
context:
space:
mode:
Diffstat (limited to 'addons/resource/models/resource.py')
-rw-r--r--addons/resource/models/resource.py89
1 files changed, 75 insertions, 14 deletions
diff --git a/addons/resource/models/resource.py b/addons/resource/models/resource.py
index 51edc52d8a7..4697c35b035 100644
--- a/addons/resource/models/resource.py
+++ b/addons/resource/models/resource.py
@@ -159,18 +159,33 @@ class ResourceCalendar(models.Model):
if not res.get('name') and res.get('company_id'):
res['name'] = _('Working Hours of %s', self.env['res.company'].browse(res['company_id']).name)
if 'attendance_ids' in fields and not res.get('attendance_ids'):
- res['attendance_ids'] = [
- (0, 0, {'name': _('Monday Morning'), 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
- (0, 0, {'name': _('Monday Afternoon'), 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
- (0, 0, {'name': _('Tuesday Morning'), 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
- (0, 0, {'name': _('Tuesday Afternoon'), 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
- (0, 0, {'name': _('Wednesday Morning'), 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
- (0, 0, {'name': _('Wednesday Afternoon'), 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
- (0, 0, {'name': _('Thursday Morning'), 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
- (0, 0, {'name': _('Thursday Afternoon'), 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
- (0, 0, {'name': _('Friday Morning'), 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
- (0, 0, {'name': _('Friday Afternoon'), 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'})
- ]
+ company_id = res.get('company_id', self.env.company.id)
+ company = self.env['res.company'].browse(company_id)
+ company_attendance_ids = company.resource_calendar_id.attendance_ids
+ if company_attendance_ids:
+ res['attendance_ids'] = [
+ (0, 0, {
+ 'name': attendance.name,
+ 'dayofweek': attendance.dayofweek,
+ 'hour_from': attendance.hour_from,
+ 'hour_to': attendance.hour_to,
+ 'day_period': attendance.day_period,
+ })
+ for attendance in company_attendance_ids
+ ]
+ else:
+ res['attendance_ids'] = [
+ (0, 0, {'name': _('Monday Morning'), 'dayofweek': '0', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
+ (0, 0, {'name': _('Monday Afternoon'), 'dayofweek': '0', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
+ (0, 0, {'name': _('Tuesday Morning'), 'dayofweek': '1', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
+ (0, 0, {'name': _('Tuesday Afternoon'), 'dayofweek': '1', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
+ (0, 0, {'name': _('Wednesday Morning'), 'dayofweek': '2', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
+ (0, 0, {'name': _('Wednesday Afternoon'), 'dayofweek': '2', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
+ (0, 0, {'name': _('Thursday Morning'), 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
+ (0, 0, {'name': _('Thursday Afternoon'), 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'}),
+ (0, 0, {'name': _('Friday Morning'), 'dayofweek': '4', 'hour_from': 8, 'hour_to': 12, 'day_period': 'morning'}),
+ (0, 0, {'name': _('Friday Afternoon'), 'dayofweek': '4', 'hour_from': 13, 'hour_to': 17, 'day_period': 'afternoon'})
+ ]
return res
name = fields.Char(required=True)
@@ -181,13 +196,14 @@ class ResourceCalendar(models.Model):
default=lambda self: self.env.company)
attendance_ids = fields.One2many(
'resource.calendar.attendance', 'calendar_id', 'Working Time',
- copy=True)
+ compute='_compute_attendance_ids', store=True, readonly=False, copy=True)
leave_ids = fields.One2many(
'resource.calendar.leaves', 'calendar_id', 'Time Off')
global_leave_ids = fields.One2many(
'resource.calendar.leaves', 'calendar_id', 'Global Time Off',
+ compute='_compute_global_leave_ids', store=True, readonly=False,
domain=[('resource_id', '=', False)], copy=True,
- )
+ )
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.")
tz = fields.Selection(
@@ -197,6 +213,42 @@ class ResourceCalendar(models.Model):
two_weeks_calendar = fields.Boolean(string="Calendar in 2 weeks mode")
two_weeks_explanation = fields.Char('Explanation', compute="_compute_two_weeks_explanation")
+ @api.depends('company_id')
+ def _compute_attendance_ids(self):
+ for calendar in self.filtered(lambda c: not c._origin or c._origin.company_id != c.company_id):
+ company_calendar = calendar.company_id.resource_calendar_id
+ calendar.write({
+ 'two_weeks_calendar': company_calendar.two_weeks_calendar,
+ 'hours_per_day': company_calendar.hours_per_day,
+ 'tz': company_calendar.tz,
+ 'attendance_ids': [(5, 0, 0)] + [
+ (0, 0, {
+ 'name': attendance.name,
+ 'dayofweek': attendance.dayofweek,
+ 'date_from': attendance.date_from,
+ 'date_to': attendance.date_to,
+ 'hour_from': attendance.hour_from,
+ 'hour_to': attendance.hour_to,
+ 'day_period': attendance.day_period,
+ 'week_type': attendance.week_type,
+ 'display_type': attendance.display_type,
+ 'sequence': attendance.sequence,
+ }) for attendance in company_calendar.attendance_ids if not attendance.resource_id]
+ })
+
+ @api.depends('company_id')
+ def _compute_global_leave_ids(self):
+ for calendar in self.filtered(lambda c: not c._origin or c._origin.company_id != c.company_id):
+ calendar.write({
+ 'global_leave_ids': [(5, 0, 0)] + [
+ (0, 0, {
+ 'name': leave.name,
+ 'date_from': leave.date_from,
+ 'date_to': leave.date_to,
+ 'time_type': leave.time_type,
+ }) for leave in calendar.company_id.resource_calendar_id.global_leave_ids]
+ })
+
@api.returns('self', lambda value: value.id)
def copy(self, default=None):
self.ensure_one()
@@ -743,6 +795,15 @@ class ResourceCalendar(models.Model):
else:
return revert(day_dt)
+ def _get_max_number_of_hours(self, start, end):
+ self.ensure_one()
+ if not self.attendance_ids:
+ return 0
+ mapped_data = defaultdict(lambda: 0)
+ for attendance in self.attendance_ids.filtered(lambda a: (not a.date_from or not a.date_to) or (a.date_from <= end and a.date_to >= start)):
+ mapped_data[(attendance.week_type, attendance.dayofweek)] += attendance.hour_to - attendance.hour_from
+ return max(mapped_data.values())
+
class ResourceCalendarAttendance(models.Model):
_name = "resource.calendar.attendance"