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


function get_unapproved_ot_records($start_date, $end_date, $employee_code, $project_id)
{
	$sql = "SELECT DISTINCT a.*, 
                        s.shift_name,
                        ots.value_name ot_status_name,
                        dfs.value_name deficit_status_name,
                        rt.value_name rate_table_name 
                FROM ".TB_PREF."attendance a,
                        ".TB_PREF."shifts s,
                        ".TB_PREF."list_values ots,
                        ".TB_PREF."list_values dfs,
                        ".TB_PREF."list_values rt,
                        ".TB_PREF."project_tasks pt     
                WHERE a.shift_id = s.id AND 
                        a.from_date_time1 >= ".db_escape($start_date)." AND 
                        a.from_date_time1 <= ".db_escape($end_date)." AND  
                        (a.ot_status_code IN ('W', 'O') OR a.deficit_status_code IN ('W', 'O')) AND                         
                        a.ot_status_code = ots.value_code AND ots.list_code = 'ATTENDANCE_STATUS'  AND              
                        a.deficit_status_code = dfs.value_code AND dfs.list_code = 'ATTENDANCE_STATUS'  AND              
                        a.rate_table_id = rt.id";
        
        if(strlen($employee_code) > 0){
            $sql .= " AND a.employee_code = ".db_escape($employee_code); 
        }
        
        if($project_id > 0){
            $sql .= " AND a.task_id = pt.task_id 
                AND pt.project_id = ".db_escape($project_id);  
        }
        
        $sql .= " ORDER BY a.from_date_time1";
	
	$result = db_query($sql,"Could not retrieve unapproved ot records");	
        
        return $result;		
}

function approve_attendance($rec_id, $granted_ot_time,
                                $rate_table_id, $deducted_deficit_time, $status_code)
{
	                
        $approved_by = $_SESSION['wa_current_user']->user;
        $approved_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."attendance 
                SET granted_ot_time = ".db_escape(time2sql($granted_ot_time)).", 
                    rate_table_id = ".db_escape($rate_table_id).", 
                    ot_status_code = ".db_escape($status_code).",  
                    deducted_deficit_time = ".db_escape(time2sql($granted_ot_time)).", 
                    deficit_status_code = ".db_escape($status_code).", 
                    approved_by = ".db_escape($approved_by).",
                    approved_on = ".db_escape($approved_on)."
                WHERE id=".db_escape($rec_id);

	db_query($sql, "Attendance records could not be updated");

}


?>