Exemplo n.º 1
0
def update_history_late_overtime_monthly():
    """ Runs every month 1st. Calculate last month's late & overtime hour

    :return:
    """
    check_month = (datetime.date.today() - datetime.timedelta(days=2)).strftime("%Y-%m")
    session = DB_Session()
    users = session.query(User).filter(
        User.is_admin == 0,
        User.is_test == 0,
    ).order_by(User.id).all()
    for user in users:
        if user.is_present == 0 and user.dismiss_time.strftime("%Y-%m") != check_month:
            continue
        attendance_list = session.query(Attendance).filter(
            Attendance.date.like("{0}%".format(check_month)),
            Attendance.userid == user.id
        ).all()
        late, overtime = get_late_overtime_total(attendance_list)
        history = HistoryLateOvertimeHour(
            userid=user.id,
            month=check_month,
            late=late,
            overtime=overtime
        )
        session.add(history)
    session.commit()
Exemplo n.º 2
0
def update_attendances_daily(check_date=None):
    """ Runs on 23:59 per day

    :param check_date: datetime object
    :return:
    """
    if not check_date:
        check_date = datetime.datetime.now()

    default_time = check_date.replace(hour=0, minute=0, second=0, microsecond=0)
    date = default_time.strftime("%Y-%m-%d")
    session = DB_Session()
    users = session.query(User).filter(
        User.is_present == 1,
        User.is_admin == 0,
        User.is_test == 0,
    ).order_by(User.id).all()
    # no_check_out_users = filter(lambda x: x.check_out is None, users)
    for user in users:
        today_attendance = filter(lambda x: x.date == date, user.attendance)
        if today_attendance:
            today_attendance = today_attendance[0]
            if today_attendance.check_out:
                continue
            else:
                today_attendance.check_out = default_time
        else:
            legal_hour, legal_minute = map(int, user.banci.split(":"))
            legal_check_in = default_time.replace(hour=legal_hour, minute=legal_minute)
            legal_check_out = legal_check_in + datetime.timedelta(hours=9)
            today_attendance = Attendance(
                userid=user.id,
                check_out=default_time,
                check_in=default_time,
                date=date,
                updatetime=datetime.datetime.now(),
                legal_check_in=legal_check_in,
                legal_check_out=legal_check_out
            )
            session.add(today_attendance)
        set_attendance_status(today_attendance)

    session.commit()