function DisplayApproveAbsenceRequestsTableBody($userID)
{
    $filter[APPR_ABS_EMPLOYEE_ID] = $userID;
    $bookings = RetrieveApprovedAbsenceBookings($filter);
    if ($bookings != NULL) {
        foreach ($bookings as $booking) {
            $absenceTypeID = $booking[APPR_ABS_ABS_TYPE_ID];
            $absenceType = RetrieveAbsenceTypeByID($absenceTypeID);
            echo '<tr>';
            echo '<td>' . $booking[APPR_ABS_START_DATE] . '</td>';
            echo '<td>' . $booking[APPR_ABS_END_DATE] . '</td>';
            echo '<td>' . $absenceType[ABS_TYPE_NAME] . '</td>';
            echo '<td> <button class="btn btn-danger" type="submit" ' . 'name="deleteApproved"  value="' . $booking[APPR_ABS_BOOKING_ID] . '">Delete</button></td>';
            echo '</tr>';
        }
    }
}
function DisplayApproveAbsenceTableBody()
{
    $bookings = RetrieveApprovedAbsenceBookings();
    if ($bookings != NULL) {
        foreach ($bookings as $booking) {
            $employeeID = $booking[APPR_ABS_EMPLOYEE_ID];
            $employee = RetrieveEmployeeByID($employeeID);
            $absenceTypeID = $booking[APPR_ABS_ABS_TYPE_ID];
            $absenceType = RetrieveAbsenceTypeByID($absenceTypeID);
            echo "<tr>";
            echo "<td>" . $employee[EMP_NAME] . "</td>";
            echo "<td>" . $booking[APPR_ABS_START_DATE] . "</td>";
            echo "<td>" . $booking[APPR_ABS_END_DATE] . "</td>";
            echo "<td>" . $absenceType[ABS_TYPE_NAME] . "</td>";
            echo '<td> <button class="btn btn-success" type="submit" name="amend"' . 'value="' . $booking[APPR_ABS_BOOKING_ID] . '">Amend</button></td>';
            echo '<td> <button class="btn btn-danger" type="submit" name="delete"' . 'value="' . $booking[APPR_ABS_BOOKING_ID] . '">Delete</button></td>';
            echo "</tr>";
        }
    }
}
function DisplaySearchTableBody($startDate, $endDate)
{
    date_default_timezone_set('UTC');
    $startDate = $_POST["startDate"];
    $startDateTime = strtotime($startDate);
    $endDate = $endDateTime = strtotime($endDate);
    $bookings = RetrieveApprovedAbsenceBookings();
    if ($bookings != NULL) {
        foreach ($bookings as $booking) {
            $bookingStartTime = strtotime($booking[APPR_ABS_START_DATE]);
            $bookingEndTime = strtotime($booking[APPR_ABS_START_DATE]);
            if ($bookingStartTime >= $startDateTime and $bookingEndTime <= $endDateTime) {
                $employee = RetrieveEmployeeByID($booking[APPR_ABS_EMPLOYEE_ID]);
                $absenceType = RetrieveAbsenceTypeByID($booking[APPR_ABS_ABS_TYPE_ID]);
                echo '<tr>';
                echo '<td>' . $employee[EMP_NAME] . '</td>';
                echo '<td>' . $booking[APPR_ABS_START_DATE] . '</td>';
                echo '<td>' . $booking[APPR_ABS_END_DATE] . '</td>';
                echo '<td>' . $absenceType[ABS_TYPE_NAME] . '</td>';
                echo '</tr>';
            }
        }
    }
}
function CalculateRemainingAnnualLeave($employeeID)
{
    //Assume no leave is remaining. Will increment this in the function.
    $annualLeaveRemaining = 0;
    $employee = RetrieveEmployeeByID($employeeID);
    if ($employee != NULL) {
        //Start with the annual leave entitlement for the employee.
        $annualLeaveRemaining = $employee[EMP_LEAVE_ENTITLEMENT];
        //Get all of the absence bookings for this employee.
        $filter[APPR_ABS_EMPLOYEE_ID] = $employeeID;
        $bookings = RetrieveApprovedAbsenceBookings($filter);
        //Q. Does employee have any absence bookings?
        if ($bookings) {
            //Yes. For each booking.....
            foreach ($bookings as $booking) {
                //Calculate how much leave is needed for the booking.
                $startDate = $booking[APPR_ABS_START_DATE];
                $endDate = $booking[APPR_ABS_END_DATE];
                $absenceType = $booking[APPR_ABS_ABS_TYPE_ID];
                $leaveRequired = CalculateAnnualLeaveRequired($startDate, $endDate, $absenceType);
                //subtract this from the annual leave entitlement.
                $annualLeaveRemaining = $annualLeaveRemaining - $leaveRequired;
            }
        }
    } else {
        error_log("Unknown employee identifier of {$employeeID}");
    }
    return $annualLeaveRemaining;
}
function DeleteEmployee($ID)
{
    $result = 0;
    $statusMessage = "";
    $employee = RetrieveEmployeeByID($ID);
    if ($employee != NULL) {
        if ($employee[EMP_MAIN_VACATION_REQ_ID] != NULL) {
            DeleteMainVacatioNRequest($employee[EMP_MAIN_VACATION_REQ_ID]);
        }
        $filter[AD_HOC_EMP_ID] = $ID;
        $adHocAbsenceRequests = RetrieveAdHocAbsenceRequests($filter);
        foreach ((array) $adHocAbsenceRequests as $value) {
            DeleteAdHocAbsenceRequest($value[AD_HOC_REQ_ID]);
        }
        unset($filter);
        $filter[APPR_ABS_EMPLOYEE_ID] = $ID;
        $approvedAbsenceBookings = RetrieveApprovedAbsenceBookings($filter);
        if ($approvedAbsenceBookings != NULL) {
            foreach ($approvedAbsenceBookings as $value) {
                DeleteApprovedAbsenceBooking($value[APPR_ABS_BOOKING_ID]);
            }
        }
        $sql = "DELETE FROM employeeTable WHERE employeeID=" . $ID . ";";
        $result = performSQL($sql);
        $statusMessage .= "Record deleted.</br>";
        GenerateStatus(true, $statusMessage);
    }
    return $result;
}
function DeleteAbsenceType($ID)
{
    $isValidRequest = TRUE;
    // Ensure there are no AdHocAbsenceRequest records which reference this record.
    $filter[AD_HOC_ABSENCE_TYPE_ID] = $ID;
    $adHocAbsenceRequests = RetrieveAdHocAbsenceRequests($filter);
    if ($adHocAbsenceRequests != NULL) {
        error_log("Attempt to DeleteAbsenceType failed. " . "One or more adHocAbsenceRequest records exist with an " . "absence type ID of " . $ID);
        $isValidRequest = FALSE;
    }
    // Ensure there are no ApprovedAbsenceBooking records which reference this record.
    unset($filter);
    $filter[APPR_ABS_ABS_TYPE_ID] = $ID;
    $approvedBookings = RetrieveApprovedAbsenceBookings($filter);
    if ($approvedBookings != NULL) {
        error_log("Attempt to DeleteAbsenceType failed. " . "One or more approvedAbsenceBooking records exist with an " . "absence type ID of " . $ID);
        $isValidRequest = FALSE;
    }
    $result = 0;
    if ($isValidRequest) {
        $sql = "DELETE FROM absenceTypeTable WHERE absenceTypeID=" . $ID . ";";
        $result = performSQLDelete($sql);
        GenerateStatus(true, "Record has been deleted.");
    }
    return $result;
}