<?php
/**********************************************************************
***********************************************************************/

function can_process(&$data, &$leave_details, $single_record = false)
{
        $input_error = 0;
        
   	if (!isset($data['employee_code']) || $data['employee_code'] == "")
   	{
            $input_error = 1;
            $msg = ( _("There is no employee selected."));
            if($single_record) set_focus('employee_code');
            return $msg;
   	}
   	
        if (date1_greater_date2(sql2date($data['from_date']),sql2date($data['to_date'])))
   	{
            $input_error = 1;
            $msg = ( _("To Date can not be earlier than From Date."));
            if($single_record) set_focus('to_date');
            return $msg;
   	}
        
        if ($data['from_date'] == $data['to_date'])
   	{
            if ($data['leave_from_time_code'] == "A" && 
                    $data['leave_to_time_code'] == "M")
            {
                $input_error = 1;
                $msg = ( _("From leave time should be earlier than To leave time or select Fullday."));
                if($single_record) set_focus('from_date');
                return $msg;
            }
   	}
        
        $leave_details = calculate_leave($data['from_date'], $data['leave_from_time_code'],
                                                $data['to_date'],$data['leave_to_time_code'],
                                                $data['is_revoke'], $data['uninformed_leave_type']);
        
       
        $no_of_days_leave = 0.0;
        foreach($leave_details as $day_leave) 
        {
            $no_of_days_leave += $day_leave[1];
        }
        //return;
        if($no_of_days_leave == 0)
        {
            $input_error = 1;
            $msg = ( _("Applicable number of leave days are zero. You do not need to apply this leave"));
            if($single_record) set_focus('from_date');
            return $msg;
        }
        
        $data["no_of_days_leave"] = $no_of_days_leave;
        
        $ler_result = get_leave_entitlement($data['leave_entitlement_id']);

        if (date1_greater_date2(sql2date($ler_result["from_date"]),sql2date($data['from_date'])) || 
                (date1_greater_date2(sql2date($data['to_date']),sql2date($ler_result["to_date"]))))
        {
            $input_error = 1;
            $msg = ( _("Leave appling period does not fall within the period of selected leave entitlement record"));
            if($single_record) set_focus('from_date');
            return $msg;
        }

        
        $lr_result = get_leave_records($data['employee_code'],$data['is_revoke'],true);

        if (db_num_rows($lr_result) > 0)
        {
            while ($lr_row = db_fetch($lr_result))
            {
                $rec_from_date = sql2date($lr_row["from_date"]);
                $rec_from_time = $lr_row["from_time_code"];
                $rec_to_date = sql2date($lr_row["to_date"]);
                $rec_to_time = $lr_row["to_time_code"];
                $user_from_date = sql2date($data['from_date']);   
                $user_from_time = ($data['leave_from_time_code']);  
                $user_to_date = sql2date($data['to_date']);  
                $user_to_time = ($data['leave_to_time_code']); 
                        
                if (!(date1_greater_date2($user_from_date, $rec_to_date) || 
                (date1_greater_date2($rec_from_date, $user_to_date))))
                {
                    if($user_from_date == $rec_to_date &&
                            $user_from_time == 'A' && $rec_to_time == 'M')
                    {
                        // valid
                    }     
                    elseif($user_to_date == $rec_from_date &&
                            $user_to_time == 'M' && $rec_from_time == 'A')
                    {
                        // valid
                    }     
                    else
                    {
                        $input_error = 1;
                        $msg = ( _($data['from_date']." - There is an already applied leave overlapping with current applying leave"));
                        if($single_record) set_focus('from_date');
                        return $msg;
                    }
                }
            }
        }
        
        if($input_error == 0){
            return "";
        }
        
        return $msg;
        
}

//--------------------------------------------------------------------------------------------------

function get_day_leave_time($date, $leave_time, $is_revoke, $uninformed_leave_type = false)
{

    $leave_days = 0.0;
    
    if($uninformed_leave_type == true){
        if($leave_time == "F")
                $leave_days = 1.0;
        elseif($leave_time == "M")
                $leave_days = 0.5;
        elseif($leave_time == "A")
                $leave_days = 0.5;
        
        return $day_leave = array($date, $leave_days, $leave_time);
    }

    $wt_result = get_year_dates($date,$date);
    $wt_row = db_fetch($wt_result);
    $work_time = $wt_row['work_time_code'];  
    
    $ht_result = get_holiday_time($date);
    $ht_row = db_fetch($ht_result);
    $holiday_time = $ht_row['holiday_time_code'];
    
    if($work_time == "F"){
        if($holiday_time == "F")
                $effective_work_time = "N";
        elseif($holiday_time == "M")
                $effective_work_time = "A";
        elseif($holiday_time == "A")
                $effective_work_time = "M";
        else{
                $effective_work_time = "F";
        }      
    }
    elseif($work_time == "M"){
        if($holiday_time == "F")
                $effective_work_time = "N";
        elseif($holiday_time == "M")
                $effective_work_time = "N";
        elseif($holiday_time == "A")
                $effective_work_time = "M";
        else{
                $effective_work_time = "M";
        }      
    }
    elseif($work_time == "A"){
        if($holiday_time == "F")
                $effective_work_time = "N";
        elseif($holiday_time == "M")
                $effective_work_time = "A";
        elseif($holiday_time == "A")
                $effective_work_time = "N";
        else{
                $effective_work_time = "A";
        }      
    }
    else{
        $effective_work_time = "N";
    }
            
    if($effective_work_time == "F")
    {
            if($leave_time == "F")
                    $leave_days = 1.0;
            elseif($leave_time == "M")
                    $leave_days = 0.5;
            elseif($leave_time == "A")
                    $leave_days = 0.5;
    }
    elseif($effective_work_time == "M")
    {
            if($leave_time == "F")
                    $leave_days = 0.5;
            elseif($leave_time == "M")
                    $leave_days = 0.5;
            elseif($leave_time == "A")
                    $leave_days = 0.0;
    }
    elseif($effective_work_time == "A")
    {
            if($leave_time == "F")
                    $leave_days = 0.5;
            elseif($leave_time == "M")
                    $leave_days = 0.0;
            elseif($leave_time == "A")
                    $leave_days = 0.5;
    }
    elseif($effective_work_time == "N")
            $leave_days = 0.0;
    
    else
            $leave_days = 0.0;
    
    
    if($is_revoke == 1 && $leave_days != 0)
    {
        $leave_days = $leave_days * (-1);
    }
            
    return $day_leave = array($date, $leave_days, $leave_time);
    
}

//--------------------------------------------------------------------------------------------------


function get_next_date($date,$days = 1)
{
    $date = strtotime("+".$days." days", strtotime($date));
    return  date("Y-m-d", $date);
}

//--------------------------------------------------------------------------------------------------


function calculate_leave($start_date, $start_session, $end_date, $end_session, $is_revoke, $uninformed_leave_type)
{
    $leave_details = array();
    $no_of_days_leave = 0;
    
    if($start_date == $end_date)
    {
        if($start_session == "M" && $end_session == "A")
        {
            $start_session = "F";
            $end_session =  "F";
        }
        elseif($start_session == "M" && $end_session == "M")
        {
            // no change
        }
        if($start_session == "M" && $end_session == "F")
        {
            $start_session = "F";
            $end_session =  "F";
        }
        elseif($start_session == "A" && $end_session == "A")
        {
            // no change
        }
        elseif($start_session == "A" && $end_session == "M")
        {
            //$_SESSION["no_of_days_leave"] = 0;
            return 0;
        }
        if($start_session == "A" && $end_session == "F")
        {
            $start_session = "F";
            $end_session =  "F";
        }
        if($start_session == "F" && $end_session == "F")
        {
            // no change
        }
        if($start_session == "F" && $end_session == "M")
        {
            $start_session = "F";
            $end_session =  "F";
        }
        if($start_session == "F" && $end_session == "A")
        {
            $start_session = "F";
            $end_session =  "F";
        }
    }
    else 
    {
        if($start_session == "M")
        {
            $start_session = "F";
        }

        if($end_session == "A")
        {
            $end_session = "F";
        }
    }
    
    $temp_date = $start_date;
    $temp_leave_session = $start_session;


    while($end_date >= $temp_date)
    {
        if($temp_date == $end_date)
        {
            $temp_leave_session = $end_session;
        }

        if($temp_date != $start_date && $temp_date != $end_date)
        {
            $temp_leave_session = "F";
        }
        
        $day_leave = get_day_leave_time($temp_date, $temp_leave_session, $is_revoke, $uninformed_leave_type);
        $leave_details[] = $day_leave;


        $temp_date = get_next_date($temp_date);

    }
    
    return $leave_details;

}

//--------------------------------------------------------------------------------------------------


function add_leave($data, $Mode, $single_record = false)
{
        $leave_details = "";
        
        $can_process_msg = can_process($data, $leave_details, $single_record);

   	if($can_process_msg != ""){
            return array("passed" => false, "message" => $can_process_msg);
        }
        
     	if ($Mode == 'ADD_ITEM') 
       	{   
            
            $rec_id = add_leave_record($data['leave_entitlement_id'],$data['employee_code'], 
                                $data['from_date'], $data['leave_from_time_code'],
                                $data['to_date'] , $data['leave_to_time_code'],
                                $data['no_of_days_leave'], $data['comment'],"N" ,
                                $data['is_revoke'], $leave_details);
            
            $Mode = 'RESET';
            
            return array("passed" => true, "message" => $rec_id);

       	} 
        
        $Mode = 'RESET';
        return array("passed" => false, "message" => "Could not add leave record");
        
}

//--------------------------------------------------------------------------------------------------

function notify_leave_approver($leave_id)
{
        $email = get_module_parameter_value("payroll", "default_leave_approver_email");
        if(strlen($email)>0){
            $leave = get_leave_by_id($leave_id);
            if($leave){
                $employee_name = "[".$leave['employee_code']."] - ".$leave['first_name']." ".$leave['middle_name']." ".$leave['last_name'];
                $subject = "A leave request from $employee_name awaits your approval";
                $message = "Leave Type: ".$leave['leave_type_name']."\r\n";
                $message .= "Leave Entitlement: ".$leave['leave_entitlement']."\r\n";
                $message .= "Leave Used: ".$leave['leave_used']."\r\n";
                $message .= "From : ".$leave['from_date']." ".$leave['from_time_name']."\r\n";
                $message .= "To : ".$leave['to_date']." ".$leave['to_time_name']."\r\n";

                //mail ($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
                mail($email, $subject, $message);
            }
        }
}

//function get_previous_nopay_days($leave_entitlement_id, $employee_code){
//    $previous_nopay_days = 0;
//    
//    $result = get_entitlement_leave_records($leave_entitlement_id, $employee_code);
//    while ($row = db_fetch($result)) {
//        $previous_nopay_days += $row['nopay_days'];
//    }
//    
//    return $previous_nopay_days;
//}


function get_leave_entitlement_balances($leave_entitlement_id, $employee_code){
    $nopay_days = 0;
    $leave_applied = 0;
    $approved = 0;
    
    $result = get_entitlement_leave_records($leave_entitlement_id, $employee_code);
    while ($row = db_fetch($result)) {
        $nopay_days += $row['nopay_days'];
        if($row['status_code'] != 'J'){
            $leave_applied+= $row['leave_days'];
        }
        if(!($row['status_code'] == 'N' || $row['status_code'] == 'W' || $row['status_code'] == 'J')){
            $approved += $row['leave_days'];
        }
    }
    
    $leave_balances = array('applied' => $leave_applied, 'approved' => $approved, 'nopay_days' => $nopay_days);
    return $leave_balances;
}

?>