1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# -*- coding: utf-8 -*-
import datetime
from datetime import datetime, timedelta
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
date_format = "%Y-%m-%d"
RESIGNATION_TYPE = [('resigned', 'Normal Resignation'),
('fired', 'Fired by the company')]
class HrResignation(models.Model):
_name = 'hr.resignation'
_inherit = 'mail.thread'
_rec_name = 'employee_id'
name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, index=True,
default=lambda self: _('New'))
employee_id = fields.Many2one('hr.employee', string="Employee", default=lambda self: self.env.user.employee_id.id,
help='Name of the employee for whom the request is creating')
department_id = fields.Many2one('hr.department', string="Department", related='employee_id.department_id',
help='Department of the employee')
resign_confirm_date = fields.Date(string="Confirmed Date",
help='Date on which the request is confirmed by the employee.',
track_visibility="always")
approved_revealing_date = fields.Date(string="Approved Last Day Of Employee",
help='Date on which the request is confirmed by the manager.',
track_visibility="always")
joined_date = fields.Date(string="Join Date", store=True,
help='Joining date of the employee.i.e Start date of the first contract')
expected_revealing_date = fields.Date(string="Last Day of Employee", required=True,
help='Employee requested date on which he is revealing from the company.')
reason = fields.Text(string="Reason", required=True, help='Specify reason for leaving the company')
notice_period = fields.Char(string="Notice Period")
state = fields.Selection(
[('draft', 'Draft'), ('confirm', 'Confirm'), ('approved', 'Approved'), ('cancel', 'Rejected')],
string='Status', default='draft', track_visibility="always")
resignation_type = fields.Selection(selection=RESIGNATION_TYPE, help="Select the type of resignation: normal "
"resignation or fired by the company")
read_only = fields.Boolean(string="check field")
employee_contract = fields.Char(String="Contract")
@api.onchange('employee_id')
@api.depends('employee_id')
def _compute_read_only(self):
""" Use this function to check weather the user has the permission to change the employee"""
res_user = self.env['res.users'].search([('id', '=', self._uid)])
print(res_user.has_group('hr.group_hr_user'))
if res_user.has_group('hr.group_hr_user'):
self.read_only = True
else:
self.read_only = False
@api.onchange('employee_id')
def set_join_date(self):
# self.joined_date = self.employee_id.joining_date if self.employee_id.joining_date else ''
self.joined_date = self.employee_id.joining_date
# @api.depends('employee_id')
# def compute_join_date(self):
# # self.joined_date = self.employee_id.joining_date if self.employee_id.joining_date else ''
# if employee_id.joining_date :
# self.joined_date = self.employee_id.joining_date
# else :
# self.joined_date = False
@api.model
def create(self, vals):
# assigning the sequence for the record
if vals.get('name', _('New')) == _('New'):
vals['name'] = self.env['ir.sequence'].next_by_code('hr.resignation') or _('New')
res = super(HrResignation, self).create(vals)
return res
@api.constrains('employee_id')
def check_employee(self):
# Checking whether the user is creating leave request of his/her own
for rec in self:
if not self.env.user.has_group('hr.group_hr_user'):
if rec.employee_id.user_id.id and rec.employee_id.user_id.id != self.env.uid:
raise ValidationError(_('You cannot create request for other employees'))
@api.onchange('employee_id')
@api.depends('employee_id')
def check_request_existence(self):
# Check whether any resignation request already exists
for rec in self:
if rec.employee_id:
resignation_request = self.env['hr.resignation'].search([('employee_id', '=', rec.employee_id.id),
('state', 'in', ['confirm', 'approved'])])
if resignation_request:
raise ValidationError(_('There is a resignation request in confirmed or'
' approved state for this employee'))
if rec.employee_id:
no_of_contract = self.env['hr.contract'].search([('employee_id', '=', self.employee_id.id)])
for contracts in no_of_contract:
if contracts.state == 'open':
rec.employee_contract = contracts.name
rec.notice_period = contracts.notice_days
@api.constrains('joined_date')
def _check_dates(self):
# validating the entered dates
for rec in self:
resignation_request = self.env['hr.resignation'].search([('employee_id', '=', rec.employee_id.id),
('state', 'in', ['confirm', 'approved'])])
if resignation_request:
raise ValidationError(_('There is a resignation request in confirmed or'
' approved state for this employee'))
def confirm_resignation(self):
if self.joined_date:
if self.joined_date >= self.expected_revealing_date:
raise ValidationError(_('Last date of the Employee must be anterior to Joining date'))
for rec in self:
rec.state = 'confirm'
rec.resign_confirm_date = str(datetime.now())
else:
raise ValidationError(_('Please set joining date for employee'))
def cancel_resignation(self):
for rec in self:
rec.state = 'cancel'
def reject_resignation(self):
for rec in self:
rec.state = 'cancel'
def reset_to_draft(self):
for rec in self:
rec.state = 'draft'
rec.employee_id.active = True
rec.employee_id.resigned = False
rec.employee_id.fired = False
def approve_resignation(self):
for rec in self:
if rec.expected_revealing_date and rec.resign_confirm_date:
no_of_contract = self.env['hr.contract'].search([('employee_id', '=', self.employee_id.id)])
for contracts in no_of_contract:
if contracts.state == 'open':
rec.employee_contract = contracts.name
rec.state = 'approved'
rec.approved_revealing_date = rec.resign_confirm_date + timedelta(days=contracts.notice_days)
else:
rec.approved_revealing_date = rec.expected_revealing_date
# Changing state of the employee if resigning today
if rec.expected_revealing_date <= fields.Date.today() and rec.employee_id.active:
rec.employee_id.active = False
# Changing fields in the employee table with respect to resignation
rec.employee_id.resign_date = rec.expected_revealing_date
if rec.resignation_type == 'resigned':
rec.employee_id.resigned = True
else:
rec.employee_id.fired = True
# Removing and deactivating user
if rec.employee_id.user_id:
rec.employee_id.user_id.active = False
rec.employee_id.user_id = None
else:
raise ValidationError(_('Please enter valid dates.'))
def update_employee_status(self):
resignation = self.env['hr.resignation'].search([('state', '=', 'approved')])
for rec in resignation:
if rec.expected_revealing_date <= fields.Date.today() and rec.employee_id.active:
rec.employee_id.active = False
# Changing fields in the employee table with respect to resignation
rec.employee_id.resign_date = rec.expected_revealing_date
if rec.resignation_type == 'resigned':
rec.employee_id.resigned = True
else:
rec.employee_id.fired = True
# Removing and deactivating user
if rec.employee_id.user_id:
rec.employee_id.user_id.active = False
rec.employee_id.user_id = None
class HrEmployee(models.Model):
_inherit = 'hr.employee'
resign_date = fields.Date('Resign Date', readonly=True, help="Date of the resignation")
resigned = fields.Boolean(string="Resigned", default=False, store=True,
help="If checked then employee has resigned")
fired = fields.Boolean(string="Fired", default=False, store=True, help="If checked then employee has fired")
|