[IMP]: hr: Added on change department in employee form to set manager
[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 from osv import fields, osv
23 import logging
24 import addons
25
26 class hr_employee_category(osv.osv):
27
28     def name_get(self, cr, uid, ids, context=None):
29         if not ids:
30             return []
31         reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
32         res = []
33         for record in reads:
34             name = record['name']
35             if record['parent_id']:
36                 name = record['parent_id'][1]+' / '+name
37             res.append((record['id'], name))
38         return res
39
40     def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
41         res = self.name_get(cr, uid, ids, context=context)
42         return dict(res)
43
44     _name = "hr.employee.category"
45     _description = "Employee Category"
46     _columns = {
47         'name': fields.char("Category", size=64, required=True),
48         'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
49         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
50         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
51     }
52
53     def _check_recursion(self, cr, uid, ids, context=None):
54         level = 100
55         while len(ids):
56             cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
57             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
58             if not level:
59                 return False
60             level -= 1
61         return True
62
63     _constraints = [
64         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
65     ]
66
67 hr_employee_category()
68
69 class hr_employee_marital_status(osv.osv):
70     _name = "hr.employee.marital.status"
71     _description = "Employee Marital Status"
72     _columns = {
73         'name': fields.char('Marital Status', size=32, required=True, translate=True),
74         'description': fields.text('Status Description'),
75     }
76
77 hr_employee_marital_status()
78
79 class hr_job(osv.osv):
80
81     def _no_of_employee(self, cr, uid, ids, name, args, context=None):
82         res = {}
83         for job in self.browse(cr, uid, ids, context=context):
84             res[job.id] = len(job.employee_ids or [])
85         return res
86
87     def _no_of_recruitement(self, cr, uid, ids, name, args, context=None):
88         res = {}
89         for job in self.browse(cr, uid, ids, context=context):
90             res[job.id] = job.expected_employees - job.no_of_employee
91         return res
92
93     _name = "hr.job"
94     _description = "Job Description"
95     _columns = {
96         'name': fields.char('Job Name', size=128, required=True, select=True),
97         'expected_employees': fields.integer('Expected Employees', help='Required number of Employees in total for that job.'),
98         'no_of_employee': fields.function(_no_of_employee, method=True, string="No of Employee", help='Number of employee with that job.'),
99         'no_of_recruitment': fields.function(_no_of_recruitement, method=True, string='Expected in Recruitment', readonly=True),
100         'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
101         'description': fields.text('Job Description'),
102         'requirements': fields.text('Requirements'),
103         'department_id': fields.many2one('hr.department', 'Department'),
104         'company_id': fields.many2one('res.company', 'Company'),
105         'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
106     }
107     _defaults = {
108         'expected_employees': 1,
109         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
110         'state': 'open',
111     }
112
113     def on_change_expected_employee(self, cr, uid, ids, expected_employee, no_of_employee, context=None):
114         if context is None:
115             context = {}
116         result = {}
117         if expected_employee:
118             result['no_of_recruitment'] = expected_employee - no_of_employee
119         return {'value': result}
120
121     def job_old(self, cr, uid, ids, *args):
122         self.write(cr, uid, ids, {'state': 'old'})
123         return True
124
125     def job_recruitement(self, cr, uid, ids, *args):
126         self.write(cr, uid, ids, {'state': 'recruit'})
127         return True
128
129     def job_open(self, cr, uid, ids, *args):
130         self.write(cr, uid, ids, {'state': 'open'})
131         return True
132
133 hr_job()
134
135 class hr_employee(osv.osv):
136     _name = "hr.employee"
137     _description = "Employee"
138     _inherits = {'resource.resource': "resource_id"}
139     _columns = {
140         'country_id': fields.many2one('res.country', 'Nationality'),
141         'birthday': fields.date("Date of Birth"),
142         'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
143         'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
144         'identification_id': fields.char('Identification No', size=32),
145         'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
146         'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
147         'department_id':fields.many2one('hr.department', 'Department'),
148         'address_id': fields.many2one('res.partner.address', 'Working Address'),
149         'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
150         '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."),
151         'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
152         'work_phone': fields.char('Work Phone', size=32, readonly=False),
153         'mobile_phone': fields.char('Work Mobile', size=32, readonly=False),
154         'work_email': fields.char('Work E-mail', size=240),
155         'work_location': fields.char('Office Location', size=32),
156         'notes': fields.text('Notes'),
157         'parent_id': fields.many2one('hr.employee', 'Manager'), 
158         'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
159         'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
160         'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
161         'coach_id': fields.many2one('hr.employee', 'Coach'),
162         'job_id': fields.many2one('hr.job', 'Job'),
163         'photo': fields.binary('Photo'),
164         'passport_id':fields.char('Passport No', size=64)
165     }
166
167     def onchange_address_id(self, cr, uid, ids, address, context=None):
168         if address:
169             address = self.pool.get('res.partner.address').browse(cr, uid, address, context=context)
170             return {'value': {'work_email': address.email, 'work_phone': address.phone, 'mobile_phone': address.mobile}}
171         return {'value': {}}
172
173     def onchange_company(self, cr, uid, ids, company, context=None):
174         address_id = False
175         if company:
176             company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
177             address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
178             address_id = address and address['default'] or False
179         return {'value': {'address_id' : address_id}}
180
181     def onchange_department_id(self, cr, uid, ids, department_id, context=None):
182         value = {'parent_id': False}
183         if department_id:
184             department = self.pool.get('hr.department').browse(cr, uid, department_id)
185             value['parent_id'] = department.manager_id.id
186         return {'value': value}
187
188     def onchange_user(self, cr, uid, ids, user_id, context=None):
189         work_email = False
190         if user_id:
191             work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).user_email
192         return {'value': {'work_email' : work_email}}
193
194     def _get_photo(self, cr, uid, context=None):
195         photo_path = addons.get_module_resource('hr','images','photo.png')
196         return open(photo_path, 'rb').read().encode('base64')
197
198     _defaults = {
199         'active': 1,
200         'photo': _get_photo,
201         'address_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).address_id.id
202     }
203
204     def _check_recursion(self, cr, uid, ids, context=None):
205         level = 100
206         while len(ids):
207             cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
208             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
209             if not level:
210                 return False
211             level -= 1
212         return True
213
214     _constraints = [
215         (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
216     ]
217
218 hr_employee()
219
220 class hr_department(osv.osv):
221     _description = "Department"
222     _inherit = 'hr.department'
223     _columns = {
224         'manager_id': fields.many2one('hr.employee', 'Manager'),
225         'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
226     }
227
228 hr_department()
229
230
231 class res_users(osv.osv):
232     _name = 'res.users'
233     _inherit = 'res.users'
234
235     def create(self, cr, uid, data, context=None):
236         user_id = super(res_users, self).create(cr, uid, data, context=context)
237         data_obj = self.pool.get('ir.model.data')
238         try:
239             data_id = data_obj._get_id(cr, uid, 'hr', 'ir_ui_view_sc_employee')
240             view_id  = data_obj.browse(cr, uid, data_id, context=context).res_id
241             self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
242                                         'user_id': user_id}, context=context)
243         except:
244             # Tolerate a missing shortcut. See product/product.py for similar code.
245             logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
246             
247         return user_id
248
249 res_users()
250
251
252 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: