Debian Codegraph @

addons/hr_holidays_attendance/models/hr_leave_allocation.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from datetime import datetime
4
5 from odoo import api, fields, models, _
6 from odoo.exceptions import ValidationError
7 from odoo.fields import Domain
8
9
10 class HrLeaveAllocation(models.Model):
11 _inherit = 'hr.leave.allocation'
12
13 @api.model
14 def default_get(self, fields):
15 res = super().default_get(fields)
16 if 'holiday_status_id' in fields and self.env.context.get('deduct_extra_hours'):
17 domain = Domain('overtime_deductible', '=', True) & Domain('requires_allocation', '=', True)
18 if self.env.context.get('deduct_extra_hours_employee_request', False):
19 # Prevent loading manager allocated time off type in self request contexts
20 domain &= Domain('employee_requests', '=', True)
21 leave_type = self.env['hr.leave.type'].search(domain, limit=1)
22 res['holiday_status_id'] = leave_type.id
23 return res
24
25 overtime_deductible = fields.Boolean(compute='_compute_overtime_deductible')
26 employee_overtime = fields.Float(compute='_compute_employee_overtime', groups='base.group_user')
27
28 @api.depends('holiday_status_id')
29 def _compute_overtime_deductible(self):
30 for allocation in self:
31 allocation.overtime_deductible = allocation.holiday_status_id.overtime_deductible
32
33 @api.depends('employee_id')
34 def _compute_employee_overtime(self):
35 diff_by_employee = self.employee_id._get_deductible_employee_overtime()
36 for allocation in self:
37 allocation.employee_overtime = diff_by_employee.get(allocation.employee_id, 0)
38
39 @api.model_create_multi
40 def create(self, vals_list):
41 res = super().create(vals_list)
42 deductible = res.employee_id._get_deductible_employee_overtime()
43 for allocation in res:
44 if allocation.overtime_deductible:
45 if deductible[allocation.employee_id] < 0:
46 raise ValidationError(_('The employee does not have enough overtime hours to request this leave.'))
47 return res
48
49 def write(self, vals):
50 res = super().write(vals)
51 if 'number_of_days' not in vals:
52 return res
53 if not self.env.user.has_group("hr_holidays.group_hr_holidays_user") and any(allocation.state not in ('draft', 'confirm') for allocation in self):
54 raise ValidationError(_('Only an Officer or Administrator is allowed to edit the allocation duration in this status.'))
55 deductible = self.employee_id._get_deductible_employee_overtime()
56 for allocation in self.sudo().filtered('overtime_deductible'):
57 if deductible[allocation.employee_id] < 0:
58 raise ValidationError(_('The employee does not have enough overtime hours to request this leave.'))
59 return res
60
61 def action_refuse(self):
62 res = super().action_refuse()
63 return res
64
65 def _get_accrual_plan_level_work_entry_prorata(self, level, start_period, start_date, end_period, end_date):
66 self.ensure_one()
67 if level.frequency != 'worked_hours':
68 return super()._get_accrual_plan_level_work_entry_prorata(level, start_period, start_date, end_period, end_date)
69 datetime_min_time = datetime.min.time()
70 start_dt = datetime.combine(start_date, datetime_min_time)
71 end_dt = datetime.combine(end_date, datetime_min_time)
72
73 # Search for any attendance overlapping the window
74 attendances = self.env['hr.attendance'].sudo().search([
75 ('employee_id', '=', self.employee_id.id),
76 ('check_in', '<', end_dt),
77 ('check_out', '>', start_dt),
78 ])
79
80 total_worked_hours = 0.0
81 for attendance in attendances:
82 total_worked_hours += attendance._get_worked_hours_in_range(start_dt, end_dt)
83
84 return total_worked_hours