[IMP] adapt rights and display for the jobs form view
[odoo/odoo.git] / addons / hr / hr.py
index 4a5529c..da8e16b 100644 (file)
 #
 ##############################################################################
 
-from osv import fields, osv
-
 import addons
+import io
+import logging
+from osv import fields, osv
+from PIL import Image
+import StringIO
 
 class hr_employee_category(osv.osv):
 
@@ -45,9 +48,10 @@ class hr_employee_category(osv.osv):
     _description = "Employee Category"
     _columns = {
         'name': fields.char("Category", size=64, required=True),
-        'complete_name': fields.function(_name_get_fnc, method=True, type="char", string='Name'),
+        'complete_name': fields.function(_name_get_fnc, 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')
+        'child_ids': fields.one2many('hr.employee.category', 'parent_id', 'Child Categories'),
+        'employee_ids': fields.many2many('hr.employee', 'employee_category_rel', 'category_id', 'emp_id', 'Employees'),
     }
 
     def _check_recursion(self, cr, uid, ids, context=None):
@@ -66,43 +70,47 @@ class hr_employee_category(osv.osv):
 
 hr_employee_category()
 
-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 [])
+            nb_employees = len(job.employee_ids or [])
+            res[job.id] = {
+                'no_of_employee': nb_employees,
+                'expected_employees': nb_employees + job.no_of_recruitment,
+            }
         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
+    def _get_job_position(self, cr, uid, ids, context=None):
+        res = []
+        for employee in self.pool.get('hr.employee').browse(cr, uid, ids, context=context):
+            if employee.job_id:
+                res.append(employee.job_id.id)
         return res
 
     _name = "hr.job"
     _description = "Job Description"
     _columns = {
         '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'),
+        'expected_employees': fields.function(_no_of_employee, string='Expected Employees', help='Required number of employees in total for that job.',
+            store = {
+                'hr.job': (lambda self,cr,uid,ids,c=None: ids, ['no_of_recruitment'], 10),
+                'hr.employee': (_get_job_position, ['job_id'], 10),
+            },
+            multi='no_of_employee'),
+        'no_of_employee': fields.function(_no_of_employee, string="Number of Employees", help='Number of employees with that job.',
+            store = {
+                'hr.employee': (_get_job_position, ['job_id'], 10),
+            },
+            multi='no_of_employee'),
+        'no_of_recruitment': fields.float('Expected in Recruitment'),
+        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees', groups='base.group_user'),
         'description': fields.text('Job Description'),
         'requirements': fields.text('Requirements'),
         'department_id': fields.many2one('hr.department', 'Department'),
         'company_id': fields.many2one('res.company', 'Company'),
-        'state': fields.selection([('open', 'In Position'),('old', 'Old'),('recruit', 'In Recruitement')], 'State', readonly=True, required=True),
+        'state': fields.selection([('open', 'In Position'),('recruit', 'In Recruitement'),('old', 'Old')], 'Status', readonly=True, required=True),
     }
     _defaults = {
         'expected_employees': 1,
@@ -110,24 +118,28 @@ class hr_job(osv.osv):
         'state': 'open',
     }
 
-    def on_change_expected_employee(self, cr, uid, ids, expected_employee, no_of_employee, context=None):
+    _sql_constraints = [
+        ('name_company_uniq', 'unique(name, company_id)', 'The name of the job position must be unique per company!'),
+    ]
+
+
+    def on_change_expected_employee(self, cr, uid, ids, no_of_recruitment, 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}
+        return {'value': {'expected_employees': no_of_recruitment + no_of_employee}}
 
     def job_old(self, cr, uid, ids, *args):
-        self.write(cr, uid, ids, {'state': 'old'})
+        self.write(cr, uid, ids, {'state': 'old', 'no_of_recruitment': 0})
         return True
 
     def job_recruitement(self, cr, uid, ids, *args):
-        self.write(cr, uid, ids, {'state': 'recruit'})
+        for job in self.browse(cr, uid, ids):
+            no_of_recruitment = job.no_of_recruitment == 0 and 1 or job.no_of_recruitment
+            self.write(cr, uid, [job.id], {'state': 'recruit', 'no_of_recruitment': no_of_recruitment})
         return True
 
     def job_open(self, cr, uid, ids, *args):
-        self.write(cr, uid, ids, {'state': 'open'})
+        self.write(cr, uid, ids, {'state': 'open', 'no_of_recruitment': 0})
         return True
 
 hr_job()
@@ -136,38 +148,84 @@ class hr_employee(osv.osv):
     _name = "hr.employee"
     _description = "Employee"
     _inherits = {'resource.resource': "resource_id"}
+
+    def onchange_photo(self, cr, uid, ids, value, context=None):
+        if not value:
+            return {'value': {'photo_big': value, 'photo': value} }
+        return {'value': {'photo_big': self._photo_resize(cr, uid, value, 540, 450, context=context), 'photo': self._photo_resize(cr, uid, value, context=context)} }
+    
+    def _set_photo(self, cr, uid, id, name, value, args, context=None):
+        if not value:
+            vals = {'photo_big': value}
+        else:
+            vals = {'photo_big': self._photo_resize(cr, uid, value, 540, 450, context=context)}
+        return self.write(cr, uid, [id], vals, context=context)
+    
+    def _photo_resize(self, cr, uid, photo, heigth=180, width=150, context=None):
+        image_stream = io.BytesIO(photo.decode('base64'))
+        img = Image.open(image_stream)
+        img.thumbnail((heigth, width), Image.ANTIALIAS)
+        img_stream = StringIO.StringIO()
+        img.save(img_stream, "JPEG")
+        return img_stream.getvalue().encode('base64')
+    
+    def _get_photo(self, cr, uid, ids, name, args, context=None):
+        result = dict.fromkeys(ids, False)
+        for hr_empl in self.browse(cr, uid, ids, context=context):
+            if hr_empl.photo_big:
+                result[hr_empl.id] = self._photo_resize(cr, uid, hr_empl.photo_big, context=context)
+        return result
+    
     _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),
+        'otherid': fields.char('Other Id', size=64),
         'gender': fields.selection([('male', 'Male'),('female', 'Female')], 'Gender'),
-        'marital': fields.many2one('hr.employee.marital.status', 'Marital Status'),
+        'marital': fields.selection([('single', 'Single'), ('married', 'Married'), ('widower', 'Widower'), ('divorced', 'Divorced')], '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'),
-        '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', domain="[('partner_id','=',partner_id)]", help="Employee bank salary account"),
+        'address_id': fields.many2one('res.partner', 'Working Address'),
+        'address_home_id': fields.many2one('res.partner', 'Home Address'),
+        'bank_account_id':fields.many2one('res.partner.bank', 'Bank Account Number', domain="[('partner_id','=',address_home_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),
+        'mobile_phone': fields.char('Work 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.related('department_id', 'manager_id', relation='hr.employee', string='Manager', type='many2one', store=True, select=True, help="It is linked with manager of Department"),
-        'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel','category_id','emp_id','Category'),
+        'parent_id': fields.many2one('hr.employee', 'Manager'),
+        'category_ids': fields.many2many('hr.employee.category', 'employee_category_rel', 'emp_id', 'category_id', 'Categories'),
         '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)
+        'photo_big': fields.binary('Big-sized employee photo', help="This field holds the photo of the employee. The photo field is used as an interface to access this field. The image is base64 encoded, and PIL-supported. Full-sized photo are however resized to 540x450 px."),
+        'photo': fields.function(_get_photo, fnct_inv=_set_photo, string='Employee photo', type="binary",
+            store = {
+                'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['photo_big'], 10),
+            }, help="Image used as photo for the employee. It is automatically resized as a 180x150 px image. A larger photo is stored inside the photo_big field."),
+        'passport_id':fields.char('Passport No', size=64),
+        'color': fields.integer('Color Index'),
+        'city': fields.related('address_id', 'city', type='char', string='City'),
+        'login': fields.related('user_id', 'login', type='char', string='Login', readonly=1),
     }
 
+    def unlink(self, cr, uid, ids, context=None):
+        resource_obj = self.pool.get('resource.resource')
+        resource_ids = []
+        for employee in self.browse(cr, uid, ids, context=context):
+            resource = employee.resource_id
+            if resource:
+                resource_ids.append(resource.id)
+        if resource_ids:
+            resource_obj.unlink(cr, uid, resource_ids, context=context)
+        return super(hr_employee, self).unlink(cr, uid, ids, context=context)
+
     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}}
+            address = self.pool.get('res.partner').browse(cr, uid, address, context=context)
+            return {'value': {'work_email': address.email, 'work_phone': address.phone, 'mobile_phone': address.mobile}}
         return {'value': {}}
 
     def onchange_company(self, cr, uid, ids, company, context=None):
@@ -178,6 +236,13 @@ class hr_employee(osv.osv):
             address_id = address and address['default'] or False
         return {'value': {'address_id' : address_id}}
 
+    def onchange_department_id(self, cr, uid, ids, department_id, context=None):
+        value = {'parent_id': False}
+        if department_id:
+            department = self.pool.get('hr.department').browse(cr, uid, department_id)
+            value['parent_id'] = department.manager_id.id
+        return {'value': value}
+
     def onchange_user(self, cr, uid, ids, user_id, context=None):
         work_email = False
         if user_id:
@@ -186,12 +251,13 @@ class hr_employee(osv.osv):
 
     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')
+        return self._photo_resize(cr, uid, open(photo_path, 'rb').read().encode('base64'), context=context)
 
     _defaults = {
         'active': 1,
         'photo': _get_photo,
-        'address_id': lambda self,cr,uid,c: self.pool.get('res.partner.address').browse(cr, uid, uid, c).partner_id.id
+        'marital': 'single',
+        'color': 0,
     }
 
     def _check_recursion(self, cr, uid, ids, context=None):
@@ -204,15 +270,8 @@ class hr_employee(osv.osv):
             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_department_id, 'Error ! You cannot select a department for which the employee is the manager.', ['department_id']),
     ]
 
 hr_employee()
@@ -227,4 +286,29 @@ class hr_department(osv.osv):
 
 hr_department()
 
+
+class res_users(osv.osv):
+    _name = 'res.users'
+    _inherit = 'res.users'
+
+    def create(self, cr, uid, data, context=None):
+        user_id = super(res_users, self).create(cr, uid, data, context=context)
+
+        # add shortcut unless 'noshortcut' is True in context
+        if not(context and context.get('noshortcut', False)):
+            data_obj = self.pool.get('ir.model.data')
+            try:
+                data_id = data_obj._get_id(cr, uid, 'hr', 'ir_ui_view_sc_employee')
+                view_id  = data_obj.browse(cr, uid, data_id, context=context).res_id
+                self.pool.get('ir.ui.view_sc').copy(cr, uid, view_id, default = {
+                                            'user_id': user_id}, context=context)
+            except:
+                # Tolerate a missing shortcut. See product/product.py for similar code.
+                logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
+
+        return user_id
+
+res_users()
+
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: