<?php
include("../../classes/manager.class.php");
$m = new Manager();
//print_r($_REQUEST);
if (isset($_REQUEST["leave_date"]) && !empty($_REQUEST["type"])) {
    $emp = EmployeeSetup::GetEmployeeSetupById($_REQUEST["employee_id"]);
    $empCat = EmployeeCategorySetup::GetEmployeeCategorySetupById($emp->category_id);
    //print_r($empCat);

    $lea = str_split($_REQUEST["leave_date"], 4);
    $leaveFilter = ' WHERE employee_id = "'.$emp->id.'" AND type = "'.$_REQUEST["type"].'" AND STR_TO_DATE(leave_date, "%Y") = "' . $lea[0] . '-00-00"';
    $Availleaves = LeaveSetup::GetLeaveSetupList($leaveFilter);
    $totalCountLeave = 0;
    if($Availleaves){
        foreach($Availleaves AS $AvailleavesRow){
            $totalCountLeave++;
        }
    }

    if($_REQUEST["type"] == 'CL' && $totalCountLeave <= $empCat->casual_leave){

    }else if($_REQUEST["type"] == 'AL' && $totalCountLeave <= $empCat->annual_leave) {

    }else{
        header("Location: " . $_REQUEST['url']."&availed");
        exit;
    }
    $data['leave_date'] = $_REQUEST["leave_date"];
    $data['employee_id'] = $_REQUEST["employee_id"];
    $data['type'] = $_REQUEST["type"];
    $data['description'] = htmlentities($_REQUEST["description"]);
    $data['create_date'] = "NOW()";
    LeaveSetup::AddLeaveSetup($data);
    header("Location: " . $_REQUEST['url']."&sucess");
}else{
    header("Location: " . $_REQUEST['url']."&error");
    exit;
}
exit();
?>