Debian Codegraph @

addons/resource/models/resource_calendar_attendance.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import math
4
5 from odoo import api, fields, models
6 from odoo.exceptions import UserError
7
8
9 class ResourceCalendarAttendance(models.Model):
10 _name = 'resource.calendar.attendance'
11 _description = "Work Detail"
12 _order = 'sequence, week_type, dayofweek, hour_from'
13
14 name = fields.Char(required=True)
15 dayofweek = fields.Selection([
16 ('0', 'Monday'),
17 ('1', 'Tuesday'),
18 ('2', 'Wednesday'),
19 ('3', 'Thursday'),
20 ('4', 'Friday'),
21 ('5', 'Saturday'),
22 ('6', 'Sunday')
23 ], 'Day of Week', required=True, index=True, default='0')
24 hour_from = fields.Float(string='Work from', default=0, required=True, index=True,
25 help="Start and End time of working.\n"
26 "A specific value of 24:00 is interpreted as 23:59:59.999999.")
27 hour_to = fields.Float(string='Work to', default=0, required=True)
28 # For the hour duration, the compute function is used to compute the value
29 # unambiguously, while the duration in days is computed for the default
30 # value based on the day_period but can be manually overridden.
31 duration_hours = fields.Float(compute='_compute_duration_hours', inverse='_inverse_duration_hours', string='Duration (hours)', store=True, readonly=False)
32 duration_days = fields.Float(compute='_compute_duration_days', string='Duration (days)', store=True, readonly=False)
33 calendar_id = fields.Many2one("resource.calendar", string="Resource's Calendar", required=True, index=True, ondelete='cascade')
34 duration_based = fields.Boolean(related="calendar_id.duration_based")
35 day_period = fields.Selection([
36 ('morning', 'Morning'),
37 ('lunch', 'Break'),
38 ('afternoon', 'Afternoon'),
39 ('full_day', 'Full Day')], required=True, default='morning')
40 week_type = fields.Selection([
41 ('1', 'Second'),
42 ('0', 'First')
43 ], 'Week Number', default=False)
44 two_weeks_calendar = fields.Boolean("Calendar in 2 weeks mode", related='calendar_id.two_weeks_calendar')
45 display_type = fields.Selection([
46 ('line_section', "Section")], default=False, help="Technical field for UX purpose.")
47 sequence = fields.Integer(default=10,
48 help="Gives the sequence of this line when displaying the resource calendar.")
49
50 @api.onchange('hour_from', 'hour_to')
51 def _onchange_hours(self):
52 # avoid negative or after midnight
53 self.hour_from = min(self.hour_from, 23.99)
54 self.hour_from = max(self.hour_from, 0.0)
55 self.hour_to = min(self.hour_to, 24)
56 self.hour_to = max(self.hour_to, 0.0)
57
58 # avoid wrong order
59 self.hour_to = max(self.hour_to, self.hour_from)
60
61 @api.constrains('day_period')
62 def _check_day_period(self):
63 for attendance in self:
64 if attendance.day_period == 'lunch' and attendance.duration_based:
65 raise UserError(self.env._("%(att)s is a break attendance, You should not have such record on duration based calendar", att=attendance.name))
66
67 @api.model
68 def get_week_type(self, date):
69 # week_type is defined by
70 # * counting the number of days from January 1 of year 1
71 # (extrapolated to dates prior to the first adoption of the Gregorian calendar)
72 # * converted to week numbers and then the parity of this number is asserted.
73 # It ensures that an even week number always follows an odd week number. With classical week number,
74 # some years have 53 weeks. Therefore, two consecutive odd week number follow each other (53 --> 1).
75 return int(math.floor((date.toordinal() - 1) / 7) % 2)
76
77 @api.depends('hour_from', 'hour_to')
78 def _compute_duration_hours(self):
79 for attendance in self.filtered('hour_to'):
80 attendance.duration_hours = (attendance.hour_to - attendance.hour_from) if attendance.day_period != 'lunch' else 0
81
82 def _inverse_duration_hours(self):
83 for calendar, attendances in self.grouped('calendar_id').items():
84 if not calendar.duration_based:
85 continue
86 for attendance in attendances:
87 if attendance.day_period == 'full_day':
88 period_duration = attendance.duration_hours / 2
89 attendance.hour_to = 12 + period_duration
90 attendance.hour_from = 12 - period_duration
91 elif attendance.day_period == 'morning':
92 attendance.hour_to = 12
93 attendance.hour_from = 12 - attendance.duration_hours
94 elif attendance.day_period == 'afternoon':
95 attendance.hour_to = 12 + attendance.duration_hours
96 attendance.hour_from = 12
97
98 @api.depends('day_period')
99 def _compute_duration_days(self):
100 for attendance in self:
101 if attendance.day_period == 'lunch':
102 attendance.duration_days = 0
103 elif attendance.day_period == 'full_day':
104 attendance.duration_days = 1
105 else:
106 attendance.duration_days = 0.5 if attendance.duration_hours <= attendance.calendar_id.hours_per_day * 3 / 4 else 1
107
108 @api.depends('week_type')
109 def _compute_display_name(self):
110 super()._compute_display_name()
111 this_week_type = str(self.get_week_type(fields.Date.context_today(self)))
112 section_names = {'0': self.env._('First week'), '1': self.env._('Second week')}
113 section_info = {True: self.env._('this week'), False: self.env._('other week')}
114 for record in self.filtered(lambda l: l.display_type == 'line_section'):
115 section_name = f"{section_names[record.week_type]} ({section_info[this_week_type == record.week_type]})"
116 record.display_name = section_name
117
118 def _copy_attendance_vals(self):
119 self.ensure_one()
120 return {
121 'name': self.name,
122 'dayofweek': self.dayofweek,
123 'hour_from': self.hour_from,
124 'hour_to': self.hour_to,
125 'day_period': self.day_period,
126 'week_type': self.week_type,
127 'display_type': self.display_type,
128 'sequence': self.sequence,
129 }
130
131 def _is_work_period(self):
132 return self.day_period != 'lunch' and not self.display_type