[MERGE] lp:887553
[odoo/odoo.git] / addons / hr_timesheet_sheet / hr_timesheet_sheet.py
index ee41905..07e0d02 100644 (file)
@@ -35,6 +35,7 @@ class one2many_mod2(fields.one2many):
         if values is None:
             values = {}
 
+        # res6: {id: date_current, ...}
         res6 = dict([(rec['id'], rec['date_current'])
             for rec in obj.read(cr, user, ids, ['date_current'], context=context)])
 
@@ -77,10 +78,8 @@ class one2many_mod(fields.one2many):
         if values is None:
             values = {}
 
-        res5 = obj.read(cr, user, ids, ['date_current'], context=context)
-        res6 = {}
-        for r in res5:
-            res6[r['id']] = r['date_current']
+        res6 = dict([(rec['id'], rec['date_current'])
+            for rec in obj.read(cr, user, ids, ['date_current'], context=context)])
 
         ids2 = []
         for id in ids:
@@ -196,13 +195,9 @@ class hr_timesheet_sheet(osv.osv):
             return total
 
         def timedelta_to_hours(delta):
-            hours = 0.0
-            seconds = float(delta.seconds)
+            hours = delta.days * 24 + float(delta.seconds) / 3600
             if delta.microseconds:
-                seconds += float(delta.microseconds) / 100000
-            hours += delta.days * 24
-            if seconds:
-                hours += seconds / 3600
+                hours += float(delta.microseconds) / 3600000000
             return hours
 
         res = {}
@@ -234,25 +229,6 @@ class hr_timesheet_sheet(osv.osv):
             res[id]['total_difference_day'] = timedelta_to_hours(total_difference_day)
         return res
 
-    def _state_attendance(self, cr, uid, ids, name, args, context=None):
-        emp_obj = self.pool.get('hr.employee')
-        result = {}
-        link_emp = {}
-        emp_ids = []
-
-        for sheet in self.browse(cr, uid, ids, context=context):
-            result[sheet.id] = 'none'
-            emp_ids2 = emp_obj.search(cr, uid,
-                    [('user_id', '=', sheet.user_id.id)], context=context)
-            if emp_ids2:
-                link_emp[emp_ids2[0]] = sheet.id
-                emp_ids.append(emp_ids2[0])
-        for emp in emp_obj.browse(cr, uid, emp_ids, context=context):
-            if emp.id in link_emp:
-                sheet_id = link_emp[emp.id]
-                result[sheet_id] = emp.state
-        return result
-
     def check_employee_attendance_state(self, cr, uid, sheet_id, context=None):
         ids_signin = self.pool.get('hr.attendance').search(cr,uid,[('sheet_id', '=', sheet_id),('action','=','sign_in')])
         ids_signout = self.pool.get('hr.attendance').search(cr,uid,[('sheet_id', '=', sheet_id),('action','=','sign_out')])
@@ -337,23 +313,31 @@ class hr_timesheet_sheet(osv.osv):
                 self.write(cr, uid, [sheet.id], {'date_current': sheet.date_to,}, context=context)
         return True
 
-    def sign(self, cr, uid, ids, typ, context=None):
-        emp_obj = self.pool.get('hr.employee')
+    def check_sign(self, cr, uid, ids, typ, context=None):
         sheet = self.browse(cr, uid, ids, context=context)[0]
-        if context is None:
-            context = {}
         if not sheet.date_current == time.strftime('%Y-%m-%d'):
             raise osv.except_osv(_('Error !'), _('You cannot sign in/sign out from an other date than today'))
-        emp_id = sheet.employee_id.id
-        context['sheet_id']=ids[0]
-        emp_obj.attendance_action_change(cr, uid, [emp_id], type=typ, context=context,)
         return True
 
+    def sign(self, cr, uid, ids, typ, context=None):
+        self.check_sign(cr, uid, ids, typ, context=context)
+        sign_obj = self.pool.get('hr.sign.in.out')
+        sheet = self.browse(cr, uid, ids, context=context)[0]
+        context['emp_id'] = [sheet.employee_id.id]
+        sign_id = sign_obj.create(cr, uid, {}, context=context)
+        methods = {'sign_in': sign_obj.si_check,
+                   'sign_out': sign_obj.so_check}
+        wizard_result = methods[typ](cr, uid, [sign_id], context=context)
+        if wizard_result.get('type', False) == 'ir.actions.act_window_close':
+            return True  # ensure we do not close the main window !
+        wizard_result['nodestroy'] = True  # do not destroy the main window !
+        return wizard_result
+
     def sign_in(self, cr, uid, ids, context=None):
-        return self.sign(cr,uid,ids,'sign_in',context=None)
+        return self.sign(cr, uid, ids, 'sign_in', context=context)
 
     def sign_out(self, cr, uid, ids, context=None):
-        return self.sign(cr,uid,ids,'sign_out',context=None)
+        return self.sign(cr, uid, ids, 'sign_out', context=context)
 
     _columns = {
         'name': fields.char('Note', size=64, select=1,
@@ -378,7 +362,7 @@ class hr_timesheet_sheet(osv.osv):
             help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed timesheet. \
                 \n* The \'Confirmed\' state is used for to confirm the timesheet by user. \
                 \n* The \'Done\' state is used when users timesheet is accepted by his/her senior.'),
-        'state_attendance' : fields.function(_state_attendance, type='selection', selection=[('absent', 'Absent'), ('present', 'Present'),('none','No employee defined')], string='Current Status'),
+        'state_attendance' : fields.related('employee_id', 'state', type='selection', selection=[('absent', 'Absent'), ('present', 'Present')], string='Current Status', readonly=True),
         'total_attendance_day': fields.function(_total, method=True, string='Total Attendance', multi="_total"),
         'total_timesheet_day': fields.function(_total, method=True, string='Total Timesheet', multi="_total"),
         'total_difference_day': fields.function(_total, method=True, string='Difference', multi="_total"),
@@ -413,7 +397,7 @@ class hr_timesheet_sheet(osv.osv):
             return time.strftime('%Y-12-31')
         return time.strftime('%Y-%m-%d')
 
-    def _default_employee(self,cr, uid, context=None):
+    def _default_employee(self, cr, uid, context=None):
         emp_ids = self.pool.get('hr.employee').search(cr, uid, [('user_id','=',uid)], context=context)
         return emp_ids and emp_ids[0] or False
 
@@ -432,7 +416,7 @@ class hr_timesheet_sheet(osv.osv):
             if new_user_id:
                 cr.execute('SELECT id \
                     FROM hr_timesheet_sheet_sheet \
-                    WHERE (date_from < %s and %s < date_to) \
+                    WHERE (date_from <= %s and %s <= date_to) \
                         AND user_id=%s \
                         AND id <> %s',(sheet.date_to, sheet.date_from, new_user_id, sheet.id))
                 if cr.fetchall():
@@ -474,6 +458,12 @@ class hr_timesheet_sheet(osv.osv):
                 raise osv.except_osv(_('Invalid action !'), _('You cannot delete a timesheet which have attendance entries!'))
         return super(hr_timesheet_sheet, self).unlink(cr, uid, ids, context=context)
 
+    def onchange_employee_id(self, cr, uid, ids, employee_id, context=None):
+        department_id =  False
+        if employee_id:
+            department_id = self.pool.get('hr.employee').browse(cr, uid, employee_id, context=context).department_id.id
+        return {'value': {'department_id': department_id}}
+
 hr_timesheet_sheet()
 
 
@@ -489,14 +479,14 @@ class hr_timesheet_line(osv.osv):
 
     def _sheet(self, cursor, user, ids, name, args, context=None):
         sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
-        res = {}.fromkeys(ids, False)
+        res = dict.fromkeys(ids, False)
         for ts_line in self.browse(cursor, user, ids, context=context):
             sheet_ids = sheet_obj.search(cursor, user,
             [('date_to', '>=', ts_line.date),
             ('date_from', '<=', ts_line.date),
             ('employee_id.user_id', '=', ts_line.user_id.id)], context=context)
             if sheet_ids:
-            # [0] because only one sheet possible for an employee between 2 dates
+                # [0] because only one sheet possible for an employee between 2 dates
                 res[ts_line.id] = sheet_obj.name_get(cursor, user, sheet_ids, context=context)[0]
         return res
 
@@ -592,7 +582,7 @@ class hr_attendance(osv.osv):
 
     def _sheet(self, cursor, user, ids, name, args, context=None):
         sheet_obj = self.pool.get('hr_timesheet_sheet.sheet')
-        res = {}.fromkeys(ids, False)
+        res = dict.fromkeys(ids, False)
         for attendance in self.browse(cursor, user, ids, context=context):
             date_to = datetime.strftime(datetime.strptime(attendance.name[0:10], '%Y-%m-%d'), '%Y-%m-%d %H:%M:%S')
             sheet_ids = sheet_obj.search(cursor, user,
@@ -779,7 +769,8 @@ class res_company(osv.osv):
     _inherit = 'res.company'
     _columns = {
         'timesheet_range': fields.selection(
-            [('day','Day'),('week','Week'),('month','Month'),('year','Year')], 'Timesheet range'),
+            [('day','Day'),('week','Week'),('month','Month')], 'Timesheet range',
+            help="Periodicity on which you validate your timesheets."),
         'timesheet_max_difference': fields.float('Timesheet allowed difference(Hours)',
             help="Allowed difference in hours between the sign in/out and the timesheet " \
                  "computation for one sheet. Set this to 0 if you do not want any control."),