[Fix] hr : problem of ids in job position form view
[odoo/odoo.git] / addons / hr / hr.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import os
23
24 from osv import fields, osv
25 import tools
26 from tools.translate import _
27
28 class hr_employee_category(osv.osv):
29
30     def name_get(self, cr, uid, ids, context=None):
31         if not len(ids):
32             return []
33         reads = self.read(cr, uid, ids, ['name','parent_id'], context)
34         res = []
35         for record in reads:
36             name = record['name']
37             if record['parent_id']:
38                 name = record['parent_id'][1]+' / '+name
39             res.append((record['id'], name))
40         return res
41
42     def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context):
43         res = self.name_get(cr, uid, ids, context)
44         return dict(res)
45
46     _name = "hr.employee.category"
47     _description = "Employee Category"
48     _columns = {
49         'name': fields.char("Category", size=64, required=True),
50         'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
51         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
52         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
53     }
54
55     def _check_recursion(self, cr, uid, ids, context=None):
56         level = 100
57         while len(ids):
58             cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
59             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
60             if not level:
61                 return False
62             level -= 1
63         return True
64
65     _constraints = [
66         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
67     ]
68
69 hr_employee_category()
70
71 class hr_employee_marital_status(osv.osv):
72     _name = "hr.employee.marital.status"
73     _description = "Employee Marital Status"
74     _columns = {
75         'name': fields.char('Marital Status', size=32, required=True),
76         'description': fields.text('Status Description'),
77     }
78
79 hr_employee_marital_status()
80
81 class hr_job(osv.osv):
82
83     _name = "hr.job"
84     _description = "Job Description"
85     _columns = {
86         'name': fields.char('Job Name', size=128, required=True, select=True),
87         'expected_employees': fields.integer('Expected Employees', help='Required number of Employees'),
88         'no_of_employee': fields.integer('No of Employees', help='Number of employee there are already in the department'),
89         'no_of_recruitment': fields.integer('No of Recruitment', readonly=True),
90         'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
91         'description': fields.text('Job Description'),
92         'requirements': fields.text('Requirements'),
93         'department_id': fields.many2one('hr.department', 'Department'),
94         'company_id': fields.many2one('res.company', 'Company'),
95         'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
96     }
97     _defaults = {
98         'expected_employees': 1,
99         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
100         'state': 'open',
101         'no_of_recruitment': 1,
102     }
103
104     def on_change_expected_employee(self, cr, uid, ids, expected_employee, no_of_employee, context=None):
105         if context is None:
106             context = {}
107         result={}
108         if expected_employee:
109             result['no_of_recruitment'] = expected_employee - no_of_employee
110         return {'value': result}
111
112     def job_old(self, cr, uid, ids, *args):
113         self.write(cr, uid, ids, {'state': 'old'})
114         return True
115
116     def job_recruitement(self, cr, uid, ids, *args):
117         self.write(cr, uid, ids, {'state': 'recruit'})
118         return True
119
120     def job_open(self, cr, uid, ids, *args):
121         self.write(cr, uid, ids, {'state': 'open'})
122         return True
123
124 hr_job()
125
126 class hr_employee(osv.osv):
127     _name = "hr.employee"
128     _description = "Employee"
129     _inherits = {'resource.resource': "resource_id"}
130     _columns = {
131         'country_id': fields.many2one('res.country', 'Nationality'),
132         'birthday': fields.date("Date of Birth"),
133         'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
134         'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
135         'identification_id': fields.char('Identification No', size=32),
136         'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
137         'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
138         'department_id':fields.many2one('hr.department', 'Department'),
139         'address_id': fields.many2one('res.partner.address', 'Working Address'),
140         'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
141         'partner_id': fields.related('address_home_id', 'partner_id', type='many2one', relation='res.partner', readonly=True, help="Partner that is related to the current employee. Accounting transaction will be written on this partner belongs to employee."),
142         'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
143         'work_phone': fields.related('address_id', 'phone', type='char', size=32, string='Work Phone', readonly=True),
144         'work_email': fields.related('address_id', 'email', type='char', size=240, string='Work E-mail'),
145         'work_location': fields.char('Office Location', size=32),
146         'notes': fields.text('Notes'),
147         'parent_id': fields.related('department_id', 'manager_id', relation='hr.employee', string='Manager', type='many2one', store=True, select=True, readonly=True, help="It is linked with manager of Department"),
148         'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
149         'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
150         'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
151         'coach_id': fields.many2one('hr.employee', 'Coach'),
152         'job_id': fields.many2one('hr.job', 'Job'),
153         'photo': fields.binary('Photo'),
154         'passport_id':fields.char('Passport', size=64)
155     }
156
157     def onchange_company(self, cr, uid, ids, company, context=None):
158         company_id = self.pool.get('res.company').browse(cr,uid,company)
159         for address in company_id.partner_id.address:
160             return {'value': {'address_id': address.id}}
161         return {'value':{'address_id':False}}
162
163     def onchange_department(self, cr, uid, ids, department_id, context=None):
164         if not department_id:
165             return {'value':{'parent_id': False}}
166         manager = self.pool.get('hr.department').browse(cr, uid, department_id).manager_id
167         return {'value': {'parent_id':manager and manager.id or False}}
168
169     def onchange_user(self, cr, uid, ids, user_id, context=None):
170         if not user_id:
171             return {'value':{'work_email': False}}
172         mail = self.pool.get('res.users').browse(cr,uid,user_id)
173         return {'value': {'work_email':mail.user_email}}
174
175     def _get_photo(self, cr, uid, context=None):
176         return open(os.path.join(
177             tools.config['addons_path'], 'hr/image', 'photo.png'),
178                     'rb') .read().encode('base64')
179
180     _defaults = {
181         'active': 1,
182         'photo': _get_photo,
183         'address_id': lambda self,cr,uid,c: self.pool.get('res.partner.address').browse(cr, uid, uid, c).partner_id.id
184     }
185
186     def _check_recursion(self, cr, uid, ids, context=None):
187         level = 100
188         while len(ids):
189             cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
190             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
191             if not level:
192                 return False
193             level -= 1
194         return True
195
196     def _check_department_id(self, cr, uid, ids, context=None):
197         for emp in self.browse(cr, uid, ids, context=context):
198             if emp.department_id.manager_id and emp.id == emp.department_id.manager_id.id:
199                 return False
200         return True
201
202     _constraints = [
203         (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
204         (_check_department_id, 'Error ! You cannot select a department for which the employee is the manager.', ['department_id']),
205     ]
206
207 hr_employee()
208
209 class hr_department(osv.osv):
210     _description = "Department"
211     _inherit = 'hr.department'
212     _columns = {
213         'manager_id': fields.many2one('hr.employee', 'Manager'),
214         'member_ids': fields.one2many('hr.employee', 'department_id', 'Members'),
215     }
216
217 hr_department()
218
219 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: