Example #1
0
 def get_turn(self, cr, uid, ids, date, context=None):
     res = {}
     if isinstance(date, str):
         date = tu.dt(date)
     for cont in self.browse(cr, uid, ids):
         if (tu.d(cont.date_start) <= date and
             (cont.date_end == False or date <= tu.d(cont.date_end))):
                 ts = filter(lambda i:
                     i.dayofweek == str(date.weekday()) or i.dayofweek == '',
                                        cont.turn_id.timesheet_id)
                 if len(ts) == 1:
                     ddate = tu.datetime.combine(date.date(), tu.time(0))
                     res[cont.id] = (ddate +
                                     tu.timedelta(hours=ts[0].hour_from),
                                     ddate +
                                     tu.timedelta(hours=
                                     24 * (ts[0].hour_from >
                                           ts[0].hour_to) + ts[0].hour_to))
                 elif len(ts) > 1:
                     raise RuntimeError("More than one turn enabled at same time. See Timesheet line.")
                 else:
                     res[cont.id] = False
         else:
             res[cont.id] = False
     return res
Example #2
0
 def get_turn(self, cr, uid, ids, date, context=None):
     res = {}
     if isinstance(date, str):
         date = tu.dt(date)
     for cont in self.browse(cr, uid, ids):
         if (tu.d(cont.date_start) <= date and
             (cont.date_end == False or date <= tu.d(cont.date_end))):
             ts = filter(
                 lambda i: i.dayofweek == str(date.weekday()) or i.dayofweek
                 == '', cont.turn_id.timesheet_id)
             if len(ts) == 1:
                 ddate = tu.datetime.combine(date.date(), tu.time(0))
                 res[cont.id] = (
                     ddate + tu.timedelta(hours=ts[0].hour_from), ddate +
                     tu.timedelta(hours=24 *
                                  (ts[0].hour_from > ts[0].hour_to) +
                                  ts[0].hour_to))
             elif len(ts) > 1:
                 raise RuntimeError(
                     "More than one turn enabled at same time. See Timesheet line."
                 )
             else:
                 res[cont.id] = False
         else:
             res[cont.id] = False
     return res
Example #3
0
 def _date_overlaps(self, cr, uid, ids, date, context=None):
     res = {}
     for j in self.browse(cr, uid, ids):
         r = True
         r = r and tu.dt(j.turn_start) - tu.timedelta(hours=2) < date
         r = r and date < tu.dt(j.turn_end) + tu.timedelta(hours=2)
         res[j.id] = r
     return res
Example #4
0
    def _get_holidays(self, cr, uid, ids, field_name, arg, context=None):
        """
        Generate list of attendance associated to the this journal.
        """
        res = {}
	data = self.read(cr, uid, ids, ['employee_id', 'date', 'turn_start', 'turn_end'], context=context)
	for d in data:
            turn_start = d['turn_start']
            turn_end = d['turn_end']
            employee_id = d['employee_id'][0]
	    date = d['date']
	    _id = d['id']
            if turn_start:
                date_start, date_end = turn_start, turn_end
            else:
                date_start, date_end = date, tu.dt2s(tu.d(date) +
                                                         tu.timedelta(days=1))

            sql_req= """
            SELECT H.id AS func_id
            FROM hr_holidays as H
            WHERE
              (H.employee_id = %d OR H.employee_id is Null) AND
              (H.date_from, H.date_to) OVERLAPS
              ('%s'::timestamp, '%s'::timestamp)
            """ % (employee_id, date_start, date_end)
            cr.execute(sql_req)
            sql_res = cr.fetchall()

            if len(sql_res) > 0:
                res[_id] = map(lambda (i,): i, sql_res)
            else:
                res[_id] = []
        return res
Example #5
0
    def _get_attendance(self, cr, uid, ids, field_name, arg, context=None):
        """
        Generate list of attendance associated to the this journal.
        """
        res = {}
	data = self.read(cr, uid, ids, ['employee_id', 'turn_start', 'turn_end'], context=context)
	delta = tu.timedelta(hours=2)
	for d in data:
            turn_start = d['turn_start']
            turn_end = d['turn_end']
            employee_id = d['employee_id'][0]
	    _id = d['id']
            if turn_start:
                sql_req= """
                SELECT A.id
                FROM hr_attendance as A
                WHERE
                  A.employee_id = %d AND
                  ('%s'::timestamp, '%s'::timestamp)
                  OVERLAPS
                  (A.name, A.name)
                ORDER BY A.name asc""" % (
                      employee_id,
                      tu.dt(turn_start) - delta,
                      tu.dt(turn_end) + delta
                  )
                cr.execute(sql_req)
                sql_res = cr.fetchall()

                if len(sql_res) > 0:
                    res[_id] = map(lambda (i,): i, sql_res)
                else:
                    res[_id] = []
        return res
Example #6
0
 def _get_turn_start(self, cr, uid, ids, field_name, arg, context=None):
     res = {}
     conts = self._get_contract(cr, uid, ids, 'contract', None, context=context)
     items = conts.items()
     if len(items) == 0: return False
     att_ids, con_ids = tuple(map(list, zip(*items)))
     cons = self.pool.get('hr.contract').browse(cr, uid, con_ids)
     atts = self.pool.get('hr.attendance').browse(cr, uid, att_ids)
     for (att, cont) in zip(atts, cons):
         turns = [ (tu.dt(att.date) +
                    tu.timedelta(days = int(i.dayofweek) - tu.dt(att.date).weekday(), hours=i.hour_from),
                    tu.dt(att.date) +
                    tu.timedelta(days = int(i.dayofweek) - tu.dt(att.date).weekday(), hours=i.hour_to + 24*(i.hour_to < i.hour_from)))
                  for i in  cont.turn_id.timesheet_id ]
         turns = filter(lambda (f,t):
                          f - tu.timedelta(hours=6) <= tu.dt(att.datetime) and
                          tu.dt(att.datetime) <= t + tu.timedelta(hours=6), turns)
         if len(turns) > 0:
             res[att.id] = tu.dt2s(turns[0][0])
         else:
             res[att.id] = False
     return res
Example #7
0
    def _inv_attendance(self, cr, uid, ids, field_name, value, arg, context=None):
        """
        Store attendance from a journal. Assign employee id and set the correct
        datetime.
        """
        jou_id = ids
        for no_se, att_id, val in value:
            if val:
                # Who is the associated journal?
                if jou_id == 0:
                    if not isinstance(ids, int):
                        raise osv.except_osv(_('Error'),
                                             _('I expect process attendance just for one journal'))
                    jou_id = ids
                jou = self.browse(cr, uid, jou_id)

                # Check date if not out of turn time.
                if not jou._date_overlaps(tu.dt(val['name']))[jou.id]:
                    an = tu.datetime.combine(tu.d(jou.date).date(), tu.dt(val['name']).time())
                    if not jou._date_overlaps(an)[jou.id]:
                        an = tu.datetime.combine(tu.d(jou.date).date() + tu.timedelta(days=1), tu.dt(val['name']).time())
                    val['name'] = tu.dt2s(an)
                    if not (jou._date_overlaps(tu.dt(val['name']))):
                        raise osv.except_osv(_('Error'),
                                             _('Attendance don\'t belong to this journal'))

        # Sort entries
        att_ids = []
        values = []
        for no_se, att_id, val in value:
            if val:
                att_ids.append(att_id)
                values.append(val) 
        att_ids.sort()
        values.sort(key=lambda i: i['name'])
        attval = dict(zip(att_ids, values))

        # Store new or modification
        for att_id, val in zip(att_ids, values):
            if val:
                if att_id == 0:
                   val['employee_id'] = jou.employee_id.id
                   self.pool.get('hr.attendance').create(cr, uid, val)
                else:
                   self.pool.get('hr.attendance').write(cr, uid, att_id, val)

        # Delete entries
        for no_se, att_id, val in value:
            if not val:
                self.pool.get('hr.attendance').unlink(cr, uid, att_id)
Example #8
0
    def _get_turn_start(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for journal in self.browse(cr, uid, ids):
            if journal.turn_id:
		wd_l = [ str(tu.d(journal.date).weekday()), '' ]
                ts = filter(lambda i:
                                i.dayofweek in wd_l,
                                journal.turn_id.timesheet_id)
                if len(ts) == 1:
                    res[journal.id] = tu.dt2s(tu.d(journal.date) +
                                        tu.timedelta(hours=ts[0].hour_from))
                elif len(ts) > 1:
                    raise osv.except_osv(_('Error'), _('Overlaped turn for the journal %s.') % journal.name)
                else:
                    res[journal.id] = False
            else:
                res[journal.id] = False
        return res
Example #9
0
    def build(self, cr, uid, date_from, date_to, context=None):
        emp_pool = self.pool.get('hr.employee')
        con_pool = self.pool.get('hr.contract')
        journal_ids = []
        for time in tu.daterange(tu.dt(date_from), tu.dt(date_to) + tu.timedelta(days=1)):
            journal_date = tu.dt2s(time)
            emp_ids = emp_pool.search(cr, uid, [], context=context)
            con_ids = emp_pool.get_valid_contract(cr, uid, emp_ids, time)

            for (emp_id, con_id) in con_ids.items():
                emp = emp_pool.browse(cr, uid, emp_id, context=context)
                con = con_pool.browse(cr, uid, con_id, context=context)

                contract_hours = (
                    str(time.weekday()) in [ ts.dayofweek for ts in
                                                con.turn_id.timesheet_id ]
                    ) * con.working_hours_per_day

                journal = {
                    'name': emp.name + time.strftime(' (%d/%m/%Y)'),
                    'date': journal_date,
                    'employee_id': emp_id,
                    'turn_id': con.turn_id.id,
                    'department_id': con.department_id.id,
                    'contract_hours': contract_hours,
                }
                jids = self.search(cr, uid, [
                    ('employee_id', '=', journal['employee_id']),
                    ('date', '=', journal['date']),
                ])
                if len(jids) == 0:
                    journal_ids.append(self.create(cr, uid, journal, context=context))
                else:
                    logger.notifyChannel('hr.aa.journal',
                                 netsvc.LOG_INFO,
                                 'Yet exists journal \'%s\'' %
                                 journal['name'])

        self.compute(cr, uid, journal_ids, context=context)
        return len(journal_ids)