Debian Codegraph @

addons/hr_holidays_attendance/models/hr_leave.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from collections import defaultdict
4 from datetime import timedelta
5
6 from odoo import api, fields, models, _
7 from odoo.exceptions import ValidationError
8 from odoo.tools import float_round
9
10
11 class HrLeave(models.Model):
12 _inherit = 'hr.leave'
13
14 employee_overtime = fields.Float(compute='_compute_employee_overtime', groups='base.group_user')
15 overtime_deductible = fields.Boolean(compute='_compute_overtime_deductible')
16
17 @api.depends('holiday_status_id')
18 def _compute_overtime_deductible(self):
19 for leave in self:
20 leave.overtime_deductible = leave.holiday_status_id.overtime_deductible and not leave.holiday_status_id.requires_allocation
21
22 @api.model_create_multi
23 def create(self, vals_list):
24 res = super().create(vals_list)
25 self._check_overtime_deductible(res)
26 return res
27
28 def write(self, vals):
29 res = super().write(vals)
30 fields_to_check = {'number_of_days', 'request_date_from', 'request_date_to', 'state', 'employee_id', 'holiday_status_id'}
31 if not any(field for field in fields_to_check if field in vals):
32 return res
33 self._check_overtime_deductible(self)
34 return res
35
36 @api.model
37 def _get_deductible_employee_overtime(self, employees):
38 # Uses the calculation logic now located in the hr.employee model.
39 return employees._get_deductible_employee_overtime()
40
41 @api.depends('number_of_hours', 'employee_id', 'holiday_status_id')
42 def _compute_employee_overtime(self):
43 diff_by_employee = self.employee_id._get_deductible_employee_overtime()
44 for leave in self:
45 leave.employee_overtime = diff_by_employee[leave.employee_id]
46
47 def _check_overtime_deductible(self, leaves):
48 # If the type of leave is overtime deductible, we have to check that the employee has enough extra hours
49 hours = leaves.employee_id._get_deductible_employee_overtime()
50 for leave in leaves.filtered('overtime_deductible'):
51 if hours[leave.employee_id] < 0:
52 if leave.employee_id.user_id == self.env.user:
53 raise ValidationError(_('You do not have enough extra hours to request this leave'))
54 raise ValidationError(_('The employee does not have enough extra hours to request this leave.'))
55
56 def action_reset_confirm(self):
57 self._check_overtime_deductible(self)
58 res = super().action_reset_confirm()
59 return res
60
61 def action_approve(self, check_state=True):
62 res = super().action_approve(check_state)
63 self._check_overtime_deductible(self)
64 return res
65
66 def action_refuse(self):
67 res = super().action_refuse()
68 return res
69
70 def _validate_leave_request(self):
71 super()._validate_leave_request()
72 self._update_leaves_overtime()
73
74 def _remove_resource_leave(self):
75 res = super()._remove_resource_leave()
76 self._update_leaves_overtime()
77 return res
78
79 def _update_leaves_overtime(self):
80 Attendance = self.env['hr.attendance']
81 dates = [
82 Attendance._attendance_date(leave.date_from, leave.employee_id)
83 for leave in self.filtered(lambda leave: leave.state == 'confirmed')
84 ]
85 if dates:
86 Attendance.search([
87 ('date', '>=', min(dates)),
88 ('date', '<=', max(dates)),
89 ('employee_id', 'in', self.employee_id.ids),
90 ])._update_overtimes()
91
92 def _force_cancel(self, *args, **kwargs):
93 super()._force_cancel(*args, **kwargs)