[IMP] hr: Unnecessary dependacy removed only highest level on __openerp__, Clean...
[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     _name = "hr.employee.category"
30     _description = "Employee Category"
31     _columns = {
32         'name': fields.char("Category", size=64, required=True),
33         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
34         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
35     }
36
37     def _check_recursion(self, cr, uid, ids, context=None):
38         if context is None:
39             context = {}
40         level = 100
41         while len(ids):
42             cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
43             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
44             if not level:
45                 return False
46             level -= 1
47         return True
48
49     _constraints = [
50         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
51     ]
52
53 hr_employee_category()
54
55 class hr_employee_marital_status(osv.osv):
56     _name = "hr.employee.marital.status"
57     _description = "Employee Marital Status"
58     _columns = {
59         'name': fields.char('Marital Status', size=32, required=True),
60         'description': fields.text('Status Description'),
61     }
62
63 hr_employee_marital_status()
64
65 class hr_job(osv.osv):
66
67     def _no_of_employee(self, cr, uid, ids, name, args, context=None):
68         if context is None:
69             context = {}
70         res = {}
71         for emp in self.browse(cr, uid, ids):
72             res[emp.id] = len(emp.employee_ids or [])
73         return res
74
75     _name = "hr.job"
76     _description = "Job Description"
77     _columns = {
78         'name': fields.char('Job Name', size=128, required=True, select=True),
79         'expected_employees': fields.integer('Expected Employees', help='Required number of Employees'),
80         'no_of_employee': fields.function(_no_of_employee, method=True, string='No of Employees', type='integer', help='Number of Employees selected'),
81         'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
82         'description': fields.text('Job Description'),
83         'requirements': fields.text('Requirements'),
84         'department_id': fields.many2one('hr.department', 'Department'),
85         'company_id': fields.many2one('res.company', 'Company'),
86         'state': fields.selection([('open', 'Open'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
87     }
88     _defaults = {
89         'expected_employees': 1,
90         'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
91         'state': 'open'
92     }
93
94     def job_old(self, cr, uid, ids, *args):
95         self.write(cr, uid, ids, {'state': 'old'})
96         return True
97
98     def job_recruitement(self, cr, uid, ids, *args):
99         self.write(cr, uid, ids, {'state': 'recruit'})
100         return True
101
102     def job_open(self, cr, uid, ids, *args):
103         self.write(cr, uid, ids, {'state': 'open'})
104         return True
105
106 hr_job()
107
108 class hr_employee(osv.osv):
109     _name = "hr.employee"
110     _description = "Employee"
111     _inherits = {'resource.resource': "resource_id"}
112     _columns = {
113         'country_id': fields.many2one('res.country', 'Nationality'),
114         'birthday': fields.date("Birthday"),
115         'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
116         'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
117         'otherid': fields.char('Other ID', size=32),
118         'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
119         'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
120         'bank_account': fields.char('Bank Account', size=64),
121         'partner_id': fields.related('company_id', 'partner_id', type='many2one', relation='res.partner', readonly=True),
122         'department_id':fields.many2one('hr.department', 'Department'),
123         'address_id': fields.many2one('res.partner.address', 'Working Address'),
124         'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
125         'work_phone': fields.related('address_id', 'phone', type='char', string='Work Phone', readonly=True),
126         'work_email': fields.related('address_id', 'email', type='char', size=240, string='Work E-mail', readonly=True),
127         'work_location': fields.char('Office Location', size=32),
128         'notes': fields.text('Notes'),
129         'parent_id': fields.related('department_id', 'manager_id', relation='hr.employee', string='Manager', type='many2one', store=True, select=True),
130         'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
131         'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
132         'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
133         'coach_id': fields.many2one('hr.employee', 'Coach'),
134         'job_id': fields.many2one('hr.job', 'Job'),
135         'photo': fields.binary('Photo')
136     }
137
138     def _get_photo(self, cr, uid, context=None):
139         if context is None:
140             context = {}
141         return open(os.path.join(
142             tools.config['addons_path'], 'hr/image', 'photo.png'),
143                     'rb') .read().encode('base64')
144
145     _defaults = {
146         'active': 1,
147         'photo': _get_photo,
148     }
149
150     def _check_recursion(self, cr, uid, ids, context=None):
151         level = 100
152         while len(ids):
153             cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
154             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
155             if not level:
156                 return False
157             level -= 1
158         return True
159
160     _constraints = [
161         (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id'])
162     ]
163
164 hr_employee()
165
166 class hr_department(osv.osv):
167     _description = "Department"
168     _inherit = 'hr.department'
169     _columns = {
170         'manager_id': fields.many2one('hr.employee', 'Manager'),
171 #        'member_ids': fields.many2many('hr.employee', 'hr_department_user_rel', 'department_id', 'user_id', 'Members'),
172         'member_ids': fields.one2many('hr.employee', 'department_id', 'Members'),
173 #       finding problem to implement one2many field as "hr_departmen_user_rel" is used in another module query
174     }
175
176 hr_department()
177
178 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: