def leaveApplications():

    #queryApprovals = db.session.query(Cadre,Departments)\
    #   .join(Departments, Departments.department_id==Cadre.department_id)
    result = []

    #get get leave applications escaleted to you though you are not the immediate supervisor

    for approval,leave,emp, ap in db.session.query(LeaveApproval,LeaveTypes,EmployeesRegistration,LeaveApplications)\
            .filter(LeaveApplications.leave_id==LeaveTypes.leave_id)\
            .filter(EmployeesRegistration.payroll_no==LeaveApplications.payroll_no)\
            .filter(LeaveApproval.application_id == LeaveApplications.application_id) \
            .filter(LeaveApproval.next_person_to_approve == getUserId()) \
            .filter(LeaveApplications.approved == 0)\
            .filter(LeaveApproval.aproval_status == 0).all():
        data = {}
        data['application_id'] = ap.application_id
        data['payroll_no'] = ap.payroll_no
        data['leave_type'] = leave.leave_name
        data['date_from'] = ap.date_from.strftime("%Y-%m-%d")
        data['date_to'] = ap.date_to.strftime("%Y-%m-%d")
        data['approved'] = ap.approved
        data['leave_note'] = ap.application_note
        data['surname'] = emp.surname
        data['other_names'] = emp.other_names

        result.append(data)

    # Normal leave application criteria
    for ap,leave,emp,auth in db.session.query(LeaveApplications,LeaveTypes,EmployeesRegistration,LeaveAuthorization)\
            .filter(LeaveApplications.leave_id==LeaveTypes.leave_id)\
            .filter(EmployeesRegistration.payroll_no==LeaveApplications.payroll_no)\
            .filter(LeaveAuthorization.leave_application_id == LeaveApplications.application_id)\
            .filter(LeaveAuthorization.supervisor_payroll_no == getUserId())\
            .filter(LeaveApplications.approved == 0).all():

        data = {}
        data['application_id'] = ap.application_id
        data['payroll_no'] = ap.payroll_no
        data['leave_type'] = leave.leave_name
        data['date_from'] = ap.date_from.strftime("%Y-%m-%d")
        data['date_to'] = ap.date_to.strftime("%Y-%m-%d")
        data['approved'] = ap.approved
        data['leave_note'] = ap.application_note
        data['surname'] = emp.surname
        data['other_names'] = emp.other_names

        result.append(data)

    leave = result

    return jsonify({'result': result})
def applicationHistoryStatus():

    daysEarned = getAccumulatedLeavedays(getUserId())
    remaining = 0

    result = []
    #.filter(LeaveTypes.leave_id == 1)
    for leave in LeaveTypes.query.all():
        data = {}

        data['leave_type'] = leave.leave_name
        data['leave_days'] = leave.no_days
        mydays = 0
        if leave.leave_id == 1:
            data['daysEarned'] = daysEarned
            mydays = daysEarned
        else:
            mydays = leave.no_days
            data['daysEarned'] = mydays

        remaining = loopApplications(leave.leave_id)
        print("DAYS RETURNED ", remaining)
        percentage = round(remaining / mydays * 100)
        data['per_remaining'] = percentage
        print("% REm ", percentage)
        diff = mydays - remaining
        data['leave_balance'] = diff
        print("% REm ***********************", remaining, " ", leave.leave_id)
        data['days'] = ""

        result.append(data)

    leave = result

    return jsonify({'result': result})
def getAllLeaveApplications():

    #get being supervised by this user
    result = []

    for myEmployees in db.session.query(StaffSupervisors)\
        .filter(StaffSupervisors.supervisor_payroll_no == getUserId()).all():
        if myEmployees is not None:


            for ap, emp ,leave in db.session.query(LeaveApplications, EmployeesRegistration,LeaveTypes)\
                    .filter(LeaveApplications.payroll_no == EmployeesRegistration.payroll_no)\
                    .filter(LeaveTypes.leave_id==LeaveApplications.leave_id)\
                    .filter(EmployeesRegistration.payroll_no == myEmployees.payroll_no).all():
                data = {}
                data['application_id'] = ap.application_id
                data['payroll_no'] = ap.payroll_no
                data['leave_type'] = leave.leave_name
                data['date_from'] = ap.date_from.strftime("%Y-%m-%d")
                data['date_to'] = ap.date_to.strftime("%Y-%m-%d")
                data['approved'] = ap.approved
                data['leave_status'] = ap.leave_status
                data['leave_note'] = ap.application_note
                data['surname'] = emp.surname
                data['other_names'] = emp.other_names
                data['application_note'] = ap.application_note

                result.append(data)
                print(result)

    leave = result

    return jsonify({'result': result})
def leaveApplicationsHistory():

    result = []

    for ap, emp ,leave in db.session.query(LeaveApplications, EmployeesRegistration,LeaveTypes)\
            .filter(LeaveApplications.payroll_no == EmployeesRegistration.payroll_no)\
            .filter(LeaveApplications.leave_id==LeaveTypes.leave_id)\
            .filter(LeaveApplications.payroll_no == getUserId()).all():
        data = {}
        data['application_id'] = ap.application_id
        data['payroll_no'] = ap.payroll_no
        data['leave_type'] = leave.leave_name
        data['date_from'] = ap.date_from.strftime("%Y-%m-%d")
        data['date_to'] = ap.date_to.strftime("%Y-%m-%d")
        data['approved'] = ap.approved
        data['leave_status'] = ap.leave_status
        data['leave_note'] = ap.application_note
        data['surname'] = emp.surname
        data['other_names'] = emp.other_names

        result.append(data)

    leave = result

    return jsonify({'result': result})
def getAll():
    print("TEST SESS AT DEP")
    #testSession()
    mysession = getUserId()
    print('AT DEP%%%%%%%%%%%%% ', mysession)

    fetchDepatments = db.session.query(Departments).all()
    department = deptArray(fetchDepatments)

    return jsonify({'status': 'success', 'result': department})
def loopApplications(leaveid):
    result = []
    remaining_days = 0
    #print("Loop up11 ", getUserId())
    queryApp = LeaveApplications.query\
        .filter(LeaveApplications.payroll_no == getUserId())\
        .filter(LeaveApplications.leave_id == leaveid).all()
    #.filter(LeaveApplications.approved==1).all()

    if queryApp is not None:
        data = {}
        for days in queryApp:
            date_from = days.date_from
            date_to = days.date_to
            days_applied = abs(date_to - date_from).days
            result.append(days_applied)

        return sum(result)
    else:
        return 0
def get_no_days(id):
    initial_balance = 0

    # Get the leave type allwable days
    query = db.session.query(LeaveTypes).filter(
        LeaveTypes.leave_id == id).first()
    leave_days = 0

    loggedInUser = getUserId()

    accruedLeaveDays = 0
    # get initial balance only for annual leave type

    print("THE INITIAL ::", query.leave_id)

    if (query.leave_id == 1):

        # get accumulated leave days
        accruedLeaveDays = getAccumulatedLeavedays(loggedInUser)
        print("ACCUMULATED DAYS ", accruedLeaveDays)

        initial_balance = initialLeaveBalance(loggedInUser)
        # if leave id is 1 use accrued leave days
        leave_days = accruedLeaveDays

    else:
        initial_balance = 0
        # else get the defined leave day from database
        leave_days = query.no_days

    # Get start and end date

    queryApproved = db.session.query(LeaveApplications) \
        .filter(LeaveApplications.leave_id == query.leave_id) \
        .filter(LeaveApplications.payroll_no == loggedInUser).all()
    if queryApproved:
        data = getApplication(queryApproved, leave_days)

        print("Leave Days ::*********2 ", leave_days)

        return jsonify({
            'status': 'success',
            'usedLeaveDays': data,
            'initial_balance': initial_balance,
            'accruedLeaveDays': leave_days,
            'remaining': leave_days - data
        })
    else:
        print("Leave Days ::********* 3", leave_days)
        return jsonify({
            'status':
            'success',
            'remaining':
            leave_days - initial_balance,
            'initial_balance':
            initial_balance,
            'accruedLeaveDays':
            leave_days,
            'usedLeaveDays':
            leave_days - (leave_days - initial_balance)
        })
Ejemplo n.º 8
0
def addLeaveApplication():
    response_object = {}
    #try:

    #the person requeting to be relieved

    userPayroll = getUserId()

    post_data = request.get_json()
    payroll_no = post_data.get('payroll_no')
    leave_id = post_data.get('leave_id')

    print("LEAVE APPLICATIONS AT POST DATA ", post_data)

    employees = LeaveApplications(leave_id=post_data.get('leave_id'),
                                  payroll_no=payroll_no,
                                  date_from=post_data.get('date_from'),
                                  application_note=post_data.get('note'),
                                  date_to=post_data.get('date_to'))

    db.session.add(employees)
    db.session.commit()

    # get leave id to be inserted into auth table
    applicationId = getLeaveApplicationId()

    #add the person to relieve you while away
    addReliever(post_data, userPayroll)

    #Notify the reliever

    # get supervisor payroll number
    supervisorid = getSupervisor(payroll_no)

    #leave_application_id =db.session.flush()
    # db.session.commit()

    #insert record into authorization table

    authTable = LeaveAuthorization(supervisor_payroll_no=supervisorid,
                                   emp_payroll_no=payroll_no,
                                   leave_id=leave_id,
                                   leave_application_id=applicationId,
                                   auth_stage=0,
                                   note="AUTH NOTE")

    db.session.add(authTable)
    db.session.commit()

    response_object = {
        'status': 'success',
        'msg': 'Leave successfully applied and Email sent to your supervisor '
    }

    #except Exception as e:

    sendEmailNotification(supervisorid)
    #response_object = {'status': 'failed','msg':'Failed some unknown error occured'}
    #print(e)

    #finally:

    return jsonify(response_object)