1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
26 class hr_employee_category(osv.osv):
28 def name_get(self, cr, uid, ids, context=None):
31 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
35 if record['parent_id']:
36 name = record['parent_id'][1]+' / '+name
37 res.append((record['id'], name))
40 def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
41 res = self.name_get(cr, uid, ids, context=context)
44 _name = "hr.employee.category"
45 _description = "Employee Category"
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')
53 def _check_recursion(self, cr, uid, ids, context=None):
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()))
64 (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
67 hr_employee_category()
69 class hr_employee_marital_status(osv.osv):
70 _name = "hr.employee.marital.status"
71 _description = "Employee Marital Status"
73 'name': fields.char('Marital Status', size=32, required=True, translate=True),
74 'description': fields.text('Status Description'),
77 hr_employee_marital_status()
79 class hr_job(osv.osv):
81 def _no_of_employee(self, cr, uid, ids, name, args, context=None):
83 for job in self.browse(cr, uid, ids, context=context):
84 nb_employees = len(job.employee_ids or [])
86 'no_of_employee': nb_employees,
87 'expected_employees': nb_employees + job.no_of_recruitment,
92 _description = "Job Description"
94 'name': fields.char('Job Name', size=128, required=True, select=True),
95 'expected_employees': fields.function(_no_of_employee, method=True, string='Expected Employees', help='Required number of Employees in total for that job.', multi="no_of_employee"),
96 'no_of_employee': fields.function(_no_of_employee, method=True, string="No of Employee", help='Number of employee with that job.', multi="no_of_employee"),
97 'no_of_recruitment': fields.float('Expected in Recruitment'),
98 'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
99 'description': fields.text('Job Description'),
100 'requirements': fields.text('Requirements'),
101 'department_id': fields.many2one('hr.department', 'Department'),
102 'company_id': fields.many2one('res.company', 'Company'),
103 'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
106 'expected_employees': 1,
107 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
111 def on_change_expected_employee(self, cr, uid, ids, no_of_recruitment, no_of_employee, context=None):
114 return {'value': {'expected_employees': no_of_recruitment + no_of_employee}}
116 def job_old(self, cr, uid, ids, *args):
117 self.write(cr, uid, ids, {'state': 'old', 'no_of_recruitment': 0})
120 def job_recruitement(self, cr, uid, ids, *args):
121 for job in self.browse(cr, uid, ids):
122 no_of_recruitment = job.no_of_recruitment == 0 and 1 or job.no_of_recruitment
123 self.write(cr, uid, [job.id], {'state': 'recruit', 'no_of_recruitment': no_of_recruitment})
126 def job_open(self, cr, uid, ids, *args):
127 self.write(cr, uid, ids, {'state': 'open', 'no_of_recruitment': 0})
132 class hr_employee(osv.osv):
133 _name = "hr.employee"
134 _description = "Employee"
135 _inherits = {'resource.resource': "resource_id"}
137 'country_id': fields.many2one('res.country', 'Nationality'),
138 'birthday': fields.date("Date of Birth"),
139 'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
140 'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
141 'identification_id': fields.char('Identification No', size=32),
142 'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
143 'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
144 'department_id':fields.many2one('hr.department', 'Department'),
145 'address_id': fields.many2one('res.partner.address', 'Working Address'),
146 'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
147 '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."),
148 'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
149 'work_phone': fields.char('Work Phone', size=32, readonly=False),
150 'mobile_phone': fields.char('Work Mobile', size=32, readonly=False),
151 'work_email': fields.char('Work E-mail', size=240),
152 'work_location': fields.char('Office Location', size=32),
153 'notes': fields.text('Notes'),
154 'parent_id': fields.many2one('hr.employee', 'Manager'),
155 'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
156 'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
157 'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
158 'coach_id': fields.many2one('hr.employee', 'Coach'),
159 'job_id': fields.many2one('hr.job', 'Job'),
160 'photo': fields.binary('Photo'),
161 'passport_id':fields.char('Passport No', size=64)
164 def onchange_address_id(self, cr, uid, ids, address, context=None):
166 address = self.pool.get('res.partner.address').browse(cr, uid, address, context=context)
167 return {'value': {'work_email': address.email, 'work_phone': address.phone, 'mobile_phone': address.mobile}}
170 def onchange_company(self, cr, uid, ids, company, context=None):
173 company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
174 address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
175 address_id = address and address['default'] or False
176 return {'value': {'address_id' : address_id}}
178 def onchange_department_id(self, cr, uid, ids, department_id, context=None):
179 value = {'parent_id': False}
181 department = self.pool.get('hr.department').browse(cr, uid, department_id)
182 value['parent_id'] = department.manager_id.id
183 return {'value': value}
185 def onchange_user(self, cr, uid, ids, user_id, context=None):
188 work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).user_email
189 return {'value': {'work_email' : work_email}}
191 def _get_photo(self, cr, uid, context=None):
192 photo_path = addons.get_module_resource('hr','images','photo.png')
193 return open(photo_path, 'rb').read().encode('base64')
198 'address_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).address_id.id
201 def _check_recursion(self, cr, uid, ids, context=None):
204 cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
205 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
212 (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
217 class hr_department(osv.osv):
218 _description = "Department"
219 _inherit = 'hr.department'
221 'manager_id': fields.many2one('hr.employee', 'Manager'),
222 'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
228 class res_users(osv.osv):
230 _inherit = 'res.users'
232 def create(self, cr, uid, data, context=None):
233 user_id = super(res_users, self).create(cr, uid, data, context=context)
234 data_obj = self.pool.get('ir.model.data')
236 data_id = data_obj._get_id(cr, uid, 'hr', 'ir_ui_view_sc_employee')
237 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
238 self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
239 'user_id': user_id}, context=context)
241 # Tolerate a missing shortcut. See product/product.py for similar code.
242 logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
249 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: