Debian Codegraph @

addons/l10n_fr_hr_holidays/models/hr_leave.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from dateutil.relativedelta import relativedelta
4
5 from odoo import fields, models, api, _
6 from odoo.exceptions import UserError
7
8 import pytz
9
10
11 class HrLeave(models.Model):
12 _inherit = 'hr.leave'
13
14 l10n_fr_date_to_changed = fields.Boolean(export_string_translation=False)
15
16 def _l10n_fr_leave_applies(self):
17 # The french l10n is meant to be computed only in very specific cases:
18 # - there is only one employee affected by the leave
19 # - the company is french
20 # - the leave_type is the reference leave_type of that company
21 self.ensure_one()
22 return self.employee_id and \
23 self.company_id.country_id.code == 'FR' and \
24 self.resource_calendar_id != self.company_id.resource_calendar_id and \
25 self.holiday_status_id == self.company_id._get_fr_reference_leave_type()
26
27 def _get_fr_date_from_to(self, date_from, date_to):
28 self.ensure_one()
29 # What we need to compute is how much we will need to push date_to in order to account for the lost days
30 # This gets even more complicated in two_weeks_calendars
31
32 # The following computation doesn't work for resource calendars in
33 # which the employee works zero hours.
34 if not (self.resource_calendar_id.attendance_ids):
35 raise UserError(_("An employee can't take paid time off in a period without any work hours."))
36
37 if not self.request_unit_hours:
38 # Use company's working schedule hours for the leave to avoid duration calculation issues.
39 def adjust_date_range(date_from, date_to, from_period, to_period, attendance_ids, employee_id):
40 period_ids_from = attendance_ids.filtered(lambda a: a.day_period in from_period
41 and int(a.dayofweek) == date_from.weekday()
42 and (not a.two_weeks_calendar or int(a.week_type) == a.get_week_type(date_from))
43 and not a.display_type)
44 period_ids_to = attendance_ids.filtered(lambda a: a.day_period in to_period
45 and int(a.dayofweek) == date_to.weekday()
46 and (not a.two_weeks_calendar or int(a.week_type) == a.get_week_type(date_to))
47 and not a.display_type)
48 if period_ids_from:
49 min_hour = min(attendance.hour_from for attendance in period_ids_from)
50 date_from = self._to_utc(date_from, min_hour, employee_id)
51 if period_ids_to:
52 max_hour = max(attendance.hour_to for attendance in period_ids_to)
53 date_to = self._to_utc(date_to, max_hour, employee_id)
54 return date_from, date_to
55
56 if self.request_unit_half:
57 from_period = ['morning'] if self.request_date_from_period == 'am' else ['afternoon']
58 to_period = ['morning'] if self.request_date_to_period == 'am' else ['afternoon']
59 else:
60 from_period = ['morning', 'afternoon']
61 to_period = ['morning', 'afternoon']
62 attendance_ids = self.company_id.resource_calendar_id.attendance_ids | self.resource_calendar_id.attendance_ids
63 date_from, date_to = adjust_date_range(date_from, date_to, from_period, to_period, attendance_ids, self.employee_id)
64
65 similar = date_from.date() == date_to.date() and self.request_date_from_period == self.request_date_to_period
66 if self.request_unit_half and similar and self.request_date_from_period == 'am':
67 # In normal workflows request_unit_half implies that date_from and date_to are the same
68 # request_unit_half allows us to choose between `am` and `pm`
69 # In a case where we work from mon-wed and request a half day in the morning
70 # we do not want to push date_to since the next work attendance is actually in the afternoon
71 date_from_weektype = str(self.env['resource.calendar.attendance'].get_week_type(date_from))
72 date_from_dayofweek = str(date_from.weekday())
73 # Fetch the attendances we care about
74 attendance_ids = self.resource_calendar_id.attendance_ids.filtered(lambda a:
75 a.dayofweek == date_from_dayofweek
76 and a.day_period != "lunch"
77 and (not self.resource_calendar_id.two_weeks_calendar or a.week_type == date_from_weektype))
78 if len(attendance_ids) == 2:
79 # The employee took the morning off on a day where he works the afternoon aswell
80 return (date_from, date_to)
81
82 # Check calendars for working days until we find the right target, start at date_to + 1 day
83 # Postpone date_target until the next working day
84 date_start = date_from
85 date_target = date_to
86 # It is necessary to move the start date up to the first work day of
87 # the employee calendar as otherwise days worked on by the company
88 # calendar before the actual start of the leave would be taken into
89 # account.
90 while not self.resource_calendar_id._works_on_date(date_start):
91 date_start += relativedelta(days=1)
92 while not self.resource_calendar_id._works_on_date(date_target + relativedelta(days=1)):
93 date_target += relativedelta(days=1)
94
95 # Undo the last day increment
96 return (date_start, date_target)
97
98 @api.depends('request_date_from_period', 'request_date_to_period', 'request_hour_from', 'request_hour_to',
99 'request_date_from', 'request_date_to', 'request_unit_half', 'request_unit_hours', 'employee_id')
100 def _compute_date_from_to(self):
101 super()._compute_date_from_to()
102 for leave in self:
103 if leave._l10n_fr_leave_applies():
104 new_date_from, new_date_to = leave._get_fr_date_from_to(leave.date_from, leave.date_to)
105 if new_date_from != leave.date_from:
106 leave.date_from = new_date_from
107 if new_date_to != leave.date_to:
108 leave.date_to = new_date_to
109 leave.l10n_fr_date_to_changed = True
110 else:
111 leave.l10n_fr_date_to_changed = False
112
113 def _get_durations(self, check_leave_type=True, resource_calendar=None):
114 """
115 In french time off laws, if an employee has a part time contract, when taking time off
116 before one of his off day (compared to the company's calendar) it should also count the time
117 between the time off and the next calendar work day/company off day (weekends).
118
119 For example take an employee working mon-wed in a company where the regular calendar is mon-fri.
120 If the employee were to take a time off ending on wednesday, the legal duration would count until friday.
121 """
122 if not resource_calendar:
123 fr_leaves = self.filtered(lambda leave: leave._l10n_fr_leave_applies())
124 duration_by_leave_id = super(HrLeave, self - fr_leaves)._get_durations(resource_calendar=resource_calendar)
125 fr_leaves_by_company = fr_leaves.grouped('company_id')
126 if fr_leaves:
127 public_holidays = self.env['resource.calendar.leaves'].search([
128 ('resource_id', '=', False),
129 ('company_id', 'in', fr_leaves.company_id.ids + [False]),
130 ('date_from', '<', max(fr_leaves.mapped('date_to')) + relativedelta(days=1)),
131 ('date_to', '>', min(fr_leaves.mapped('date_from')) - relativedelta(days=1)),
132 ])
133 for company, leaves in fr_leaves_by_company.items():
134 company_cal = company.resource_calendar_id
135 holidays_days_list = []
136 public_holidays_filtered = public_holidays.filtered_domain([
137 ('calendar_id', 'in', [False, company_cal.id]),
138 ('company_id', '=', company.id)
139 ])
140 for holiday in public_holidays_filtered:
141 tz = pytz.timezone(holiday.write_uid.tz)
142 current = holiday.date_from.replace(tzinfo=pytz.utc).astimezone(tz).date()
143 holiday_date_to = holiday.date_to.replace(tzinfo=pytz.utc).astimezone(tz).date()
144 while current <= holiday_date_to:
145 holidays_days_list.append(current)
146 current += relativedelta(days=1)
147 for leave in leaves:
148 if leave.request_unit_half:
149 duration_by_leave_id.update(leave._get_durations(resource_calendar=company_cal))
150 continue
151 # Extend the end date to next working day
152 date_start = leave.date_from
153 date_end = leave.date_to
154 while not leave.resource_calendar_id._works_on_date(date_start):
155 date_start += relativedelta(days=1)
156 extended_date_end = date_end
157 while not company_cal._works_on_date(extended_date_end + relativedelta(days=1)):
158 extended_date_end += relativedelta(days=1)
159 # Count number of days in company calendar
160 current = date_start.date()
161 end_date = extended_date_end.date()
162 legal_days = 0.0
163 while current <= end_date:
164 if current in holidays_days_list:
165 current += relativedelta(days=1)
166 continue
167 if company_cal._works_on_date(current):
168 legal_days += 1.0
169 current += relativedelta(days=1)
170 standard_duration = super()._get_durations(resource_calendar=resource_calendar)
171 _, hours = standard_duration.get(leave.id, (0.0, 0.0))
172
173 duration_by_leave_id[leave.id] = (legal_days, hours)
174
175 return duration_by_leave_id
176 return super()._get_durations(resource_calendar=resource_calendar)