| 1 |
|
| 2 |
|
| 3 |
from odoo import fields, models, _ |
| 4 |
|
| 5 |
import pytz |
| 6 |
|
| 7 |
|
| 8 |
class HrLeave(models.Model): |
| 9 |
_inherit = "hr.leave" |
| 10 |
|
| 11 |
timesheet_ids = fields.One2many('account.analytic.line', 'holiday_id', string="Analytic Lines") |
| 12 |
|
| 13 |
def _validate_leave_request(self): |
| 14 |
self._generate_timesheets() |
| 15 |
return super()._validate_leave_request() |
| 16 |
|
| 17 |
def _generate_timesheets(self, ignored_resource_calendar_leaves=None): |
| 18 |
""" Timesheet will be generated on leave validation |
| 19 |
internal_project_id and leave_timesheet_task_id are used. |
| 20 |
The generated timesheet will be attached to this project/task. |
| 21 |
""" |
| 22 |
vals_list = [] |
| 23 |
leave_ids = [] |
| 24 |
calendar_leaves_data = self.env['resource.calendar.leaves']._read_group([('holiday_id', 'in', self.ids)], ['holiday_id'], ['id:array_agg']) |
| 25 |
mapped_calendar_leaves = {leave: calendar_leave_ids[0] for leave, calendar_leave_ids in calendar_leaves_data} |
| 26 |
for leave in self: |
| 27 |
project, task = leave.employee_id.company_id.internal_project_id, leave.employee_id.company_id.leave_timesheet_task_id |
| 28 |
|
| 29 |
if not project or not task or leave.holiday_status_id.time_type == 'other': |
| 30 |
continue |
| 31 |
|
| 32 |
leave_ids.append(leave.id) |
| 33 |
if not leave.employee_id: |
| 34 |
continue |
| 35 |
|
| 36 |
calendar = leave.employee_id.resource_calendar_id |
| 37 |
calendar_timezone = pytz.timezone((calendar or leave.employee_id).tz) |
| 38 |
|
| 39 |
if calendar.flexible_hours and (leave.request_unit_hours or leave.request_unit_half or leave.date_from.date() == leave.date_to.date()): |
| 40 |
leave_date = leave.date_from.astimezone(calendar_timezone).date() |
| 41 |
if leave.request_unit_hours: |
| 42 |
hours = leave.request_hour_to - leave.request_hour_from |
| 43 |
elif leave.request_unit_half: |
| 44 |
hours = calendar.hours_per_day / 2 |
| 45 |
else: |
| 46 |
hours = calendar.hours_per_day |
| 47 |
work_hours_data = [(leave_date, hours)] |
| 48 |
else: |
| 49 |
ignored_resource_calendar_leaves = ignored_resource_calendar_leaves or [] |
| 50 |
if leave in mapped_calendar_leaves: |
| 51 |
ignored_resource_calendar_leaves.append(mapped_calendar_leaves[leave]) |
| 52 |
work_hours_data = leave.employee_id._list_work_time_per_day( |
| 53 |
leave.date_from, |
| 54 |
leave.date_to, |
| 55 |
domain=[('id', 'not in', ignored_resource_calendar_leaves)] if ignored_resource_calendar_leaves else None)[leave.employee_id.id] |
| 56 |
|
| 57 |
for index, (day_date, work_hours_count) in enumerate(work_hours_data): |
| 58 |
vals_list.append(leave._timesheet_prepare_line_values(index, work_hours_data, day_date, work_hours_count, project, task)) |
| 59 |
|
| 60 |
|
| 61 |
old_timesheets = self.env["account.analytic.line"].sudo().search([('project_id', '!=', False), ('holiday_id', 'in', leave_ids)]) |
| 62 |
if old_timesheets: |
| 63 |
old_timesheets.holiday_id = False |
| 64 |
old_timesheets.unlink() |
| 65 |
|
| 66 |
self.env['account.analytic.line'].sudo().create(vals_list) |
| 67 |
|
| 68 |
def _timesheet_prepare_line_values(self, index, work_hours_data, day_date, work_hours_count, project, task): |
| 69 |
self.ensure_one() |
| 70 |
return { |
| 71 |
'name': _("Time Off (%(index)s/%(total)s)", index=index + 1, total=len(work_hours_data)), |
| 72 |
'project_id': project.id, |
| 73 |
'task_id': task.id, |
| 74 |
'account_id': project.sudo().account_id.id, |
| 75 |
'unit_amount': work_hours_count, |
| 76 |
'user_id': self.employee_id.user_id.id, |
| 77 |
'date': day_date, |
| 78 |
'holiday_id': self.id, |
| 79 |
'employee_id': self.employee_id.id, |
| 80 |
'company_id': task.sudo().company_id.id or project.sudo().company_id.id, |
| 81 |
} |
| 82 |
|
| 83 |
def _check_missing_global_leave_timesheets(self): |
| 84 |
if not self: |
| 85 |
return |
| 86 |
min_date = min(self.mapped('date_from')) |
| 87 |
max_date = max(self.mapped('date_to')) |
| 88 |
|
| 89 |
global_leaves = self.env['resource.calendar.leaves'].search([ |
| 90 |
("resource_id", "=", False), |
| 91 |
("date_to", ">=", min_date), |
| 92 |
("date_from", "<=", max_date), |
| 93 |
("company_id.internal_project_id", "!=", False), |
| 94 |
("company_id.leave_timesheet_task_id", "!=", False), |
| 95 |
]) |
| 96 |
if global_leaves: |
| 97 |
global_leaves._generate_public_time_off_timesheets(self.employee_id) |
| 98 |
|
| 99 |
def action_refuse(self): |
| 100 |
""" Remove the timesheets linked to the refused holidays """ |
| 101 |
result = super().action_refuse() |
| 102 |
timesheets = self.sudo().mapped('timesheet_ids') |
| 103 |
timesheets.write({'holiday_id': False}) |
| 104 |
timesheets.unlink() |
| 105 |
self._check_missing_global_leave_timesheets() |
| 106 |
return result |
| 107 |
|
| 108 |
def _action_user_cancel(self, reason=None): |
| 109 |
res = super()._action_user_cancel(reason) |
| 110 |
timesheets = self.sudo().timesheet_ids |
| 111 |
timesheets.write({'holiday_id': False}) |
| 112 |
timesheets.unlink() |
| 113 |
self._check_missing_global_leave_timesheets() |
| 114 |
return res |
| 115 |
|
| 116 |
def _force_cancel(self, *args, **kwargs): |
| 117 |
super()._force_cancel(*args, **kwargs) |
| 118 |
|
| 119 |
timesheets = self.sudo().timesheet_ids |
| 120 |
timesheets.holiday_id = False |
| 121 |
timesheets.unlink() |
| 122 |
|
| 123 |
def write(self, vals): |
| 124 |
res = super().write(vals) |
| 125 |
|
| 126 |
timesheet_ids_to_remove = [] |
| 127 |
for leave in self: |
| 128 |
if leave.number_of_days == 0 and leave.sudo().timesheet_ids: |
| 129 |
leave.sudo().timesheet_ids.holiday_id = False |
| 130 |
timesheet_ids_to_remove.extend(leave.timesheet_ids) |
| 131 |
self.env['account.analytic.line'].browse(set(timesheet_ids_to_remove)).sudo().unlink() |
| 132 |
return res |