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

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

function add_attendance_file($description,
	$filename, $unique_name, $filesize, $filetype)
{
	$date = date2sql(Today());
	$sql = "INSERT INTO ".TB_PREF."attendance_files (description, filename, unique_name,
		filesize, filetype, tran_date) VALUES (".db_escape($description).", "
		.db_escape($filename).", ".db_escape($unique_name).", ".db_escape($filesize)
		.", ".db_escape($filetype).", '$date')";
	db_query($sql, "Attendance file record could not be inserted");		
}
//----------------------------------------------------------------------------------------

function update_attendance_file($selected_id, $description,
	$filename, $unique_name, $filesize, $filetype)
{
	$date = date2sql(Today());
	$sql = "UPDATE ".TB_PREF."attendance_files SET
		type_no=".db_escape($filterType).",
		trans_no=".db_escape($trans_no).",
		description=".db_escape($description).", ";
	if ($filename != "")
	{
		$sql .= "filename=".db_escape($filename).",
		unique_name=".db_escape($unique_name).",
		filesize=".db_escape($filesize).",
		filetype=".db_escape($filetype).",";
	}	
	$sql .= "tran_date='$date' WHERE id=".db_escape($selected_id);
	db_query($sql, "Attendance file record could not be updated");		
}

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

function delete_attendance_file($id)
{
	$sql = "DELETE FROM ".TB_PREF."attendance_files WHERE id = ".db_escape($id);
	db_query($sql, "Could not delete attachment");
}

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

function get_attendance_file_string($id)
{
	global $path_to_root;
    $str_return = "";    
    $sql = "SELECT * FROM ".TB_PREF."attendance_files WHERE trans_no=".db_escape($id)." ORDER BY trans_no";
    $return = db_query($sql, "Could not retrieve attachments");
    while ($attachment = db_fetch($return))
    {
        if (strlen($str_return))
            $str_return = $str_return . " \n";    
        $str_return .= _("Attached File:")." <a href='$path_to_root/admin/attachments.php?vw=".$attachment["id"]." ' target='blanc_'> ". 
        	$attachment["id"] . " " . $attachment["description"]. " - ". $attachment["filename"]."</a><br/>";
    }
    return $str_return . "<br/>";
}

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

function get_attendance_files($type)
{
	$sql = "SELECT * FROM ".TB_PREF."attendance_files WHERE type_no=".db_escape($type)
	." ORDER BY trans_no";
	return db_query($sql, "Could not retrieve attachments");
}

function get_attendance_file($id)
{
	$sql = "SELECT * FROM ".TB_PREF."attendance_files WHERE id=".db_escape($id);
	$result = db_query($sql, "Could not retrieve attachments");
	return db_fetch($result);
}

function has_attendance_file($type, $id)
{
	$sql = "SELECT DISTINCT * FROM ".TB_PREF."attendance_files WHERE type_no=".db_escape($type)." AND trans_no=".db_escape($id);
	$result = db_query($sql, "Could not retrieve attachments");
	$myrow = db_fetch($result);
	if ($myrow === false)
		return 0;
	else
		return $myrow['id'];
}

function get_sql_for_attendance_files()
{
	$sql = "SELECT description, filename, tran_date, filesize, unique_name, filetype, id
                FROM ".TB_PREF."attendance_files 
                ORDER BY id DESC";
        
        return $sql;
}

function get_current_shift_info($employee_code, $from_date_time1)
{
    $sql = "SELECT es.employee_code, es.start_date, es.shift_id, 
                    s.shift_name, s.day_start, s.split_shift, s.table1_id  
            FROM ".TB_PREF."employee_shifts es,
                    ".TB_PREF."shifts s  
            WHERE es.start_date <= ".db_escape($from_date_time1)." AND 
                        es.employee_code = ".db_escape($employee_code)." AND 
                        es.shift_id = s.id 
            ORDER BY es.start_date DESC LIMIT 1 ";
    
    $result = db_query($sql, "Current shift info could not be retrieved");  
    
    return db_fetch($result);
}

function check_employee_code($employee_code)
{
        $sql = "SELECT employee_code 
            FROM ".TB_PREF."employee_master 
            WHERE employee_code = ".db_escape($employee_code);
    
    $result = db_query($sql, "Employee code not be retrieved");  
    
    return db_fetch($result);
}

function add_import_attendance_record($file_id, $employee_code, $date,
                                    $shift_id, $table_id, $task_id,
                                    $in, $out, $non_working_time,
                                    $times)
{
    $imported_by = $_SESSION['wa_current_user']->user;
    
    $sql = "INSERT INTO ".TB_PREF."import_attendance_data(
                    file_id, employee_code, entry_date,
                    shift_id, table_id, task_id,
                    in_time, out_time, non_working_time,
                    times, imported_by) 
            VALUES (".db_escape($file_id).","
                        .db_escape($employee_code).",".db_escape($date).","
                        .db_escape($shift_id).",".db_escape($table_id).","
                        .db_escape($task_id).",".db_escape($in).","
                        .db_escape($out).",".db_escape($non_working_time).","
                        .db_escape($times).",".db_escape($imported_by).")";
                            
    db_query($sql, "Import attendance record could not be added."); 
}

function update_import_attendance_record($wa_masg, $rec_id)
{
    if($wa_masg['passed'] == true){
        $attendance_rec_id = $wa_masg['message'];
        $message = "Imported";
    }
    else{
        $attendance_rec_id = 0;
        $message = $wa_masg['message'];
    }
    $sql = "UPDATE ".TB_PREF."import_attendance_data 
            SET attendance_rec_id = ".db_escape($attendance_rec_id).", 
                message = ".db_escape($message)." 
            WHERE id = ".$rec_id;
    
    db_query($sql, "Attendance record could not be updated"); 
}

function get_import_attendance_data($selected_id)
{
    $sql = "SELECT *
            FROM ".TB_PREF."import_attendance_data 
            WHERE file_id = ".db_escape($selected_id);
    
    return db_query($sql, "Attendance record could not be retrieved"); 
}


function delete_saved_records($selected_id)
{
    $sql = "DELETE 
            FROM ".TB_PREF."import_attendance_data 
            WHERE file_id = ".db_escape($selected_id);
    
    $result = db_query($sql, "Attendance records could not be deleted"); 
    return db_num_affected_rows($result);
}
?>