| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from collections import defaultdict |
| 4 |
import pytz |
| 5 |
|
| 6 |
from odoo import models |
| 7 |
|
| 8 |
|
| 9 |
class HrVersion(models.Model): |
| 10 |
_inherit = 'hr.version' |
| 11 |
|
| 12 |
def _get_version_work_entries_values(self, date_start, date_stop): |
| 13 |
# Add the work entries difference for french payroll |
| 14 |
# Work entries by default are not generated on days the employee does not work |
| 15 |
# So we have to fill the gaps with work entries for those periods |
| 16 |
result = super()._get_version_work_entries_values(date_start, date_stop) |
| 17 |
fr_contracts = self.filtered(lambda c: c.company_id.country_id.code == 'FR' and c.resource_calendar_id != c.company_id.resource_calendar_id) |
| 18 |
if not fr_contracts: |
| 19 |
return result |
| 20 |
start_dt = pytz.utc.localize(date_start) if not date_start.tzinfo else date_start |
| 21 |
end_dt = pytz.utc.localize(date_stop) if not date_stop.tzinfo else date_stop |
| 22 |
# l10n_fr_date_to_changed is False when no adjustment had to be done |
| 23 |
all_leaves = self.env['hr.leave'].search([ |
| 24 |
('employee_id', 'in', fr_contracts.employee_id.ids), |
| 25 |
('state', '=', 'validate'), |
| 26 |
('date_from', '<=', end_dt.astimezone(pytz.utc).replace(tzinfo=None)), |
| 27 |
('date_to', '>=', start_dt.astimezone(pytz.utc).replace(tzinfo=None)), |
| 28 |
('l10n_fr_date_to_changed', '=', True), |
| 29 |
]) |
| 30 |
leaves_per_employee = defaultdict(lambda: self.env['hr.leave']) |
| 31 |
for leave in all_leaves: |
| 32 |
leaves_per_employee[leave.employee_id] |= leave |
| 33 |
for contract in fr_contracts: |
| 34 |
employee = contract.employee_id |
| 35 |
employee_calendar = contract.resource_calendar_id |
| 36 |
company = contract.company_id |
| 37 |
company_calendar = company.resource_calendar_id |
| 38 |
resource = employee.resource_id |
| 39 |
tz = pytz.timezone(employee_calendar.tz) |
| 40 |
|
| 41 |
for leave in leaves_per_employee[employee]: |
| 42 |
leave_start_dt = max(start_dt, leave.date_from.astimezone(tz)) |
| 43 |
leave_end_dt_fr = min(end_dt, leave.date_to.astimezone(tz)) |
| 44 |
|
| 45 |
# Compute the attendances for the company calendar and the employee calendar |
| 46 |
# and then compute and keep the difference between those two |
| 47 |
company_attendances = company_calendar._attendance_intervals_batch( |
| 48 |
leave_start_dt, leave_end_dt_fr, resources=resource, tz=tz, |
| 49 |
)[resource.id] |
| 50 |
# Dates on which work entries should already be generated |
| 51 |
employee_dates = set() |
| 52 |
for vals in result: |
| 53 |
employee_dates.add(vals['date_start'].date()) |
| 54 |
employee_dates.add(vals['date_stop'].date()) |
| 55 |
leave_work_entry_type = leave.holiday_status_id.work_entry_type_id |
| 56 |
result += [{ |
| 57 |
'name': '%s%s' % (leave_work_entry_type.name + ': ' if leave_work_entry_type else "", employee.name), |
| 58 |
'date_start': interval[0].astimezone(pytz.utc).replace(tzinfo=None), |
| 59 |
'date_stop': interval[1].astimezone(pytz.utc).replace(tzinfo=None), |
| 60 |
'work_entry_type_id': leave_work_entry_type.id, |
| 61 |
'employee_id': employee.id, |
| 62 |
'company_id': contract.company_id.id, |
| 63 |
'state': 'draft', |
| 64 |
'version_id': contract.id, |
| 65 |
'leave_id': leave.id, |
| 66 |
} for interval in company_attendances if interval[0].date() not in employee_dates] |
| 67 |
|
| 68 |
return result |