<?php
/**********************************************************************
***********************************************************************/
include_once($path_to_root . "/includes/date_functions.inc");

//$pcs = "";


function read_file1($row, $selected_id)
{
        $file_data = array();
        
        $handle = fopen(company_path()."/attendance_files/".$row['unique_name'], "r");
        
        if ($handle) {            
            while (($line = fgets($handle)) !== false)
            {
                    $line .= "  ";
                    $department = read_line($line);
                    $employee_code = read_line($line);
                    $employee_name = read_line($line);
                    $enroll_id = read_line($line);
                    $date = read_line($line);
                    $date = change_date_format('DDMMYYYY', $date, 'YYYYMMDD', '-');
                    
                    while(strpos($line, ",") > 0){
                        $r = strpos($line, ",");
                        $time1 = read_line($line);
                        if(strlen(trim($time1)) > 0){
                            add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $time1, $file_data);
                        }
                    }
                    
                    $time2 = read_line($line);
                    if(strlen(trim($time2)) > 0){
                        add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $time2, $file_data);
                    }                    
            } 
            
            fclose($handle);
            
            return $file_data;
        } 
        else {
        // error opening the file.
        }
}

function read_file($row, $selected_id)
{
        $file_data = array();
        
        $handle = fopen(company_path()."/attendance_files/".$row['unique_name'], "r");
        
        if ($handle) {            
            while (($line = fgets($handle)) !== false)
            {
                 read_daily_attendance_data($selected_id, $line, $file_data);
            } 
            
            fclose($handle);
            
            return $file_data;
        } 
        else {
        // error opening the file.
        }
}

function read_daily_attendance_data($selected_id, $line, &$file_data)
{
        $l = explode(',',$line);
        $department = '';
        $employee_code = str_replace('"', '', $l[24]);
        $employee_name = '';
        $enroll_id = str_replace('"', '', $l[24]);
        $date = str_replace('"', '', $l[3]);
        $date = change_date_format('DDMMYYYY', $date, 'YYYYMMDD', '-');
                    
        $Shift_In = str_replace('"', '', $l[27]);
        $Break_Out = str_replace('"', '', $l[28]);
        $Break_In = str_replace('"', '', $l[29]);
        $Shift_Out = str_replace('"', '', $l[30]);

        if($Shift_In != ""){
            add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $Shift_In, $file_data);
        }
        
        if($Break_Out != "00:00"){
            add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $Break_Out, $file_data);
        }  
        
        if($Break_In != "00:00"){
            add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $Break_In, $file_data);
        } 
        
        if($Shift_Out != "00:00"){
            add_to_file_data_line($selected_id, $department, $employee_code, $employee_name, $enroll_id, $date, $Shift_Out, $file_data);
        } 
}

?>
