~initos.com/openerp-hr/7.0-add-holiday-weekend

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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#-*- coding:utf-8 -*-
#
#
#    Copyright (C) 2013 Michael Telahun Makonnen <mmakonnen@gmail.com>.
#    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, osv
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as OE_DATEFORMAT
from openerp.tools.translate import _


class hr_job(osv.Model):

    _name = 'hr.job'
    _inherit = 'hr.job'

    _columns = {
        'max_employees': fields.integer('Maximum Number of Employees'),
        'max_employees_fuzz': fields.integer('Expected Turnover',
                                             help="Recruitment module will allow you to \
                                                  create this number of additional applicants and \
                                                  contracts above the maximum value. Use this \
                                                  number as a buffer to have additional \
                                                  employees on hand to cover employee turnover."),
    }

    # Do not write negative values for no. of recruitment
    def write(self, cr, uid, ids, vals, context=None):

        value = vals.get('no_of_recruitment', False)
        if value and value < 0:
            vals['no_of_recruitment'] = 0

        return super(hr_job, self).write(cr, uid, ids, vals, context=context)


class hr_applicant(osv.Model):

    _name = 'hr.applicant'
    _inherit = 'hr.applicant'

    def create(self, cr, uid, vals, context=None):

        if vals.get('job_id', False):
            data = self.pool.get('hr.job').read(cr, uid, vals['job_id'],
                                                ['max_employees', 'no_of_employee', 'state',
                                                 'max_employees_fuzz'],
                                                context=context)
            if data.get('state', False):
                if data['state'] != 'recruit' and int(data['no_of_employee']) >= (int(data['max_employees']) + data['max_employees_fuzz']):
                    raise osv.except_osv(_('Job not open for recruitment!'),
                                         _('You may not register applicants for jobs that are not recruiting.'))

        return super(hr_applicant, self).create(cr, uid, vals, context=context)


class hr_contract(osv.Model):

    _name = 'hr.contract'
    _inherit = 'hr.contract'

    def _get_job_from_applicant(self, cr, uid, context=None):
        """If the applicant went through recruitment get the job id from there."""

        res = False
        if context != None:
            ee_ids = context.get('search_default_employee_id', False)
            if ee_ids and len(ee_ids) > 0:
                # If this is the first contract try to obtain job position from
                # application
                if len(self.search(cr, uid, [('employee_id', 'in', ee_ids)], context=context)) > 0:
                    return res
                applicant_obj = self.pool.get('hr.applicant')
                applicant_ids = applicant_obj.search(
                    cr, uid, [('emp_id', '=', ee_ids[0])], context=context)
                if len(applicant_ids) > 0:
                    data = applicant_obj.read(
                        cr, uid, applicant_ids[0], ['job_id'], context=context)
                    res = data['job_id'][0]

        return res

    _defaults = {
        'job_id': _get_job_from_applicant,
    }

    def create(self, cr, uid, vals, context=None):

        # If the contract is for an employee with a pre-existing contract for
        # the same job, then bypass checks.
        employee_id = vals.get('employee_id', False)
        if employee_id:
            contract_ids = self.search(cr, uid, [
                ('employee_id', '=', employee_id),
                ('state', 'not in', [
                    'draft', 'done']),
            ],
                context=context)
            for contract in self.browse(cr, uid, contract_ids, context=context):
                if vals.get('job_id', False) == contract.job_id.id:
                    return super(hr_contract, self).create(cr, uid, vals, context=context)

        # 1. Verify job is in recruitment
        if vals.get('job_id', False):
            data = self.pool.get('hr.job').read(cr, uid, vals['job_id'],
                                                ['name', 'max_employees', 'max_employees_fuzz',
                                                    'no_of_employee', 'state'],
                                                context=context)
            if data.get('state', False):
                if data['state'] != 'recruit' and int(data['no_of_employee']) >= (int(data['max_employees']) + data['max_employees_fuzz']):
                    raise osv.except_osv(
                        _('The Job "%s" is not in recruitment!') % (
                            data['name']),
                        _('You may not create contracts for jobs that are not in recruitment state.'))

        # 2. Verify that the number of open contracts < total expected
        # employees
        if vals.get('job_id', False):
            contract_ids = self.search(cr, uid, [
                ('job_id', '=', vals[
                    'job_id']),
                ('state', 'not in', ['done']),
            ],
                context=context)

            data = self.pool.get('hr.job').read(cr, uid, vals['job_id'],
                                                ['name', 'expected_employees',
                                                    'max_employees', 'max_employees_fuzz'],
                                                context=context)
            expected_employees = data.get(
                'expected_employees', False) and data['expected_employees'] or 0
            max_employees = data.get(
                'max_employees', False) and data['max_employees'] or 0
            max_employees_fuzz = data.get(
                'max_employees_fuzz', False) and data['max_employees_fuzz'] or 0

            if len(contract_ids) >= max(expected_employees, max_employees + max_employees_fuzz):
                raise osv.except_osv(
                    _('Maximum Number of Employees Exceeded!'),
                    _('The maximum number of employees for "%s" has been exceeded.') % (data['name']))

        return super(hr_contract, self).create(cr, uid, vals, context=context)


class hr_recruitment_request(osv.Model):

    _name = 'hr.recruitment.request'
    _description = 'Request for recruitment of additional personnel'
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    _columns = {
        'name': fields.char('Description', size=64),
        'user_id': fields.many2one('res.users', 'Requesting User', required=True),
        'department_id': fields.many2one('hr.department', 'Department'),
        'job_id': fields.many2one('hr.job', 'Job', required=True),
        'number': fields.integer('Number to Recruit'),
        'current_number': fields.related('job_id', 'no_of_employee', type='integer', string="Current Number of Employees", readonly=True),
        'max_number': fields.related('job_id', 'max_employees', type='integer', string="Maximum Number of Employees", readonly=True),
        'reason': fields.text('Reason for Request'),
        'state': fields.selection([('draft', 'Draft'),
                                   ('confirm', 'Confirmed'),
                                   ('exception', 'Exception'),
                                   ('recruitment', 'In Recruitment'),
                                   ('decline', 'Declined'),
                                   ('done', 'Done'),
                                   ('cancel', 'Cancelled'),
                                   ],
                                  'State', readonly=True),
    }

    _order = 'department_id, job_id'

    _defaults = {
        'number': 1,
        'user_id': lambda self, cr, uid, context=None: uid,
    }

    _track = {
        'state': {
            'hr_labour_recruitment.mt_alert_request_confirmed': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'confirm',
            'hr_labour_recruitment.mt_alert_request_exception': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'exception',
            'hr_labour_recruitment.mt_alert_request_approved': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'recruitment',
            'hr_labour_recruitment.mt_alert_request_declined': lambda self, cr, uid, obj, ctx=None: obj['state'] == 'decline',
        },
    }

    def onchange_job(self, cr, uid, ids, job_id, context=None):

        res = {'value': {'deparment_id': False, 'name': False}}
        if job_id:
            data = self.pool.get('hr.job').read(
                cr, uid, job_id, ['name', 'department_id'], context=context)
            if data.get('department_id', False):
                res['value']['department_id'] = data['department_id'][0]
            res['value']['name'] = 'Personnel Request: ' + str(data['name'])

        return res

    def _needaction_domain_get(self, cr, uid, context=None):

        users_obj = self.pool.get('res.users')

        domain = []
        has_prev_domain = False
        if users_obj.has_group(cr, uid, 'base.group_hr_manager'):
            domain = [('state', '=', 'recruitment')]
            has_prev_domain = True
        if users_obj.has_group(cr, uid, 'hr_security.group_hr_director'):
            if has_prev_domain:
                domain = ['|'] + domain
            domain = domain + [('state', 'in', ['confirm', 'exception'])]

        if len(domain) == 0:
            return False

        return domain

    def condition_exception(self, cr, uid, ids, context=None):

        for req in self.browse(cr, uid, ids, context=context):
            if req.number + req.job_id.expected_employees > req.job_id.max_employees:
                return True

        return False

    def _state(self, cr, uid, ids, state, context=None):

        job_obj = self.pool.get('hr.job')

        for req in self.browse(cr, uid, ids, context=context):

            if state == 'recruitment':
                job_obj.write(cr, uid, req.job_id.id, {
                              'no_of_recruitment': req.number}, context=context)
                job_obj.job_recruitement(cr, uid, [req.job_id.id])
            elif state in ['done', 'cancel']:
                job_obj.job_open(cr, uid, [req.job_id.id])

            self.write(cr, uid, req.id, {'state': state}, context=context)

        return True

    def _state_subscribe_users(self, cr, uid, ids, state, context=None):

        imd_obj = self.pool.get('ir.model.data')
        model, group1_id = imd_obj.get_object_reference(
            cr, uid, 'base', 'group_hr_manager')
        model, group2_id = imd_obj.get_object_reference(
            cr, uid, 'hr_security', 'group_hr_director')
        data = self.pool.get('res.groups').read(
            cr, uid, [group1_id, group2_id], ['users'], context=context)
        user_ids = list(set(data[0]['users'] + data[1]['users']))
        self.message_subscribe_users(
            cr, uid, ids, user_ids=user_ids, context=context)

        return self._state(cr, uid, ids, state, context=context)

    def state_confirm(self, cr, uid, ids, context=None):

        return self._state_subscribe_users(cr, uid, ids, 'confirm', context=context)

    def state_exception(self, cr, uid, ids, context=None):

        return self._state_subscribe_users(cr, uid, ids, 'exception', context=context)

    def state_recruitment(self, cr, uid, ids, context=None):

        return self._state_subscribe_users(cr, uid, ids, 'recruitment', context=context)

    def state_done(self, cr, uid, ids, context=None):

        return self._state(cr, uid, ids, 'done', context=context)

    def state_cancel(self, cr, uid, ids, context=None):

        return self._state(cr, uid, ids, 'cancel', context=context)