-
Notifications
You must be signed in to change notification settings - Fork 0
/
hr.py
247 lines (210 loc) · 8.04 KB
/
hr.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# -*- coding:utf-8 -*-
#
#
# Copyright (C) 2013 Michael Telahun Makonnen <[email protected]>.
# All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
from openerp.osv import fields, orm
from openerp.tools.translate import _
import logging
_l = logging.getLogger(__name__)
class hr_job(orm.Model):
_name = 'hr.job'
_inherit = 'hr.job'
def _get_all_child_ids(self, cr, uid, ids, field_name, arg, context=None):
result = dict.fromkeys(ids)
for i in ids:
result[i] = self.search(
cr, uid, [('parent_id', 'child_of', i)], context=context)
return result
_columns = {
'department_manager': fields.boolean(
'Department Manager',
),
'parent_id': fields.many2one(
'hr.job',
'Immediate Superior',
ondelete='cascade',
),
'child_ids': fields.one2many(
'hr.job',
'parent_id',
'Immediate Subordinates',
),
'all_child_ids': fields.function(
_get_all_child_ids,
type='many2many',
relation='hr.job',
),
'parent_left': fields.integer(
'Left Parent',
select=1,
),
'parent_right': fields.integer(
'Right Parent',
select=1,
),
}
_parent_name = "parent_id"
_parent_store = True
_parent_order = 'name'
_order = 'parent_left'
def _check_recursion(self, cr, uid, ids, context=None):
# Copied from product.category
# This is a brute-force approach to the problem, but should be good
# enough.
#
level = 100
while len(ids):
cr.execute(
'select distinct parent_id from hr_job where id IN %s',
(tuple(ids), )
)
ids = filter(None, map(lambda x: x[0], cr.fetchall()))
if not level:
return False
level -= 1
return True
def _rec_message(self, cr, uid, ids, context=None):
return _('Error!\nYou cannot create recursive jobs.')
_constraints = [
(_check_recursion, _rec_message, ['parent_id']),
]
def write(self, cr, uid, ids, vals, context=None):
res = super(hr_job, self).write(cr, uid, ids, vals, context=None)
if isinstance(ids, (int, long)):
ids = [ids]
dept_obj = self.pool.get('hr.department')
if vals.get('department_manager', False):
for di in ids:
job = self.browse(cr, uid, di, context=context)
dept_id = False
if vals.get('department_id', False):
dept_id = vals['department_id']
else:
dept_id = job.department_id.id
employee_id = False
for ee in job.employee_ids:
employee_id = ee.id
if employee_id:
dept_obj.write(
cr, uid, dept_id, {
'manager_id': employee_id,
}, context=context
)
elif vals.get('department_id', False):
for di in ids:
job = self.browse(cr, uid, di, context=context)
if job.department_manager:
employee_id = False
for ee in job.employee_ids:
employee_id = ee.id
dept_obj.write(
cr, uid, vals['department_id'], {
'manager_id': employee_id,
}, context=context
)
elif vals.get('parent_id', False):
ee_obj = self.pool.get('hr.employee')
parent_job = self.browse(
cr, uid, vals['parent_id'], context=context)
parent_id = False
for ee in parent_job.employee_ids:
parent_id = ee.id
for job in self.browse(cr, uid, ids, context=context):
ee_obj.write(
cr, uid, [ee.id for ee in job.employee_ids], {
'parent_id': parent_id,
}, context=context
)
return res
class hr_contract(orm.Model):
_name = 'hr.contract'
_inherit = 'hr.contract'
def create(self, cr, uid, vals, context=None):
res = super(hr_contract, self).create(cr, uid, vals, context=context)
if not vals.get('job_id', False):
return res
ee_obj = self.pool.get('hr.employee')
job = self.pool.get('hr.job').browse(
cr, uid, vals['job_id'], context=context)
# Write the employee's manager
if job and job.parent_id:
parent_id = False
for ee in job.parent_id.employee_ids:
parent_id = ee.id
if parent_id and vals.get('employee_id'):
ee_obj.write(
cr, uid, vals['employee_id'], {'parent_id': parent_id},
context=context)
# Write any employees with jobs that are immediate descendants of this
# job
if job:
job_child_ids = [child.id for child in job.child_ids]
if len(job_child_ids) > 0:
ee_ids = ee_obj.search(
cr, uid, [('job_id', 'in', job_child_ids)])
if len(ee_ids) > 0:
parent_id = False
for ee in job.employee_ids:
parent_id = ee.id
if parent_id:
ee_obj.write(
cr, uid, ee_ids, {
'parent_id': parent_id,
}, context=context
)
return res
def write(self, cr, uid, ids, vals, context=None):
res = super(hr_contract, self).write(
cr, uid, ids, vals, context=context)
if not vals.get('job_id', False):
return res
if isinstance(ids, (int, long)):
ids = [ids]
ee_obj = self.pool.get('hr.employee')
job = self.pool.get('hr.job').browse(
cr, uid, vals['job_id'], context=context)
# Write the employee's manager
if job and job.parent_id:
parent_id = False
for ee in job.parent_id.employee_ids:
parent_id = ee.id
if parent_id:
for contract in self.browse(cr, uid, ids, context=context):
ee_obj.write(cr, uid, contract.employee_id.id, {
'parent_id': parent_id}, context=context)
# Write any employees with jobs that are immediate descendants of this
# job
if job:
job_child_ids = []
[job_child_ids.append(child.id) for child in job.child_ids]
if len(job_child_ids) > 0:
ee_ids = ee_obj.search(
cr, uid, [('job_id', 'in', job_child_ids),
('active', '=', True)])
if len(ee_ids) > 0:
parent_id = False
for ee in job.employee_ids:
parent_id = ee.id
if parent_id:
ee_obj.write(
cr, uid, ee_ids, {
'parent_id': parent_id,
}, context=context
)
return res