[MERGE] OPW 383649
[odoo/odoo.git] / addons / hr / hr.py
index d25ac4e..fd4826a 100644 (file)
@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as
 #    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/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
-from mx import DateTime
-import time
-import math
-
 from osv import fields, osv
-from tools.translate import _
-
-class hr_timesheet_group(osv.osv):
-    _name = "hr.timesheet.group"
-    _description = "Working Time"
-    _columns = {
-        'name' : fields.char("Group name", size=64, required=True),
-        'timesheet_id' : fields.one2many('hr.timesheet', 'tgroup_id', 'Working Time'),
-        'manager' : fields.many2one('res.users', 'Workgroup manager'),
-    }
-    def interval_min_get(self, cr, uid, id, dt_from, hours):
-        if not id:
-            return [(dt_from-DateTime.RelativeDateTime(hours=int(hours)*3), dt_from)]
-        todo = hours
-        cycle = 0
-        result = []
-        maxrecur = 100
-        current_hour = dt_from.hour
-        while (todo>0) and maxrecur:
-            cr.execute("select hour_from,hour_to from hr_timesheet where dayofweek='%s' and tgroup_id=%s order by hour_from desc", (dt_from.day_of_week,id))
-            for (hour_from,hour_to) in cr.fetchall():
-                if (hour_from<current_hour) and (todo>0):
-                    m = min(hour_to, current_hour)
-                    if (m-hour_from)>todo:
-                        hour_from = m-todo
-                    d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_from)),int((hour_from%1) * 60))
-                    d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
-                    result.append((d1, d2))
-                    current_hour = hour_from
-                    todo -= (m-hour_from)
-            dt_from -= DateTime.RelativeDateTime(days=1)
-            current_hour = 24
-            maxrecur -= 1
-        result.reverse()
-        return result
-
-    def interval_get(self, cr, uid, id, dt_from, hours, byday=True):
-        if not id:
-            return [(dt_from,dt_from+DateTime.RelativeDateTime(hours=int(hours)*3))]
-        todo = hours
-        cycle = 0
-        result = []
-        maxrecur = 100
-        current_hour = dt_from.hour
-        while (todo>0) and maxrecur:
-            cr.execute("select hour_from,hour_to from hr_timesheet where dayofweek='%s' and tgroup_id=%s order by hour_from", (dt_from.day_of_week,id))
-            for (hour_from,hour_to) in cr.fetchall():
-                if (hour_to>current_hour) and (todo>0):
-                    m = max(hour_from, current_hour)
-                    if (hour_to-m)>todo:
-                        hour_to = m+todo
-                    d1 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(m)),int((m%1) * 60))
-                    d2 = DateTime.DateTime(dt_from.year,dt_from.month,dt_from.day,int(math.floor(hour_to)),int((hour_to%1) * 60))
-                    result.append((d1, d2))
-                    current_hour = hour_to
-                    todo -= (hour_to - m)
-            dt_from += DateTime.RelativeDateTime(days=1)
-            current_hour = 0
-            maxrecur -= 1
-        return result
-
-hr_timesheet_group()
 
+import addons
 
 class hr_employee_category(osv.osv):
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not ids:
+            return []
+        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
+        res = []
+        for record in reads:
+            name = record['name']
+            if record['parent_id']:
+                name = record['parent_id'][1]+' / '+name
+            res.append((record['id'], name))
+        return res
+
+    def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
+        res = self.name_get(cr, uid, ids, context=context)
+        return dict(res)
+
     _name = "hr.employee.category"
     _description = "Employee Category"
-    
     _columns = {
-        'name' : fields.char("Category", size=64, required=True),
+        'name': fields.char("Category", size=64, required=True),
+        'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
         'parent_id': fields.many2one('hr.employee.category', 'Parent Category', select=True),
         'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories')
     }
-    
-    def _check_recursion(self, cr, uid, ids):
+
+    def _check_recursion(self, cr, uid, ids, context=None):
         level = 100
         while len(ids):
-            cr.execute('select distinct parent_id from hr_employee_category where id in ('+','.join(map(str, ids))+')')
+            cr.execute('select distinct parent_id from hr_employee_category where id IN %s', (tuple(ids), ))
             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
             if not level:
                 return False
             level -= 1
         return True
-    
+
     _constraints = [
         (_check_recursion, 'Error ! You cannot create recursive Categories.', ['parent_id'])
     ]
-    
+
 hr_employee_category()
 
-class hr_employee(osv.osv):
-    _name = "hr.employee"
-    _description = "Employee"
+class hr_employee_marital_status(osv.osv):
+    _name = "hr.employee.marital.status"
+    _description = "Employee Marital Status"
+    _columns = {
+        'name': fields.char('Marital Status', size=32, required=True, translate=True),
+        'description': fields.text('Status Description'),
+    }
+
+hr_employee_marital_status()
+
+class hr_job(osv.osv):
 
+    def _no_of_employee(self, cr, uid, ids, name, args, context=None):
+        res = {}
+        for job in self.browse(cr, uid, ids, context=context):
+            res[job.id] = len(job.employee_ids or [])
+        return res
+
+    def _no_of_recruitement(self, cr, uid, ids, name, args, context=None):
+        res = {}
+        for job in self.browse(cr, uid, ids, context=context):
+            res[job.id] = job.expected_employees - job.no_of_employee
+        return res
+
+    _name = "hr.job"
+    _description = "Job Description"
     _columns = {
-        'name' : fields.char("Employee's Name", size=128, required=True),
-        'active' : fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the employee record without removing it."),
+        'name': fields.char('Job Name', size=128, required=True, select=True),
+        'expected_employees': fields.integer('Expected Employees', help='Required number of Employees in total for that job.'),
+        'no_of_employee': fields.function(_no_of_employee, method=True, string="No of Employee", help='Number of employee with that job.'),
+        'no_of_recruitment': fields.function(_no_of_recruitement, method=True, string='Expected in Recruitment', readonly=True),
+        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
+        'description': fields.text('Job Description'),
+        'requirements': fields.text('Requirements'),
+        'department_id': fields.many2one('hr.department', 'Department'),
         'company_id': fields.many2one('res.company', 'Company'),
-        'user_id' : fields.many2one('res.users', 'Related User'),
+        'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
+    }
+    _defaults = {
+        'expected_employees': 1,
+        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.job', context=c),
+        'state': 'open',
+    }
 
-        'country_id' : fields.many2one('res.country', 'Nationality'),
-        'birthday' : fields.date("Birthday"),
-        'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
-        'sinid': fields.char('SIN No', size=32),
-        'otherid': fields.char('Other ID', size=32),
-        'gender': fields.selection([('',''),('male','Male'),('female','Female')], 'Gender'),
-        'marital': fields.selection([('married','Married'),('unmarried','Unmarried'),('divorced','Divorced'),('other','Other')],'Marital Status', size=32),
+    def on_change_expected_employee(self, cr, uid, ids, expected_employee, no_of_employee, context=None):
+        if context is None:
+            context = {}
+        result = {}
+        if expected_employee:
+            result['no_of_recruitment'] = expected_employee - no_of_employee
+        return {'value': result}
 
+    def job_old(self, cr, uid, ids, *args):
+        self.write(cr, uid, ids, {'state': 'old'})
+        return True
+
+    def job_recruitement(self, cr, uid, ids, *args):
+        self.write(cr, uid, ids, {'state': 'recruit'})
+        return True
+
+    def job_open(self, cr, uid, ids, *args):
+        self.write(cr, uid, ids, {'state': 'open'})
+        return True
+
+hr_job()
+
+class hr_employee(osv.osv):
+    _name = "hr.employee"
+    _description = "Employee"
+    _inherits = {'resource.resource': "resource_id"}
+    _columns = {
+        'country_id': fields.many2one('res.country', 'Nationality'),
+        'birthday': fields.date("Date of Birth"),
+        'ssnid': fields.char('SSN No', size=32, help='Social Security Number'),
+        'sinid': fields.char('SIN No', size=32, help="Social Insurance Number"),
+        'identification_id': fields.char('Identification No', size=32),
+        'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
+        'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
+        'department_id':fields.many2one('hr.department', 'Department'),
         'address_id': fields.many2one('res.partner.address', 'Working Address'),
         'address_home_id': fields.many2one('res.partner.address', 'Home Address'),
-        'work_phone': fields.char('Work Phone', size=32),
-        'work_email': fields.char('Work Email', size=128),
+        '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."),
+        'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
+        'work_phone': fields.char('Work Phone', size=32, readonly=False),
+        'mobile_phone': fields.char('Mobile', size=32, readonly=False),
+        'work_email': fields.char('Work E-mail', size=240),
         'work_location': fields.char('Office Location', size=32),
-
         'notes': fields.text('Notes'),
-        'parent_id': fields.many2one('hr.employee', 'Manager', select=True),
-        'category_id' : fields.many2one('hr.employee.category', 'Category'),
-        'child_ids': fields.one2many('hr.employee', 'parent_id','Subordinates'),
+        '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"),
+        'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
+        'child_ids': fields.one2many('hr.employee', 'parent_id', 'Subordinates'),
+        'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
+        'coach_id': fields.many2one('hr.employee', 'Coach'),
+        'job_id': fields.many2one('hr.job', 'Job'),
+        'photo': fields.binary('Photo'),
+        'passport_id':fields.char('Passport No', size=64)
     }
+
+    def onchange_address_id(self, cr, uid, ids, address, context=None):
+        if address:
+            address = self.pool.get('res.partner.address').browse(cr, uid, address, context=context)
+            return {'value': {'work_email': address.email, 'work_phone': address.phone}}
+        return {'value': {}}
+
+    def onchange_company(self, cr, uid, ids, company, context=None):
+        address_id = False
+        if company:
+            company_id = self.pool.get('res.company').browse(cr, uid, company, context=context)
+            address = self.pool.get('res.partner').address_get(cr, uid, [company_id.partner_id.id], ['default'])
+            address_id = address and address['default'] or False
+        return {'value': {'address_id' : address_id}}
+
+    def onchange_user(self, cr, uid, ids, user_id, context=None):
+        work_email = False
+        if user_id:
+            work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).user_email
+        return {'value': {'work_email' : work_email}}
+
+    def _get_photo(self, cr, uid, context=None):
+        photo_path = addons.get_module_resource('hr','images','photo.png')
+        return open(photo_path, 'rb').read().encode('base64')
+
     _defaults = {
-        'active' : lambda *a: True,
+        'active': 1,
+        'photo': _get_photo,
     }
-    
-    def _check_recursion(self, cr, uid, ids):
+
+    def _check_recursion(self, cr, uid, ids, context=None):
         level = 100
         while len(ids):
-            cr.execute('select distinct parent_id from hr_employee where id in ('+','.join(map(str, ids))+')')
+            cr.execute('SELECT DISTINCT parent_id FROM hr_employee WHERE id IN %s AND parent_id!=id',(tuple(ids),))
             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
             if not level:
                 return False
             level -= 1
         return True
-    
+
+    def _check_department_id(self, cr, uid, ids, context=None):
+        for emp in self.browse(cr, uid, ids, context=context):
+            if emp.department_id.manager_id and emp.id == emp.department_id.manager_id.id:
+                return False
+        return True
+
     _constraints = [
-        (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id'])
+        (_check_recursion, 'Error ! You cannot create recursive Hierarchy of Employees.', ['parent_id']),
+        (_check_department_id, 'Error ! You cannot select a department for which the employee is the manager.', ['department_id']),
     ]
-    
+
 hr_employee()
 
-class hr_timesheet(osv.osv):
-    _name = "hr.timesheet"
-    _description = "Timesheet Line"
+class hr_department(osv.osv):
+    _description = "Department"
+    _inherit = 'hr.department'
     _columns = {
-        'name' : fields.char("Name", size=64, required=True),
-        'dayofweek': fields.selection([('0','Monday'),('1','Tuesday'),('2','Wednesday'),('3','Thursday'),('4','Friday'),('5','Saturday'),('6','Sunday')], 'Day of week'),
-        'date_from' : fields.date('Starting date'),
-        'hour_from' : fields.float('Work from', size=8, required=True),
-        'hour_to' : fields.float("Work to", size=8, required=True),
-        'tgroup_id' : fields.many2one("hr.timesheet.group", "Employee's timesheet group", select=True),
+        'manager_id': fields.many2one('hr.employee', 'Manager'),
+        'member_ids': fields.one2many('hr.employee', 'department_id', 'Members', readonly=True),
     }
-    _order = 'dayofweek, hour_from'
-hr_timesheet()
 
+hr_department()
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: